Introduction
Gurobi can be accessed through our own API in many programming languages, including gurobipy for Python. Although we recommend using the official APIs, many of our users come from a situation where they already have implemented a model in an open-source modeling framework and wish to solve that model with Gurobi without rewriting their code. This article describes how to do that for the most popular modeling frameworks
Comparison with official Gurobi APIs
There are important advantages to using the Gurobi APIs:
- They are officially supported by our R&D and Experts team
- They expose all Gurobi functionality, while much of that is hidden by modeling frameworks
- They avoid any overhead (runtime and memory) caused by handing over the model from framework to Gurobi
- They always work with the latest version of Gurobi
Using an existing model in an open-source modeling framework
This article won't go into detail on each modeling framework, but provides a high-level overview of the steps required to get started using Gurobi in each framework.
Pyomo
- Install the gurobipy package; you don't need the full Gurobi installation
- Create or download your Gurobi license file
- Create a solver instance using
opt = SolverFactory("gurobi_direct")
; you may need to importSolverFactory
frompyomo.opt
. Note that you can choose to either let Pyomo manage the creation of a Gurobi environment, or use the default environment. The first option is useful in case you want to provide environment parameter programmatically (e.g. WLS or Instant Cloud keys) without relying on a license file. - Use that solver to optimize your model using
results = opt.solve(model, ...)
- If you plan on solving the model more than once (making changes in between), it is recommended to use either
SolverFactory("gurobi_persistent")
or the APPSI interface. When using the latter, make sure to release the Gurobi license withopt.release_license()
after you've solved your model.
PuLP
- Install the gurobipy package; you don't need the full Gurobi installation
- Create or download your Gurobi license file
- Assuming you have
import pulp as pl
and your model is defined in variableprob
, solve your model usingprob.solve(pl.GUROBI())
- From PuLP v2.8.0 you can use gurobipy environments via the use of the parameters
env
ormanageEnv
:- Option 1 (manage and provide an environment yourself):
import gurobipy as gp
import pulp as pl
options = {
"WLSACCESSID": "access-id",
"WLSSECRET": "secret",
"LICENSEID": "license-id",
}
with gp.Env(params=options) as env:
# Pass environment as a parameter
solver = pl.GUROBI(env=env)
prob.solve(solver)
solver.close() # dispose the model
-
- Option 2 (let PuLP manage the Gurobi environment):
import pulp as pl
options = {
"WLSACCESSID": "access-id",
"WLSSECRET": "secret",
"LICENSEID": "license-id",
}
solver = pl.GUROBI(manageEnv=True, envOptions=options)
prob.solve(solver)
solver.close()
Google OR-Tools
- Install the full Gurobi software; OR-Tools will not work if you only have gurobipy available
- Create or download your Gurobi license file
- At runtime, OR-Tools will search for the Gurobi shared library in the default install path of the Gurobi installers, or by using the
GUROBI_HOME
environment variable. - Import the required package using
from ortools.linear_solver import pywraplp
- Create your model solver using
solver = pywraplp.Solver.CreateSolver("GUROBI")
- Once the model is defined properly, call
status = solver.Solve()
to solve it with Gurobi
PythonMIP
- Install the full Gurobi software; PythonMIP will not work if you only have gurobipy available
- During installation, make sure to define the
GUROBI_HOME
environment variable - Create or download your Gurobi license file
- PythonMIP will use Gurobi automatically
CVXPY
- Install the gurobipy package; you don't need the full Gurobi installation
- Create or download your Gurobi license file
- Assuming you have
import cvxpy as cp
and your model is defined in variableprob
, solve your model usingprob.solve(solver=cp.GUROBI())
Comments
0 comments
Article is closed for comments.