GurobiDirect

(class from pyomo.solvers.plugins.solvers.gurobi_direct)

class pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect(manage_env=False, **kwds)[source]

Bases: DirectSolver

A direct interface to Gurobi using gurobipy.

Parameters:
  • manage_env (bool) – Set to True if this solver instance should create and manage its own Gurobi environment (defaults to False)

  • options (dict) – Dictionary of Gurobi parameters to set

If manage_env is set to True, the GurobiDirect object creates a local Gurobi environment and manage all associated Gurobi resources. Importantly, this enables Gurobi licenses to be freed and connections terminated when the solver context is exited:

with SolverFactory('gurobi', solver_io='python', manage_env=True) as opt:
    opt.solve(model)

# All Gurobi models and environments are freed

If manage_env is set to False (the default), the GurobiDirect object uses the global default Gurobi environment:

with SolverFactory('gurobi', solver_io='python') as opt:
    opt.solve(model)

# Only models created by `opt` are freed, the global default
# environment remains active

manage_env=True is required when setting license or connection parameters programmatically. The options argument is used to pass parameters to the Gurobi environment. For example, to connect to a Gurobi Cluster Manager:

options = {
    "CSManager": "<url>",
    "CSAPIAccessID": "<access-id>",
    "CSAPISecret": "<api-key>",
}
with SolverFactory(
    'gurobi', solver_io='python', manage_env=True, options=options
) as opt:
    opt.solve(model)  # Model solved on compute server
# Compute server connection terminated
__init__(manage_env=False, **kwds)[source]

Constructor

Methods

__init__([manage_env])

Constructor

available([exception_flag])

Returns True if the solver is available.

close()

Frees local Gurobi resources used by this solver instance.

close_global()

Frees all Gurobi models used by this solver, and frees the global default Gurobi environment.

config_block([init])

default_variable_value()

has_capability(cap)

Returns a boolean value representing whether a solver supports a specific feature.

license_is_valid()

True if the solver is present and has a valid license (if applicable)

load_duals([cons_to_load])

Load the duals into the 'dual' suffix.

load_rc(vars_to_load)

Load the reduced costs into the 'rc' suffix.

load_slacks([cons_to_load])

Load the values of the slack variables into the 'slack' suffix.

load_vars([vars_to_load])

Load the values from the solver's variables into the corresponding pyomo variables.

problem_format()

Returns the current problem format.

reset()

Reset the state of the solver

results_format()

Returns the current results format.

set_callback(name[, callback_fn])

Set the callback function for a named callback.

set_options(istr)

set_problem_format(format)

Set the current problem format (if it's valid) and update the results format to something valid for this problem format.

set_results_format(format)

Set the current results format (if it's valid for the current problem format).

solve(*args, **kwds)

Solve the problem

version()

Returns a 4-tuple describing the solver executable version.

warm_start_capable()

True is the solver can accept a warm-start solution

Attributes

keepfiles

log_file

soln_file

suffixes

symbolic_solver_labels

tee

warm_start_file_name

warm_start_solve

results

A results object return from the solve method.

Member Documentation

available(exception_flag=True)[source]

Returns True if the solver is available.

Parameters:

exception_flag (bool) – If True, raise an exception instead of returning False if the solver is unavailable (defaults to False)

In general, available() does not need to be called by the user, as the check is run automatically when solving a model. However it is useful for a simple retry loop when using a shared Gurobi license:

with SolverFactory('gurobi', solver_io='python') as opt:
    while not available(exception_flag=False):
        time.sleep(1)
    opt.solve(model)
close()[source]

Frees local Gurobi resources used by this solver instance.

All Gurobi models created by the solver are freed. If the solver was created with manage_env=True, this method also closes the Gurobi environment used by this solver instance. Calling .close() achieves the same result as exiting the solver context (although using context managers is preferred where possible):

opt = SolverFactory('gurobi', solver_io='python', manage_env=True)
try:
    opt.solve(model)
finally:
    opt.close()
# Gurobi models and environments created by `opt` are freed

As with the context manager, if manage_env=False (the default) was used, only the Gurobi models created by this solver are freed. The default global Gurobi environment will still be active:

opt = SolverFactory('gurobi', solver_io='python')
try:
    opt.solve(model)
finally:
    opt.close()
# Gurobi models created by `opt` are freed; however the
# default/global Gurobi environment is still active
close_global()[source]

Frees all Gurobi models used by this solver, and frees the global default Gurobi environment.

The default environment is used by all GurobiDirect solvers started with manage_env=False (the default). To guarantee that all Gurobi resources are freed, all instantiated GurobiDirect solvers must also be correctly closed.

The following example will free all Gurobi resources assuming the user did not create any other models (e.g. via another GurobiDirect object with manage_env=False):

opt = SolverFactory('gurobi', solver_io='python')
try:
    opt.solve(model)
finally:
    opt.close_global()
# All Gurobi models created by `opt` are freed and the default
# Gurobi environment is closed
has_capability(cap)

Returns a boolean value representing whether a solver supports a specific feature. Defaults to ‘False’ if the solver is unaware of an option. Expects a string.

Example: # prints True if solver supports sos1 constraints, and False otherwise print(solver.has_capability(‘sos1’)

# prints True is solver supports ‘feature’, and False otherwise print(solver.has_capability(‘feature’)

Parameters:

cap (str) – The feature

Returns:

val – Whether or not the solver has the specified capability.

Return type:

bool

license_is_valid()

True if the solver is present and has a valid license (if applicable)

load_duals(cons_to_load=None)[source]

Load the duals into the ‘dual’ suffix. The ‘dual’ suffix must live on the parent model.

Parameters:

cons_to_load (list of Constraint)

load_rc(vars_to_load)[source]

Load the reduced costs into the ‘rc’ suffix. The ‘rc’ suffix must live on the parent model.

Parameters:

vars_to_load (list of Var)

load_slacks(cons_to_load=None)[source]

Load the values of the slack variables into the ‘slack’ suffix. The ‘slack’ suffix must live on the parent model.

Parameters:

cons_to_load (list of Constraint)

load_vars(vars_to_load=None)

Load the values from the solver’s variables into the corresponding pyomo variables.

Parameters:

vars_to_load (list of Var)

problem_format()

Returns the current problem format.

reset()

Reset the state of the solver

results_format()

Returns the current results format.

set_callback(name, callback_fn=None)

Set the callback function for a named callback.

A call-back function has the form:

def fn(solver, model):

pass

where ‘solver’ is the native solver interface object and ‘model’ is a Pyomo model instance object.

set_problem_format(format)

Set the current problem format (if it’s valid) and update the results format to something valid for this problem format.

set_results_format(format)

Set the current results format (if it’s valid for the current problem format).

solve(*args, **kwds)

Solve the problem

version()

Returns a 4-tuple describing the solver executable version.

warm_start_capable()[source]

True is the solver can accept a warm-start solution

results

A results object return from the solve method.