Skip to content

Commit

Permalink
[#56] Move lpsum and summation_functions dict to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballs committed May 9, 2017
1 parent 5edca76 commit 1cf6e71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
25 changes: 4 additions & 21 deletions conference_scheduler/lp_problem/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,10 @@
from conference_scheduler.lp_problem import utils as lpu


# According to David MacIver, using this function is more efficient than
# using sum() or plain addition
# This code is taken from his gist at:
# https://gist.github.com/DRMacIver/4b6561c8e4776597bf7568ccac52742f
def lpsum(variables):
result = pulp.LpAffineExpression()
for v in variables:
result.addInPlace(v)
return result


summation_functions = {
'lpsum': lpsum,
None: sum
}


def _schedule_all_events(events, slots, X, summation_type=None):

shape = Shape(len(events), len(slots))
summation = summation_functions[summation_type]
summation = lpu.summation_functions[summation_type]

label = 'Event either not scheduled or scheduled multiple times'
for event in range(shape.events):
Expand All @@ -37,7 +20,7 @@ def _schedule_all_events(events, slots, X, summation_type=None):
def _max_one_event_per_slot(events, slots, X, summation_type=None):

shape = Shape(len(events), len(slots))
summation = summation_functions[summation_type]
summation = lpu.summation_functions[summation_type]

label = 'Slot with multiple events scheduled'
for slot in range(shape.slots):
Expand All @@ -55,7 +38,7 @@ def _events_in_session_share_a_tag(events, slots, X, summation_type=None):
"""

session_array, tag_array = lpu.session_array(slots), lpu.tag_array(events)
summation = summation_functions[summation_type]
summation = lpu.summation_functions[summation_type]

label = 'Dissimilar events schedule in same session'
event_indices = range(len(tag_array))
Expand Down Expand Up @@ -105,7 +88,7 @@ def _events_available_during_other_events(
Constraint that ensures that an event is not scheduled at the same time as
another event for which it is unavailable.
"""
summation = summation_functions[summation_type]
summation = lpu.summation_functions[summation_type]
event_availability_array = lpu.event_availability_array(events)

label = 'Event clashes with another event'
Expand Down
17 changes: 17 additions & 0 deletions conference_scheduler/lp_problem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
from conference_scheduler.resources import Shape


# According to David MacIver, using this function is more efficient than
# using sum() or plain addition
# This code is taken from his gist at:
# https://gist.github.com/DRMacIver/4b6561c8e4776597bf7568ccac52742f
def lpsum(variables):
result = pulp.LpAffineExpression()
for v in variables:
result.addInPlace(v)
return result


summation_functions = {
'lpsum': lpsum,
None: sum
}


def variables(shape: Shape):
return pulp.LpVariable.dicts(
"x",
Expand Down

0 comments on commit 1cf6e71

Please sign in to comment.