Skip to content

Commit

Permalink
Merge pull request #97 from PyconUK/change-output-of-heuristic
Browse files Browse the repository at this point in the history
Change output of heuristic to be solution.
  • Loading branch information
meatballs authored Aug 8, 2017
2 parents 81922b0 + 5b111aa commit dae3a8f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
19 changes: 16 additions & 3 deletions src/conference_scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,21 @@ def heuristic(events,
Returns
-------
array
A numpy array
list
A list of tuples giving the event and slot index (for the given
events and slots lists) for all scheduled items.
Example
-------
For a solution where
* event 0 is scheduled in slot 1
* event 1 is scheduled in slot 4
* event 2 is scheduled in slot 5
the resulting list would be::
[(0, 1), (1, 4), (2, 5)]
"""
X = heu.get_initial_array(events=events, slots=slots)

Expand All @@ -81,7 +94,7 @@ def func(array):
objective_function=func,
**objective_function_algorithm_kwargs)

return X
return list(zip(*np.nonzero(X)))


def solution(events, slots, objective_function=None, solver=None, **kwargs):
Expand Down
21 changes: 6 additions & 15 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,36 +327,27 @@ def test_removed_slot_schedule_difference(events, slots):

def test_heuristic_solution(events, slots):
np.random.seed(1)
array_solution = scheduler.heuristic(events=events, slots=slots)
solution = scheduler.heuristic(events=events, slots=slots)

expected_array = np.array([[0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0]])
assert np.array_equal(array_solution, expected_array)
assert solution == [(0, 4), (1, 0), (2, 5)]


def test_heuristic_solution_with_simulated_annealing(events, slots):
np.random.seed(1)
array_solution = scheduler.heuristic(
solution = scheduler.heuristic(
events=events,
slots=slots,
algorithm=heu.simulated_annealing,
objective_function=of.capacity_demand_difference)

expected_array = np.array([[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0]])
assert np.array_equal(array_solution, expected_array)
assert solution == [(0, 2), (1, 3), (2, 1)]

array_solution = scheduler.heuristic(
solution = scheduler.heuristic(
events=events,
slots=slots,
algorithm=heu.simulated_annealing,
initial_solution_algorithm_kwargs={"max_iterations": 10},
objective_function_algorithm_kwargs={"max_iterations": 2},
objective_function=of.capacity_demand_difference)

expected_array = np.array([[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1]])
assert np.array_equal(array_solution, expected_array)
assert solution == [(0, 2), (1, 3), (2, 6)]

0 comments on commit dae3a8f

Please sign in to comment.