C++ Reference
C++ Reference: Linear solver
linear_solver.h
bool variable_is_extracted(int var_index) const
Definition: linear_solver.h:1601
Linear Programming solver using GLOP (Recommended solver).
Definition: linear_solver.h:191
int GetIntegerParam(MPSolverParameters::IntegerParam param) const
Returns the value of an integer parameter.
Algorithm to solve linear programs.
Definition: linear_solver.h:1331
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable on the constraint.
void * underlying_solver()
Advanced usage: returns the underlying solver.
static bool SupportsProblemType(OptimizationProblemType problem_type)
Whether the given problem type is supported (this will depend on the targets that you linked).
virtual bool IsContinuous() const =0
void Reset()
Advanced usage: resets extracted model to solve from scratch.
virtual bool NextSolution()
Definition: linear_solver.h:1645
friend class XpressInterface
Definition: linear_solver.h:975
The class for variables of a Mathematical Programming (MP) model.
Definition: linear_solver.h:1000
This mathematical programming (MP) solver class is the main class though which users build and solve ...
Definition: linear_solver.h:173
void ResetDoubleParam(MPSolverParameters::DoubleParam param)
Sets a double parameter to its default value (default value defined in MPSolverParameters if it exist...
MPConstraint * LookupConstraintOrNull(const std::string &constraint_name) const
Looks up a constraint by name, and returns nullptr if it does not exist.
friend class KnapsackInterface
Definition: linear_solver.h:979
MPSolverInterface(MPSolver *const solver)
Presolve is off.
Definition: linear_solver.h:1341
bool indicator_value() const
Definition: linear_solver.h:1190
MPSolver::ResultStatus result_status_
Definition: linear_solver.h:1659
friend class KnapsackInterface
Definition: linear_solver.h:1229
std::vector< double > ComputeConstraintActivities() const
Advanced usage: compute the "activities" of all constraints, which are the sums of their linear terms...
friend class CLPInterface
Definition: linear_solver.h:969
Advanced usage: tolerance for dual feasibility of basic solutions.
Definition: linear_solver.h:1323
int index() const
Returns the index of the variable in the MPSolver::variables_.
Definition: linear_solver.h:1021
static const PresolveValues kDefaultPresolve
Definition: linear_solver.h:1393
void Clear()
Clears all variables and coefficients. Does not clear the bounds.
virtual void SetVariableBounds(int index, double lb, double ub)=0
void MakeBoolVarArray(int nb, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of boolean variables.
static bool ParseSolverType(absl::string_view solver, OptimizationProblemType *type)
Parses the name of the solver.
friend class BopInterface
Definition: linear_solver.h:1086
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable in the objective.
void MakeIntVarArray(int nb, double lb, double ub, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of integer variables.
void MinimizeLinearExpr(const LinearExpr &linear_expr)
Resets the current objective to minimize linear_expr.
Definition: linear_solver.h:922
virtual void SetIntegerParamToUnsupportedValue(MPSolverParameters::IntegerParam param, int value)
bool ExportModelAsMpsFormat(bool fixed_format, bool obfuscate, std::string *model_str) const
bool ExportModelAsLpFormat(bool obfuscate, std::string *model_str) const
Shortcuts to the homonymous MPModelProtoExporter methods, via exporting to a MPModelProto with Export...
friend class CplexInterface
Definition: linear_solver.h:763
The class for constraints of a Mathematical Programming (MP) model.
Definition: linear_solver.h:1125
virtual bool CheckSolutionExists() const
friend class XpressInterface
Definition: linear_solver.h:1083
feasible, or stopped by limit.
Definition: linear_solver.h:406
ABSL_MUST_USE_RESULT bool NextSolution()
Some solvers (MIP only, not LP) can produce multiple solutions to the problem.
virtual void ExtractNewVariables()=0
friend class CBCInterface
Definition: linear_solver.h:1076
virtual void SetOptimizationDirection(bool maximize)=0
const absl::string_view ToString(MPSolver::OptimizationProblemType optimization_problem_type)
double objective_value_
Definition: linear_solver.h:1669
void SetIntegerParam(MPSolverParameters::IntegerParam param, int value)
Sets a integer parameter to a specific value.
virtual bool IsMIP() const =0
double GetCoefficient(const MPVariable *const var) const
Gets the coefficient of a given variable in the objective.
bool minimization() const
Is the optimization direction set to minimize?
static const double kDefaultRelativeMipGap
Definition: linear_solver.h:1390
virtual void ClearConstraint(MPConstraint *const constraint)=0
bool InterruptSolve()
Interrupts the Solve() execution to terminate processing if possible.
friend class KnapsackInterface
Definition: linear_solver.h:770
virtual util::Status SetNumThreads(int num_threads)
void set_variable_as_extracted(int var_index, bool extracted)
Definition: linear_solver.h:1604
static const double kDefaultDualTolerance
Definition: linear_solver.h:1392
util::Status SetNumThreads(int num_threads)
Sets the number of threads to use by the underlying solver.
void set_time_limit(int64 time_limit_milliseconds)
Definition: linear_solver.h:744
friend class XpressInterface
Definition: linear_solver.h:1225
Definition: linear_expr.h:84
virtual bool InterruptSolve()
Definition: linear_solver.h:1642
Advanced usage: enable or disable matrix scaling.
Definition: linear_solver.h:1335
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Returns a map from variables to their coefficients in the objective.
Definition: linear_solver.h:901
friend class GurobiInterface
Definition: linear_solver.h:1081
virtual void * underlying_solver()=0
PresolveValues
For each categorical parameter, enumeration of possible values.
Definition: linear_solver.h:1339
void ExportModelToProto(MPModelProto *output_model) const
Exports model to protocol buffer.
static const int kDummyVariableIndex
Definition: linear_solver.h:1676
friend class GLOPInterface
Definition: linear_solver.h:1226
MPSolverResponseStatus LoadModelFromProtoWithUniqueNamesOrDie(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
friend class SatInterface
Definition: linear_solver.h:1228
static const IncrementalityValues kDefaultIncrementality
Definition: linear_solver.h:1394
static const double kUnknownDoubleParamValue
Definition: linear_solver.h:1382
Limit for relative MIP gap.
Definition: linear_solver.h:1313
virtual void ExtractNewConstraints()=0
double dual_value() const
Advanced usage: returns the dual value of the constraint in the current solution (only available for ...
int last_variable_index_
Definition: linear_solver.h:1666
This class stores parameter settings for LP and MIP solvers.
Definition: linear_solver.h:1308
virtual bool IsLP() const =0
bool IsMIP() const
void SetUnsupportedDoubleParam(MPSolverParameters::DoubleParam param)
bool CheckSolutionIsSynchronized() const
int branching_priority() const
Advanced usage: Certain MIP solvers (e.g.
Definition: linear_solver.h:1070
virtual void Reset()=0
bool VerifySolution(double tolerance, bool log_errors) const
Advanced usage: Verifies the correctness of the solution.
ResultStatus Solve()
Solves the problem using default parameter values.
virtual OptimizationProblemType ProblemType() const
Returns the optimization problem type set at construction.
Definition: linear_solver.h:259
std::string AbslUnparseFlag(MPSolver::OptimizationProblemType solver_type)
Definition: linear_solver.h:867
void set_is_lazy(bool laziness)
Advanced usage: sets the constraint "laziness".
Definition: linear_solver.h:1187
SynchronizationStatus
Definition: linear_solver.h:1459
friend class GurobiInterface
Definition: linear_solver.h:1223
double unrounded_solution_value() const
Advanced usage: unrounded solution value.
static const double kDefaultPrimalTolerance
Definition: linear_solver.h:1391
void SetTimeLimit(absl::Duration time_limit)
Definition: linear_solver.h:658
virtual int64 nodes() const =0
friend class MPVariableSolutionValueTest
Definition: linear_solver.h:1085
std::string GetSolverSpecificParametersAsString() const
Definition: linear_solver.h:588
virtual void SetCoefficient(MPConstraint *const constraint, const MPVariable *const variable, double new_value, double old_value)=0
Advanced usage: incrementality from one solve to the next.
Definition: linear_solver.h:1333
MPSolver::BasisStatus basis_status() const
Advanced usage: returns the basis status of the variable in the current solution (only available for ...
MPConstraint(int index, double lb, double ub, const std::string &name, MPSolverInterface *const interface_in)
Definition: linear_solver.h:1234
void SetOffset(double value)
Sets the constant term in the objective.
friend class GLOPInterface
Definition: linear_solver.h:976
static const int64 kUnknownNumberOfIterations
Definition: linear_solver.h:1474
friend class BopInterface
Definition: linear_solver.h:1227
friend class GLOPInterface
Definition: linear_solver.h:1084
int index() const
Returns the index of the constraint in the MPSolver::constraints_.
Definition: linear_solver.h:1193
void SetCommonParameters(const MPSolverParameters ¶m)
virtual void BranchingPriorityChangedForVariable(int var_index)
Definition: linear_solver.h:1548
virtual MPSolver::BasisStatus row_status(int constraint_index) const =0
virtual bool SetSolverSpecificParametersAsString(const std::string ¶meters)
MPVariable(int index, double lb, double ub, bool integer, const std::string &name, MPSolverInterface *const interface_in)
Definition: linear_solver.h:1093
void Reset()
Sets all parameters to their default value.
void Clear()
Clears the objective (including the optimization direction), all variables and constraints.
void set_dual_value(double dual_value)
Definition: linear_solver.h:1246
bool is_lazy() const
Advanced usage: returns true if the constraint is "lazy" (see below).
Definition: linear_solver.h:1172
void ExtractModel()
void set_solution_value(double value)
Definition: linear_solver.h:1104
virtual void SetPresolveMode(int value)=0
bool CheckSolutionIsSynchronizedAndExists() const
Definition: linear_solver.h:1578
double GetCoefficient(const MPVariable *const var) const
Gets the coefficient of a given variable on the constraint (which is 0 if the variable does not appea...
double reduced_cost() const
Advanced usage: returns the reduced cost of the variable in the current solution (only available for ...
int64 nodes() const
Returns the number of branch-and-bound nodes evaluated during the solve.
virtual void AddRowConstraint(MPConstraint *const ct)=0
MPSolver::BasisStatus basis_status() const
Advanced usage: returns the basis status of the constraint.
friend class SatInterface
Definition: linear_solver.h:1087
virtual std::string SolverVersion() const =0
MPSolverParameters()
The constructor sets all parameters to their default value.
static const int kDefaultIntegerParamValue
Definition: linear_solver.h:1379
bool AbslParseFlag(absl::string_view text, MPSolver::OptimizationProblemType *solver_type, std::string *error)
SAT based solver (requires only integer and Boolean variables).
Definition: linear_solver.h:227
bool OwnsVariable(const MPVariable *var) const
void set_constraint_as_extracted(int ct_index, bool extracted)
Definition: linear_solver.h:1610
virtual MPSolver::ResultStatus Solve(const MPSolverParameters ¶m)=0
abnormal, i.e., error of some kind.
Definition: linear_solver.h:412
void SetBounds(double lb, double ub)
Sets both the lower and upper bounds.
friend class CLPInterface
Definition: linear_solver.h:1077
void ResetExtractionInformation()
double objective_value() const
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
Definition: linear_expr.h:114
Start solve from scratch.
Definition: linear_solver.h:1359
friend class SLMInterface
Definition: linear_solver.h:1080
static const int kUnknownIntegerParamValue
Definition: linear_solver.h:1383
MPVariable * LookupVariableOrNull(const std::string &var_name) const
Looks up a variable by name, and returns nullptr if it does not exist.
virtual void SetObjectiveOffset(double value)=0
MPVariable * MakeIntVar(double lb, double ub, const std::string &name)
Creates an integer variable.
virtual int64 iterations() const =0
virtual ~MPSolverInterface()
friend class CplexInterface
Definition: linear_solver.h:974
void SetInteger(bool integer)
Sets the integrality requirement of the variable.
friend class SLMInterface
Definition: linear_solver.h:1222
Mixed integer Programming Solver using SCIP.
Definition: linear_solver.h:204
virtual bool CheckBestObjectiveBoundExists() const
virtual void AddVariable(MPVariable *const var)=0
MPVariable * MakeNumVar(double lb, double ub, const std::string &name)
Creates a continuous variable.
bool integer() const
Returns the integrality requirement of the variable.
Definition: linear_solver.h:1009
virtual void SetParameters(const MPSolverParameters ¶m)=0
virtual double best_objective_bound() const =0
absl::Duration TimeLimit() const
Definition: linear_solver.h:657
virtual double ComputeExactConditionNumber() const
IntegerParam
Enumeration of parameters that take integer or categorical values.
Definition: linear_solver.h:1327
void MakeVarArray(int nb, double lb, double ub, bool integer, const std::string &name_prefix, std::vector< MPVariable * > *vars)
Creates an array of variables.
std::ostream & operator<<(std::ostream &stream, const LinearExpr &linear_expr)
virtual void SetRelativeMipGap(double value)=0
friend class SCIPInterface
Definition: linear_solver.h:761
void MaximizeLinearExpr(const LinearExpr &linear_expr)
Resets the current objective to maximize linear_expr.
Definition: linear_solver.h:918
Mixed integer Programming Solver using Coin CBC.
Definition: linear_solver.h:212
void FillSolutionResponseProto(MPSolutionResponse *response) const
Encodes the current solution in a solution response protocol buffer.
friend class GurobiInterface
Definition: linear_solver.h:762
void SetMIPParameters(const MPSolverParameters ¶m)
virtual bool ReadParameterFile(const std::string &filename)
friend class GLPKInterface
Definition: linear_solver.h:758
MPObjective * MutableObjective()
Returns the mutable objective object.
Definition: linear_solver.h:394
virtual void SetScalingMode(int value)=0
Advanced usage: presolve mode.
Definition: linear_solver.h:1329
bool SetSolverSpecificParametersAsString(const std::string ¶meters)
Advanced usage: pass solver specific parameters in text format.
void SetDoubleParamToUnsupportedValue(MPSolverParameters::DoubleParam param, double value)
friend class CBCInterface
Definition: linear_solver.h:968
friend class SCIPInterface
Definition: linear_solver.h:1079
friend class XpressInterface
Definition: linear_solver.h:764
OptimizationProblemType
The type of problems (LP or MIP) that will be solved and the underlying solver (GLOP,...
Definition: linear_solver.h:181
virtual void ExtractObjective()=0
const std::string & Name() const
Returns the name of the model set at construction.
Definition: linear_solver.h:254
double GetDoubleParam(MPSolverParameters::DoubleParam param) const
Returns the value of a double parameter.
void SetBounds(double lb, double ub)
Sets both the lower and upper bounds.
void ResetIntegerParam(MPSolverParameters::IntegerParam param)
Sets an integer parameter to its default value (default value defined in MPSolverParameters if it exi...
the model is trivially invalid (NaN coefficients, etc).
Definition: linear_solver.h:414
util::Status LoadSolutionFromProto(const MPSolutionResponse &response, double tolerance=kDefaultPrimalTolerance)
Load a solution encoded in a protocol buffer onto this solver for easy access via the MPSolver interf...
virtual std::string ValidFileExtensionForParameterFile() const
const std::vector< MPConstraint * > & constraints() const
Returns the array of constraints handled by the MPSolver.
Definition: linear_solver.h:343
virtual void SetDualTolerance(double value)=0
friend class CLPInterface
Definition: linear_solver.h:1219
void Write(const std::string &file_name)
Writes the model using the solver internal write function.
virtual void SetConstraintBounds(int index, double lb, double ub)=0
bool maximization() const
Is the optimization direction set to maximize?
friend class SatInterface
Definition: linear_solver.h:978
static const int64 kUnknownNumberOfNodes
Definition: linear_solver.h:1477
friend class SLMInterface
Definition: linear_solver.h:972
int GetNumThreads() const
Returns the number of threads to be used during solve.
Definition: linear_solver.h:579
friend class GLPKInterface
Definition: linear_solver.h:970
double Value() const
Returns the objective value of the best solution found so far.
virtual void SetLpAlgorithm(int value)=0
friend class GurobiInterface
Definition: linear_solver.h:973
const absl::flat_hash_map< const MPVariable *, double > & terms() const
Returns a map from variables to their coefficients in the constraint.
Definition: linear_solver.h:1152
void OptimizeLinearExpr(const LinearExpr &linear_expr, bool is_maximization)
Resets the current objective to take the value of linear_expr, and sets the objective direction to ma...
Linear Boolean Programming Solver.
Definition: linear_solver.h:223
MPVariable * MakeVar(double lb, double ub, bool integer, const std::string &name)
Creates a variable with the given bounds, integrality requirement and name.
virtual void SetPrimalTolerance(double value)=0
A class to express a linear objective.
Definition: linear_solver.h:873
util::Status ClampSolutionWithinBounds()
Resets values of out of bound variables to the corresponding bound and returns an error if any of the...
virtual void SetStartingLpBasis(const std::vector< MPSolver::BasisStatus > &variable_statuses, const std::vector< MPSolver::BasisStatus > &constraint_statuses)
Definition: linear_solver.h:1636
Advanced usage: tolerance for primal feasibility of basic solutions.
Definition: linear_solver.h:1321
absl::Duration DurationSinceConstruction() const
Definition: linear_solver.h:663
friend class CplexInterface
Definition: linear_solver.h:1082
double time_limit_in_secs() const
Definition: linear_solver.h:749
friend class SCIPInterface
Definition: linear_solver.h:1221
std::string SolverVersion() const
Returns a std::string describing the underlying solver and its version.
MPSolver *const solver_
Definition: linear_solver.h:1654
friend class GLPKInterface
Definition: linear_solver.h:1220
friend class BopInterface
Definition: linear_solver.h:977
BasisStatus
Advanced usage: possible basis status values for a variable and the slack variable of a linear constr...
Definition: linear_solver.h:611
MPSolver(const std::string &name, OptimizationProblemType problem_type)
Create a solver with the given name and underlying solver backend.
MPSolverResponseStatus LoadModelFromProto(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
static const double kDefaultDoubleParamValue
Definition: linear_solver.h:1378
void AddLinearExpr(const LinearExpr &linear_expr)
Adds linear_expr to the current objective, does not change the direction.
const MPVariable * indicator_variable() const
Definition: linear_solver.h:1189
void set_quiet(bool quiet_value)
Definition: linear_solver.h:1617
friend class SCIPInterface
Definition: linear_solver.h:971
bool constraint_is_extracted(int ct_index) const
Definition: linear_solver.h:1607
double ComputeExactConditionNumber() const
Advanced usage: computes the exact condition number of the current scaled basis: L1norm(B) * L1norm(i...
Reuse results from previous solve as much as the underlying solver allows.
Definition: linear_solver.h:1365
constexpr double kDefaultPrimalTolerance
Definition: linear_solver.h:161
virtual void ClearObjective()=0
double solution_value() const
Returns the value of the variable in the current solution.
bool OutputIsEnabled() const
Controls (or queries) the amount of output produced by the underlying solver.
const std::vector< MPVariable * > & variables() const
Returns the array of variables handled by the MPSolver.
Definition: linear_solver.h:277
virtual absl::optional< MPSolutionResponse > DirectlySolveProto(const MPModelRequest &request)
Definition: linear_solver.h:1492
virtual void SetUnsupportedIntegerParam(MPSolverParameters::IntegerParam param)
void SetBranchingPriority(int priority)
void SetHint(std::vector< std::pair< const MPVariable *, double > > hint)
Sets a hint for solution.
virtual void SetObjectiveCoefficient(const MPVariable *const variable, double coefficient)=0
void set_reduced_cost(double reduced_cost)
Definition: linear_solver.h:1105
friend class CBCInterface
Definition: linear_solver.h:1218
MPSolver::ResultStatus result_status() const
Definition: linear_solver.h:1620
int last_constraint_index_
Definition: linear_solver.h:1664
friend class KnapsackInterface
Definition: linear_solver.h:1088
double BestBound() const
Returns the best objective bound.
double trivial_worst_objective_bound() const
void Clear()
Clears the offset, all variables and coefficients, and the optimization direction.
friend class GLOPInterface
Definition: linear_solver.h:767
void SetDoubleParam(MPSolverParameters::DoubleParam param, double value)
Sets a double parameter to a specific value.
void SetStartingLpBasis(const std::vector< MPSolver::BasisStatus > &variable_statuses, const std::vector< MPSolver::BasisStatus > &constraint_statuses)
Advanced usage: Incrementality.
virtual MPSolver::BasisStatus column_status(int variable_index) const =0
virtual ~MPSolver()
void SetOptimizationDirection(bool maximize)
Sets the optimization direction (maximize: true or minimize: false).
virtual bool AddIndicatorConstraint(MPConstraint *const ct)
Definition: linear_solver.h:1522
int last_variable_index() const
Definition: linear_solver.h:1599
friend class CplexInterface
Definition: linear_solver.h:1224
int64 iterations() const
Returns the number of simplex iterations.
virtual void Write(const std::string &filename)
static void SolveWithProto(const MPModelRequest &model_request, MPSolutionResponse *response)
Solves the model encoded by a MPModelRequest protocol buffer and fills the solution encoded as a MPSo...
MPConstraint * MakeRowConstraint()
Creates a constraint with -infinity and +infinity bounds.
SynchronizationStatus sync_status_
Definition: linear_solver.h:1656
void InvalidateSolutionSynchronization()
MPVariable * MakeBoolVar(const std::string &name)
Creates a boolean variable.
void MakeNumVarArray(int nb, double lb, double ub, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of continuous variables.
virtual void SetVariableInteger(int index, bool integer)=0
friend class GLPKInterface
Definition: linear_solver.h:1078