Gurobi

Initial setup

from pyoptinterface import gurobi

model = gurobi.Model()

You need to follow the instructions in Getting Started to set up the optimizer correctly.

If you want to manage the license of Gurobi manually, you can create a gurobi.Env object and pass it to the constructor of the gurobi.Model object, otherwise we will initialize an implicit global gurobi.Env object automatically and use it.

env = gurobi.Env()

model = gurobi.Model(env)

For example, you can set the parameter of the gurobi.Env object to choose the licensing behavior.

env = gurobi.Env(empty=True)
env.set_raw_parameter("ComputeServer", "myserver1:32123")
env.set_raw_parameter("ServerPassword", "pass")
env.start()

model = gurobi.Model(env)

The capability of gurobi.Model

Supported constraints

Supported model attribute

Attribute

Get

Set

Name

ObjectiveSense

DualStatus

PrimalStatus

RawStatusString

TerminationStatus

BarrierIterations

DualObjectiveValue

NodeCount

NumberOfThreads

ObjectiveBound

ObjectiveValue

RelativeGap

Silent

SimplexIterations

SolverName

SolverVersion

SolveTimeSec

TimeLimitSec

Supported variable attribute

Attribute

Get

Set

Value

LowerBound

UpperBound

Domain

PrimalStart

Name

Supported constraint attribute

Attribute

Get

Set

Name

Primal

Dual

Solver-specific operations

Parameter

For solver-specific parameters, we provide get_raw_parameter and set_raw_parameter methods to get and set the parameters.

model = gurobi.Model()

# get the value of the parameter
value = model.get_raw_parameter("TimeLimit")

# set the value of the parameter
model.set_raw_parameter("TimeLimit", 10.0)

Attribute

Gurobi supports a lot of attributes for the model, variable, and constraint. We provide methods to get or set the value of the attribute.

  • Model attribute: model.get_model_raw_attribute(name: str) and model.set_model_raw_attribute(name: str, value: Any)

  • Variable attribute: model.get_variable_raw_attribute(variable, name: str) and model.set_variable_raw_attribute(variable, name: str, value: Any)

  • Constraint attribute: model.get_constraint_raw_attribute(constraint, name: str) and model.set_constraint_raw_attribute(constraint, name: str, value: Any)