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
For several modeling frameworks we have dedicated pages that show exactly how to use Gurobi through the framework, the caveats to be aware of and how to accomplish common tasks:
For several other modeling framework, below we provide a high-level overview of the steps required to get started using Gurobi in each framework.
Google OR-Tools
- Do the full installation of Gurobi Optimizer; OR-Tools will not work if you only have gurobipy available
- Make sure you are installing the most recent release of OR-Tools; this is currently only available via pip install (PyPI), and not via conda
- 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_HOMEenvironment 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_HOMEenvironment 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 cpand your model is defined in variableprob, solve your model usingprob.solve(solver=cp.GUROBI())