 |
OR-Tools
8.1
|
Go to the documentation of this file.
22 #include "absl/status/status.h"
23 #include "absl/strings/str_format.h"
35 #include "CbcConfig.h"
36 #include "CbcMessage.hpp"
37 #include "CbcModel.hpp"
38 #include "CoinModel.hpp"
39 #include "OsiClpSolverInterface.hpp"
52 void Reset()
override;
61 num_threads_ = num_threads;
62 return absl::OkStatus();
74 bool IsLP()
const override {
return false; }
75 bool IsMIP()
const override {
return true; }
88 const MPVariable*
const variable,
double new_value,
89 double old_value)
override {
114 LOG(
FATAL) <<
"Basis status only available for continuous problems";
119 LOG(
FATAL) <<
"Basis status only available for continuous problems";
137 void ResetBestObjectiveBound();
142 void SetRelativeMipGap(
double value)
override;
143 void SetPrimalTolerance(
double value)
override;
144 void SetDualTolerance(
double value)
override;
145 void SetPresolveMode(
int value)
override;
146 void SetScalingMode(
int value)
override;
147 void SetLpAlgorithm(
int value)
override;
149 OsiClpSolverInterface osi_;
154 double relative_mip_gap_;
155 int num_threads_ = 1;
166 osi_.setStrParam(OsiProbName,
solver_->name_);
176 osi_.setStrParam(OsiProbName,
solver_->name_);
180 void CBCInterface::ResetBestObjectiveBound() {
191 osi_.setObjSense(maximize ? -1 : 1);
199 int MPSolverVarIndexToCbcVarIndex(
int var_index) {
return var_index + 1; }
205 osi_.setColBounds(MPSolverVarIndexToCbcVarIndex(var_index), lb, ub);
216 osi_.setInteger(MPSolverVarIndexToCbcVarIndex(var_index));
218 osi_.setContinuous(MPSolverVarIndexToCbcVarIndex(var_index));
228 osi_.setRowBounds(
index, lb, ub);
248 if (!
solver_->variables_.empty()) {
251 if (!
solver_->constraints_.empty()) {
266 if (
solver_->variables_.empty() &&
solver_->constraints_.empty()) {
281 build.addColumn(0,
nullptr,
nullptr, 1.0, 1.0,
283 const int nb_vars =
solver_->variables_.size();
284 for (
int i = 0; i < nb_vars; ++i) {
288 if (
var->name().empty()) {
289 build.addColumn(0,
nullptr,
nullptr,
var->lb(),
var->ub(), obj_coeff,
290 nullptr,
var->integer());
292 build.addColumn(0,
nullptr,
nullptr,
var->lb(),
var->ub(), obj_coeff,
293 var->name().c_str(),
var->integer());
298 int max_row_length = 0;
299 for (
int i = 0; i <
solver_->constraints_.size(); ++i) {
302 if (
ct->coefficients_.size() > max_row_length) {
303 max_row_length =
ct->coefficients_.size();
306 std::unique_ptr<int[]> indices(
new int[max_row_length]);
307 std::unique_ptr<double[]> coefs(
new double[max_row_length]);
309 for (
int i = 0; i <
solver_->constraints_.size(); ++i) {
311 const int size =
ct->coefficients_.size();
313 for (
const auto& entry :
ct->coefficients_) {
314 const int index = MPSolverVarIndexToCbcVarIndex(entry.first->index());
316 coefs[j] = entry.second;
319 if (
ct->name().empty()) {
320 build.addRow(size, indices.get(), coefs.get(),
ct->lb(),
ct->ub());
322 build.addRow(size, indices.get(), coefs.get(),
ct->lb(),
ct->ub(),
326 osi_.loadFromCoinModel(build);
342 VLOG(1) << absl::StrFormat(
"Model built in %.3f seconds.", timer.
Get());
344 ResetBestObjectiveBound();
347 CbcModel
model(osi_);
350 CoinMessageHandler message_handler;
351 model.passInMessageHandler(&message_handler);
353 message_handler.setLogLevel(0, 0);
354 message_handler.setLogLevel(1, 0);
355 message_handler.setLogLevel(2, 0);
356 message_handler.setLogLevel(3, 0);
358 message_handler.setLogLevel(0, 1);
359 message_handler.setLogLevel(1, 1);
360 message_handler.setLogLevel(2, 1);
361 message_handler.setLogLevel(3, 1);
376 SetParameters(param);
379 model.setTypePresolve(0);
382 model.setAllowableFractionGap(relative_mip_gap_);
386 ? callCbc(
"-solve ",
model)
387 : callCbc(absl::StrCat(
"-threads ", num_threads_,
" -solve "),
model);
388 const int kBadReturnStatus = 777;
389 CHECK_NE(kBadReturnStatus, return_status);
392 VLOG(1) << absl::StrFormat(
"Solved in %.3f seconds.", timer.
Get());
395 int tmp_status =
model.status();
397 VLOG(1) <<
"cbc result status: " << tmp_status;
410 switch (tmp_status) {
414 if (
model.isProvenOptimal()) {
416 }
else if (
model.isContinuousUnbounded()) {
418 }
else if (
model.isProvenInfeasible()) {
420 }
else if (
model.isAbandoned()) {
427 if (
model.bestSolution() !=
nullptr) {
443 const double*
const values =
model.bestSolution();
444 if (values !=
nullptr) {
446 for (
int i = 0; i <
solver_->variables_.size(); ++i) {
448 const int var_index = MPSolverVarIndexToCbcVarIndex(
var->index());
449 const double val = values[var_index];
450 var->set_solution_value(val);
451 VLOG(3) <<
var->name() <<
"=" << val;
454 VLOG(1) <<
"No feasible solution found.";
458 iterations_ =
model.getIterationCount();
459 nodes_ =
model.getNodeCount();
493 void CBCInterface::SetRelativeMipGap(
double value) {
494 relative_mip_gap_ =
value;
497 void CBCInterface::SetPrimalTolerance(
double value) {
505 void CBCInterface::SetDualTolerance(
double value) {
513 void CBCInterface::SetPresolveMode(
int value) {
525 void CBCInterface::SetScalingMode(
int value) {
529 void CBCInterface::SetLpAlgorithm(
int value) {
538 #endif // #if defined(USE_CBC)
void * underlying_solver() override
#define VLOG(verboselevel)
void ClearObjective() override
@ DUAL_TOLERANCE
Advanced usage: tolerance for dual feasibility of basic solutions.
virtual void ExtractModel()
@ LP_ALGORITHM
Algorithm to solve linear programs.
const MPObjective & Objective() const
Returns the objective object.
@ UNBOUNDED
proven unbounded.
double GetCoefficient(const MPVariable *const var) const
Gets the coefficient of a given variable in the objective.
#define CHECK_GE(val1, val2)
This mathematical programming (MP) solver class is the main class though which users build and solve ...
void SetVariableBounds(int var_index, double lb, double ub) override
MPVariable * LookupVariableOrNull(const std::string &var_name) const
Looks up a variable by name, and returns nullptr if it does not exist.
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
double offset() const
Gets the constant term in the objective.
void InvalidateSolutionSynchronization()
MPSolver::BasisStatus column_status(int variable_index) const override
@ ABNORMAL
abnormal, i.e., error of some kind.
This class stores parameter settings for LP and MIP solvers.
The class for constraints of a Mathematical Programming (MP) model.
void AddVariable(MPVariable *const var) override
@ PRESOLVE_ON
Presolve is on.
MPSolverInterface * BuildCBCInterface(MPSolver *const solver)
int GetIntegerParam(MPSolverParameters::IntegerParam param) const
Returns the value of an integer parameter.
void ResetExtractionInformation()
CBCInterface(MPSolver *const solver)
void ClearConstraint(MPConstraint *const constraint) override
static constexpr int64 kUnknownNumberOfIterations
bool IsMIP() const override
void SetUnsupportedDoubleParam(MPSolverParameters::DoubleParam param)
@ NOT_SOLVED
not been solved yet.
void ExtractNewVariables() override
static constexpr int64 kUnknownNumberOfNodes
@ PRESOLVE
Advanced usage: presolve mode.
void SetObjectiveCoefficient(const MPVariable *const variable, double coefficient) override
void AddRowConstraint(MPConstraint *const ct) override
BasisStatus
Advanced usage: possible basis status values for a variable and the slack variable of a linear constr...
MPConstraint * LookupConstraintOrNull(const std::string &constraint_name) const
Looks up a constraint by name, and returns nullptr if it does not exist.
static const double kDefaultPrimalTolerance
int64 iterations() const override
absl::Status SetNumThreads(int num_threads) override
virtual void SetUnsupportedIntegerParam(MPSolverParameters::IntegerParam param)
@ INCREMENTALITY_OFF
Start solve from scratch.
SynchronizationStatus sync_status_
std::string SolverVersion() const override
double time_limit_in_secs() const
@ PRIMAL_TOLERANCE
Advanced usage: tolerance for primal feasibility of basic solutions.
void SetConstraintBounds(int row_index, double lb, double ub) override
void ExtractObjective() override
ResultStatus
The status of solving the problem.
void SetObjectiveOffset(double value) override
void SetMIPParameters(const MPSolverParameters ¶m)
bool IsLP() const override
static const double kDefaultDualTolerance
bool IsContinuous() const override
void SetCoefficient(MPConstraint *const constraint, const MPVariable *const variable, double new_value, double old_value) override
void set_variable_as_extracted(int var_index, bool extracted)
@ SCALING
Advanced usage: enable or disable matrix scaling.
void SetVariableInteger(int var_index, bool integer) override
@ INCREMENTALITY
Advanced usage: incrementality from one solve to the next.
The class for variables of a Mathematical Programming (MP) model.
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
void SetCommonParameters(const MPSolverParameters ¶m)
int64 nodes() const override
@ INFEASIBLE
proven infeasible.
#define CHECK_NE(val1, val2)
void SetOptimizationDirection(bool maximize) override
bool CheckSolutionIsSynchronized() const
MPSolver::BasisStatus row_status(int constraint_index) const override
MPSolver::ResultStatus result_status_
void set_constraint_as_extracted(int ct_index, bool extracted)
void ExtractNewConstraints() override
MPSolver::ResultStatus Solve(const MPSolverParameters ¶m) override
@ FEASIBLE
feasible, or stopped by limit.
double best_objective_bound_