How to configure a solver in PuLP

A typical problem PuLP users have is trying to connect to a solver that is installed in their pc. Here, we show the main concepts and ways to be sure PuLP can talk to the solver in question.

Checking which solvers PuLP has access to

PuLP has some helper functions that permit a user to query which solvers are available and initialize a solver from its name.

import pulp as pl
solver_list = pl.listSolvers()
print(solver_list)
# ['CyLP', 'GLPK_CMD', 'PYGLPK', 'CPLEX_CMD', 'CPLEX_PY', 'GUROBI', 'GUROBI_CMD',
#  'MOSEK', 'CPSAT', 'GLOP', 'CLARABEL', 'XPRESS', 'XPRESS_PY', 'COIN_CMD',
#  'COINMP_DLL', 'CHOCO_CMD', 'MIPCL_CMD', 'SCIP_CMD', 'FSCIP_CMD', 'SCIP_PY',
#  'HiGHS', 'HiGHS_CMD', 'COPT', 'COPT_DLL', 'COPT_CMD', 'SAS94', 'SASCAS', 'CUOPT']

If passed the onlyAvailable=True argument, PuLP lists the solvers that are currently available:

import pulp as pl
solver_list = pl.listSolvers(onlyAvailable=True)
print(solver_list)
# ['CyLP', 'GLPK_CMD', 'PYGLPK', 'GUROBI', 'CLARABEL', 'COIN_CMD', 'SCIP_PY', 'HiGHS']

Also, it’s possible to get a solver object by using the name of the solver. Any arguments passed to this function are passed to the constructor:

import pulp as pl
solver = pl.getSolver('CPLEX_CMD')
solver = pl.getSolver('CPLEX_CMD', timeLimit=10)

In the next sections, we will explain how to configure a solver to be accessible by PuLP.

Solvers supported by PuLP

Solver

PuLP API(s)

Problems

How to get it

COIN-OR CBC

COIN_CMD

LP, MIP

pip install pulp[cbc] or cbc on PATH

COIN-OR Clp / Cbc (CyLP)

CYLP

LP, MIP

pip install pulp[cylp] (not on Windows)

GLPK

GLPK_CMD, PYGLPK

LP, MIP

glpsol on PATH / pip install pulp[glpk]

HiGHS

HiGHS, HiGHS_CMD

LP, MIP

pip install pulp[highs] / pip install pulp[highs_cmd] or highs on PATH

SCIP / FSCIP

SCIP_PY, SCIP_CMD, FSCIP_CMD

LP, MIP

pip install pulp[scip] / scip or fscip on PATH

OR-Tools CP-SAT

CPSAT

bounded integer models

pip install pulp[ortools]

OR-Tools GLOP

GLOP

LP

pip install pulp[ortools]

Clarabel

CLARABEL

LP

pip install pulp[clarabel]

CHOCO

CHOCO_CMD

MIP (constraint programming)

choco-parsers-with-dependencies.jar on PATH

MIPCL

MIPCL_CMD

LP, MIP

mps_mipcl on PATH

NVIDIA cuOpt

CUOPT

LP, MIP (GPU)

the cuopt Python package from NVIDIA

Gurobi

GUROBI, GUROBI_CMD

LP, MIP

pip install pulp[gurobi] (license needed for large models)

CPLEX

CPLEX_PY, CPLEX_CMD

LP, MIP

pip install pulp[cplex] (license needed for large models)

FICO Xpress

XPRESS_PY, XPRESS_CMD

LP, MIP

pip install pulp[xpress] (license needed for large models)

COPT

COPT, COPT_DLL, COPT_CMD

LP, MIP

pip install pulp[copt] (license needed for large models)

MOSEK

MOSEK

LP, MIP

pip install pulp[mosek] (license required)

SAS

SAS94, SASCAS

LP, MIP

saspy / swat and access to a SAS installation

The open_py extra installs HiGHS, SCIP, CBC, GLPK and Clarabel at once; the public_py extra installs the PyPI packages of Gurobi, COPT, Xpress and CPLEX.

What is an environment variable

An environment variable is probably better explained somewhere else. For the sake of this document, it is a text value stored during your session that allows you to configure some applications that make use of them. For example, when you write:

python

in your command line, it usually opens a python console. But how did your computer know where to find python? It knew because there is an environment variable call “PATH” that stores a list of locations in your hard-drive where your pc looks for executables that match the thing you write.

It has many advantages such as not leaving any trace in the pc and being fairly cross-platform, among many others.

Types of PuLP integrations (API) to solvers

API means “Application Programming Interface”. PuLP has usually several ways to connect to solvers. Depending on the way it connects to the solver, configuring the connection may vary. We can summarize the integrations in two big groups:

  • Using the command line interface of the solver.

  • Using the python library of the solver.

Not all solvers have a python library, but most have a command line interface. If you want to know which one are you using it’s easy. If the name of the solver API ends with CMD (such as COIN_CMD, CPLEX_CMD, GUROBI_CMD, etc.) it’s the former. Otherwise, it is the latter.

COIN-OR CBC (COIN_CMD)

PuLP no longer ships a CBC executable or the PULP_CBC_CMD class. CBC is used through COIN_CMD, which resolves the cbc / cbc.exe binary in this order:

  1. If the optional cbcbox package is installed (python -m pip install pulp[cbc]), PuLP uses the CBC binary bundled in that wheel.

  2. Otherwise, PuLP looks for cbc / cbc.exe on your PATH (same as any other command-line tool).

You can always pass an explicit path:

import pulp as pl
solver = pl.COIN_CMD(path=r"C:\path\to\cbc.exe")

HiGHS (HiGHS_CMD)

HiGHS_CMD runs the highs executable found on your PATH (or the one given with path=). The optional highsbox package provides one:

python -m pip install pulp[highs_cmd]

To use HiGHS without an executable, install pulp[highs] and use the HiGHS API instead.

Configuring the path to the solver

In order for PuLP to be able to use a solver via the CMD API, the solver needs to be executed by PuLP via the command line. For this to happen one of two things is needed:

  1. The user passes the path to the solver to the solver initialization.

  2. The user has configured the PATH environment variable to the directory where the solver is.

We will do the example for CPLEX in Windows, but the idea is the same for other solvers and other Operating Systems.

Both options imply knowing where the solver is. So first we have to go look for it in the pc. Mine is in C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x64_win64\cplex.exe.

Imagine using the CPLEX_CMD solver, the first one is really simple:

path_to_cplex = r'C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x64_win64\cplex.exe'
import pulp as pl
model = pl.LpProblem("Example", pl.LpMinimize)
_var = model.add_variable('a')
_var2 = model.add_variable('a2')
solver = pl.CPLEX_CMD(path=path_to_cplex)
model += _var + _var2 == 1
result = model.solve(solver)

The only thing to do was to look for the ‘cplex.exe’ file (if you’re in Windows, for Linux and Mac you look for the ‘cplex’ file) and pass the absolute path to the solver.

The second one is a little more cumbersome but you only do it once per machine. You need to configure the PATH environment variable to include the path to the C:\Program Files\IBM\ILOG\CPLEX_Studio128\cplex\bin\x64_win64 directory.

Here is one random guide to editing environment variables in: Windows and Linux or Mac. The idea is that once it is correctly configured you can forget about it (until you change pc or solver version).

Once we have done that, we just do something very similar to the previous example:

import pulp as pl
model = pl.LpProblem("Example", pl.LpMinimize)
_var = model.add_variable('a')
_var2 = model.add_variable('a2')
solver = pl.CPLEX_CMD()
model += _var + _var2 == 1
result = model.solve(solver)

The only difference is that we do not need to tell PuLP where the solver is. The system will find it using the PATH environment variable just as the python example above. Magic!

Additional environment variables per solver

Sometimes, giving the path to the solver is not enough. This can be because the solver needs to know where other files are found (dynamic libraries it will use when running) or the PuLP API needs to import some specific python packages that are deployed with the solver (in case of the solvers that do not have a _CMD at the end).

Whatever the reason, it’s better to be safe than sorry and this means knowing what variables are usually used by which solver. Here are the necessary environment variables that are needed for each solver. The procedure is very similar to what we did with the PATH variable: sometimes you need to edit an existing environment variable and sometimes you need to create a new environment variable. So it looks explicit, I will be using my own paths to variables, but you will have to adapt them to your actual paths (e.g., if the version of the solver is not the same). I will be using my Linux paths, since it just implies copying the last lines of my ~.bashrc file. I’ve adapted them to the Windows command line but, preferably, you would like to edit them via the GUI in windows.

CPLEX

Linux / Mac: add the following lines to the ~.bashrc (or ~.profile or /etc/profile or /etc/bash.bashrc) file:

export CPLEX_HOME="/opt/ibm/ILOG/CPLEX_Studio128/cplex"
export CPO_HOME="/opt/ibm/ILOG/CPLEX_Studio128/cpoptimizer"
export PATH="${PATH}:${CPLEX_HOME}/bin/x86-64_linux:${CPO_HOME}/bin/x86-64_linux"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${CPLEX_HOME}/bin/x86-64_linux:${CPO_HOME}/bin/x86-64_linux"
export PYTHONPATH="${PYTHONPATH}:/opt/ibm/ILOG/CPLEX_Studio128/cplex/python/3.5/x86-64_linux"

Windows: add the following environment variables (via the command line or the graphical user interface):

set CPLEX_HOME=C:/Program Files/IBM/ILOG/CPLEX_Studio128/cplex
set CPO_HOME=C:/Program Files/IBM/ILOG/CPLEX_Studio128/cpoptimizer
set PATH=%PATH%;%CPLEX_HOME%/bin/x64_win64;%CPO_HOME%/bin/x64_win64
set LD_LIBRARY_PATH=%LD_LIBRARY_PATH%;%CPLEX_HOME%/bin/x64_win64;%CPO_HOME%/bin/x64_win64
set PYTHONPATH=%PYTHONPATH%;/opt/ibm/ILOG/CPLEX_Studio128/cplex/python/3.5/x64_win64

GUROBI

Linux / Mac: add the following lines to the ~.bashrc (or ~.profile or /etc/profile or /etc/bash.bashrc) file:

export GUROBI_HOME="/opt/gurobi801/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"

Windows: add the following environment variables (via the command line or graphical user interface):

set GUROBI_HOME=/opt/gurobi801/linux64
set PATH=%PATH%;%GUROBI_HOME%/bin
set LD_LIBRARY_PATH=%LD_LIBRARY_PATH%;%GUROBI_HOME%/lib

XPRESS

Linux / Mac: add the following lines to the ~.bashrc (or ~.profile or /etc/profile or /etc/bash.bashrc) file:

export XPRESSDIR="/opt/xpressmp"
export PATH="${PATH}:${XPRESSDIR}/bin"
export XPAUTH_PATH="${XPRESSDIR}/bin/xpauth.xpr"

Windows: add the following environment variables (via the command line or graphical user interface):

set XPRESSDIR=C:/xpressmp
set PATH=%PATH%;%XPRESSDIR%/bin
set XPAUTH_PATH=%XPRESSDIR%/bin/xpauth.xpr

Configuring where the CMD solvers write their temporary files

In the case of solver APIs that use the command line (again, those that end in CMD), sometimes a user wants to control where the files are written. There are plenty of options.

By default, PuLP does not keep the intermediary files (the *.mps, *.lp, *.mst, *.sol) and they are written in a temporary directory of the operating system. PuLP looks for the TEMP, TMP and TMPDIR environment variables to write the file (in that order). After using them, PuLP deletes them. If you change any of these environment variables before solving, you should be able to choose where you want PuLP to write the results.

import pulp as pl
model = pl.LpProblem("Example", pl.LpMinimize)
_var = model.add_variable('a')
_var2 = model.add_variable('a2')
model += _var + _var2 == 1
solver = pl.COIN_CMD()
result = model.solve(solver)

Another option, is passing the argument keepFiles=True to the solver. With this, the solver creates the files in the current directory and they are not deleted (although they will be overwritten if you re-execute).

import pulp as pl
model = pl.LpProblem("Example", pl.LpMinimize)
_var = model.add_variable('a')
_var2 = model.add_variable('a2')
model += _var + _var2 == 1
solver = pl.COIN_CMD(keepFiles=True)
result = model.solve(solver)

Finally, one can manually edit the tmpDir attribute of the solver object before actually solving.

import pulp as pl
model = pl.LpProblem("Example", pl.LpMinimize)
_var = model.add_variable('a')
_var2 = model.add_variable('a2')
model += _var + _var2 == 1
solver = pl.COIN_CMD()
solver.tmpDir = 'PUT_SOME_ALTERNATIVE_PATH_HERE'
result = model.solve(solver)

Using the official solver API

PuLP has the integrations with the official python API solvers for the following solvers:

  • Mosek (MOSEK)

  • Gurobi (GUROBI)

  • Cplex (CPLEX_PY)

  • Xpress (XPRESS_PY)

  • HiGHS (HiGHS)

  • SCIP (SCIP_PY)

  • COPT (COPT)

  • OR-Tools CP-SAT (CPSAT)

  • OR-Tools GLOP (GLOP)

  • Clarabel (CLARABEL)

  • GLPK (PYGLPK)

  • COIN-OR Clp / Cbc (CYLP)

  • NVIDIA cuOpt (CUOPT)

These API offer a series of advantages over using the command line option:

  • They are usually faster to initialize a problem (they do not involve writing files to disk).

  • They offer a lot more functionality and information (extreme rays, dual prices, reduced costs).

Keyword arguments that a solver does not name itself are handed to the solver as its own parameters. A parameter the solver does not accept raises the error of the solver’s library (PulpSolverError for HiGHS, whose library only reports it by a status).

Installing CPSAT (OR-Tools CP-SAT)

The CPSAT solver uses the ortools Python package (ortools.sat.python.cp_model). Install PuLP with the optional extra:

python -m pip install pulp[ortools]

No separate executable or license file is required. CP-SAT is a constraint programming / MIP-style solver that works on discrete models only:

  • Every variable must have finite lower and upper bounds (unbounded variables raise PulpSolverError).

  • Variable names must be unique within the problem.

  • LpContinuous variables are converted to integers on [ceil(lowBound), floor(upBound)].

  • After solving, the underlying CpModel is available as prob.solverModel.

Example:

import pulp as pl

prob = pl.LpProblem("cpsat_example", pl.LpMinimize)
x = prob.add_variable("x", 0, 10, cat=pl.LpInteger)
y = prob.add_variable("y", 0, 10, cat=pl.LpInteger)
prob += x + 2 * y
prob += x + y >= 7

stats = prob.solve(pl.CPSAT(msg=False, timeLimit=60))
print(stats.status_str, x.value(), y.value())

Optional arguments include warmStart=True (pass current varValue hints), timeLimit (seconds), and any CpSolver.parameters names as keyword arguments (for example num_search_workers=8).

Installing GLOP (OR-Tools GLOP)

The GLOP solver is Google’s simplex LP solver, driven through OR-Tools’ MathOpt API (ortools.math_opt.python.mathopt). It comes with the same extra as CPSAT:

python -m pip install pulp[ortools]

GLOP solves linear programs only. A problem with integer variables (or SOS groups) raises PulpSolverError unless the solver is created with mip=False, which solves its LP relaxation.

Besides the variable values, a solve sets the dual values (constraint.pi), slacks (constraint.slack) and reduced costs (variable.dj). GLOP keeps the model between solves, so prob.resolve() updates it with the edits made since the last solve (new columns and rows, coefficients, bounds, objective) and restarts from the previous basis: column generation and cutting planes need few pivots per round.

prob.solverModel holds the MathOpt Model (.model), the incremental solver (.solver), the parameters of the next solve (.params) and the SolveResult of the last one (.result: basis, solve statistics, primal and dual rays…).

Example:

import pulp as pl

prob = pl.LpProblem("glop_example", pl.LpMaximize)
x = prob.add_variable("x", 0, 4)
y = prob.add_variable("y", 0)
prob += 3 * x + 2 * y
prob += x + y <= 5, "c1"

stats = prob.solve(pl.GLOP(msg=False, timeLimit=60))
print(stats.status_str, x.value(), prob.constraints()[0].pi)

prob += y <= 0.5, "cut"
stats = prob.resolve()  # starts from the previous basis

Optional arguments:

  • timeLimit (seconds), threads and logPath.

  • interrupter: an ortools.util.python.solve_interrupter.SolveInterrupter. Calling its interrupt() (from another thread, or from the callback) stops the solve with status Interrupted.

  • Any field of MathOpt’s SolveParameters (for example iteration_limit=1000, presolve=False, lp_algorithm="dual_simplex") or of GlopParameters (for example use_dual_simplex=True, primal_feasibility_tolerance=1e-9) as keyword arguments.

prob.solve(solver, callback=...) calls the callback with each batch of log lines (a list of str). GLOP has no other callbacks: MathOpt rejects every callback event for it.

Note

OR-Tools ships its own HiGHS library under the same name as the one in the highspy package. When their versions differ, only the first one loaded in a process works: PuLP loads highspy first, so GLOP is not available alongside a highspy built on another HiGHS version.

Installing CLARABEL

The CLARABEL solver is the Clarabel interior point solver, driven through its python package:

python -m pip install pulp[clarabel]

Clarabel solves linear programs only. A problem with integer variables raises PulpSolverError unless the solver is created with mip=False, which solves its LP relaxation.

Besides the variable values, a solve sets the dual values (constraint.pi), slacks (constraint.slack) and reduced costs (variable.dj). Being an interior point method, when the optimum is not unique Clarabel returns a point inside the optimal face rather than a vertex. It has no warm start, so prob.resolve() builds the model again. prob.solverModel holds the Clarabel DefaultSolver.

Example:

import pulp as pl

prob = pl.LpProblem("clarabel_example", pl.LpMaximize)
x = prob.add_variable("x", 0, 4)
y = prob.add_variable("y", 0)
prob += 3 * x + 2 * y
prob += x + y <= 5, "c1"

stats = prob.solve(pl.CLARABEL(msg=False, timeLimit=60))
print(stats.status_str, x.value(), prob.constraints()[0].pi)

Optional arguments:

  • timeLimit (seconds), threads and logPath (the log goes to the file instead of the console).

  • Any field of Clarabel’s DefaultSettings (for example max_iter=500, tol_gap_rel=1e-6, presolve_enable=False) as keyword arguments.

prob.solve(solver, callback=...) calls the callback with Clarabel’s DefaultInfo on every iteration; returning True stops the solve with status Interrupted.

Installing CYLP

The CYLP solver drives COIN-OR Clp (LP) and Cbc (MIP) through the CyLP cython bindings, without writing files to disk. Install PuLP with the optional extra (not available on Windows):

python -m pip install pulp[cylp]

A problem with integer variables is solved by Cbc, unless the solver is created with mip=False, which solves its LP relaxation with Clp. After the solve, prob.solverModel holds a CyCbcModel (MIP) or a CyClpSimplex (LP). For LPs, Clp keeps the model between solves, so prob.resolve() only applies the changes made since the last solve and starts from the previous basis; MIPs are built again.

Example:

import pulp as pl

prob = pl.LpProblem("cylp_example", pl.LpMinimize)
x = prob.add_variable("x", 0, 10, cat=pl.LpInteger)
y = prob.add_variable("y", 0, 10)
prob += x + 2 * y
prob += x + y >= 7.5

stats = prob.solve(pl.CYLP(msg=False, timeLimit=60))
print(stats.status_str, x.value(), y.value())

Optional arguments:

  • gapRel, gapAbs, threads, timeLimit, maxNodes and maxSolutions (used by Cbc).

  • maxIterations and presolve (used by Clp).

  • logPath: the log goes to that file instead of the console.

  • Any attribute of the Clp or Cbc model as a keyword argument (for example primalTolerance=1e-9 for Clp, allowableGap=0.1 for Cbc). resolveMethod="primal" or "dual" forces the simplex used by Clp on a resolve, and perturbation sets Clp’s perturbation.

Installing CUOPT

The CUOPT solver uses NVIDIA cuOpt, a GPU-accelerated LP and MIP solver. It needs an NVIDIA GPU and the cuopt Python package, which is distributed by NVIDIA (it is not a PuLP extra): follow the cuOpt installation instructions for your CUDA version, and also install numpy.

Example:

import pulp as pl

prob = pl.LpProblem("cuopt_example", pl.LpMinimize)
x = prob.add_variable("x", 0, 10, cat=pl.LpInteger)
y = prob.add_variable("y", 0, 10)
prob += x + 2 * y
prob += x + y >= 7.5

stats = prob.solve(pl.CUOPT(msg=False, timeLimit=60))
print(stats.status_str, x.value(), y.value())

Optional arguments include timeLimit, gapRel, warmStart=True (passes the current variable values as a start), logPath, and any cuOpt solver setting as a keyword argument.

Installing PYGLPK

The PYGLPK solver uses the swiglpk Python package, which bundles the GLPK library, so no separate GLPK installation or executable is required. Install PuLP with the optional extra:

python -m pip install pulp[glpk]

Unlike GLPK_CMD, which shells out to the glpsol binary, PYGLPK talks to GLPK directly through its C API, which avoids the overhead of writing files to disk:

import pulp as pl

prob = pl.LpProblem("pyglpk_example", pl.LpMinimize)
x = prob.add_variable("x", 0, 3)
y = prob.add_variable("y", 0, 3)
prob += x + y
prob += x + y >= 2

status = prob.solve(pl.PYGLPK(msg=False))
print(pl.LpStatus[status], x.value(), y.value())

Installing CPLEX_PY

For this solver to work there are two options:

  1. Installing the python package that comes with the CPLEX installation (it is not installed automatically).

  2. (recomended) Adding the path to the package to the PYTHON_PATH environment variable.

The second step is already shown in the example above. The first one requires admin rights, the second one does not.

Installing GUROBI

For this solver to work, the only option is to install the python package that comes with the gurobi installation.

Following my installation paths it would be (Linux):

cd /opt/gurobi801/linux64/
sudo python3 setup.py install

As you can see, it is necessary to have admin rights to install it.

The gurobipy package is also available from PyPI (it includes a size-limited license):

python -m pip install pulp[gurobi]

To build large models faster, also install SciPy (it installs NumPy too):

python -m pip install pulp[gurobi] scipy

With SciPy, GUROBI adds all the constraints to the Gurobi model in a single call. Without it, the constraints are added one by one, which is slower on large models but gives the same model.

Installing SAS94 and SASCAS

The SAS94 and SASCAS solvers send the model to the SAS Optimization procedures, on a SAS 9.4 server through saspy or on SAS Viya (CAS) through swat. Both need access to a licensed SAS installation; install the package that matches it:

python -m pip install saspy   # SAS94
python -m pip install swat    # SASCAS

The connection is configured as usual for saspy / swat (configuration file, or keyword arguments: cfgname / cfgfile for SAS94, and connection options such as hostname, port, username and password for SASCAS).

Installing XPRESS_PY

This solver works by installing the xpress Python package, which includes a runtime distribution of the Xpress Optimizer libraries, and a Community License (free of charge, problem size limits apply).

To install the Xpress Python package:
  • PYPI: pip install xpress

  • CONDA: conda install -c fico-xpress xpress

If you plan to work with larger models and acquire an Academic or Commercial License, contact FICO support to obtain a license file, and then set the XPAUTH_PATH environment variable to the location of your license file.

Linux / Mac: add the following line to the ~.bashrc (or ~.profile or /etc/profile or /etc/bash.bashrc) file:

export XPAUTH_PATH="/path/to/your/xpauth.xpr"

Windows: add the following environment variable (via the command line or graphical user interface):

set XPAUTH_PATH=C:/path/to/your/xpauth.xpr

Even though the XPRESS_PY solver interface in PuLP works for Xpress versions 9.0+, it has been updated and tested using Xpress version 9.8. If you have an older version of Xpress, please consider updating to this version.

License tokens: XPRESS (CMD) acquires and releases a license token per subprocess - once per model.solve() call. XPRESS_PY holds a token for the lifetime of the Python process from the moment xpress is imported. In batch environments where license tokens are shared across jobs, use xp.init() as a context manager around each job to scope the token lifetime and release it between solves.

Using solver-specific functionality

In order to access this functionality, the user needs to use the solver object included inside the PuLP problem. PuLP uses the solverModel attribute on the problem object. This attribute is created and filled when the method buildSolverModel() is executed.

For example, using the CPSAT API we can access the OR-Tools CpModel after solving:

import pulp

prob = pulp.LpProblem("name", pulp.LpMinimize)
x = prob.add_variable("x", 0, 10, cat=pulp.LpInteger)
prob += x

status = prob.solve(pulp.CPSAT(msg=False))
prob.solverModel  # ortools.sat.python.cp_model.CpModel

For example, using the CPLEX_PY API we can access the api object after the solving is done:

import pulp

prob = pulp.LpProblem('name', pulp.LpMinimize)
x = prob.add_variable('x', lowBound=0)
prob += x

solver = pulp.CPLEX_PY()
status = prob.solve(solver)
# you can now access the information from the cplex API python object
prob.solverModel

Also, you can access the python api object before solving by using the lower-level methods:

import pulp

prob = pulp.LpProblem('name', pulp.LpMinimize)
x = prob.add_variable('x', lowBound=0)
prob += x

solver = pulp.CPLEX_PY()
solver.buildSolverModel(prob)
# you can now edit the object or do something with it before solving
# for example, load a MIP_START file for CPLEX_PY:
solver.solverModel.MIP_starts.read(SOME_MST_FILE)
# the, you can call the solver to solve the problem
result = solver.callSolver(prob)
# finally, you fill the PuLP variables with the solution
status, has_solution = solver.findSolutionValues(prob, result)

These three steps work the same way for every solver that uses an official python interface (the subclasses of LpSolver_API).

For more information on how to use the solverModel, one needs to check the official documentation depending on the solver.

Importing and exporting a solver

Exporting a solver can be useful to backup the configuration that was used to solve a model.

In order to export it one needs can export it to a dictionary or a json file:

import pulp
solver = pulp.COIN_CMD()
solver_dict = solver.toDict()

The structure of the returned dictionary is quite simple:

{'keepFiles': 0,
 'mip': True,
 'msg': True,
 'options': [],
 'solver': 'COIN_CMD',
 'timeLimit': None,
 'warmStart': False}

It’s also possible to export it directly to a json file:

solver.toJson("some_file_name.json")

In order to import it, one needs to do:

import pulp
solver = pulp.getSolverFromDict(solver_dict)

Or from a file:

import pulp
solver = pulp.getSolverFromJson("some_file_name.json")

For json, we use the base json package.