What is the Gurobi solver in Python PuLP?

The Gurobi solver

Gurobi is an optimization software developed by Gurobi Optimization, LLC. It is considered one of the most diverse optimization software that can solve a wide range of problem types. These include linear programming, mixed-integer linear programming, quadratic programming, mixed-integer quadratic programming, quadratically constrained programming, and mixed-integer quadratically constrained programming. It is accessible in the Python PuLP library through an API.

Set up the Gurobi solver in PuLP

PuLP uses an API solver from a list of available optimizers to solve a given linear programming problem. We use PuLP's listSolvers() method to view the list of solver APIs it can access:

print(listSolvers())
print(listSolvers(onlyAvailable = True))

Explanation

  • Line 1: We print a list of solvers accessible through PuLP. We can see that GUROBI is on this list. However, this does not mean that Gurobi is necessarily available for use.
  • Line 2: We print a list of solvers currently available to PuLP by passing the onlyAvailable = True argument to PuLP's listSolvers() method. This is the second list in our output. We can see that GUROBI is on this list as well. This confirms that Gurobi is available for use in our environment.

Note: If you are attempting to use Gurobi with PuLP on your local machine or any other environment, and don't see GUROBI in the list returned by the listSolvers(onlyAvailable = True) command, try to run pip install gurobipy on your shell. This will install a "Restricted license" version of Gurobi, like the one we have installed here, as shown in the output between the two lists.

Specify a solver in PuLP

To use a particular solver in PuLP, the user needs to specify the solver in the code. PuLP's solve() method allows users to specify their solver of choice. A solver instance can be loaded using the PuLP getSolver() method. The code below shows different ways to use the Gurobi solver using PuLP:

# using "solve()"
prob.solve(GUROBI(msg = True))
print("******************************************")
# using "getSolver()"
solver = getSolver('GUROBI')
solver.solve(prob)
print("******************************************")
# or
solver = GUROBI()
solver.solve(prob)
print("******************************************")
# we can also call the solver directly
GUROBI().solve(prob)

Explanation

  • Line 2: We provide a way to specify our solver of choice at the time of solving prob—an instance of PuLP's LpProblem class—by passing it as an argument to the solve() method.
  • Lines 5–6 and 9–10: We provide a way to load a solver instance to the solver variable and then pass it as an argument to the solve() method.
  • Line 13: We provide a way to call a solver directly without storing it in a variable. In this case, the LpProblem instance prob is passed to the solver's solve() method.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved