From b5f1e89dad7221780cac43e55dc27e0b786f7165 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Thu, 9 Apr 2009 22:05:44 +0000 Subject: [PATCH] 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 | 14 +++++++------- app/soc/views/models/program.py | 24 +++++++++--------------- tests/app/soc/logic/test_allocations.py | 4 ++-- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/app/soc/logic/allocations.py b/app/soc/logic/allocations.py index 8123a7e4..7714f046 100644 --- a/app/soc/logic/allocations.py +++ b/app/soc/logic/allocations.py @@ -47,16 +47,17 @@ class Allocator(object): # 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 @@ class Allocator(object): 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 @@ class Allocator(object): 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. """ diff --git a/app/soc/views/models/program.py b/app/soc/views/models/program.py index 6b8e9d90..12511a2b 100644 --- a/app/soc/views/models/program.py +++ b/app/soc/views/models/program.py @@ -303,6 +303,10 @@ class View(presence.View): 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 @@ class View(presence.View): 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({ diff --git a/tests/app/soc/logic/test_allocations.py b/tests/app/soc/logic/test_allocations.py index ce5bb8a6..75eb17ff 100644 --- a/tests/app/soc/logic/test_allocations.py +++ b/tests/app/soc/logic/test_allocations.py @@ -66,7 +66,7 @@ class AllocationsTest(unittest.TestCase): 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 @@ class AllocationsTest(unittest.TestCase): 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. -- 2.11.4.GIT