author | Sverre Rabbelier <srabbelier@gmail.com> |
Thu, 29 Jan 2009 01:39:31 +0000 | |
changeset 1056 | d0c82bdc2de2 |
child 1307 | 091a21cf3627 |
permissions | -rw-r--r-- |
1056
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""Slot allocation logic. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
] |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
import itertools |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
import math |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
class Error(Exception): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
"""Error class for the Allocation module. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
pass |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
class Allocator(object): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
"""A simple student slots allocator. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
The buildSets method is used to validate the allocation data as well as |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
construct the sets that the algorithm then uses to distribute the slots. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
By separating these steps it is possible to write a different allocation |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
algorithm but re-use the sets and validation logic. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
# I tried to write explicit code that does not require any |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
# additional comments (with the exception of the set notation for |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
# the convenience of any mathematicians that happen to read this |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
# piece of code ;). |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
def __init__(self, orgs, applications, slots, max_slots_per_org): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
"""Initializes the allocator. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
Args: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
orgs: a list of all the orgs that need to be allocated |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
applications: a dictionary with for each org a list of applicants |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
slots: the total amount of available slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
all_applications = [] |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
for _, value in applications.iteritems(): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
all_applications += value |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
self.slots = slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
self.max_slots_per_org = max_slots_per_org |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
self.orgs = set(orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
self.applications = applications |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
self.all_applications = set(all_applications) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
def allocate(self, locked_slots, adjusted_slots): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
"""Allocates the slots and returns the result. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
Args: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
locked_slots: a dict with orgs and the number of slots they get |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
adjusted_slots: a dict with orgs and the number of extra slots they get |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
self.locked_slots = locked_slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
self.adjusted_slots = adjusted_slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
self.buildSets() |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
return self.iterativeAllocation() |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
def buildSets(self): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
"""Allocates slots with the specified constraints |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
# set s |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
all_applications = self.all_applications |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
locked_slots = self.locked_slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
adjusted_slots = self.adjusted_slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
# set a and b |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
locked_orgs = set(locked_slots.keys()) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
adjusted_orgs = set(adjusted_slots.keys()) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
# set a' and b' |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
99 |
unlocked_orgs = self.orgs.difference(locked_orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
unadjusted_orgs = self.orgs.difference(adjusted_orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
# set a*b and a'*b' |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
103 |
locked_and_adjusted_orgs = locked_orgs.intersection(adjusted_orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
104 |
unlocked_and_unadjusted_orgs = unlocked_orgs.intersection(unadjusted_orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
105 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
# a+o and b+o should be o |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
107 |
locked_orgs_or_orgs = self.orgs.union(locked_orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
108 |
adjusted_orgs_or_orgs = self.orgs.union(adjusted_orgs) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
109 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
110 |
# an item can be only a or b, so a*b should be empty |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
111 |
if locked_and_adjusted_orgs: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
112 |
raise Error("Cannot have an org locked and adjusted") |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
113 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
114 |
# a+o should be o, testing length is enough though |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
115 |
if len(locked_orgs_or_orgs) != len(self.orgs): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
116 |
raise Error("Unknown org as locked slot") |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
117 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
118 |
# same for b+o |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
119 |
if len(adjusted_orgs_or_orgs) != len(self.orgs): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
120 |
raise Error("Unknown org as adjusted slot") |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
121 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
122 |
# set l and l' |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
123 |
locked_applications = set(itertools.chain(*locked_slots.keys())) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
124 |
unlocked_applications = all_applications.difference(locked_applications) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
126 |
self.adjusted_orgss = adjusted_orgs |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
127 |
self.locked_orgs = locked_orgs |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
128 |
self.unlocked_applications = unlocked_applications |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
129 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
130 |
def iterativeAllocation(self): |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
131 |
"""A simple iterative algorithm. |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
132 |
""" |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
133 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
134 |
adjusted_orgs = self.adjusted_orgss |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
135 |
adjusted_slots = self.adjusted_slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
136 |
locked_orgs = self.locked_orgs |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
137 |
locked_slots = self.locked_slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
138 |
unlocked_applications = self.unlocked_applications |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
139 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
140 |
unlocked_applications_count = len(unlocked_applications) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
141 |
unallocated_applications_count = unlocked_applications_count |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
142 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
143 |
available_slots = self.slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
allocations = {} |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
for org in self.orgs: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
147 |
org_applications = self.applications[org] |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
148 |
org_applications_count = len(org_applications) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
149 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
150 |
if org in locked_orgs: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
151 |
slots = locked_slots[org] |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
152 |
else: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
153 |
weight = float(org_applications_count) / unallocated_applications_count |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
slots = int(math.floor(weight*available_slots)) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
156 |
if org in adjusted_orgs: |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
157 |
slots += adjusted_slots[org] |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
159 |
slots = min(slots, self.max_slots_per_org) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
160 |
slots = min(slots, org_applications_count) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
161 |
slots = min(slots, available_slots) |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
163 |
allocations[org] = slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
available_slots -= slots |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
unallocated_applications_count -= org_applications_count |
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
|
d0c82bdc2de2
Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
167 |
return allocations |