23#include "absl/base/attributes.h"
24#include "google/protobuf/text_format.h"
30#include "ortools/bop/bop_parameters.pb.h"
50 LOG(DFATAL) <<
"Invalid bop::BopSolveStatus";
65 void Reset()
override;
73 double new_value,
double old_value)
override;
82 int64_t
nodes()
const override;
88 bool IsLP()
const override;
89 bool IsMIP()
const override;
110 void NonIncrementalChange();
114 std::vector<MPSolver::BasisStatus> column_status_;
115 std::vector<MPSolver::BasisStatus> row_status_;
116 bop::BopParameters parameters_;
117 std::atomic<bool> interrupt_solver_;
127 interrupt_solver_(false) {}
133 if (interrupt_solver_) {
152 parameters_.set_max_time_in_seconds(
155 parameters_.set_log_search_progress(!
quiet());
158 if (!
solver_->solution_hint_.empty()) {
159 const int num_vars =
solver_->variables_.size();
160 if (
solver_->solution_hint_.size() != num_vars) {
161 LOG(WARNING) <<
"Bop currently doesn't handle partial solution hints. "
162 <<
"Filling the missing positions with zeros...";
165 for (
const std::pair<const MPVariable*, double>& p :
167 initial_solution[glop::ColIndex(p.first->index())] =
173 solver_->solver_specific_parameter_string_);
177 time_limit->RegisterExternalBooleanAsLimit(&interrupt_solver_);
179 initial_solution.empty()
194 const size_t num_vars =
solver_->variables_.size();
196 for (
int var_id = 0; var_id < num_vars; ++var_id) {
198 const glop::ColIndex lp_solver_var_id(
var->index());
201 var->set_solution_value(
static_cast<double>(solution_value));
205 const size_t num_constraints =
solver_->constraints_.size();
214 linear_program_.
Clear();
215 interrupt_solver_ =
false;
219 NonIncrementalChange();
223 NonIncrementalChange();
227 NonIncrementalChange();
231 NonIncrementalChange();
235 NonIncrementalChange();
239 NonIncrementalChange();
244 double new_value,
double old_value) {
245 NonIncrementalChange();
249 NonIncrementalChange();
254 NonIncrementalChange();
262 LOG(DFATAL) <<
"Number of iterations not available";
267 LOG(DFATAL) <<
"Number of nodes not available";
276 return column_status_[variable_index];
289 interrupt_solver_ =
true;
300 const glop::ColIndex num_cols(
solver_->variables_.size());
304 DCHECK_EQ(new_col,
col);
307 if (
var->integer()) {
318 const glop::RowIndex num_rows(
solver_->constraints_.size());
319 for (glop::RowIndex
row(0);
row < num_rows; ++
row) {
323 const double lb =
ct->lb();
324 const double ub =
ct->ub();
326 DCHECK_EQ(new_row,
row);
329 for (
const auto& entry :
ct->coefficients_) {
330 const int var_index = entry.first->index();
333 const double coeff = entry.second;
342 for (
const auto& entry :
solver_->objective_->coefficients_) {
343 const int var_index = entry.first->index();
345 const double coeff = entry.second;
380 google::protobuf::TextFormat::MergeFromString(
parameters, ¶meters_);
385void BopInterface::NonIncrementalChange() {
bool IsMIP() const override
Returns true if the problem is discrete and linear.
void SetParameters(const MPSolverParameters ¶m) override
Sets all parameters in the underlying solver.
bool InterruptSolve() override
void SetRelativeMipGap(double value) override
Sets each parameter in the underlying solver.
void SetScalingMode(int value) override
Sets the scaling mode.
bool IsLP() const override
Returns true if the problem is continuous and linear.
MPSolver::BasisStatus row_status(int constraint_index) const override
Returns the basis status of a row.
void AddVariable(MPVariable *var) override
Add a variable.
void Reset() override
--— Model modifications and extraction --—
void * underlying_solver() override
Returns the underlying solver.
int64_t nodes() const override
void SetVariableInteger(int index, bool integer) override
Modifies integrality of an extracted variable.
void SetObjectiveCoefficient(const MPVariable *variable, double coefficient) override
Changes a coefficient in the linear objective.
void ExtractNewVariables() override
std::string SolverVersion() const override
Returns a string describing the underlying solver and its version.
void ClearObjective() override
Clears the objective from all its terms.
int64_t iterations() const override
---— Query statistics on the solution and the solve ---—
void ExtractObjective() override
BopInterface(MPSolver *solver)
void SetLpAlgorithm(int value) override
void SetCoefficient(MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override
Changes a coefficient in a constraint.
void SetVariableBounds(int index, double lb, double ub) override
Modifies bounds of an extracted variable.
bool SetSolverSpecificParametersAsString(const std::string ¶meters) override
void SetOptimizationDirection(bool maximize) override
Sets the optimization direction (min/max).
void SetConstraintBounds(int index, double lb, double ub) override
Modify bounds of an extracted variable.
void SetDualTolerance(double value) override
bool IsContinuous() const override
--— Misc --—
void SetPresolveMode(int value) override
void ClearConstraint(MPConstraint *constraint) override
Clears a constraint from all its terms.
MPSolver::BasisStatus column_status(int variable_index) const override
Returns the basis status of a constraint.
void ExtractNewConstraints() override
void SetObjectiveOffset(double value) override
Changes the constant term in the linear objective.
void AddRowConstraint(MPConstraint *ct) override
Adds a linear constraint.
void SetPrimalTolerance(double value) override
All these have no effect.
MPSolver::ResultStatus Solve(const MPSolverParameters ¶m) override
--— Solve --—
double offset() const
Gets the constant term in the objective.
void set_variable_as_extracted(int var_index, bool extracted)
static constexpr int64_t kUnknownNumberOfIterations
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
bool quiet() const
Returns the boolean indicating the verbosity of the solver output.
static constexpr int64_t kUnknownNumberOfNodes
void ExtractModel()
Extracts model stored in MPSolver.
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 maximize_
Optimization direction.
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.
@ PRESOLVE_OFF
Presolve is off.
@ PRESOLVE_ON
Presolve is on.
static const int kDefaultIntegerParamValue
@ PRESOLVE
Advanced usage: presolve mode.
int64_t time_limit() const
@ FEASIBLE
feasible, or stopped by limit.
@ NOT_SOLVED
not been solved yet.
@ INFEASIBLE
proven infeasible.
@ ABNORMAL
abnormal, i.e., error of some kind.
const MPObjective & Objective() const
bool SetSolverSpecificParametersAsString(const std::string ¶meters)
The class for variables of a Mathematical Programming (MP) model.
static std::unique_ptr< TimeLimit > FromParameters(const Parameters ¶meters)
const glop::DenseRow & variable_values() const
glop::Fractional best_bound() const
Returns the best bound found so far.
ABSL_MUST_USE_RESULT BopSolveStatus SolveWithTimeLimit(const glop::LinearProgram &linear_problem, TimeLimit *time_limit)
glop::Fractional objective_value() const
Returns the objective value of the solution with its offset.
void SetParameters(const BopParameters ¶meters)
void Clear()
Clears, i.e. reset the object to its initial value.
@ INTEGER
The variable must only take integer values.
void SetObjectiveOffset(Fractional objective_offset)
void SetObjectiveCoefficient(ColIndex col, Fractional value)
void SetVariableBounds(ColIndex col, Fractional lower_bound, Fractional upper_bound)
void SetVariableType(ColIndex col, VariableType type)
Set the type of the variable.
void SetConstraintBounds(RowIndex row, Fractional lower_bound, Fractional upper_bound)
void SetCoefficient(RowIndex row, ColIndex col, Fractional value)
Defines the coefficient for col / row.
RowIndex CreateNewConstraint()
ColIndex CreateNewVariable()
void SetMaximizationProblem(bool maximize)
void assign(IntType size, const T &v)
const ParentType & get() const
BopSolveStatus
Status of the solve of Bop.
@ NO_SOLUTION_FOUND
The solver didn't find any solution.
@ OPTIMAL_SOLUTION_FOUND
The solver found the proven optimal solution.
@ INFEASIBLE_PROBLEM
The problem is infeasible.
@ INVALID_PROBLEM
The problem is invalid.
@ FEASIBLE_SOLUTION_FOUND
In SWIG mode, we don't want anything besides these top-level includes.
MPSolverInterface * BuildBopInterface(MPSolver *const solver)
Register BOP in the global linear solver factory.