app/soc/logic/allocations.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 07 Mar 2009 18:16:54 +0000
changeset 1721 9acf4fe1b9bb
parent 1718 ca34f4a8c61b
child 1737 9dc63101b107
permissions -rw-r--r--
Adjust the total_popularity after ranging This way 'off the scale' orgs (many applicants, with only few mentors available) will not pull the total amount of allocated slots down as much as before. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1056
diff changeset
     3
# Copyright 2009 the Melange authors.
1056
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
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    50
  def __init__(self, orgs, applications, mentors, slots,
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    51
               max_slots_per_org, min_slots_per_org, iterative):
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
    """Initializes the allocator.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
    Args:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
      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
    56
      applications: a dictionary with for each org a list of applicants
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    57
      mentors: the amount of assigned mentors per org
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
      slots: the total amount of available slots
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    59
      max_slots_per_org: how many slots an org should get at most
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    60
      min_slots_per_org: how many slots an org should at least get
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
    all_applications = []
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
    for _, value in applications.iteritems():
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
      all_applications += value
1651
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    67
    
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    68
    self.locked_slots = {}
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    69
    self.adjusted_slots = {}
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    70
    self.adjusted_orgs = []
1651
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    71
    self.locked_orgs = []
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
    72
    self.unlocked_applications = []
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
    self.slots = slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
    self.max_slots_per_org = max_slots_per_org
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    75
    self.min_slots_per_org = min_slots_per_org
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
    self.orgs = set(orgs)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
    self.applications = applications
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    78
    self.mentors = mentors
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
    self.all_applications = set(all_applications)
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
    80
    self.iterative = iterative
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
  def allocate(self, locked_slots, adjusted_slots):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
    """Allocates the slots and returns the result.
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
    Args:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
      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
    87
      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
    88
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
    self.locked_slots = locked_slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
    self.adjusted_slots = adjusted_slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    self.buildSets()
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
    95
    if self.iterative:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
    96
      return self.iterativeAllocation()
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
    97
    else:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
    98
      return self.preprocessingAllocation()
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
  def buildSets(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
    """Allocates slots with the specified constraints
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
    # set s
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
    all_applications = self.all_applications
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
    locked_slots = self.locked_slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
    adjusted_slots = self.adjusted_slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
    # set a and b
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    locked_orgs = set(locked_slots.keys())
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
    adjusted_orgs = set(adjusted_slots.keys())
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
    # set a' and b'
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   114
    unlocked_orgs = self.orgs.difference(locked_orgs)
1651
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
   115
    # unadjusted_orgs = self.orgs.difference(adjusted_orgs)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
    # set a*b and a'*b'
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
    locked_and_adjusted_orgs = locked_orgs.intersection(adjusted_orgs)
1651
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
   119
    
ce52003ca18f Comment out unused variables and add used but not declared class variables to __init__ method in soc.logic.allocations module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1307
diff changeset
   120
    # unlocked_and_unadjusted_orgs = unlocked_orgs.intersection(unadjusted_orgs)
1056
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
    # a+o and b+o should be o
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
    locked_orgs_or_orgs = self.orgs.union(locked_orgs)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
    adjusted_orgs_or_orgs = self.orgs.union(adjusted_orgs)
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
    # 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
   127
    if locked_and_adjusted_orgs:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
      raise Error("Cannot have an org locked and adjusted")
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
    # 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
   131
    if len(locked_orgs_or_orgs) != len(self.orgs):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
      raise Error("Unknown org as locked slot")
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
    # same for b+o
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
    if len(adjusted_orgs_or_orgs) != len(self.orgs):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
      raise Error("Unknown org as adjusted slot")
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
    # set l and l'
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
    locked_applications = set(itertools.chain(*locked_slots.keys()))
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
    unlocked_applications = all_applications.difference(locked_applications)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   142
    self.adjusted_orgs = adjusted_orgs
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   143
    self.unlocked_orgs = unlocked_orgs
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
    self.locked_orgs = locked_orgs
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
    self.unlocked_applications = unlocked_applications
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   147
    popularity = ((k, len(v)) for k, v in self.applications.iteritems())
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   148
    self.popularity = dict(popularity)
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   149
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   150
  def rangeSlots(self, slots, org):
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   151
    """Returns the amount of slots for the org within the required bounds.
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   152
    """
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   153
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   154
    slots = int(math.floor(slots))
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   155
    slots = min(slots, self.max_slots_per_org)
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   156
    slots = max(slots, self.min_slots_per_org)
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   157
    slots = min(slots, self.mentors[org])
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   158
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   159
    return slots
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   160
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
  def iterativeAllocation(self):
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
    """A simple iterative algorithm.
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
    """
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   165
    adjusted_orgs = self.adjusted_orgs
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
    adjusted_slots = self.adjusted_slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
    locked_orgs = self.locked_orgs
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
    locked_slots = self.locked_slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
    unlocked_applications = self.unlocked_applications
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
    unlocked_applications_count = len(unlocked_applications)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
    unallocated_applications_count = unlocked_applications_count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
    available_slots = self.slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
    allocations = {}
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
    for org in self.orgs:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
      org_applications = self.applications[org]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
      org_applications_count = len(org_applications)
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   180
      mentors = self.mentors[org]
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
      if org in locked_orgs:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
        slots = locked_slots[org]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
      else:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
        weight = float(org_applications_count) / unallocated_applications_count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
        slots = int(math.floor(weight*available_slots))
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
      if org in adjusted_orgs:
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
        slots += adjusted_slots[org]
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
      slots = min(slots, self.max_slots_per_org)
1717
d4c4c8668871 Added assigned mentors and amount of minimum slots per org
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1651
diff changeset
   192
      slots = min(slots, mentors)
1056
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
      slots = min(slots, available_slots)
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
      allocations[org] = slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
      available_slots -= slots
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
      unallocated_applications_count -= org_applications_count
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
d0c82bdc2de2 Added a simple slot allocation algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
    return allocations
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   200
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   201
  def preprocessingAllocation(self):
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   202
    """An algorithm that pre-processes the input before running as normal.
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   203
    """
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   204
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   205
    adjusted_orgs = self.adjusted_orgs
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   206
    adjusted_slots = self.adjusted_slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   207
    locked_orgs = self.locked_orgs
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   208
    locked_slots = self.locked_slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   209
    unlocked_orgs = self.unlocked_orgs
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   210
    unlocked_applications = self.unlocked_applications
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   211
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   212
    total_popularity = sum(self.popularity.values())
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   213
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   214
    available_slots = self.slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   215
    allocations = {}
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   216
    slack = {}
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   217
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   218
    for org in locked_orgs:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   219
      popularity = self.popularity[org]
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   220
      slots = locked_slots[org]
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   221
      slots = self.rangeSlots(slots, org)
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   222
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   223
      total_popularity -= popularity
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   224
      available_slots -= slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   225
      allocations[org] = slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   226
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   227
    # adjust the orgs in need of adjusting
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   228
    for org in adjusted_orgs:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   229
      slots = adjusted_slots[org]
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   230
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   231
      adjustment = (float(total_popularity)/float(available_slots))*slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   232
      adjustment = int(math.ceil(adjustment))
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   233
      self.popularity[org] += adjustment
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   234
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   235
    # adjust the popularity so that the invariants are always met
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   236
    for org in unlocked_orgs:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   237
      popularity = self.popularity[org]
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   238
      mentors = self.mentors[org]
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   239
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   240
      slots = (float(popularity)/float(total_popularity))*available_slots
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   241
      slots = self.rangeSlots(slots, org)
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   242
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   243
      popularity = (float(total_popularity)/float(available_slots))*slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   244
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   245
      self.popularity[org] = popularity
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   246
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   247
    total_popularity = sum(self.popularity.values())
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   248
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   249
    # do the actual calculation
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   250
    for org in unlocked_orgs:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   251
      org_applications = self.applications[org]
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   252
      org_applications_count = len(org_applications)
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   253
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   254
      popularity = self.popularity[org]
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   255
      raw_slots = (float(popularity)/float(total_popularity))*available_slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   256
      slots = int(math.floor(raw_slots))
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   257
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   258
      slack[org] = raw_slots - slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   259
      allocations[org] = slots
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   260
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   261
    slots_left = available_slots - sum(allocations.values())
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   262
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   263
    # add leftover slots, sorted by slack, decending
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   264
    for org, slack in sorted(slack.iteritems(), key=lambda (k,v): v, reverse=True):
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   265
      if slots_left < 1:
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   266
        break
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   267
1721
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   268
      current = allocations[org]
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   269
      slots = self.rangeSlots(current + 1, org)
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   270
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   271
      slots_left += slots - current
9acf4fe1b9bb Adjust the total_popularity after ranging
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1718
diff changeset
   272
      allocations[org] = slots
1718
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   273
ca34f4a8c61b Added an pre-processing algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1717
diff changeset
   274
    return allocations