Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::math_opt::GurobiSolver Class Reference

#include <gurobi_solver.h>

Inheritance diagram for operations_research::math_opt::GurobiSolver:
operations_research::math_opt::SolverInterface

Public Member Functions

absl::StatusOr< SolveResultProto > Solve (const SolveParametersProto &parameters, const ModelSolveParametersProto &model_parameters, MessageCallback message_cb, const CallbackRegistrationProto &callback_registration, Callback cb, const SolveInterrupter *interrupter) override
 
absl::StatusOr< bool > Update (const ModelUpdateProto &model_update) override
 
absl::StatusOr< ComputeInfeasibleSubsystemResultProto > ComputeInfeasibleSubsystem (const SolveParametersProto &parameters, MessageCallback message_cb, const SolveInterrupter *interrupter) override
 
- Public Member Functions inherited from operations_research::math_opt::SolverInterface
 SolverInterface ()=default
 
 SolverInterface (const SolverInterface &)=delete
 
SolverInterfaceoperator= (const SolverInterface &)=delete
 
virtual ~SolverInterface ()=default
 

Static Public Member Functions

static absl::StatusOr< std::unique_ptr< GurobiSolver > > New (const ModelProto &input_model, const SolverInterface::InitArgs &init_args)
 

Additional Inherited Members

- Public Types inherited from operations_research::math_opt::SolverInterface
using MessageCallback = std::function<void(const std::vector<std::string>&)>
 
using Callback
 
using Factory
 

Detailed Description

Definition at line 52 of file gurobi_solver.h.

Member Function Documentation

◆ ComputeInfeasibleSubsystem()

absl::StatusOr< ComputeInfeasibleSubsystemResultProto > operations_research::math_opt::GurobiSolver::ComputeInfeasibleSubsystem ( const SolveParametersProto & parameters,
MessageCallback message_cb,
const SolveInterrupter * interrupter )
overridevirtual
Todo
(b/277339044): Remove code duplication with GurobiSolver::Solve().

Need to run GRBupdatemodel before:

  1. setting parameters (to test if the problem is a MIP)
  2. registering callbacks (to test if the problem is a MIP),

Gurobi will silently impose that indicator variables are binary even if not so specified by the user in the model. We return an error here if this is the case to be consistent across solvers.

We must set the parameters before calling RegisterCallback since it changes some parameters depending on the callback registration.

We use a local interrupter that will triggers the calls to GRBterminate() when either the user interrupter is triggered or when a callback returns a true terminate.

Make an immediate call to GRBterminate() as soon as this interrupter is triggered (which may immediately happen in the code below when it is chained with the optional user interrupter).

This call may happen too early. This is not an issue since we will repeat this call at each call of the Gurobi callback. See the comment in GurobiCallbackImpl() for details.

Chain the user interrupter to the local interrupter. If/when the user interrupter is triggered, this triggers the local interrupter. This may happen immediately if the user interrupter is already triggered.

The local interrupter can also be triggered by a callback returning a true terminate.

Here we register the callback when we either have a message callback or a local interrupter. The rationale for doing so when we have only an interrupter is explained in GurobiCallbackImpl().

We flush message callbacks before testing for Gurobi error in case where the unfinished line of message would help with the error.

Reset Gurobi parameters.

Todo
(b/277246682): ensure that resetting parameters does not degrade incrementalism performance.

Implements operations_research::math_opt::SolverInterface.

Definition at line 3037 of file gurobi_solver.cc.

◆ New()

absl::StatusOr< std::unique_ptr< GurobiSolver > > operations_research::math_opt::GurobiSolver::New ( const ModelProto & input_model,
const SolverInterface::InitArgs & init_args )
static

Definition at line 2715 of file gurobi_solver.cc.

◆ Solve()

absl::StatusOr< SolveResultProto > operations_research::math_opt::GurobiSolver::Solve ( const SolveParametersProto & parameters,
const ModelSolveParametersProto & model_parameters,
MessageCallback message_cb,
const CallbackRegistrationProto & callback_registration,
Callback cb,
const SolveInterrupter * interrupter )
overridevirtual

Solves the current model (included all updates).

All input arguments are ensured (by solver.cc) to be valid. Furthermore, since all parameters are references or functions (which could be a lambda expression), the implementation should not keep a reference or copy of them, as they may become invalid reference after the invocation if this function.

Parameters message_cb, cb and interrupter are optional. They are nullptr when not set.

When parameter message_cb is not null the value of parameters.enable_output should be ignored the solver should behave as it is was false (i.e. not print anything).

When parameter message_cb is not null and the underlying solver does not supports message callbacks, it should ignore it.

Solvers should return a InvalidArgumentError when called with events on callback_registration that are not supported by the solver for the type of model being solved (for example MIP events if the model is an LP, or events that are not emitted by the solver). Solvers should use CheckRegisteredCallbackEvents() to implement that.

Need to run GRBupdatemodel before:

  1. setting parameters (to test if the problem is a MIP)
  2. registering callbacks (to test if the problem is a MIP),
  3. setting basis and getting the obj sense. We just run it first.

Gurobi returns "infeasible" when bounds are inverted.

Gurobi will silently impose that indicator variables are binary even if not so specified by the user in the model. We return an error here if this is the case to be consistent across solvers.

We must set the parameters before calling RegisterCallback since it changes some parameters depending on the callback registration.

We use a local interrupter that will triggers the calls to GRBterminate() when either the user interrupter is triggered or when a callback returns a true terminate.

Make an immediate call to GRBterminate() as soon as this interrupter is triggered (which may immediately happen in the code below when it is chained with the optional user interrupter).

This call may happen too early. This is not an issue since we will repeat this call at each call of the Gurobi callback. See the comment in GurobiCallbackImpl() for details.

Chain the user interrupter to the local interrupter. If/when the user interrupter is triggered, this triggers the local interrupter. This may happen immediately if the user interrupter is already triggered.

The local interrupter can also be triggered by a callback returning a true terminate.

We select a value of "1" here, which means that the lazy constraints will be separated at feasible solutions, and that Gurobi has latitude to select which violated constraints to add to the model if multiple are violated. This seems like a reasonable default choice for us, but we may want to revisit later (or expose this choice to the user).

Here we register the callback when we either have a user callback or a local interrupter. The rationale for doing so when we have only an interrupter is explained in GurobiCallbackImpl().

We flush message callbacks before testing for Gurobi error in case where the unfinished line of message would help with the error.

Reset Gurobi parameters.

Todo
(b/277246682): ensure that resetting parameters does not degrade incrementalism performance.

Implements operations_research::math_opt::SolverInterface.

Definition at line 2905 of file gurobi_solver.cc.

◆ Update()

absl::StatusOr< bool > operations_research::math_opt::GurobiSolver::Update ( const ModelUpdateProto & model_update)
overridevirtual

Updates the model to solve and returns true, or returns false if this update is not supported.

The implementation should assume the input ModelUpdate is valid and is free to assert if this is not the case.

As of 2022-12-06 we do not support incrementalism for multi-objective models: not adding/deleting/modifying the auxiliary objectives...

...or modifying the primary objective of a multi-objective model.

Now we update quadratic_objective_coefficients_, removing any terms where either one or both of the involved variables are about to be deleted.

Note
Introducing more state and complexity should speed this up, but we opt for the simpler approach for now.

We cache all Gurobi variables and constraint indices that must be deleted, and perform deletions at the end of the update call.

Otherwise the constraint is not actually registered with Gurobi.

If we are removing variables or constraints we remove them after adding any variable or constraint. This is to avoid problems with the numbering of possibly new variables and constraints. After that we must update the model so that sequence of updates don't interfere with one-another.

Synchronize all pending changes.

Implements operations_research::math_opt::SolverInterface.

Definition at line 2487 of file gurobi_solver.cc.


The documentation for this class was generated from the following files: