Skip to content

Commit

Permalink
Add ability to pass another solver.
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
drvinceknight committed May 6, 2017
1 parent 8018f52 commit 1ffa2e7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
16 changes: 12 additions & 4 deletions conference_scheduler/scheduler.py
Expand Up @@ -12,7 +12,7 @@
# schedule: a generator for a list of ScheduledItem instances


def solution(events, slots, objective_function=None, **kwargs):
def solution(events, slots, objective_function=None, solver=None, **kwargs):
"""Setup up the ILP problem, submit it to pulp and return the solution
Parameters
Expand All @@ -21,8 +21,12 @@ def solution(events, slots, objective_function=None, **kwargs):
of resources.Event instances
slots : list or tuple
of resources.Slot instances
solve : pulp.solver
a pulp solver
objective_function: callable
from lp_problem.objective_functions
kwargs : keyword arguments
arguments for the objective function
Returns
-------
Expand Down Expand Up @@ -51,7 +55,7 @@ def solution(events, slots, objective_function=None, **kwargs):
problem += objective_function(events=events, slots=slots, X=X,
**kwargs)

status = problem.solve()
status = problem.solve(solver=solver)
if status == 1:
return (
item for item, variable in X.items()
Expand Down Expand Up @@ -96,7 +100,7 @@ def array(events, slots, objective_function=None):
)


def schedule(events, slots, objective_function=None, **kwargs):
def schedule(events, slots, objective_function=None, solver=None, **kwargs):
"""Compute the ILP solution and return it in schedule form
Parameters
Expand All @@ -105,16 +109,20 @@ def schedule(events, slots, objective_function=None, **kwargs):
of resources.Event instances
slots : list or tuple
of resources.Slot instances
solver : pulp.solver
a pulp solver
objective_function : callable
from lp_problem.objective_functions
kwargs : keyword arguments
arguments for the objective function
Returns
-------
Generator
of tuples of instances of resources.ScheduledItem
"""
return solution_to_schedule(
solution(events, slots, objective_function, **kwargs),
solution(events, slots, objective_function, solver=solver, **kwargs),
events, slots
)

Expand Down
4 changes: 4 additions & 0 deletions conference_scheduler/tests/test_scheduler.py
Expand Up @@ -81,6 +81,10 @@ def test_unsolvable_raises_error(events):
scheduler.solution(events, slots)


def test_solver_recognised_by_pulp_raises_error(events, slots):
with pytest.raises(AttributeError):
scheduler.solution(events, slots, solver="Not a real solver")

# Array form
# Less testing needed here since it simply calls scheduler.solution and
# converts the result to array form
Expand Down
7 changes: 6 additions & 1 deletion docs/howtos/index.rst
@@ -1,4 +1,9 @@
How tos
=======

TBD
A number of specific instructions for particular things you might want to do:

.. toctree::
:maxdepth: 2

use_different_solver.rst
17 changes: 17 additions & 0 deletions docs/howtos/use_different_solver.rst
@@ -0,0 +1,17 @@
Using different solvers
=======================

The `Pulp <https://pythonhosted.org/PuLP/index.html>`_ library is a Python
interface to underlying solution engines. By default it comes packaged with `CBC
<https://projects.coin-or.org/Cbc>`_

It is however possible to use a number of other solvers (see the Pulp
documentation for details) and these can be passed to the scheduler. For example
here is how we would use the `GLPK <https://www.gnu.org/software/glpk/>`_
solver for a given set of :code:`events` and :code:`slots`::

>>> scheduler.schedule(events=events, slots=slots, solver=pulp.GLPK()) # doctest: +SKIP

Different solvers can have major impact on the performance of the scheduler.
This can be an important consideration when scheduling large or highly
constrained problems.

0 comments on commit 1ffa2e7

Please sign in to comment.