GurobiDirect
Methods
|
Returns True if the solver is available. |
Frees local Gurobi resources used by this solver instance. |
|
Frees all Gurobi models used by this solver, and frees the global default Gurobi environment. |
|
|
Solve the problem |
Returns a 4-tuple describing the solver executable version. |
- class pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect(manage_env=False, **kwds)[source]
A direct interface to Gurobi using gurobipy.
- Parameters:
If
manage_env
is set to True, theGurobiDirect
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), theGurobiDirect
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. Theoptions
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
- 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 withmanage_env=False
(the default). To guarantee that all Gurobi resources are freed, all instantiatedGurobiDirect
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 withmanage_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
- solve(*args, **kwds)
Solve the problem
- version()
Returns a 4-tuple describing the solver executable version.