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

#include <linear_solver.h>

Inheritance diagram for operations_research::MPSolverInterface:
operations_research::BopInterface operations_research::CBCInterface operations_research::CLPInterface operations_research::CplexInterface operations_research::GLOPInterface operations_research::GLPKInterface operations_research::GurobiInterface operations_research::HighsInterface operations_research::KnapsackInterface operations_research::PdlpInterface operations_research::SCIPInterface operations_research::SatInterface operations_research::XpressInterface

Public Types

enum  SynchronizationStatus { MUST_RELOAD , MODEL_SYNCHRONIZED , SOLUTION_SYNCHRONIZED }
 

Public Member Functions

 MPSolverInterface (MPSolver *solver)
 
virtual ~MPSolverInterface ()
 
virtual MPSolver::ResultStatus Solve (const MPSolverParameters &param)=0
 
virtual bool SupportsDirectlySolveProto (std::atomic< bool > *) const
 
virtual MPSolutionResponse DirectlySolveProto (LazyMutableCopy< MPModelRequest >, std::atomic< bool > *)
 
virtual void Write (const std::string &filename)
 
virtual void Reset ()=0
 
virtual void SetOptimizationDirection (bool maximize)=0
 Sets the optimization direction (min/max).
 
virtual void SetVariableBounds (int index, double lb, double ub)=0
 Modifies bounds of an extracted variable.
 
virtual void SetVariableInteger (int index, bool integer)=0
 Modifies integrality of an extracted variable.
 
virtual void SetConstraintBounds (int index, double lb, double ub)=0
 Modify bounds of an extracted variable.
 
virtual void AddRowConstraint (MPConstraint *ct)=0
 Adds a linear constraint.
 
virtual bool AddIndicatorConstraint (MPConstraint *const)
 
virtual void AddVariable (MPVariable *var)=0
 Add a variable.
 
virtual void SetCoefficient (MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value)=0
 Changes a coefficient in a constraint.
 
virtual void ClearConstraint (MPConstraint *constraint)=0
 Clears a constraint from all its terms.
 
virtual void SetObjectiveCoefficient (const MPVariable *variable, double coefficient)=0
 Changes a coefficient in the linear objective.
 
virtual void SetObjectiveOffset (double value)=0
 Changes the constant term in the linear objective.
 
virtual void ClearObjective ()=0
 Clears the objective from all its terms.
 
virtual void BranchingPriorityChangedForVariable (int)
 
virtual int64_t iterations () const =0
 
virtual int64_t nodes () const =0
 
double best_objective_bound () const
 
double objective_value () const
 Returns the objective value of the best solution found so far.
 
virtual MPSolver::BasisStatus row_status (int constraint_index) const =0
 Returns the basis status of a row.
 
virtual MPSolver::BasisStatus column_status (int variable_index) const =0
 Returns the basis status of a constraint.
 
bool CheckSolutionIsSynchronized () const
 
virtual bool CheckSolutionExists () const
 
bool CheckSolutionIsSynchronizedAndExists () const
 Handy shortcut to do both checks above (it is often used).
 
virtual bool IsContinuous () const =0
 
virtual bool IsLP () const =0
 Returns true if the problem is continuous and linear.
 
virtual bool IsMIP () const =0
 Returns true if the problem is discrete and linear.
 
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 std::string SolverVersion () const =0
 Returns a string describing the underlying solver and its version.
 
virtual void * underlying_solver ()=0
 Returns the underlying solver.
 
virtual double ComputeExactConditionNumber () const
 
virtual void SetStartingLpBasis (const std::vector< MPSolver::BasisStatus > &, const std::vector< MPSolver::BasisStatus > &)
 See MPSolver::SetStartingLpBasis().
 
virtual double infinity ()
 
virtual bool InterruptSolve ()
 
virtual bool NextSolution ()
 See MPSolver::NextSolution() for contract.
 
virtual void SetCallback (MPCallback *)
 See MPSolver::SetCallback() for details.
 
virtual bool SupportsCallbacks () const
 

Static Public Attributes

static constexpr int64_t kUnknownNumberOfIterations = -1
 
static constexpr int64_t kUnknownNumberOfNodes = -1
 

Protected Member Functions

void ExtractModel ()
 Extracts model stored in MPSolver.
 
virtual void ExtractNewVariables ()=0
 Extracts the variables that have not been extracted yet.
 
virtual void ExtractNewConstraints ()=0
 Extracts the constraints that have not been extracted yet.
 
virtual void ExtractObjective ()=0
 Extracts the objective.
 
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.
 
virtual void SetParameters (const MPSolverParameters &param)=0
 Sets all 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 void SetRelativeMipGap (double value)=0
 Sets each parameter in the underlying solver.
 
virtual void SetPrimalTolerance (double value)=0
 
virtual void SetDualTolerance (double value)=0
 
virtual void SetPresolveMode (int value)=0
 
virtual absl::Status SetNumThreads (int num_threads)
 Sets the number of threads to be used by the solver.
 
virtual bool SetSolverSpecificParametersAsString (const std::string &parameters)
 
virtual void SetScalingMode (int value)=0
 Sets the scaling mode.
 
virtual void SetLpAlgorithm (int value)=0
 

Protected Attributes

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

static const int kDummyVariableIndex = 0
 -------— MPSolverInterface -------—
 

Friends

class MPSolver
 
class MPConstraint
 To access the maximize_ bool and the MPSolver.
 
class MPObjective
 

Detailed Description

This class wraps the actual mathematical programming solvers. Each solver (GLOP, CLP, CBC, GLPK, SCIP) has its own interface class that derives from this abstract class. This class is never directly accessed by the user.

See also
glop_interface.cc
cbc_interface.cc
clp_interface.cc
glpk_interface.cc
scip_interface.cc

Definition at line 1634 of file linear_solver.h.

Member Enumeration Documentation

◆ SynchronizationStatus

Enumerator
MUST_RELOAD 

The underlying solver (CLP, GLPK, ...) and MPSolver are not in sync for the model nor for the solution.

MODEL_SYNCHRONIZED 

The underlying solver and MPSolver are in sync for the model but not for the solution: the model has changed since the solution was computed last.

SOLUTION_SYNCHRONIZED 

The underlying solver and MPSolver are in sync for the model and the solution.

Definition at line 1636 of file linear_solver.h.

Constructor & Destructor Documentation

◆ MPSolverInterface()

operations_research::MPSolverInterface::MPSolverInterface ( MPSolver * solver)
explicit

Constructor. The user will access the MPSolverInterface through the MPSolver passed as argument.

Todo
(user): Initialize objective value and bound to +/- inf (depending on optimization direction).

Definition at line 1958 of file linear_solver.cc.

◆ ~MPSolverInterface()

operations_research::MPSolverInterface::~MPSolverInterface ( )
virtual

Definition at line 1969 of file linear_solver.cc.

Member Function Documentation

◆ AddIndicatorConstraint()

virtual bool operations_research::MPSolverInterface::AddIndicatorConstraint ( MPConstraint * const )
inlinevirtual

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

Reimplemented in operations_research::GurobiInterface, operations_research::SatInterface, and operations_research::SCIPInterface.

Definition at line 1710 of file linear_solver.h.

◆ AddRowConstraint()

◆ AddVariable()

◆ best_objective_bound()

double operations_research::MPSolverInterface::best_objective_bound ( ) const

Returns the best objective bound. The problem must be discrete, otherwise it crashes, or returns trivial bound (+/- inf) in NDEBUG mode.

Special case for empty model.

Definition at line 2038 of file linear_solver.cc.

◆ BranchingPriorityChangedForVariable()

virtual void operations_research::MPSolverInterface::BranchingPriorityChangedForVariable ( int )
inlinevirtual

◆ CheckSolutionExists()

bool operations_research::MPSolverInterface::CheckSolutionExists ( ) const
virtual

Checks whether a feasible solution exists. The behavior is similar to CheckSolutionIsSynchronized() above.

Default version that can be overwritten by a solver-specific version to accommodate for the quirks of each solver.

Reimplemented in operations_research::GLPKInterface.

Definition at line 2023 of file linear_solver.cc.

◆ CheckSolutionIsSynchronized()

bool operations_research::MPSolverInterface::CheckSolutionIsSynchronized ( ) const

Checks whether the solution is synchronized with the model, i.e. whether the model has changed since the solution was computed last. If it isn't, it crashes in NDEBUG, and returns false otherwise.

Definition at line 2011 of file linear_solver.cc.

◆ CheckSolutionIsSynchronizedAndExists()

bool operations_research::MPSolverInterface::CheckSolutionIsSynchronizedAndExists ( ) const
inline

Handy shortcut to do both checks above (it is often used).

Definition at line 1763 of file linear_solver.h.

◆ ClearConstraint()

◆ ClearObjective()

◆ column_status()

◆ ComputeExactConditionNumber()

double operations_research::MPSolverInterface::ComputeExactConditionNumber ( ) const
virtual

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

Override this method in interfaces that actually support it.

Reimplemented in operations_research::CplexInterface, operations_research::GLPKInterface, operations_research::GurobiInterface, and operations_research::XpressInterface.

Definition at line 2062 of file linear_solver.cc.

◆ constraint_is_extracted()

bool operations_research::MPSolverInterface::constraint_is_extracted ( int ct_index) const
inline

Definition at line 1789 of file linear_solver.h.

◆ DirectlySolveProto()

virtual MPSolutionResponse operations_research::MPSolverInterface::DirectlySolveProto ( LazyMutableCopy< MPModelRequest > ,
std::atomic< bool > *  )
inlinevirtual

◆ ExtractModel()

void operations_research::MPSolverInterface::ExtractModel ( )
protected

Extracts model stored in MPSolver.

Everything has already been extracted.

Nothing has changed since last solve.

Definition at line 1975 of file linear_solver.cc.

◆ ExtractNewConstraints()

◆ ExtractNewVariables()

◆ ExtractObjective()

◆ infinity()

virtual double operations_research::MPSolverInterface::infinity ( )
inlinevirtual

Reimplemented in operations_research::SCIPInterface.

Definition at line 1824 of file linear_solver.h.

◆ InterruptSolve()

◆ InvalidateSolutionSynchronization()

void operations_research::MPSolverInterface::InvalidateSolutionSynchronization ( )
protected

Change synchronization status from SOLUTION_SYNCHRONIZED to MODEL_SYNCHRONIZED. To be used for model changes.

Definition at line 2056 of file linear_solver.cc.

◆ IsContinuous()

virtual bool operations_research::MPSolverInterface::IsContinuous ( ) const
pure virtual

--— Misc --— Queries problem type. For simplicity, the distinction between continuous and discrete is based on the declaration of the user when the solver is created (example: GLPK_LINEAR_PROGRAMMING vs. GLPK_MIXED_INTEGER_PROGRAMMING), not on the actual content of the model. Returns true if the problem is continuous.

Implemented in operations_research::BopInterface, operations_research::CBCInterface, operations_research::CLPInterface, operations_research::CplexInterface, operations_research::GLOPInterface, operations_research::GLPKInterface, operations_research::GurobiInterface, operations_research::HighsInterface, operations_research::KnapsackInterface, operations_research::PdlpInterface, operations_research::SatInterface, operations_research::SCIPInterface, and operations_research::XpressInterface.

◆ IsLP()

◆ IsMIP()

◆ iterations()

virtual int64_t operations_research::MPSolverInterface::iterations ( ) const
pure virtual

◆ last_variable_index()

int operations_research::MPSolverInterface::last_variable_index ( ) const
inline

Returns the index of the last variable extracted.

Definition at line 1781 of file linear_solver.h.

◆ NextSolution()

virtual bool operations_research::MPSolverInterface::NextSolution ( )
inlinevirtual

◆ nodes()

◆ objective_value()

double operations_research::MPSolverInterface::objective_value ( ) const

Returns the objective value of the best solution found so far.

Definition at line 2033 of file linear_solver.cc.

◆ quiet()

bool operations_research::MPSolverInterface::quiet ( ) const
inline

Returns the boolean indicating the verbosity of the solver output.

Definition at line 1797 of file linear_solver.h.

◆ Reset()

◆ ResetExtractionInformation()

void operations_research::MPSolverInterface::ResetExtractionInformation ( )
protected

Resets the extraction information.

Todo
(user): remove this method.

Definition at line 2003 of file linear_solver.cc.

◆ result_status()

MPSolver::ResultStatus operations_research::MPSolverInterface::result_status ( ) const
inline

Returns the result status of the last solve.

Definition at line 1802 of file linear_solver.h.

◆ row_status()

◆ set_constraint_as_extracted()

void operations_research::MPSolverInterface::set_constraint_as_extracted ( int ct_index,
bool extracted )
inline

Definition at line 1792 of file linear_solver.h.

◆ set_quiet()

void operations_research::MPSolverInterface::set_quiet ( bool quiet_value)
inline

Sets the boolean indicating the verbosity of the solver output.

Definition at line 1799 of file linear_solver.h.

◆ set_variable_as_extracted()

void operations_research::MPSolverInterface::set_variable_as_extracted ( int var_index,
bool extracted )
inline

Definition at line 1786 of file linear_solver.h.

◆ SetCallback()

virtual void operations_research::MPSolverInterface::SetCallback ( MPCallback * )
inlinevirtual

◆ SetCoefficient()

◆ SetCommonParameters()

void operations_research::MPSolverInterface::SetCommonParameters ( const MPSolverParameters & param)
protected

Sets parameters common to LP and MIP in the underlying solver.

Todo
(user): Overhaul the code that sets parameters to enable changing GLOP parameters without issuing warnings. By default, we let GLOP keep its own default tolerance, much more accurate than for the rest of the solvers.
Todo
(user): In the future, we could distinguish between the algorithm to solve the root LP and the algorithm to solve node LPs. Not sure if underlying solvers support it.

Definition at line 2071 of file linear_solver.cc.

◆ SetConstraintBounds()

◆ SetDoubleParamToUnsupportedValue()

void operations_research::MPSolverInterface::SetDoubleParamToUnsupportedValue ( MPSolverParameters::DoubleParam param,
double value )
protected

Sets a supported double parameter to an unsupported value.

Definition at line 2107 of file linear_solver.cc.

◆ SetDualTolerance()

◆ SetIntegerParamToUnsupportedValue()

void operations_research::MPSolverInterface::SetIntegerParamToUnsupportedValue ( MPSolverParameters::IntegerParam param,
int value )
protectedvirtual

Sets a supported integer parameter to an unsupported value.

Definition at line 2112 of file linear_solver.cc.

◆ SetLpAlgorithm()

◆ SetMIPParameters()

void operations_research::MPSolverInterface::SetMIPParameters ( const MPSolverParameters & param)
protected

Sets MIP specific parameters in the underlying solver.

Definition at line 2092 of file linear_solver.cc.

◆ SetNumThreads()

absl::Status operations_research::MPSolverInterface::SetNumThreads ( int num_threads)
protectedvirtual

Sets the number of threads to be used by the solver.

Reimplemented in operations_research::CBCInterface, operations_research::HighsInterface, operations_research::PdlpInterface, and operations_research::SatInterface.

Definition at line 2118 of file linear_solver.cc.

◆ SetObjectiveCoefficient()

◆ SetObjectiveOffset()

◆ SetOptimizationDirection()

◆ SetParameters()

◆ SetPresolveMode()

◆ SetPrimalTolerance()

◆ SetRelativeMipGap()

virtual void operations_research::MPSolverInterface::SetRelativeMipGap ( double value)
protectedpure virtual

◆ SetScalingMode()

◆ SetSolverSpecificParametersAsString()

bool operations_research::MPSolverInterface::SetSolverSpecificParametersAsString ( const std::string & parameters)
protectedvirtual

Pass solver specific parameters in text format. The format is solver-specific and is the same as the corresponding solver configuration file format. Returns true if the operation was successful.

Default implementation returns true if the input is empty. It returns false and logs a WARNING if the input is not empty.

Reimplemented in operations_research::BopInterface, operations_research::CplexInterface, operations_research::GLOPInterface, operations_research::HighsInterface, operations_research::PdlpInterface, and operations_research::SatInterface.

Definition at line 2123 of file linear_solver.cc.

◆ SetStartingLpBasis()

virtual void operations_research::MPSolverInterface::SetStartingLpBasis ( const std::vector< MPSolver::BasisStatus > & ,
const std::vector< MPSolver::BasisStatus > &  )
inlinevirtual

◆ SetUnsupportedDoubleParam()

void operations_research::MPSolverInterface::SetUnsupportedDoubleParam ( MPSolverParameters::DoubleParam param)
protected

Sets an unsupported double parameter.

Definition at line 2099 of file linear_solver.cc.

◆ SetUnsupportedIntegerParam()

void operations_research::MPSolverInterface::SetUnsupportedIntegerParam ( MPSolverParameters::IntegerParam param)
protectedvirtual

Sets an unsupported integer parameter.

Definition at line 2103 of file linear_solver.cc.

◆ SetVariableBounds()

◆ SetVariableInteger()

◆ Solve()

◆ SolverVersion()

◆ SupportsCallbacks()

virtual bool operations_research::MPSolverInterface::SupportsCallbacks ( ) const
inlinevirtual

◆ SupportsDirectlySolveProto()

virtual bool operations_research::MPSolverInterface::SupportsDirectlySolveProto ( std::atomic< bool > * ) const
inlinevirtual

◆ underlying_solver()

◆ variable_is_extracted()

bool operations_research::MPSolverInterface::variable_is_extracted ( int var_index) const
inline

Definition at line 1783 of file linear_solver.h.

◆ Write()

void operations_research::MPSolverInterface::Write ( const std::string & filename)
virtual

Writes the model using the solver internal write function. Currently only available for GurobiInterface and XpressInterface.

Reimplemented in operations_research::GurobiInterface, and operations_research::XpressInterface.

Definition at line 1971 of file linear_solver.cc.

Friends And Related Symbol Documentation

◆ MPConstraint

friend class MPConstraint
friend

To access the maximize_ bool and the MPSolver.

Definition at line 1841 of file linear_solver.h.

◆ MPObjective

friend class MPObjective
friend

Definition at line 1842 of file linear_solver.h.

◆ MPSolver

friend class MPSolver
friend

Definition at line 1838 of file linear_solver.h.

Member Data Documentation

◆ best_objective_bound_

double operations_research::MPSolverInterface::best_objective_bound_
protected

The value of the best objective bound. Used only for MIP solvers.

Definition at line 1863 of file linear_solver.h.

◆ kDummyVariableIndex

const int operations_research::MPSolverInterface::kDummyVariableIndex = 0
staticprotected

-------— MPSolverInterface -------—

Index of dummy variable created for empty constraints or the objective offset.

Definition at line 1870 of file linear_solver.h.

◆ kUnknownNumberOfIterations

int64_t operations_research::MPSolverInterface::kUnknownNumberOfIterations = -1
staticconstexpr

When the underlying solver does not provide the number of simplex iterations.

Definition at line 1651 of file linear_solver.h.

◆ kUnknownNumberOfNodes

int64_t operations_research::MPSolverInterface::kUnknownNumberOfNodes = -1
staticconstexpr

When the underlying solver does not provide the number of branch-and-bound nodes.

Definition at line 1654 of file linear_solver.h.

◆ last_constraint_index_

int operations_research::MPSolverInterface::last_constraint_index_
protected

Index in MPSolver::variables_ of last constraint extracted.

Definition at line 1855 of file linear_solver.h.

◆ last_variable_index_

int operations_research::MPSolverInterface::last_variable_index_
protected

Index in MPSolver::constraints_ of last variable extracted.

Definition at line 1857 of file linear_solver.h.

◆ maximize_

bool operations_research::MPSolverInterface::maximize_
protected

Optimization direction.

Definition at line 1852 of file linear_solver.h.

◆ objective_value_

double operations_research::MPSolverInterface::objective_value_
protected

The value of the objective function.

Definition at line 1860 of file linear_solver.h.

◆ quiet_

bool operations_research::MPSolverInterface::quiet_
protected

Boolean indicator for the verbosity of the solver output.

Definition at line 1866 of file linear_solver.h.

◆ result_status_

MPSolver::ResultStatus operations_research::MPSolverInterface::result_status_
protected

Indicates whether the solve has reached optimality, infeasibility, a limit, etc.

Definition at line 1850 of file linear_solver.h.

◆ solver_

MPSolver* const operations_research::MPSolverInterface::solver_
protected

Definition at line 1845 of file linear_solver.h.

◆ sync_status_

SynchronizationStatus operations_research::MPSolverInterface::sync_status_
protected

Indicates whether the model and the solution are synchronized.

Definition at line 1847 of file linear_solver.h.


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