Change iterative to algorithm in slot allocator
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 09 Apr 2009 22:05:44 +0000
changeset 2139 43a02512ebf7
parent 2138 70b4a5e90eb0
child 2140 32b0731f0bf5
Change iterative to algorithm in slot allocator Also store the slots in program model instead of in each org. Patch by: Sverre Rabbelier
app/soc/logic/allocations.py
app/soc/views/models/program.py
tests/app/soc/logic/test_allocations.py
--- a/app/soc/logic/allocations.py	Thu Apr 09 22:05:07 2009 +0000
+++ b/app/soc/logic/allocations.py	Thu Apr 09 22:05:44 2009 +0000
@@ -47,16 +47,17 @@
   # piece of code ;).
 
   def __init__(self, orgs, popularity, max, slots,
-               max_slots_per_org, min_slots_per_org, iterative):
+               max_slots_per_org, min_slots_per_org, algorithm):
     """Initializes the allocator.
 
     Args:
       orgs: a list of all the orgs that need to be allocated
       popularity: the amount of applications per org
-      mentors: the amount of assigned mentors per org
+      max: the amount of assigned mentors per org
       slots: the total amount of available slots
       max_slots_per_org: how many slots an org should get at most
       min_slots_per_org: how many slots an org should at least get
+      algorithm: the algorithm to use
     """
 
     self.locked_slots = {}
@@ -73,14 +74,13 @@
     self.total_popularity = None
     self.initial_popularity = popularity
     self.max = max
-    self.iterative = iterative
+    self.algorithm = algorithm
 
   def allocate(self, locked_slots):
     """Allocates the slots and returns the result.
 
     Args:
       locked_slots: a dict with orgs and the number of slots they get
-      adjusted_slots: a dict with orgs and the number of extra slots they get
     """
 
     self.locked_slots = locked_slots
@@ -90,11 +90,11 @@
     if not sum(self.popularity.values()) or not sum(self.max.values()):
       return dict([(i, 0) for i in self.orgs])
 
-    if self.iterative:
-      return self.iterativeAllocation()
-    else:
+    if self.algorithm == 1:
       return self.preprocessingAllocation()
 
+    return self.iterativeAllocation()
+
   def buildSets(self):
     """Allocates slots with the specified constraints.
     """
--- a/app/soc/views/models/program.py	Thu Apr 09 22:05:07 2009 +0000
+++ b/app/soc/views/models/program.py	Thu Apr 09 22:05:44 2009 +0000
@@ -303,6 +303,10 @@
       from_json = simplejson.loads(result)
       locked_slots = dicts.groupDictBy(from_json, 'locked', 'slots')
 
+      if submit:
+        program.slots_allocation = result
+        program.put()
+
     orgs = {}
     applications = {}
     max = {}
@@ -312,29 +316,19 @@
       applications[org.link_id] = org.nr_applications
       max[org.link_id] = min(org.nr_mentors, org.slots_desired)
 
-      if submit:
-        org_post = from_json[org.link_id]
-        org_slots = org_post['slots']
-        try:
-          org_slots = int(org_slots)
-        except ValueError:
-          continue
-        org.slots = org_slots
-        org.put()
-
-    # TODO: Use configuration variables here
-    max_slots_per_org = 50
-    min_slots_per_org = 2
-    iterative = False
+    max_slots_per_org = program.max_slots
+    min_slots_per_org = program.min_slots
+    algorithm = 1
 
     allocator = allocations.Allocator(orgs.keys(), applications, max,
                                       program_slots, max_slots_per_org,
-                                      min_slots_per_org, iterative)
+                                      min_slots_per_org, algorithm)
 
     result = allocator.allocate(locked_slots)
 
     data = []
 
+    # TODO: remove adjustment here and in the JS
     for link_id, count in result.iteritems():
       org = orgs[link_id]
       data.append({
--- a/tests/app/soc/logic/test_allocations.py	Thu Apr 09 22:05:07 2009 +0000
+++ b/tests/app/soc/logic/test_allocations.py	Thu Apr 09 22:05:44 2009 +0000
@@ -66,7 +66,7 @@
     self.max_slots_per_org = 40
     self.min_slots_per_org = 2
     self.allocated = 0
-    self.iterative = False
+    self.algorithm = 1
 
     apps = {
         'asf': (20, 20),
@@ -83,7 +83,7 @@
 
     self.allocater = allocations.Allocator(
         self.orgs, self.popularity, self.mentors, self.slots,
-        self.max_slots_per_org, self.min_slots_per_org, self.iterative)
+        self.max_slots_per_org, self.min_slots_per_org, self.algorithm)
 
   def testInitialAllocation(self):
     """Test that an allocation with no arguments does not crash.