Gurobi#
Gurobi is a powerful commercial suite of optimization products, which include simplex, parallel and nonlinear barrier, and primal-dual hybrid gradient (PDHG) solvers to handle linear programming (LP), quadratic programming (QP), and nonlinear programming (NLP); also quadratically constrained (QCP, SOCP) and the mixed-integer variations thereof (MILP, MIQP, MIQCP, MISOCP, MINLP). The (MI)NLP capability is provided via nonlinear expressions (formulas). The MP Library used by the AMPL Gurobi driver supports automatic reformulation for many expression types; the modeling guide can be found here.
Product Page | Modeling guide | Features | Options | Changes
Download Gurobi | Start a Gurobi Trial Now!
Our enhanced Gurobi driver is now the default. The new driver provides significantly extended modeling support for logical and nonlinear operators natively through Gurobi’s built-in nonlinear and “general constraints” and through linearizations performed by the MP Library. There are two binaries in this package: gurobi [options] is the new version, gurobiasl [options] is the legacy version. If you are upgrading an existing installation and encounter any issues with the new version please report them to support@ampl.com.
How to use it#
How to install using amplpy:
# Install Python API for AMPL:
$ python -m pip install amplpy --upgrade
# Install AMPL & solver modules:
$ python -m amplpy.modules install gurobi # install Gurobi
# Activate your license (e.g., free ampl.com/ce or ampl.com/academia licenses):
$ python -m amplpy.modules activate <your-license-uuid>
How to use:
from amplpy import AMPL
ampl = AMPL()
...
ampl.solve(solver="gurobi",
gurobi_options="option1=value1 option2=value2")
Learn more about what we have to offer to implement and deploy Optimization in Python.
AMPL APIs give access to AMPL from a programming language:
AMPLS (AMPL solver libraries) load an AMPL model
into the solver in C++ or Python and give direct access to the solver’s
own API, for example for callbacks. Python packages: amplpy-gurobi,
amplpy-cplex, amplpy-xpress, amplpy-copt.
ampl: option solver gurobi; # Select solver
ampl: option gurobi_options 'option1=value1 option2=value2';
ampl: solve;
Resources#
Features#
Problem types and algorithms#
Problem types
LP, QP, QCP, SOCP, NLP
MILP, MIQP, MIQCP, MISOCP, MINLP
Received natively: and, or, indicator constraints, min, max, abs,
piecewise-linear functions, SOS; quadratic terms; exp, log, x^a, a^x,
/, sin, cos, tan, tanh, logistic and signed power. MP reformulates
the other constructs.
All constructs: native constraints and expressions by driver.
LP and convex QP: simplex, barrier, PDHG
alg:method: -1 automatic (default), 0 primal simplex, 1 dual simplex,
2 barrier, 3/4 concurrent, 6 PDHG (first-order). PDHG on a GPU:
alg:pdhggpu=1. After barrier, bar:crossover=0 returns the interior
solution without a basis.
MIP: branch-and-cut
Relative and absolute gaps: mip:gap, mip:gapabs. Search focus:
mip:focus. Most other controls are mip:... and cut:... options.
Nonconvex and nonlinear models: global
Nonconvex quadratic terms and nonlinear functions are solved to global
optimality by spatial branch-and-bound (qp:nonconvex, default -1).
Gurobi’s piecewise-linear approximation of nonlinear functions remains
available with pre:funcnonlinear=-1 acc:_expr=0.
Nonlinear models: local
alg:optimalitytarget=1 uses Gurobi’s nonlinear barrier, which returns a
local optimum without a gap. Only for continuous models: add alg:relax=1
to solve the relaxation of a model with integer variables. Its iteration
limit nlbar:iterlim (default 1000) can stop large models before a
feasible point; raise it together with lim:time.
ampl.solve(solver="gurobi", gurobi_options="optimalitytarget=1 nlbar:iterlim=100000")
ampl: option solver gurobi;
ampl: option gurobi_options 'optimalitytarget=1 nlbar:iterlim=100000';
ampl: solve;
Instrumentation#
For a generic summary of the options and features used most often, see Important features in the MP documentation.
For all models
Model export
Option tech:writemodel. Details
Solution export
Option tech:writesolution. Details
Feasibility relaxation
Option alg:feasrelax. Details
Big-M for relaxing indicator constraints: alg:feasrelaxbigm.
Irreducible inconsistent subset (IIS)
Option iis:find. Details
Method: iis:method; IIS even for a feasible model part: iis:force.
Basis input and output
Option alg:basis. Details
Condition number (kappa)
Option alg:kappa. Details
Exact computation: alg:kappa_exact.
Multiple solutions
Option sol:stub. Details
Multiple objectives
Option obj:multi. Details
Native hierarchical and blended objectives; obj:multi:method, obj:multi:inheritparams.
Sensitivity analysis
Option alg:sens. Details
Unbounded rays and Farkas certificates
Option alg:rays. Details
Warm start
Option alg:start. Details
For MIP models
Fixed model (duals and basis for a MIP)
Option mip:basis. Details
Algorithm for the fixed model: mip:fixedmethod.
Lazy constraints and user cuts
Option mip:lazy. Details
MIP gap
Option mip:return_gap. Details
Best dual bound
Option mip:bestbound. Details
Branching priorities
Option mip:priorities. Details
Stop on a plateau
Option mip:plateau:time. Details
Stop on a plateau of the MIP gap
Option mip:plateau:relgaptol. Details
Gurobi options#
Full list of AMPL/Gurobi options:
Many solver parameters can be changed directly from AMPL, by specifying them as a space-separated string in the AMPL option gurobi_options, or mp_options.
A list of all supported solver options is available here or can be obtained by executing the solver driver with the -= command line parameter:
import os
exit_code = os.system("gurobi -=")
ampl: shell "gurobi -=";
$ gurobi -=
Solver options can have multiple aliases, to accomodate for different user types.
The main nomenclature is given first in the -= output, then followed by aliases in brackets,
see for example the listing for lim:iter:
lim:iter (iterlim, iterlimit)
Iteration limit (default: no limit).
The main nomenclature contains a prefix (lim: in this case) to help categorize and find the
options relevant to a context. To list only the options with a specific prefix (lim: for this example),
run:
gurobi -=lim:
More details on solver options: MP solver options.
Specify solver options and solve#
After formulating the model in AMPL, execute the following to select gurobi as the solver and pass the two options:
return_mipgap=3 and outlev=1.
ampl.option["show_stats"] = 2
ampl.solve(solver="gurobi",
mp_options="return_mipgap=3 outlev=1", # or: gurobi_options
verbose=True)
ampl: option solver gurobi; # Select solver
ampl: option mp_options 'return_mipgap=3 outlev=1';
ampl: solve [>output.log]; # Solve [redirect output]
$ [auxfiles=rc] ampl -obmodel snapshot.run # Produce an NL file
# [with name files]
$ mp_options='lim:time=100' gurobi model # Version 1
$ gurobi_options='kappa_exact=1' gurobi model # Version 2
$ gurobi model lim:time=100 wantsol=7 # Version 3
Retrieve solutions#
The outcome of the last optimization is stored in the AMPL parameter solve_result_num and the related message in
solve_result.
print(ampl.solve_result, ampl.solve_result_num)
ROI_Values = ampl.var['ROI_Level'].to_pandas()
ampl.param["a"] = ampl2.var["x"].get_values()
ampl.eval(r" x >(“obj” & n & “.out”) ;") #save solution in a .out file
ampl: display solve_result_num, solve_result;
ampl: display _varname, _var, _conname, _con.slack;
ampl: display _logconname, min {i in 1.._nlogcons} _logcon[i].val;
# Logical constraints
ampl: display _cconname, _ccon.slack; # Complementarities
$ gurobi model lim:time=100 wantsol=6 # Print solution
$ gurobi model timing=2 wantsol=1 # Save .sol file
# to be explored in AMPL
Gurobi solve result codes can be obtained by running gurobi -! or ampl: shell "gurobi -!";:
Solve result table for Gurobi 13.0.3
0- 99 solved: optimal for an optimization problem,
feasible for a satisfaction problem
100-199 solved? solution candidate returned but error likely
120 locally optimal solution
150 solved? MP solution check failed (option sol:chk:fail)
200-299 infeasible
220 locally infeasible solution
300-349 unbounded, feasible solution returned
350-399 unbounded, no feasible solution returned
400-449 limit, feasible: stopped, e.g., on iterations or Ctrl-C
401 interrupted, feasible solution
402 time limit, feasible solution
403 iteration limit, feasible solution
404 node limit, feasible solution
405 bestobjstop or bestbndstop reached, feasible solution
408 solution limit
409 work limit, feasible solution
410 soft memory limit, feasible solution
450-469 limit, problem is either infeasible or unbounded.
Disable dual reductions or run IIS finder for definitive answer.
470-499 limit, no solution returned
471 interrupted, without a feasible solution
472 time limit, without a feasible solution
473 iteration limit, without a feasible soluton
474 node limit, without a feasible soluton
475 objective cutoff
477 bestbndstop reached, without a feasible solution
479 work limit, without a feasible solution
480 soft memory limit, without a feasible solution
500-999 failure, no solution returned
550 failure: numeric issue, no feasible solution
601 Could not talk to Gurobi Instant Cloud or Gurobi Server.
602 Job rejected by Gurobi Instant Cloud or Gurobi Server.
603 No license for specified Gurobi Instant Cloud or Gurobi Server.
604 Surprise failure while starting the cloud/server environment.
605 Bad value for cloudid or cloudkey, or Gurobi Cloud out of reach.
Solve result codes of the legacy driver gurobiasl
Solve result table (AMPL default ranges)
0- 99 solved: optimal for an optimization problem, feasible for a satisfaction problem
100-199 solved? solution candidate returned but error likely
200-299 infeasible
300-399 unbounded
400-499 limit
500-999 failure, no solution returned
For general information, see MP result codes guide.
Handle infeasibility#
Irreducible inconsistent set (IIS)#
When a model is infeasible, one useful information kind is the IIS, which are subsets of constraints that are incompatible. See the details here.
Feasibility relaxation#
Another approach to tackle infeasibilities is the feasibility relaxation to find a solution which only penalizes infeasibilities. See the details here.
Gurobi compute server and Gurobi cloud#
Gurobi supports solving your model on other machines in two alternative ways:
Compute server - where a computer (or a cluster) can be configured to the specific task of solving models via Gurobi
Gurobi cloud - where the compute server is by Gurobi itself
To use a compute server, the option tech:server must be set, together with appropriate values for the tech:server_* options.
For Gurobi instant cloud, the options tech:cloudid and tech:cloudkey must be set, and optionally the other options tech:cloud*, for example:
# Solve with compute server
option gurobi_options "tech:server=192.168.1.55";
solve;
# Solve with Gurobi instant cloud
option gurobi_options "tech:cloudid=mygurobiid tech:cloudkey=myprivatekey";
solve;
Changelog#
- GUROBI Changelog
- 20261029
- 20260908
- 20260624
- 20260515
- 20260414
- 20260130
- 20260109
- 20251213
- 20251121
- 20251021
- 20251015
- 20250814
- 20250801
- 20250723
- 20250617
- 20250515
- 20250429
- 20250424
- 20250329
- 20250311
- 20250308
- 20250204
- 20241119
- 20241114
- 20240808
- 20240728
- 20240724
- 20240617
- 20240604
- 20240529
- 20240518
- 20240429
- 20240327
- 20240320
- 20240311
- 20240310
- 20240115
- 20231208
- 20231206
- 20231117
- 20231103
- 20231017
- 20230920
- 20230919
- 20230831
- 20230817
- 20230728
- 20230726
- 20230724
- 20230625
- 20230621
- 20230616
- 20230531
- 20230522
- 20230426
- 20230330
- 20230321
- 20230207
- 20230206
- 20221228
- 20221222
- 20221211
- 20221113
- 20221012
- 20220928
- 20220802
- 20220725
- 20220720
- 20220706
- 20220511
- 20220408
- 20220303
- 20220217
- 20220202
- 20220128