app/soc/logic/allocations.py
changeset 1721 9acf4fe1b9bb
parent 1718 ca34f4a8c61b
child 1737 9dc63101b107
--- a/app/soc/logic/allocations.py	Sat Mar 07 17:55:14 2009 +0000
+++ b/app/soc/logic/allocations.py	Sat Mar 07 18:16:54 2009 +0000
@@ -147,6 +147,17 @@
     popularity = ((k, len(v)) for k, v in self.applications.iteritems())
     self.popularity = dict(popularity)
 
+  def rangeSlots(self, slots, org):
+    """Returns the amount of slots for the org within the required bounds.
+    """
+
+    slots = int(math.floor(slots))
+    slots = min(slots, self.max_slots_per_org)
+    slots = max(slots, self.min_slots_per_org)
+    slots = min(slots, self.mentors[org])
+
+    return slots
+
   def iterativeAllocation(self):
     """A simple iterative algorithm.
     """
@@ -206,9 +217,8 @@
 
     for org in locked_orgs:
       popularity = self.popularity[org]
-      mentors = self.mentors[org]
       slots = locked_slots[org]
-      slots = min(slots, mentors)
+      slots = self.rangeSlots(slots, org)
 
       total_popularity -= popularity
       available_slots -= slots
@@ -228,14 +238,14 @@
       mentors = self.mentors[org]
 
       slots = (float(popularity)/float(total_popularity))*available_slots
-      slots = min(slots, self.max_slots_per_org)
-      slots = max(slots, self.min_slots_per_org)
-      slots = min(slots, mentors)
+      slots = self.rangeSlots(slots, org)
 
       popularity = (float(total_popularity)/float(available_slots))*slots
 
       self.popularity[org] = popularity
 
+    total_popularity = sum(self.popularity.values())
+
     # do the actual calculation
     for org in unlocked_orgs:
       org_applications = self.applications[org]
@@ -255,7 +265,10 @@
       if slots_left < 1:
         break
 
-      slots_left -= 1
-      allocations[org] += 1
+      current = allocations[org]
+      slots = self.rangeSlots(current + 1, org)
+
+      slots_left += slots - current
+      allocations[org] = slots
 
     return allocations