PuLP is one of the modeling frameworks that support Gurobi. In this article, we describe how to use Gurobi through PuLP. For more information, or when you encounter PuLP-related issues, please have a look at the official PuLP website and the specific documentation of their Gurobi interface.
Interfaces to Gurobi
PuLP provides two interfaces to Gurobi. These interfaces transform the PuLP model definition into a Gurobi model. Each interface provides slightly different features and has different default behaviour. Therefore, picking the right interface is an important first step. These are the interfaces available; more details are provided below.
-
GUROBIdirectly translates the model togurobipywithout generating temporary files. This is usually the recommended approach. -
GUROBI_CMDgenerates an LP file describing the model and then callsgurobi_clon the command-line. This approach is usually slower and provides fewer options.
In the sections below, we will describe the details of each of those interfaces. All code snippets assume we have already constructed a PuLP model, e.g.
from pulp import *
prob = LpProblem("Sample problem", LpMinimize)
x = LpVariable("x", 0, 5)
y = LpVariable("y", 0, 4)
prob += x + y >= 1, "c"
prob += 2*x + 3*y, "Obj"Direct interface
Using the interface
To solve models using the direct interface, use the following code:
solver = GUROBI()
prob.solve(solver)Managing the Gurobi environment
By default, PuLP relies on the global (default) Gurobi environment object. The downside is that this environment is created automatically upon first use, but is not disposed until your complete Python script finishes. This may lead to issues when other processes depend on the same license.
You may call solver.close() to ensure the model is being released. Then, you can ask Gurobi to dispose the default environment using gp.disposeDefaultEnv().
Alternatively, you may pass an existing Gurobi environment as the env argument when constructing the solver. PuLP will assume that you are responsible for setting up and disposing this environment. You would still call solver.close() to free the model.
import gurobipy as gp
params = { } # Any environment parameter goes here
with gp.Env(params=params) as env:
solver = GUROBI(env=env)
prob.solve(solver)
solver.close()
# You can keep using the environmentFinally, you may also let PuLP create, manage, and release an environment for you and just provide the options for initializing the environment. In this case, you set the manageEnv=True argument and (optionally) provide environment parameters through the envOptions argument.
params = { } # Any environment parameter goes here
solver = GUROBI(manageEnv=True, envOptions=params)
prob.solve(solver)
solver.close()Setting environment parameters
When PuLP uses the default Gurobi environment, parameters cannot be provided in code and you must rely on a license file. With the other two approaches suggested above, you can provide parameters as demonstrated in the code snippets.
Setting model parameters
Parameters for solving a particular model cannot be provided when calling prob.solve(solver). Instead, provide these parameters as extra keyword arguments when constructing the solver, e.g. GUROBI(TimeLimit=60). Note that PuLP has some special names for various parameters, which will be translated to the corresponding Gurobi parameter automatically. So, you can call GUROBI(timeLimit=60, gapRel=0.01), which will result in the same behaviour as GUROBI(TimeLimit=60, MIPGap=0.01).
Exporting models
PuLP can generate MPS and LP files without relying on Gurobi. This means the files can be generated already before installing Gurobi. PuLP will always use the variable and constraint names you provided while constructing your model; these cannot be anonymized during export.
Use one of the following functions:
prob.writeLP("test.lp")
prob.writeMPS("test.mps", with_objsense=True)Please always use with_objsense=True to avoid the known issue that a maximization objective sense is lost during the model export. To obtain a truly equivalent model export to share with Gurobi Support, please use the ResultFile parameter instead, which instructs Gurobi to export an MPS file when the parameter is set to "test.mps".
Logging
By default, PuLP will not change the Gurobi logging behaviour, meaning you will see output on screen but no log file is created.
- To generate a logfile, set the Gurobi
LogFileparameter or provide thelogPathparameter which has the same effect. - To disable logging to the console, set the
msg=Falseargument when constructing the solver.
Supported model types
PuLP only supports LP and MIP models. Quadratic or general non-linear models are not supported.
Accessing the underlying Gurobi model object
You can access the underlying model object as follows. The example below computes the IIS.
prob.solverModel.computeIIS()Note that you will need to access the individual gurobipy variables and constraints to request the results of such a call, as this information is not fed back to Pyomo variables and constraints. The interface stores the generated Gurobi variables and constraints on the original PuLP variables and constraints:
# Access the Gurobi variable
gurobi_x = x.solverVar
# Store a constraint upon construction
c2 = x + y >= 3
prob += c2, "c2"
# Access the Gurobi constraint (later)
gurobi_c2 = c2.solverConstraintCommand-line interface
Using the interface
To use the command-line interface, use the following approach:
solver = GUROBI_CMD()
prob.solve(solver)Managing the Gurobi environment
Since the command-line interface actually runs Gurobi as an external process, you cannot customize how the Gurobi environment is initialized or disposed. Gurobi will use your license file if available.
Setting model parameters
Provide any solve-related parameters upon construction of the GUROBI_CMD object, in the form of a list consisting of (key, value) pairs. For example, GUROBI_CMD([("LogFile", "test.log")]). These parameters are sent to Gurobi as command-line arguments.
Other notes
- For exporting models, use the same method as above.
- Logging behaviour is the same as above.
- The same model types are supported (only LP and MIP, not quadratic or general non-linear)
- When using the command-line interface, there is no underlying
gurobipymodel representation. Gurobi-specific functionality is not available. Specifically for IIS, you can consider adding theResultFileparameter with a filename with.ilpextension.