| 1 |
How to add a new solver to PuLP |
| 2 |
====================================== |
| 3 |
|
| 4 |
Solver API are located in the directory ``pulp/apis/`` and are organized as one file per solver. Each file can contain more than one API (usually between 1 and 2). |
| 5 |
|
| 6 |
General information on configuring solvers and how pulp finds solvers installed in a system are in the :doc:`/guides/how_to_configure_solvers` section. |
| 7 |
|
| 8 |
|
| 9 |
Example |
| 10 |
---------- |
| 11 |
|
| 12 |
The smallest api for a solver can be found in the file :py:class:`pulp.apis.mipcl_api.MIPCL_CMD` located in ``pulp/apis/mipcl_api.py``. We will use it as an example to list all the changes that need to be done. |
| 13 |
|
| 14 |
Inheriting base classes |
| 15 |
------------------------------ |
| 16 |
|
| 17 |
The solver needs to inherit one of two classes: :py:class:`pulp.apis.LpSolver` or :py:class:`pulp.apis.LpSolver_CMD` located in ``pulp.apis.core.py``. The first one is a generic class. The second one is used for command-line based APIs to solvers. This is the one used for the example:: |
| 18 |
|
| 19 |
from .core import LpSolver_CMD, subprocess, PulpSolverError |
| 20 |
import os |
| 21 |
from .. import constants |
| 22 |
import warnings |
| 23 |
|
| 24 |
class MIPCL_CMD(LpSolver_CMD): |
| 25 |
|
| 26 |
name = "MIPCL_CMD" |
| 27 |
|
| 28 |
def __init__( |
| 29 |
self, |
| 30 |
path=None, |
| 31 |
keepFiles=False, |
| 32 |
mip=True, |
| 33 |
msg=True, |
| 34 |
options=None, |
| 35 |
timeLimit=None, |
| 36 |
): |
| 37 |
LpSolver_CMD.__init__( |
| 38 |
self, |
| 39 |
mip=mip, |
| 40 |
msg=msg, |
| 41 |
timeLimit=timeLimit, |
| 42 |
options=options, |
| 43 |
path=path, |
| 44 |
keepFiles=keepFiles, |
| 45 |
) |
| 46 |
|
| 47 |
As can be seen in the example, the main things when declaring a solver are: |
| 48 |
|
| 49 |
#. declaring a class-level attribute with its name. |
| 50 |
#. defining some arguments for the solver (ideally, following the convention of other solvers). |
| 51 |
#. initializing the parent class with available data. |
| 52 |
#. (optional) doing some other particular logic. |
| 53 |
|
| 54 |
In addition to these some minimal methods are expected to be implemented by the solver. We will cover each one. |
| 55 |
|
| 56 |
``available`` method |
| 57 |
-------------------------- |
| 58 |
|
| 59 |
Required by :py:class:`pulp.apis.LpSolver` and :py:class:`pulp.apis.LpSolver_CMD`. |
| 60 |
|
| 61 |
Returns ``True`` if the solver is available and operational. ``False`` otherwise:: |
| 62 |
|
| 63 |
def available(self): |
| 64 |
return self.executable(self.path) |
| 65 |
|
| 66 |
``actualSolve`` method |
| 67 |
-------------------------- |
| 68 |
|
| 69 |
Required by :py:class:`pulp.apis.LpSolver` and :py:class:`pulp.apis.LpSolver_CMD`. |
| 70 |
|
| 71 |
Takes an :py:class:`pulp.pulp.LpProblem` as argument, solves it, stores the solution, and returns a status code:: |
| 72 |
|
| 73 |
def actualSolve(self, lp): |
| 74 |
"""Solve a well formulated lp problem""" |
| 75 |
if not self.executable(self.path): |
| 76 |
raise PulpSolverError("PuLP: cannot execute " + self.path) |
| 77 |
tmpMps, tmpSol = self.create_tmp_files(lp.name, "mps", "sol") |
| 78 |
if lp.sense == constants.LpMaximize: |
| 79 |
# we swap the objectives |
| 80 |
# because it does not handle maximization. |
| 81 |
warnings.warn( |
| 82 |
"MIPCL_CMD does not allow maximization, " |
| 83 |
"we will minimize the inverse of the objective function." |
| 84 |
) |
| 85 |
lp += -lp.objective |
| 86 |
lp.checkDuplicateVars() |
| 87 |
lp.checkLengthVars(52) |
| 88 |
lp.writeMPS(tmpMps, mpsSense=lp.sense) |
| 89 |
|
| 90 |
# just to report duplicated variables: |
| 91 |
try: |
| 92 |
os.remove(tmpSol) |
| 93 |
except: |
| 94 |
pass |
| 95 |
cmd = self.path |
| 96 |
cmd += " %s" % tmpMps |
| 97 |
cmd += " -solfile %s" % tmpSol |
| 98 |
if self.timeLimit is not None: |
| 99 |
cmd += " -time %s" % self.timeLimit |
| 100 |
for option in self.options: |
| 101 |
cmd += " " + option |
| 102 |
if lp.isMIP(): |
| 103 |
if not self.mip: |
| 104 |
warnings.warn("MIPCL_CMD cannot solve the relaxation of a problem") |
| 105 |
if self.msg: |
| 106 |
pipe = None |
| 107 |
else: |
| 108 |
pipe = open(os.devnull, "w") |
| 109 |
|
| 110 |
return_code = subprocess.call(cmd.split(), stdout=pipe, stderr=pipe) |
| 111 |
# We need to undo the objective swap before finishing |
| 112 |
if lp.sense == constants.LpMaximize: |
| 113 |
lp += -lp.objective |
| 114 |
if return_code != 0: |
| 115 |
raise PulpSolverError("PuLP: Error while trying to execute " + self.path) |
| 116 |
if not os.path.exists(tmpSol): |
| 117 |
status = constants.LpStatusNotSolved |
| 118 |
status_sol = constants.LpSolutionNoSolutionFound |
| 119 |
values = None |
| 120 |
else: |
| 121 |
status, values, status_sol = self.readsol(tmpSol) |
| 122 |
self.delete_tmp_files(tmpMps, tmpSol) |
| 123 |
lp.assignStatus(status, status_sol) |
| 124 |
if status not in [constants.LpStatusInfeasible, constants.LpStatusNotSolved]: |
| 125 |
lp.assignVarsVals(values) |
| 126 |
|
| 127 |
return status |
| 128 |
|
| 129 |
``defaultPath`` method |
| 130 |
------------------------- |
| 131 |
|
| 132 |
Only required by :py:class:`pulp.apis.LpSolver_CMD`. It returns the default path of the command-line solver:: |
| 133 |
|
| 134 |
def defaultPath(self): |
| 135 |
return self.executableExtension("mps_mipcl") |
| 136 |
|
| 137 |
|
| 138 |
Making the solver available to PuLP |
| 139 |
--------------------------------------------------- |
| 140 |
|
| 141 |
Modify the ``pulp/apis/__init__.py`` file to import your solver and add it to the ``_all_solvers`` list:: |
| 142 |
|
| 143 |
from .mipcl_api import MIPCL_CMD |
| 144 |
_all_solvers = [ |
| 145 |
# (...) |
| 146 |
MIPCL_CMD, |
| 147 |
] |
| 148 |
|
| 149 |
Including the solver in tests suite |
| 150 |
-------------------------------------------------- |
| 151 |
|
| 152 |
Include the solver in PuLP's test suite by adding a couple of lines corresponding to your solver to the ``pulp/tests/test_pulp.py`` file:: |
| 153 |
|
| 154 |
# (...) |
| 155 |
class MIPCL_CMDTest(BaseSolverTest.PuLPTest): |
| 156 |
solveInst = MIPCL_CMD |