Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::GurobiInterface Class Reference
Inheritance diagram for operations_research::GurobiInterface:
operations_research::MPSolverInterface

Public Member Functions

 GurobiInterface (MPSolver *solver, bool mip)
 Constructor that takes a name for the underlying GRB solver.
 
 ~GurobiInterface () override
 
void SetOptimizationDirection (bool maximize) override
 Sets the optimization direction (min/max).
 
MPSolver::ResultStatus Solve (const MPSolverParameters &param) override
 
std::optional< MPSolutionResponse > DirectlySolveProto (const MPModelRequest &request, std::atomic< bool > *interrupt) override
 
void Write (const std::string &filename) override
 Writes the model.
 
void Reset () override
 ---— Model modifications and extraction --—
 
void SetVariableBounds (int var_index, double lb, double ub) override
 Modifies bounds.
 
void SetVariableInteger (int var_index, bool integer) override
 Modifies integrality of an extracted variable.
 
void SetConstraintBounds (int row_index, double lb, double ub) override
 Modify bounds of an extracted variable.
 
void AddRowConstraint (MPConstraint *ct) override
 Adds Constraint incrementally.
 
bool AddIndicatorConstraint (MPConstraint *ct) override
 
void AddVariable (MPVariable *var) override
 Adds variable incrementally.
 
void SetCoefficient (MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override
 Changes a coefficient in a constraint.
 
void ClearConstraint (MPConstraint *constraint) override
 Clears a constraint from all its terms.
 
void SetObjectiveCoefficient (const MPVariable *variable, double coefficient) override
 Changes a coefficient in the linear objective.
 
void SetObjectiveOffset (double value) override
 Changes the constant term in the linear objective.
 
void ClearObjective () override
 Clears the objective from all its terms.
 
void BranchingPriorityChangedForVariable (int var_index) override
 
int64_t iterations () const override
 ---— Query statistics on the solution and the solve ---—
 
int64_t nodes () const override
 Number of branch-and-bound nodes. Only available for discrete problems.
 
MPSolver::BasisStatus row_status (int constraint_index) const override
 Returns the basis status of a row.
 
MPSolver::BasisStatus column_status (int variable_index) const override
 Returns the basis status of a column.
 
bool IsContinuous () const override
 
bool IsLP () const override
 Returns true if the problem is continuous and linear.
 
bool IsMIP () const override
 Returns true if the problem is discrete and linear.
 
void ExtractNewVariables () override
 Extracts new variables.
 
void ExtractNewConstraints () override
 Extracts the constraints that have not been extracted yet.
 
void ExtractObjective () override
 Extracts the objective.
 
std::string SolverVersion () const override
 Returns a string describing the underlying solver and its version.
 
bool InterruptSolve () override
 
void * underlying_solver () override
 Returns the underlying solver.
 
double ComputeExactConditionNumber () const override
 
bool NextSolution () override
 Iterates through the solutions in Gurobi's solution pool.
 
void SetCallback (MPCallback *mp_callback) override
 See MPSolver::SetCallback() for details.
 
bool SupportsCallbacks () const override
 
- Public Member Functions inherited from operations_research::MPSolverInterface
 MPSolverInterface (MPSolver *solver)
 
virtual ~MPSolverInterface ()
 
double best_objective_bound () const
 
double objective_value () const
 Returns the objective value of the best solution found so far.
 
bool CheckSolutionIsSynchronized () const
 
virtual bool CheckSolutionExists () const
 
bool CheckSolutionIsSynchronizedAndExists () const
 Handy shortcut to do both checks above (it is often used).
 
int last_variable_index () const
 Returns the index of the last variable extracted.
 
bool variable_is_extracted (int var_index) const
 
void set_variable_as_extracted (int var_index, bool extracted)
 
bool constraint_is_extracted (int ct_index) const
 
void set_constraint_as_extracted (int ct_index, bool extracted)
 
bool quiet () const
 Returns the boolean indicating the verbosity of the solver output.
 
void set_quiet (bool quiet_value)
 Sets the boolean indicating the verbosity of the solver output.
 
MPSolver::ResultStatus result_status () const
 Returns the result status of the last solve.
 
virtual void SetStartingLpBasis (const std::vector< MPSolver::BasisStatus > &, const std::vector< MPSolver::BasisStatus > &)
 See MPSolver::SetStartingLpBasis().
 
virtual double infinity ()
 

Additional Inherited Members

- Public Types inherited from operations_research::MPSolverInterface
enum  SynchronizationStatus { MUST_RELOAD , MODEL_SYNCHRONIZED , SOLUTION_SYNCHRONIZED }
 
- Static Public Attributes inherited from operations_research::MPSolverInterface
static constexpr int64_t kUnknownNumberOfIterations = -1
 
static constexpr int64_t kUnknownNumberOfNodes = -1
 
- Protected Member Functions inherited from operations_research::MPSolverInterface
void ExtractModel ()
 Extracts model stored in MPSolver.
 
void ResetExtractionInformation ()
 Resets the extraction information.
 
void InvalidateSolutionSynchronization ()
 
void SetCommonParameters (const MPSolverParameters &param)
 Sets parameters common to LP and MIP in the underlying solver.
 
void SetMIPParameters (const MPSolverParameters &param)
 Sets MIP specific parameters in the underlying solver.
 
void SetUnsupportedDoubleParam (MPSolverParameters::DoubleParam param)
 Sets an unsupported double parameter.
 
virtual void SetUnsupportedIntegerParam (MPSolverParameters::IntegerParam param)
 Sets an unsupported integer parameter.
 
void SetDoubleParamToUnsupportedValue (MPSolverParameters::DoubleParam param, double value)
 Sets a supported double parameter to an unsupported value.
 
virtual void SetIntegerParamToUnsupportedValue (MPSolverParameters::IntegerParam param, int value)
 Sets a supported integer parameter to an unsupported value.
 
virtual absl::Status SetNumThreads (int num_threads)
 Sets the number of threads to be used by the solver.
 
- Protected Attributes inherited from operations_research::MPSolverInterface
MPSolver *const solver_
 
SynchronizationStatus sync_status_
 Indicates whether the model and the solution are synchronized.
 
MPSolver::ResultStatus result_status_
 
bool maximize_
 Optimization direction.
 
int last_constraint_index_
 Index in MPSolver::variables_ of last constraint extracted.
 
int last_variable_index_
 Index in MPSolver::constraints_ of last variable extracted.
 
double objective_value_
 The value of the objective function.
 
double best_objective_bound_
 The value of the best objective bound. Used only for MIP solvers.
 
bool quiet_
 Boolean indicator for the verbosity of the solver output.
 
- Static Protected Attributes inherited from operations_research::MPSolverInterface
static const int kDummyVariableIndex = 0
 -------— MPSolverInterface -------—
 

Detailed Description

Definition at line 80 of file gurobi_interface.cc.

Constructor & Destructor Documentation

◆ GurobiInterface()

operations_research::GurobiInterface::GurobiInterface ( MPSolver * solver,
bool mip )
explicit

Constructor that takes a name for the underlying GRB solver.

Creates a LP/MIP instance with the specified name and minimization objective.

Definition at line 612 of file gurobi_interface.cc.

◆ ~GurobiInterface()

operations_research::GurobiInterface::~GurobiInterface ( )
override

Definition at line 631 of file gurobi_interface.cc.

Member Function Documentation

◆ AddIndicatorConstraint()

bool operations_research::GurobiInterface::AddIndicatorConstraint ( MPConstraint * )
overridevirtual

Adds an indicator constraint. Returns true if the feature is supported by the underlying solver.

Reimplemented from operations_research::MPSolverInterface.

Definition at line 724 of file gurobi_interface.cc.

◆ AddRowConstraint()

void operations_research::GurobiInterface::AddRowConstraint ( MPConstraint * ct)
overridevirtual

Adds Constraint incrementally.

Implements operations_research::MPSolverInterface.

Definition at line 720 of file gurobi_interface.cc.

◆ AddVariable()

void operations_research::GurobiInterface::AddVariable ( MPVariable * var)
overridevirtual

Adds variable incrementally.

Implements operations_research::MPSolverInterface.

Definition at line 730 of file gurobi_interface.cc.

◆ BranchingPriorityChangedForVariable()

void operations_research::GurobiInterface::BranchingPriorityChangedForVariable ( int var_index)
overridevirtual

Reimplemented from operations_research::MPSolverInterface.

Definition at line 797 of file gurobi_interface.cc.

◆ ClearConstraint()

void operations_research::GurobiInterface::ClearConstraint ( MPConstraint * constraint)
overridevirtual

Clears a constraint from all its terms.

Todo
(user): this is difficult to make incremental, like SetConstraintBounds(), because of the auxiliary Gurobi variables that range constraints introduce.

Implements operations_research::MPSolverInterface.

Definition at line 756 of file gurobi_interface.cc.

◆ ClearObjective()

void operations_research::GurobiInterface::ClearObjective ( )
overridevirtual

Clears the objective from all its terms.

Implements operations_research::MPSolverInterface.

Definition at line 785 of file gurobi_interface.cc.

◆ column_status()

MPSolver::BasisStatus operations_research::GurobiInterface::column_status ( int variable_index) const
overridevirtual

Returns the basis status of a column.

Implements operations_research::MPSolverInterface.

Definition at line 897 of file gurobi_interface.cc.

◆ ComputeExactConditionNumber()

double operations_research::GurobiInterface::ComputeExactConditionNumber ( ) const
inlineoverridevirtual

Computes exact condition number. Only available for continuous problems and only implemented in GLPK.

Todo
(user): Not yet working.

double cond = 0.0; const int status = GRBgetdblattr(model_, GRB_DBL_ATTR_KAPPA, &cond); if (0 == status) { return cond; } else { LOG(DFATAL) << "Condition number only available for " << "continuous problems"; return 0.0; }

Override this method in interfaces that actually support it.

Reimplemented from operations_research::MPSolverInterface.

Definition at line 161 of file gurobi_interface.cc.

◆ DirectlySolveProto()

std::optional< MPSolutionResponse > operations_research::GurobiInterface::DirectlySolveProto ( const MPModelRequest & ,
std::atomic< bool > *  )
overridevirtual

Attempts to directly solve a MPModelRequest, bypassing the MPSolver data structures entirely. Like MPSolver::SolveWithProto(), optionally takes in an 'interrupt' boolean. Returns {} (eg. absl::nullopt) if direct-solve is not supported by the underlying solver (possibly because interrupt != nullptr), in which case the user should fall back to using MPSolver.

Interruption via atomic<bool> is not directly supported by Gurobi.

Here we reuse the Gurobi environment to support single-use license that forbids creating a second environment if one already exists.

Special case: if something is not implemented yet, fall back to solving through MPSolver.

Reimplemented from operations_research::MPSolverInterface.

Definition at line 1336 of file gurobi_interface.cc.

◆ ExtractNewConstraints()

void operations_research::GurobiInterface::ExtractNewConstraints ( )
overridevirtual

Extracts the constraints that have not been extracted yet.

Add each new constraint.

Using GRBaddrangeconstr for constraints that don't require it adds a slack which is not always removed by presolve.

NOTE(user): range constraints implicitly add an extra variable to the model.

Implements operations_research::MPSolverInterface.

Definition at line 966 of file gurobi_interface.cc.

◆ ExtractNewVariables()

void operations_research::GurobiInterface::ExtractNewVariables ( )
overridevirtual

Extracts new variables.

Define new variables.

Add new variables to existing constraints.

If there was a nonincremental change/the model is not incremental (e.g. there is an indicator constraint), we should never enter this loop, as last_variable_index_ will be reset to zero before ExtractNewVariables() is called.

Implements operations_research::MPSolverInterface.

Definition at line 915 of file gurobi_interface.cc.

◆ ExtractObjective()

void operations_research::GurobiInterface::ExtractObjective ( )
overridevirtual

Extracts the objective.

Implements operations_research::MPSolverInterface.

Definition at line 1033 of file gurobi_interface.cc.

◆ InterruptSolve()

bool operations_research::GurobiInterface::InterruptSolve ( )
inlineoverridevirtual

Reimplemented from operations_research::MPSolverInterface.

Definition at line 153 of file gurobi_interface.cc.

◆ IsContinuous()

bool operations_research::GurobiInterface::IsContinuous ( ) const
inlineoverridevirtual

--— Misc --— Queries problem type.

Implements operations_research::MPSolverInterface.

Definition at line 138 of file gurobi_interface.cc.

◆ IsLP()

bool operations_research::GurobiInterface::IsLP ( ) const
inlineoverridevirtual

Returns true if the problem is continuous and linear.

Implements operations_research::MPSolverInterface.

Definition at line 139 of file gurobi_interface.cc.

◆ IsMIP()

bool operations_research::GurobiInterface::IsMIP ( ) const
inlineoverridevirtual

Returns true if the problem is discrete and linear.

Implements operations_research::MPSolverInterface.

Definition at line 140 of file gurobi_interface.cc.

◆ iterations()

int64_t operations_research::GurobiInterface::iterations ( ) const
overridevirtual

---— Query statistics on the solution and the solve ---—

---— Query statistics on the solution and the solve ---— Number of simplex or interior-point iterations

Implements operations_research::MPSolverInterface.

Definition at line 803 of file gurobi_interface.cc.

◆ NextSolution()

bool operations_research::GurobiInterface::NextSolution ( )
overridevirtual

Iterates through the solutions in Gurobi's solution pool.

Next solution only supported for MIP

Make sure we have successfully solved the problem and not modified it.

Check if we are out of solutions.

Todo
(user): This reset may not be necessary, investigate.

Reimplemented from operations_research::MPSolverInterface.

Definition at line 1358 of file gurobi_interface.cc.

◆ nodes()

int64_t operations_research::GurobiInterface::nodes ( ) const
overridevirtual

Number of branch-and-bound nodes. Only available for discrete problems.

Implements operations_research::MPSolverInterface.

Definition at line 810 of file gurobi_interface.cc.

◆ Reset()

void operations_research::GurobiInterface::Reset ( )
overridevirtual

---— Model modifications and extraction --—

--— Model modifications and extraction --— Resets extracted model

We hold calls to GRBterminate() until the new model_ is ready.

Copy all existing parameters from the previous model to the new one. This ensures that if a user calls multiple times SetSolverSpecificParametersAsString() and then Reset() is called, we still take into account all parameters.

The current code only reapplies the parameters stored in solver_specific_parameter_string_ at the start of the solve; other parameters set by previous calls are only kept in the Gurobi model.

Implements operations_research::MPSolverInterface.

Definition at line 638 of file gurobi_interface.cc.

◆ row_status()

MPSolver::BasisStatus operations_research::GurobiInterface::row_status ( int constraint_index) const
overridevirtual

Returns the basis status of a row.

Implements operations_research::MPSolverInterface.

Definition at line 874 of file gurobi_interface.cc.

◆ SetCallback()

void operations_research::GurobiInterface::SetCallback ( MPCallback * )
overridevirtual

See MPSolver::SetCallback() for details.

Reimplemented from operations_research::MPSolverInterface.

Definition at line 1407 of file gurobi_interface.cc.

◆ SetCoefficient()

void operations_research::GurobiInterface::SetCoefficient ( MPConstraint * constraint,
const MPVariable * variable,
double new_value,
double old_value )
overridevirtual

Changes a coefficient in a constraint.

Cannot be const, GRBchgcoeffs needs non-const pointer.

Todo
(user): investigate if this has bad performance.

Implements operations_research::MPSolverInterface.

Definition at line 734 of file gurobi_interface.cc.

◆ SetConstraintBounds()

void operations_research::GurobiInterface::SetConstraintBounds ( int index,
double lb,
double ub )
overridevirtual

Modify bounds of an extracted variable.

Todo
(user): this is nontrivial to make incremental:
  1. Make sure it is a linear constraint (not an indicator or indicator range constraint).
  2. Check if the sense of the constraint changes. If it was previously a range constraint, we can do nothing, and if it becomes a range constraint, we can do nothing. We could support range constraints if we tracked the auxiliary variable that is added with range constraints.

Implements operations_research::MPSolverInterface.

Definition at line 705 of file gurobi_interface.cc.

◆ SetObjectiveCoefficient()

void operations_research::GurobiInterface::SetObjectiveCoefficient ( const MPVariable * variable,
double coefficient )
overridevirtual

Changes a coefficient in the linear objective.

Implements operations_research::MPSolverInterface.

Definition at line 764 of file gurobi_interface.cc.

◆ SetObjectiveOffset()

void operations_research::GurobiInterface::SetObjectiveOffset ( double value)
overridevirtual

Changes the constant term in the linear objective.

Implements operations_research::MPSolverInterface.

Definition at line 776 of file gurobi_interface.cc.

◆ SetOptimizationDirection()

void operations_research::GurobiInterface::SetOptimizationDirection ( bool maximize)
overridevirtual

Sets the optimization direction (min/max).

Implements operations_research::MPSolverInterface.

Definition at line 672 of file gurobi_interface.cc.

◆ SetVariableBounds()

void operations_research::GurobiInterface::SetVariableBounds ( int var_index,
double lb,
double ub )
overridevirtual

Modifies bounds.

Implements operations_research::MPSolverInterface.

Definition at line 677 of file gurobi_interface.cc.

◆ SetVariableInteger()

void operations_research::GurobiInterface::SetVariableInteger ( int index,
bool integer )
overridevirtual

Modifies integrality of an extracted variable.

Implements operations_research::MPSolverInterface.

Definition at line 689 of file gurobi_interface.cc.

◆ Solve()

MPSolver::ResultStatus operations_research::GurobiInterface::Solve ( const MPSolverParameters & param)
overridevirtual

--— Solve --— Solves the problem using the parameter values specified.

Set log level.

Sync solver.

Set solution hints if any.

Pass branching priority annotations if at least one has been updated.

Time limit.

We first set our internal MPSolverParameters from 'param' and then set any user-specified internal solver parameters via solver_specific_parameter_string_. Default MPSolverParameters can override custom parameters (for example for presolving) and therefore we apply MPSolverParameters first.

Logs all parameters not at default values in the model environment.

Solve

Get the status.

Todo
(user): We could introduce our own "infeasible or unbounded" status.

Get the results.

Implements operations_research::MPSolverInterface.

Definition at line 1156 of file gurobi_interface.cc.

◆ SolverVersion()

std::string operations_research::GurobiInterface::SolverVersion ( ) const
inlineoverridevirtual

Returns a string describing the underlying solver and its version.

Implements operations_research::MPSolverInterface.

Definition at line 146 of file gurobi_interface.cc.

◆ SupportsCallbacks()

bool operations_research::GurobiInterface::SupportsCallbacks ( ) const
inlineoverridevirtual

Reimplemented from operations_research::MPSolverInterface.

Definition at line 188 of file gurobi_interface.cc.

◆ underlying_solver()

void * operations_research::GurobiInterface::underlying_solver ( )
inlineoverridevirtual

Returns the underlying solver.

Implements operations_research::MPSolverInterface.

Definition at line 159 of file gurobi_interface.cc.

◆ Write()

void operations_research::GurobiInterface::Write ( const std::string & filename)
overridevirtual

Writes the model.

Sync solver.

Reimplemented from operations_research::MPSolverInterface.

Definition at line 1389 of file gurobi_interface.cc.


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