23#include "absl/base/attributes.h"
24#include "absl/log/check.h"
27#include "ortools/glop/parameters.pb.h"
54 std::atomic<bool>* interrupt)
override {
59 void Reset()
override;
67 double new_value,
double old_value)
override;
70 double coefficient)
override;
76 int64_t
nodes()
const override;
82 bool IsLP()
const override;
83 bool IsMIP()
const override;
93 const std::vector<MPSolver::BasisStatus>& variable_statuses,
94 const std::vector<MPSolver::BasisStatus>& constraint_statuses)
override;
104 const std::string& parameters)
override;
107 void NonIncrementalChange();
111 std::vector<MPSolver::BasisStatus> column_status_;
112 std::vector<MPSolver::BasisStatus> row_status_;
113 glop::GlopParameters parameters_;
114 std::atomic<bool> interrupt_solver_;
124 interrupt_solver_(false) {}
132 linear_program_.Clear();
133 interrupt_solver_ =
false;
137 linear_program_.SetMaximizationProblem(
maximize_);
138 linear_program_.CleanUp();
142 VLOG(1) <<
"Setting time limit = " <<
solver_->time_limit() <<
" ms.";
143 parameters_.set_max_time_in_seconds(
144 static_cast<double>(
solver_->time_limit()) / 1000.0);
147 solver_->SetSolverSpecificParametersAsString(
148 solver_->solver_specific_parameter_string_);
149 lp_solver_.SetParameters(parameters_);
152 time_limit->RegisterExternalBooleanAsLimit(&interrupt_solver_);
154 lp_solver_.SolveWithTimeLimit(linear_program_,
time_limit.get());
161 const int num_vars =
solver_->variables_.size();
163 for (
int var_id = 0; var_id < num_vars; ++var_id) {
165 const glop::ColIndex lp_solver_var_id(var->
index());
168 lp_solver_.variable_values()[lp_solver_var_id];
172 lp_solver_.reduced_costs()[lp_solver_var_id];
176 lp_solver_.variable_statuses()[lp_solver_var_id];
180 const int num_constraints =
solver_->constraints_.size();
182 for (
int ct_id = 0; ct_id < num_constraints; ++ct_id) {
184 const glop::RowIndex lp_solver_ct_id(ct->
index());
187 lp_solver_.dual_values()[lp_solver_ct_id];
191 lp_solver_.constraint_statuses()[lp_solver_ct_id];
199 interrupt_solver_ =
true;
210 NonIncrementalChange();
214 NonIncrementalChange();
218 LOG(WARNING) <<
"Glop doesn't deal with integer variables.";
222 NonIncrementalChange();
226 NonIncrementalChange();
230 NonIncrementalChange();
235 double new_value,
double old_value) {
236 NonIncrementalChange();
240 NonIncrementalChange();
244 double coefficient) {
245 NonIncrementalChange();
253 return lp_solver_.GetNumberOfSimplexIterations();
257 LOG(DFATAL) <<
"Number of nodes only available for discrete problems";
262 return row_status_[constraint_index];
266 return column_status_[variable_index];
285 const glop::ColIndex num_cols(
solver_->variables_.size());
288 const glop::ColIndex new_col = linear_program_.CreateNewVariable();
289 DCHECK_EQ(new_col, col);
291 linear_program_.SetVariableBounds(col, var->
lb(), var->
ub());
298 const glop::RowIndex num_rows(
solver_->constraints_.size());
299 for (glop::RowIndex row(0); row < num_rows; ++row) {
303 const double lb = ct->
lb();
304 const double ub = ct->
ub();
305 const glop::RowIndex new_row = linear_program_.CreateNewConstraint();
306 DCHECK_EQ(new_row, row);
307 linear_program_.SetConstraintBounds(row, lb, ub);
309 for (
const auto& entry : ct->coefficients_) {
310 const int var_index = entry.first->index();
312 const glop::ColIndex col(var_index);
313 const double coeff = entry.second;
314 linear_program_.SetCoefficient(row, col, coeff);
320 linear_program_.SetObjectiveOffset(
solver_->Objective().offset());
321 for (
const auto& entry :
solver_->objective_->coefficients_) {
322 const int var_index = entry.first->index();
323 const glop::ColIndex col(var_index);
324 const double coeff = entry.second;
325 linear_program_.SetObjectiveCoefficient(col, coeff);
330 const std::vector<MPSolver::BasisStatus>& variable_statuses,
331 const std::vector<MPSolver::BasisStatus>& constraint_statuses) {
340 lp_solver_.SetInitialBasis(glop_variable_statuses, glop_constraint_statuses);
345 parameters_.set_log_search_progress(!
quiet_);
379 parameters_.set_use_preprocessing(
false);
382 parameters_.set_use_preprocessing(
true);
394 parameters_.set_use_scaling(
false);
397 parameters_.set_use_scaling(
true);
409 parameters_.set_use_dual_simplex(
true);
412 parameters_.set_use_dual_simplex(
false);
423 const std::string& parameters) {
429 lp_solver_.SetParameters(parameters_);
435void GLOPInterface::NonIncrementalChange() {
int64_t nodes() const override
void SetParameters(const MPSolverParameters ¶m) override
Sets all parameters in the underlying solver.
void SetRelativeMipGap(double value) override
Sets each parameter in the underlying solver.
void ExtractNewConstraints() override
Extracts the constraints that have not been extracted yet.
void SetCoefficient(MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override
Changes a coefficient in a constraint.
bool IsMIP() const override
Returns true if the problem is discrete and linear.
void ExtractNewVariables() override
Extracts the variables that have not been extracted yet.
bool InterruptSolve() override
bool IsContinuous() const override
--— Misc --—
void SetVariableInteger(int index, bool integer) override
Modifies integrality of an extracted variable.
GLOPInterface(MPSolver *solver)
bool SetSolverSpecificParametersAsString(const std::string ¶meters) override
MPSolutionResponse DirectlySolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt) override
void SetObjectiveOffset(double value) override
Changes the constant term in the linear objective.
void Reset() override
--— Model modifications and extraction --—
void ClearConstraint(MPConstraint *constraint) override
Clears a constraint from all its terms.
void SetPrimalTolerance(double value) override
void * underlying_solver() override
Returns the underlying solver.
void AddRowConstraint(MPConstraint *ct) override
Adds a linear constraint.
void ExtractObjective() override
Extracts the objective.
void SetObjectiveCoefficient(const MPVariable *variable, double coefficient) override
Changes a coefficient in the linear objective.
MPSolver::ResultStatus Solve(const MPSolverParameters ¶m) override
--— Solve --—
void ClearObjective() override
Clears the objective from all its terms.
~GLOPInterface() override
MPSolver::BasisStatus row_status(int constraint_index) const override
Returns the basis status of a row.
int64_t iterations() const override
---— Query statistics on the solution and the solve ---—
MPSolver::BasisStatus column_status(int variable_index) const override
Returns the basis status of a constraint.
void SetConstraintBounds(int index, double lb, double ub) override
Modify bounds of an extracted variable.
bool IsLP() const override
Returns true if the problem is continuous and linear.
void SetOptimizationDirection(bool maximize) override
Sets the optimization direction (min/max).
void SetDualTolerance(double value) override
void SetStartingLpBasis(const std::vector< MPSolver::BasisStatus > &variable_statuses, const std::vector< MPSolver::BasisStatus > &constraint_statuses) override
See MPSolver::SetStartingLpBasis().
void SetVariableBounds(int index, double lb, double ub) override
Modifies bounds of an extracted variable.
std::string SolverVersion() const override
Returns a string describing the underlying solver and its version.
void SetPresolveMode(int value) override
void AddVariable(MPVariable *var) override
Add a variable.
void SetScalingMode(int value) override
Sets the scaling mode.
void SetLpAlgorithm(int value) override
bool SupportsDirectlySolveProto(std::atomic< bool > *interrupt) const override
--— Directly solve proto is supported —
void set_dual_value(double dual_value)
double lb() const
Returns the lower bound.
double ub() const
Returns the upper bound.
int index() const
Returns the index of the constraint in the MPSolver::constraints_.
void set_variable_as_extracted(int var_index, bool extracted)
friend class MPConstraint
To access the maximize_ bool and the MPSolver.
void set_constraint_as_extracted(int ct_index, bool extracted)
void ResetExtractionInformation()
Resets the extraction information.
int last_variable_index_
Index in MPSolver::constraints_ of last variable extracted.
virtual void SetIntegerParamToUnsupportedValue(MPSolverParameters::IntegerParam param, int value)
Sets a supported integer parameter to an unsupported value.
int last_constraint_index_
Index in MPSolver::variables_ of last constraint extracted.
bool variable_is_extracted(int var_index) const
static constexpr int64_t kUnknownNumberOfNodes
void ExtractModel()
Extracts model stored in MPSolver.
double objective_value_
The value of the objective function.
void SetDoubleParamToUnsupportedValue(MPSolverParameters::DoubleParam param, double value)
Sets a supported double parameter to an unsupported value.
bool maximize_
Optimization direction.
MPSolverInterface(MPSolver *solver)
bool quiet_
Boolean indicator for the verbosity of the solver output.
void SetCommonParameters(const MPSolverParameters ¶m)
Sets parameters common to LP and MIP in the underlying solver.
MPSolver::ResultStatus result_status_
SynchronizationStatus sync_status_
Indicates whether the model and the solution are synchronized.
@ DUAL_TOLERANCE
Advanced usage: tolerance for dual feasibility of basic solutions.
@ RELATIVE_MIP_GAP
Limit for relative MIP gap.
@ PRESOLVE_OFF
Presolve is off.
@ PRESOLVE_ON
Presolve is on.
static const int kDefaultIntegerParamValue
@ PRESOLVE
Advanced usage: presolve mode.
@ LP_ALGORITHM
Algorithm to solve linear programs.
@ SCALING
Advanced usage: enable or disable matrix scaling.
@ SCALING_ON
Scaling is on.
@ SCALING_OFF
Scaling is off.
int GetIntegerParam(MPSolverParameters::IntegerParam param) const
Returns the value of an integer parameter.
static const double kDefaultDoubleParamValue
The class for variables of a Mathematical Programming (MP) model.
double lb() const
Returns the lower bound.
double ub() const
Returns the upper bound.
void set_reduced_cost(double reduced_cost)
void set_solution_value(double value)
int index() const
Returns the index of the variable in the MPSolver::variables_.
static std::unique_ptr< TimeLimit > FromParameters(const Parameters ¶meters)
A full-fledged linear programming solver.
static std::string GlopVersion()
Returns a string that describes the version of the solver.
void push_back(const value_type &val)
StrictITIVector< RowIndex, ConstraintStatus > ConstraintStatusColumn
Column of constraints (slack variables) statuses.
StrictITIVector< ColIndex, VariableStatus > VariableStatusRow
Row of variable statuses.
ProblemStatus
Different statuses for a given problem.
In SWIG mode, we don't want anything besides these top-level includes.
MPSolver::ResultStatus GlopToMPSolverResultStatus(glop::ProblemStatus s)
glop::VariableStatus MPSolverToGlopVariableStatus(MPSolver::BasisStatus s)
glop::ConstraintStatus MPSolverToGlopConstraintStatus(MPSolver::BasisStatus s)
MPSolutionResponse GlopSolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt_solve, std::function< void(const std::string &)> logging_callback)
bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string, ProtoType *proto)
MPSolver::BasisStatus GlopToMPSolverConstraintStatus(glop::ConstraintStatus s)
MPSolver::BasisStatus GlopToMPSolverVariableStatus(glop::VariableStatus s)
MPSolverInterface * BuildGLOPInterface(MPSolver *const solver)
Register GLOP in the global linear solver factory.