29#include "absl/status/status.h"
30#include "absl/status/statusor.h"
31#include "absl/strings/ascii.h"
32#include "absl/strings/match.h"
33#include "absl/strings/str_cat.h"
34#include "absl/strings/str_format.h"
35#include "absl/strings/str_replace.h"
36#include "absl/synchronization/mutex.h"
37#include "absl/synchronization/notification.h"
38#include "absl/time/time.h"
46#include "ortools/linear_solver/linear_solver.pb.h"
54 "Systematically verify the solution when calling Solve()"
55 ", and change the return value of Solve() to ABNORMAL if"
56 " an error was detected.");
58 "If --verify_solution is set: LOG(ERROR) all errors detected"
59 " during the verification of the solution.");
60ABSL_FLAG(
bool, linear_solver_enable_verbose_output,
false,
61 "If set, enables verbose output for the solver. Setting this flag"
62 " is the same as calling MPSolver::EnableOutput().");
64ABSL_FLAG(
bool, mpsolver_bypass_model_validation,
false,
65 "If set, the user-provided Model won't be verified before Solve()."
66 " Invalid models will typically trigger various error responses"
67 " from the underlying solvers; sometimes crashes.");
72 switch (solver_type) {
73 case MPModelRequest::PDLP_LINEAR_PROGRAMMING:
74 case MPModelRequest::GLOP_LINEAR_PROGRAMMING:
75 case MPModelRequest::CLP_LINEAR_PROGRAMMING:
76 case MPModelRequest::GLPK_LINEAR_PROGRAMMING:
77 case MPModelRequest::GUROBI_LINEAR_PROGRAMMING:
78 case MPModelRequest::XPRESS_LINEAR_PROGRAMMING:
79 case MPModelRequest::CPLEX_LINEAR_PROGRAMMING:
82 case MPModelRequest::SCIP_MIXED_INTEGER_PROGRAMMING:
83 case MPModelRequest::GLPK_MIXED_INTEGER_PROGRAMMING:
84 case MPModelRequest::CBC_MIXED_INTEGER_PROGRAMMING:
85 case MPModelRequest::GUROBI_MIXED_INTEGER_PROGRAMMING:
86 case MPModelRequest::KNAPSACK_MIXED_INTEGER_PROGRAMMING:
87 case MPModelRequest::BOP_INTEGER_PROGRAMMING:
88 case MPModelRequest::SAT_INTEGER_PROGRAMMING:
89 case MPModelRequest::XPRESS_MIXED_INTEGER_PROGRAMMING:
90 case MPModelRequest::CPLEX_MIXED_INTEGER_PROGRAMMING:
93 LOG(DFATAL) <<
"Invalid SolverType: " << solver_type;
99 if (
var ==
nullptr)
return 0.0;
105 if (
var ==
nullptr)
return;
107 auto it = coefficients_.find(
var);
115 if (it != coefficients_.end() && it->second != 0.0) {
116 const double old_value = it->second;
122 auto insertion_result = coefficients_.insert(std::make_pair(
var,
coeff));
123 const double old_value =
124 insertion_result.second ? 0.0 : insertion_result.first->second;
125 insertion_result.first->second =
coeff;
131 coefficients_.clear();
135 const bool change =
lb != lb_ ||
ub != ub_;
145 LOG(DFATAL) <<
"Dual value only available for continuous problems";
154 LOG(DFATAL) <<
"Basis status only available for continuous problems";
164bool MPConstraint::ContainsNewVariables() {
166 for (
const auto& entry : coefficients_) {
167 const int variable_index = entry.first->index();
168 if (variable_index >= last_variable_index ||
180 if (
var ==
nullptr)
return 0.0;
186 if (
var ==
nullptr)
return;
188 auto it = coefficients_.find(
var);
191 if (it == coefficients_.end() || it->second == 0.0)
return;
206 for (
auto var_value_pair : linear_expr.
terms()) {
208 <<
"Bad MPVariable* in LinearExpr, did you try adding an integer to an "
209 "MPVariable* directly?";
215 bool is_maximization) {
216 CheckLinearExpr(*interface_->
solver_, linear_expr);
218 coefficients_.clear();
220 for (
const auto& kv : linear_expr.
terms()) {
227 CheckLinearExpr(*interface_->
solver_, linear_expr);
229 for (
const auto& kv : linear_expr.
terms()) {
236 coefficients_.clear();
276 return (integer_ && interface_->
IsMIP()) ? round(solution_value_)
282 return solution_value_;
287 LOG(DFATAL) <<
"Reduced cost only available for continuous problems";
291 return reduced_cost_;
296 LOG(DFATAL) <<
"Basis status only available for continuous problems";
307 const bool change =
lb != lb_ ||
ub != ub_;
325 if (priority == branching_priority_)
return;
326 branching_priority_ = priority;
335 return interface_->SolverVersion();
343 if (num_threads < 1) {
344 return absl::InvalidArgumentError(
"num_threads must be a positive number.");
346 const absl::Status
status = interface_->SetNumThreads(num_threads);
348 num_threads_ = num_threads;
355 solver_specific_parameter_string_ =
parameters;
356 return interface_->SetSolverSpecificParametersAsString(
parameters);
361#if defined(USE_CLP) || defined(USE_CBC)
379#if defined(USE_CPLEX)
382#if defined(USE_XPRESS)
389 DCHECK(solver !=
nullptr);
401 return BuildGLPKInterface(
false, solver);
403 return BuildGLPKInterface(
true, solver);
405#if defined(USE_CLP) || defined(USE_CBC)
421#if defined(USE_CPLEX)
423 return BuildCplexInterface(
false, solver);
425 return BuildCplexInterface(
true, solver);
427#if defined(USE_XPRESS)
429 return BuildXpressInterface(
true, solver);
431 return BuildXpressInterface(
false, solver);
435 LOG(
FATAL) <<
"Linear solver not recognized.";
442int NumDigits(
int n) {
446 return static_cast<int>(
std::max(1.0L, log(1.0L * n) / log(10.0L) + 1.0));
448 return static_cast<int>(
std::max(1.0, log10(
static_cast<double>(n)) + 1.0));
457 construction_time_(
absl::Now()) {
458 interface_.reset(BuildSolverInterface(
this));
459 if (absl::GetFlag(FLAGS_linear_solver_enable_verbose_output)) {
462 objective_.reset(
new MPObjective(interface_.get()));
513struct NamedOptimizationProblemType {
547 const std::string
id =
548 absl::StrReplaceAll(absl::AsciiStrToUpper(solver_id), {{
"-",
"_"}});
552 if (MPModelRequest::SolverType_Parse(
id, &solver_type)) {
558 std::string lower_id = absl::AsciiStrToLower(
id);
561 if (absl::EndsWith(lower_id,
"_mip")) {
562 lower_id = lower_id.substr(0, lower_id.size() - 4);
566 if (lower_id ==
"cp_sat") {
572 if (named_solver.name == lower_id) {
573 *type = named_solver.problem_type;
584 if (named_solver.problem_type == optimization_problem_type) {
585 return named_solver.name;
588 LOG(
FATAL) <<
"Unrecognized solver type: "
589 <<
static_cast<int>(optimization_problem_type);
595 std::string* error) {
596 DCHECK(solver_type !=
nullptr);
600 *error = absl::StrCat(
"Solver type: ", text,
" does not exist.");
607 const std::string& solver_id) {
617 LOG(
WARNING) <<
"Unrecognized solver type: " << solver_id;
622 <<
" not linked in, or the license was not found.";
630 if (!variable_name_to_index_) GenerateVariableNameIndex();
632 absl::flat_hash_map<std::string, int>::const_iterator it =
633 variable_name_to_index_->find(var_name);
634 if (it == variable_name_to_index_->end())
return nullptr;
635 return variables_[it->second];
639 const std::string& constraint_name)
const {
640 if (!constraint_name_to_index_) GenerateConstraintNameIndex();
642 const auto it = constraint_name_to_index_->find(constraint_name);
643 if (it == constraint_name_to_index_->end())
return nullptr;
644 return constraints_[it->second];
650 const MPModelProto& input_model, std::string* error_message) {
657 return LoadModelFromProtoInternal(input_model,
true,
663 const MPModelProto& input_model, std::string* error_message) {
667 GenerateVariableNameIndex();
668 GenerateConstraintNameIndex();
670 return LoadModelFromProtoInternal(input_model,
false,
675MPSolverResponseStatus MPSolver::LoadModelFromProtoInternal(
676 const MPModelProto& input_model,
bool clear_names,
677 bool check_model_validity, std::string* error_message) {
678 CHECK(error_message !=
nullptr);
679 if (check_model_validity) {
681 if (!error.empty()) {
682 *error_message = error;
684 <<
"Invalid model given to LoadModelFromProto(): " << error;
685 if (absl::GetFlag(FLAGS_mpsolver_bypass_model_validation)) {
687 <<
"Ignoring the model error(s) because of"
688 <<
" --mpsolver_bypass_model_validation.";
690 return absl::StrContains(error,
"Infeasible") ? MPSOLVER_INFEASIBLE
691 : MPSOLVER_MODEL_INVALID;
696 if (input_model.has_quadratic_objective()) {
698 "Optimizing a quadratic objective is only supported through direct "
699 "proto solves. Please use MPSolver::SolveWithProto, or the solver's "
700 "direct proto solve function.";
701 return MPSOLVER_MODEL_INVALID;
706 const std::string empty;
707 for (
int i = 0; i < input_model.variable_size(); ++i) {
708 const MPVariableProto& var_proto = input_model.variable(i);
710 MakeNumVar(var_proto.lower_bound(), var_proto.upper_bound(),
711 clear_names ? empty : var_proto.name());
713 if (var_proto.branching_priority() != 0) {
716 objective->SetCoefficient(
variable, var_proto.objective_coefficient());
719 for (
const MPConstraintProto& ct_proto : input_model.constraint()) {
720 if (ct_proto.lower_bound() == -
infinity() &&
721 ct_proto.upper_bound() ==
infinity()) {
725 MPConstraint*
const ct =
727 clear_names ? empty : ct_proto.name());
728 ct->set_is_lazy(ct_proto.is_lazy());
729 for (
int j = 0; j < ct_proto.var_index_size(); ++j) {
730 ct->SetCoefficient(variables_[ct_proto.var_index(j)],
731 ct_proto.coefficient(j));
735 for (
const MPGeneralConstraintProto& general_constraint :
736 input_model.general_constraint()) {
737 switch (general_constraint.general_constraint_case()) {
738 case MPGeneralConstraintProto::kIndicatorConstraint: {
740 general_constraint.indicator_constraint().constraint();
747 MPConstraint*
const constraint =
new MPConstraint(
748 constraint_index,
proto.lower_bound(),
proto.upper_bound(),
749 clear_names ?
"" :
proto.name(), interface_.get());
750 if (constraint_name_to_index_) {
755 constraint_is_extracted_.push_back(
false);
758 for (
int j = 0; j <
proto.var_index_size(); ++j) {
760 proto.coefficient(j));
764 variables_[general_constraint.indicator_constraint().var_index()];
767 general_constraint.indicator_constraint().var_value();
769 if (!interface_->AddIndicatorConstraint(
constraint)) {
770 *error_message =
"Solver doesn't support indicator constraints";
771 return MPSOLVER_MODEL_INVALID;
776 *error_message = absl::StrFormat(
777 "Optimizing general constraints of type %i is only supported "
778 "through direct proto solves. Please use MPSolver::SolveWithProto, "
779 "or the solver's direct proto solve function.",
780 general_constraint.general_constraint_case());
781 return MPSOLVER_MODEL_INVALID;
785 objective->SetOptimizationDirection(input_model.maximize());
786 if (input_model.has_objective_offset()) {
787 objective->SetOffset(input_model.objective_offset());
791 solution_hint_.clear();
792 for (
int i = 0; i < input_model.solution_hint().var_index_size(); ++i) {
793 solution_hint_.push_back(
794 std::make_pair(variables_[input_model.solution_hint().var_index(i)],
795 input_model.solution_hint().var_value(i)));
797 return MPSOLVER_MODEL_IS_VALID;
801MPSolverResponseStatus ResultStatusToMPSolverResponseStatus(
805 return MPSOLVER_OPTIMAL;
807 return MPSOLVER_FEASIBLE;
809 return MPSOLVER_INFEASIBLE;
811 return MPSOLVER_UNBOUNDED;
813 return MPSOLVER_ABNORMAL;
815 return MPSOLVER_MODEL_INVALID;
817 return MPSOLVER_NOT_SOLVED;
819 return MPSOLVER_UNKNOWN_STATUS;
827 ResultStatusToMPSolverResponseStatus(interface_->result_status_));
831 for (
int i = 0; i < variables_.size(); ++i) {
832 response->add_variable_value(variables_[i]->solution_value());
835 if (interface_->IsMIP()) {
836 response->set_best_objective_bound(interface_->best_objective_bound());
839 for (
int j = 0; j < constraints_.size(); ++j) {
840 response->add_dual_value(constraints_[j]->dual_value());
843 for (
int i = 0; i < variables_.size(); ++i) {
844 response->add_reduced_cost(variables_[i]->reduced_cost());
851bool InCategory(
int status,
int category) {
852 if (category == MPSOLVER_OPTIMAL)
return status == MPSOLVER_OPTIMAL;
854 return status == category;
857void AppendStatusStr(
const std::string& msg, MPSolutionResponse*
response) {
859 absl::StrCat(
response->status_str(),
860 (
response->status_str().empty() ?
"" :
"\n"), msg));
867 std::atomic<bool>* interrupt) {
870 if (interrupt !=
nullptr &&
872 response->set_status(MPSOLVER_INCOMPATIBLE_OPTIONS);
874 "Called MPSolver::SolveWithProto with an underlying solver that "
875 "doesn't support interruption.");
879 MPSolver solver(model_request.model().name(),
881 model_request.solver_type()));
882 if (model_request.enable_internal_solver_output()) {
888 auto optional_response =
889 solver.interface_->DirectlySolveProto(model_request, interrupt);
890 if (optional_response) {
891 *
response = std::move(optional_response).value();
895 const absl::optional<LazyMutableCopy<MPModelProto>> optional_model =
897 if (!optional_model) {
898 LOG_IF(
WARNING, model_request.enable_internal_solver_output())
899 <<
"Failed to extract a valid model from protocol buffer. Status: "
900 << ProtoEnumToString<MPSolverResponseStatus>(
response->status()) <<
" ("
904 std::string error_message;
905 response->set_status(solver.LoadModelFromProtoInternal(
906 optional_model->get(),
true,
907 false, &error_message));
910 if (
response->status() != MPSOLVER_MODEL_IS_VALID) {
911 response->set_status_str(error_message);
912 LOG_IF(
WARNING, model_request.enable_internal_solver_output())
913 <<
"LoadModelFromProtoInternal() failed even though the model was "
915 << ProtoEnumToString<MPSolverResponseStatus>(
response->status()) <<
" ("
916 <<
response->status() <<
"); Error: " << error_message;
919 if (model_request.has_solver_time_limit_seconds()) {
921 absl::Seconds(model_request.solver_time_limit_seconds()));
923 std::string warning_message;
924 if (model_request.has_solver_specific_parameters()) {
926 model_request.solver_specific_parameters())) {
927 if (model_request.ignore_solver_specific_parameters_failure()) {
930 "Warning: the solver specific parameters were not successfully "
933 response->set_status(MPSOLVER_MODEL_INVALID_SOLVER_PARAMETERS);
939 if (interrupt ==
nullptr) {
945 const absl::Time start_time = absl::Now();
946 absl::Time interrupt_time;
947 bool interrupted_by_user =
false;
949 absl::Notification solve_finished;
950 auto polling_func = [&interrupt, &solve_finished, &solver,
951 &interrupted_by_user, &interrupt_time,
953 constexpr absl::Duration kPollDelay = absl::Microseconds(100);
954 constexpr absl::Duration kMaxInterruptionDelay = absl::Seconds(10);
956 while (!interrupt->load()) {
957 if (solve_finished.HasBeenNotified())
return;
958 absl::SleepFor(kPollDelay);
963 solver.InterruptSolve();
964 interrupt_time = absl::Now();
965 interrupted_by_user =
true;
980 for (absl::Duration poll_delay = kPollDelay;
981 absl::Now() <= interrupt_time + kMaxInterruptionDelay;
983 if (solve_finished.WaitForNotificationWithTimeout(poll_delay)) {
986 solver.InterruptSolve();
991 <<
"MPSolver::InterruptSolve() seems to be ignored by the "
992 "underlying solver, despite repeated calls over at least "
993 << absl::FormatDuration(kMaxInterruptionDelay)
994 <<
". Solver type used: "
995 << MPModelRequest_SolverType_Name(model_request.solver_type());
1008 thread_pool.
Schedule(polling_func);
1012 if (!interrupt->load()) {
1014 solver.FillSolutionResponseProto(
response);
1016 response->set_status(MPSOLVER_CANCELLED_BY_USER);
1018 "Solve not started, because the user set the atomic<bool> in "
1019 "MPSolver::SolveWithProto() to true before solving could "
1022 solve_finished.Notify();
1027 if (interrupted_by_user) {
1030 if (InCategory(
response->status(), MPSOLVER_NOT_SOLVED)) {
1031 response->set_status(MPSOLVER_CANCELLED_BY_USER);
1035 "User interrupted MPSolver::SolveWithProto() by setting the "
1036 "atomic<bool> to true at %s (%s after solving started.)",
1037 absl::FormatTime(interrupt_time),
1038 absl::FormatDuration(interrupt_time - start_time)),
1043 if (!warning_message.empty()) {
1044 AppendStatusStr(warning_message,
response);
1049 DCHECK(output_model !=
nullptr);
1050 output_model->Clear();
1052 output_model->set_name(
Name());
1054 for (
int j = 0; j < variables_.size(); ++j) {
1056 MPVariableProto*
const variable_proto = output_model->add_variable();
1059 variable_proto->set_name(
var->name());
1060 variable_proto->set_lower_bound(
var->lb());
1061 variable_proto->set_upper_bound(
var->ub());
1062 variable_proto->set_is_integer(
var->integer());
1063 if (objective_->GetCoefficient(
var) != 0.0) {
1064 variable_proto->set_objective_coefficient(
1065 objective_->GetCoefficient(
var));
1067 if (
var->branching_priority() != 0) {
1068 variable_proto->set_branching_priority(
var->branching_priority());
1078 absl::flat_hash_map<const MPVariable*, int> var_to_index;
1079 for (
int j = 0; j < variables_.size(); ++j) {
1080 var_to_index[variables_[j]] = j;
1084 for (
int i = 0; i < constraints_.size(); ++i) {
1086 MPConstraintProto* constraint_proto;
1088 MPGeneralConstraintProto*
const general_constraint_proto =
1089 output_model->add_general_constraint();
1091 MPIndicatorConstraint*
const indicator_constraint_proto =
1092 general_constraint_proto->mutable_indicator_constraint();
1093 indicator_constraint_proto->set_var_index(
1096 constraint_proto = indicator_constraint_proto->mutable_constraint();
1098 constraint_proto = output_model->add_constraint();
1106 std::vector<std::pair<int, double>> linear_term;
1107 for (
const auto& entry :
constraint->coefficients_) {
1111 const double coeff = entry.second;
1112 linear_term.push_back(std::pair<int, double>(var_index,
coeff));
1116 std::sort(linear_term.begin(), linear_term.end());
1118 for (
const std::pair<int, double>& var_and_coeff : linear_term) {
1119 constraint_proto->add_var_index(var_and_coeff.first);
1120 constraint_proto->add_coefficient(var_and_coeff.second);
1124 output_model->set_maximize(
Objective().maximization());
1125 output_model->set_objective_offset(
Objective().offset());
1127 if (!solution_hint_.empty()) {
1128 PartialVariableAssignment*
const hint =
1129 output_model->mutable_solution_hint();
1130 for (
const auto& var_value_pair : solution_hint_) {
1131 hint->add_var_index(var_value_pair.first->index());
1132 hint->add_var_value(var_value_pair.second);
1140 if (
response.status() != MPSOLVER_OPTIMAL &&
1141 response.status() != MPSOLVER_FEASIBLE) {
1142 return absl::InvalidArgumentError(absl::StrCat(
1143 "Cannot load a solution unless its status is OPTIMAL or FEASIBLE"
1145 ProtoEnumToString<MPSolverResponseStatus>(
response.status()),
")"));
1150 if (
response.variable_value_size() != variables_.size()) {
1151 return absl::InvalidArgumentError(absl::StrCat(
1152 "Trying to load a solution whose number of variables (",
1154 ") does not correspond to the Solver's (", variables_.size(),
")"));
1156 interface_->ExtractModel();
1160 double largest_error = 0;
1161 int num_vars_out_of_bounds = 0;
1162 int last_offending_var = -1;
1163 for (
int i = 0; i <
response.variable_value_size(); ++i) {
1164 const double var_value =
response.variable_value(i);
1167 const double lb_error =
var->lb() - var_value;
1168 const double ub_error = var_value -
var->ub();
1169 if (lb_error > tolerance || ub_error > tolerance) {
1170 ++num_vars_out_of_bounds;
1172 last_offending_var = i;
1175 if (num_vars_out_of_bounds > 0) {
1176 return absl::InvalidArgumentError(absl::StrCat(
1177 "Loaded a solution whose variables matched the solver's, but ",
1178 num_vars_out_of_bounds,
" of ", variables_.size(),
1179 " variables were out of their bounds, by more than the primal"
1180 " tolerance which is: ",
1181 tolerance,
". Max error: ", largest_error,
", last offender var is #",
1182 last_offending_var,
": '", variables_[last_offending_var]->name(),
1186 for (
int i = 0; i <
response.variable_value_size(); ++i) {
1187 variables_[i]->set_solution_value(
response.variable_value(i));
1189 if (
response.dual_value_size() > 0) {
1190 if (
response.dual_value_size() != constraints_.size()) {
1191 return absl::InvalidArgumentError(absl::StrCat(
1192 "Trying to load a dual solution whose number of entries (",
1193 response.dual_value_size(),
") does not correspond to the Solver's (",
1194 constraints_.size(),
")"));
1196 for (
int i = 0; i <
response.dual_value_size(); ++i) {
1197 constraints_[i]->set_dual_value(
response.dual_value(i));
1200 if (
response.reduced_cost_size() > 0) {
1201 if (
response.reduced_cost_size() != variables_.size()) {
1202 return absl::InvalidArgumentError(absl::StrCat(
1203 "Trying to load a reduced cost solution whose number of entries (",
1205 ") does not correspond to the Solver's (", variables_.size(),
")"));
1207 for (
int i = 0; i <
response.reduced_cost_size(); ++i) {
1208 variables_[i]->set_reduced_cost(
response.reduced_cost(i));
1213 if (
response.has_objective_value()) {
1214 interface_->objective_value_ =
response.objective_value();
1216 if (
response.has_best_objective_bound()) {
1217 interface_->best_objective_bound_ =
response.best_objective_bound();
1222 return absl::OkStatus();
1227 absl::MutexLock lock(&global_count_mutex_);
1228 global_num_variables_ += variables_.size();
1229 global_num_constraints_ += constraints_.size();
1234 if (variable_name_to_index_) {
1235 variable_name_to_index_->clear();
1237 variable_is_extracted_.clear();
1238 if (constraint_name_to_index_) {
1239 constraint_name_to_index_->clear();
1241 constraint_is_extracted_.clear();
1242 interface_->Reset();
1243 solution_hint_.clear();
1251 const std::vector<BasisStatus>& variable_statuses,
1252 const std::vector<BasisStatus>& constraint_statuses) {
1253 interface_->SetStartingLpBasis(variable_statuses, constraint_statuses);
1257 const std::string&
name) {
1260 new MPVariable(var_index, lb, ub, integer,
name, interface_.get());
1261 if (variable_name_to_index_) {
1264 variables_.push_back(v);
1265 variable_is_extracted_.push_back(
false);
1266 interface_->AddVariable(v);
1271 const std::string&
name) {
1276 const std::string&
name) {
1285 const std::string&
name,
1286 std::vector<MPVariable*>* vars) {
1288 if (nb <= 0)
return;
1289 const int num_digits = NumDigits(nb);
1290 for (
int i = 0; i < nb; ++i) {
1295 absl::StrFormat(
"%s%0*d",
name.c_str(), num_digits, i);
1296 vars->push_back(
MakeVar(lb, ub, integer, vname));
1302 const std::string&
name,
1303 std::vector<MPVariable*>* vars) {
1308 const std::string&
name,
1309 std::vector<MPVariable*>* vars) {
1314 std::vector<MPVariable*>* vars) {
1327 const std::string&
name) {
1331 if (constraint_name_to_index_) {
1336 constraint_is_extracted_.push_back(
false);
1350 const std::string&
name) {
1360int MPSolver::ComputeMaxConstraintSize(
int min_constraint_index,
1361 int max_constraint_index)
const {
1362 int max_constraint_size = 0;
1364 DCHECK_LE(max_constraint_index, constraints_.size());
1365 for (
int i = min_constraint_index; i < max_constraint_index; ++i) {
1367 if (
ct->coefficients_.size() > max_constraint_size) {
1368 max_constraint_size =
ct->coefficients_.size();
1371 return max_constraint_size;
1374bool MPSolver::HasInfeasibleConstraints()
const {
1375 bool hasInfeasibleConstraints =
false;
1376 for (
int i = 0; i < constraints_.size(); ++i) {
1377 if (constraints_[i]->lb() > constraints_[i]->ub()) {
1378 LOG(
WARNING) <<
"Constraint " << constraints_[i]->name() <<
" (" << i
1379 <<
") has contradictory bounds:"
1380 <<
" lower bound = " << constraints_[i]->lb()
1381 <<
" upper bound = " << constraints_[i]->ub();
1382 hasInfeasibleConstraints =
true;
1385 return hasInfeasibleConstraints;
1388bool MPSolver::HasIntegerVariables()
const {
1389 for (
const MPVariable*
const variable : variables_) {
1397 return Solve(default_param);
1406 if (HasInfeasibleConstraints()) {
1408 return interface_->result_status_;
1412 if (absl::GetFlag(FLAGS_verify_solution)) {
1414 VLOG(1) <<
"--verify_solution enabled, but the solver did not find a"
1415 <<
" solution: skipping the verification.";
1418 absl::GetFlag(FLAGS_log_verification_errors))) {
1420 interface_->result_status_ =
status;
1428 interface_->Write(file_name);
1433 const std::string prefix =
"Variable '" +
var.
name() +
"': domain = ";
1436 return prefix +
"∅";
1440 if (
var.integer() &&
var.ub() -
var.lb() <= 1) {
1441 const int64_t lb =
static_cast<int64_t
>(ceil(
var.lb()));
1442 const int64_t ub =
static_cast<int64_t
>(floor(
var.ub()));
1444 return prefix +
"∅";
1445 }
else if (lb == ub) {
1446 return absl::StrFormat(
"%s{ %d }", prefix.c_str(), lb);
1448 return absl::StrFormat(
"%s{ %d, %d }", prefix.c_str(), lb, ub);
1452 if (
var.lb() ==
var.ub()) {
1453 return absl::StrFormat(
"%s{ %f }", prefix.c_str(),
var.lb());
1455 return prefix + (
var.integer() ?
"Integer" :
"Real") +
" in " +
1457 ? std::string(
"]-∞")
1458 :
absl::StrFormat(
"[%f",
var.lb())) +
1460 (
var.ub() >= MPSolver::infinity() ?
std::string(
"+∞[")
1461 :
absl::StrFormat(
"%f]",
var.ub()));
1464std::string PrettyPrintConstraint(
const MPConstraint& constraint) {
1465 std::string prefix =
"Constraint '" + constraint.name() +
"': ";
1468 constraint.lb() > constraint.ub()) {
1469 return prefix +
"ALWAYS FALSE";
1473 return prefix +
"ALWAYS TRUE";
1475 prefix +=
"<linear expr>";
1477 if (constraint.lb() == constraint.ub()) {
1478 return absl::StrFormat(
"%s = %f", prefix.c_str(), constraint.lb());
1482 return absl::StrFormat(
"%s ≤ %f", prefix.c_str(), constraint.ub());
1485 return absl::StrFormat(
"%s ≥ %f", prefix.c_str(), constraint.lb());
1487 return absl::StrFormat(
"%s ∈ [%f, %f]", prefix.c_str(), constraint.lb(),
1493 interface_->ExtractModel();
1496 if (std::isnan(
value)) {
1497 return absl::InvalidArgumentError(
1498 absl::StrCat(
"NaN value for ", PrettyPrintVar(*
variable)));
1500 if (value < variable->lb()) {
1507 return absl::OkStatus();
1512 if (!interface_->CheckSolutionIsSynchronizedAndExists())
return {};
1513 std::vector<double> activities(constraints_.size(), 0.0);
1514 for (
int i = 0; i < constraints_.size(); ++i) {
1517 for (
const auto& entry :
constraint.coefficients_) {
1518 sum.
Add(entry.first->solution_value() * entry.second);
1520 activities[i] = sum.
Value();
1527 double max_observed_error = 0;
1528 if (tolerance < 0) tolerance =
infinity();
1532 for (
int i = 0; i < variables_.size(); ++i) {
1534 const double value =
var.solution_value();
1536 if (std::isnan(
value)) {
1539 LOG_IF(
ERROR, log_errors) <<
"NaN value for " << PrettyPrintVar(
var);
1544 if (
value <
var.lb() - tolerance) {
1548 <<
"Value " <<
value <<
" too low for " << PrettyPrintVar(
var);
1553 if (
value >
var.ub() + tolerance) {
1557 <<
"Value " <<
value <<
" too high for " << PrettyPrintVar(
var);
1562 if (fabs(
value - round(
value)) > tolerance) {
1564 max_observed_error =
1567 <<
"Non-integer value " <<
value <<
" for " << PrettyPrintVar(
var);
1571 if (!
IsMIP() && HasIntegerVariables()) {
1572 LOG_IF(
INFO, log_errors) <<
"Skipped variable integrality check, because "
1573 <<
"a continuous relaxation of the model was "
1574 <<
"solved (i.e., the selected solver does not "
1575 <<
"support integer variables).";
1580 for (
int i = 0; i < constraints_.size(); ++i) {
1582 const double activity = activities[i];
1584 double inaccurate_activity = 0.0;
1585 for (
const auto& entry :
constraint.coefficients_) {
1586 inaccurate_activity += entry.first->solution_value() * entry.second;
1589 if (std::isnan(activity) || std::isnan(inaccurate_activity)) {
1593 <<
"NaN value for " << PrettyPrintConstraint(
constraint);
1603 max_observed_error =
1606 <<
"Activity " << activity <<
" too low for "
1608 }
else if (inaccurate_activity <
constraint.
lb() - tolerance) {
1610 <<
"Activity " << activity <<
", computed with the (inaccurate)"
1611 <<
" standard sum of its terms, is too low for "
1618 max_observed_error =
1621 <<
"Activity " << activity <<
" too high for "
1623 }
else if (inaccurate_activity >
constraint.
ub() + tolerance) {
1625 <<
"Activity " << activity <<
", computed with the (inaccurate)"
1626 <<
" standard sum of its terms, is too high for "
1637 double inaccurate_objective_value = objective.
offset();
1638 for (
const auto& entry : objective.coefficients_) {
1639 const double term = entry.first->solution_value() * entry.second;
1640 objective_sum.
Add(term);
1641 inaccurate_objective_value += term;
1643 const double actual_objective_value = objective_sum.
Value();
1645 objective.
Value(), actual_objective_value, tolerance, tolerance)) {
1648 max_observed_error, fabs(actual_objective_value - objective.
Value()));
1650 <<
"Objective value " << objective.
Value() <<
" isn't accurate"
1651 <<
", it should be " << actual_objective_value
1652 <<
" (delta=" << actual_objective_value - objective.
Value() <<
").";
1654 inaccurate_objective_value,
1655 tolerance, tolerance)) {
1657 <<
"Objective value " << objective.
Value() <<
" doesn't correspond"
1658 <<
" to the value computed with the standard (and therefore inaccurate)"
1659 <<
" sum of its terms.";
1661 if (num_errors > 0) {
1663 <<
"There were " << num_errors <<
" errors above the tolerance ("
1664 << tolerance <<
"), the largest was " << max_observed_error;
1681 return interface_->ComputeExactConditionNumber();
1685 if (
var ==
nullptr)
return false;
1686 if (
var->index() >= 0 &&
var->index() < variables_.size()) {
1688 return variables_[
var->index()] ==
var;
1694 std::string* model_str)
const {
1699 const auto status_or =
1701 *model_str = status_or.value_or(
"");
1702 return status_or.ok();
1706 std::string* model_str)
const {
1711 const auto status_or =
1713 *model_str = status_or.value_or(
"");
1714 return status_or.ok();
1718 for (
const auto& var_value_pair : hint) {
1720 <<
"hint variable does not belong to this solver";
1722 solution_hint_ = std::move(hint);
1725void MPSolver::GenerateVariableNameIndex()
const {
1726 if (variable_name_to_index_)
return;
1727 variable_name_to_index_ = absl::flat_hash_map<std::string, int>();
1733void MPSolver::GenerateConstraintNameIndex()
const {
1734 if (constraint_name_to_index_)
return;
1735 constraint_name_to_index_ = absl::flat_hash_map<std::string, int>();
1736 for (
const MPConstraint*
const cst : constraints_) {
1744 interface_->SetCallback(mp_callback);
1748 return interface_->SupportsCallbacks();
1752absl::Mutex MPSolver::global_count_mutex_(absl::kConstInit);
1753int64_t MPSolver::global_num_variables_ = 0;
1754int64_t MPSolver::global_num_constraints_ = 0;
1759 absl::MutexLock lock(&global_count_mutex_);
1760 return global_num_variables_;
1766 absl::MutexLock lock(&global_count_mutex_);
1767 return global_num_constraints_;
1773 case MPSOLVER_OPTIMAL:
1774 case MPSOLVER_FEASIBLE:
1775 case MPSOLVER_INFEASIBLE:
1776 case MPSOLVER_NOT_SOLVED:
1777 case MPSOLVER_UNBOUNDED:
1778 case MPSOLVER_ABNORMAL:
1779 case MPSOLVER_UNKNOWN_STATUS:
1783 case MPSOLVER_MODEL_IS_VALID:
1784 case MPSOLVER_CANCELLED_BY_USER:
1787 case MPSOLVER_MODEL_INVALID:
1788 case MPSOLVER_MODEL_INVALID_SOLUTION_HINT:
1789 case MPSOLVER_MODEL_INVALID_SOLVER_PARAMETERS:
1790 case MPSOLVER_SOLVER_TYPE_UNAVAILABLE:
1791 case MPSOLVER_INCOMPATIBLE_OPTIONS:
1795 <<
"MPSolverResponseStatusIsRpcError() called with invalid status "
1796 <<
"(value: " <<
status <<
")";
1808 sync_status_(MODEL_SYNCHRONIZED),
1809 result_status_(
MPSolver::NOT_SOLVED),
1811 last_constraint_index_(0),
1812 last_variable_index_(0),
1813 objective_value_(0.0),
1814 best_objective_bound_(0.0),
1820 LOG(
WARNING) <<
"Writing model not implemented in this solver interface.";
1855 solver_->variable_is_extracted_.assign(
solver_->variables_.size(),
false);
1856 solver_->constraint_is_extracted_.assign(
solver_->constraints_.size(),
false);
1862 <<
"The model has been changed since the solution was last computed."
1863 <<
" MPSolverInterface::sync_status_ = " <<
sync_status_;
1874 LOG(DFATAL) <<
"No solution exists. MPSolverInterface::result_status_ = "
1887 const double trivial_worst_bound =
1888 maximize_ ? -std::numeric_limits<double>::infinity()
1889 : std::numeric_limits<double>::infinity();
1891 VLOG(1) <<
"Best objective bound only available for discrete problems.";
1892 return trivial_worst_bound;
1895 return trivial_worst_bound;
1898 if (
solver_->variables_.empty() &&
solver_->constraints_.empty()) {
1912 LOG(DFATAL) <<
"ComputeExactConditionNumber not implemented for "
1913 << ProtoEnumToString<MPModelRequest::SolverType>(
1949 LOG(
WARNING) <<
"Trying to set an unsupported parameter: " << param <<
".";
1953 LOG(
WARNING) <<
"Trying to set an unsupported parameter: " << param <<
".";
1957 LOG(
WARNING) <<
"Trying to set a supported parameter: " << param
1958 <<
" to an unsupported value: " <<
value;
1962 LOG(
WARNING) <<
"Trying to set a supported parameter: " << param
1963 <<
" to an unsupported value: " <<
value;
1967 return absl::UnimplementedError(
1968 absl::StrFormat(
"SetNumThreads() not supported by %s.",
SolverVersion()));
1977 LOG(
WARNING) <<
"SetSolverSpecificParametersAsString() not supported by "
2002 : relative_mip_gap_value_(kDefaultRelativeMipGap),
2004 dual_tolerance_value_(kDefaultDualTolerance),
2005 presolve_value_(kDefaultPresolve),
2006 scaling_value_(kDefaultIntegerParamValue),
2007 lp_algorithm_value_(kDefaultIntegerParamValue),
2008 incrementality_value_(kDefaultIncrementality),
2009 lp_algorithm_is_default_(true) {}
2015 relative_mip_gap_value_ =
value;
2019 primal_tolerance_value_ =
value;
2023 dual_tolerance_value_ =
value;
2027 LOG(
ERROR) <<
"Trying to set an unknown parameter: " << param <<
".";
2037 LOG(
ERROR) <<
"Trying to set a supported parameter: " << param
2038 <<
" to an unknown value: " <<
value;
2040 presolve_value_ =
value;
2045 LOG(
ERROR) <<
"Trying to set a supported parameter: " << param
2046 <<
" to an unknown value: " <<
value;
2048 scaling_value_ =
value;
2053 LOG(
ERROR) <<
"Trying to set a supported parameter: " << param
2054 <<
" to an unknown value: " <<
value;
2056 lp_algorithm_value_ =
value;
2057 lp_algorithm_is_default_ =
false;
2062 LOG(
ERROR) <<
"Trying to set a supported parameter: " << param
2063 <<
" to an unknown value: " <<
value;
2065 incrementality_value_ =
value;
2069 LOG(
ERROR) <<
"Trying to set an unknown parameter: " << param <<
".";
2090 LOG(
ERROR) <<
"Trying to reset an unknown parameter: " << param <<
".";
2107 lp_algorithm_is_default_ =
true;
2115 LOG(
ERROR) <<
"Trying to reset an unknown parameter: " << param <<
".";
2134 return relative_mip_gap_value_;
2137 return primal_tolerance_value_;
2140 return dual_tolerance_value_;
2143 LOG(
ERROR) <<
"Trying to get an unknown parameter: " << param <<
".";
2153 return presolve_value_;
2157 return lp_algorithm_value_;
2160 return incrementality_value_;
2163 return scaling_value_;
2166 LOG(
ERROR) <<
"Trying to get an unknown parameter: " << param <<
".";
#define DLOG_IF(severity, condition)
#define LOG_IF(severity, condition)
#define DCHECK_LE(val1, val2)
#define DCHECK_NE(val1, val2)
#define DCHECK_GE(val1, val2)
#define DCHECK(condition)
#define DCHECK_EQ(val1, val2)
#define VLOG(verboselevel)
void Add(const FpNumber &value)
LinearExpr models a quantity that is linear in the decision variables (MPVariable) of an optimization...
const absl::flat_hash_map< const MPVariable *, double > & terms() const
An expression of the form:
const LinearExpr & linear_expr() const
double upper_bound() const
double lower_bound() const
The class for constraints of a Mathematical Programming (MP) model.
void SetBounds(double lb, double ub)
Sets both the lower and upper bounds.
const std::string & name() const
Returns the name of the constraint.
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable on the constraint.
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 ub() const
Returns the upper bound.
bool indicator_value() const
const MPVariable * indicator_variable() const
void Clear()
Clears all variables and coefficients. Does not clear the bounds.
bool is_lazy() const
Advanced usage: returns true if the constraint is "lazy" (see below).
void set_is_lazy(bool laziness)
Advanced usage: sets the constraint "laziness".
double lb() const
Returns the lower bound.
MPSolver::BasisStatus basis_status() const
Advanced usage: returns the basis status of the constraint.
double dual_value() const
Advanced usage: returns the dual value of the constraint in the current solution (only available for ...
A class to express a linear objective.
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable in the objective.
double GetCoefficient(const MPVariable *const var) const
Gets the coefficient of a given variable in the objective.
void SetOffset(double value)
Sets the constant term in the objective.
bool maximization() const
Is the optimization direction set to maximize?
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...
void AddLinearExpr(const LinearExpr &linear_expr)
Adds linear_expr to the current objective, does not change the direction.
double Value() const
Returns the objective value of the best solution found so far.
double offset() const
Gets the constant term in the objective.
double BestBound() const
Returns the best objective bound.
bool minimization() const
Is the optimization direction set to minimize?
void Clear()
Clears the offset, all variables and coefficients, and the optimization direction.
void SetMinimization()
Sets the optimization direction to minimize.
void SetOptimizationDirection(bool maximize)
Sets the optimization direction (maximize: true or minimize: false).
This mathematical programming (MP) solver class is the main class though which users build and solve ...
void FillSolutionResponseProto(MPSolutionResponse *response) const
Encodes the current solution in a solution response protocol buffer.
int NumConstraints() const
Returns the number of constraints.
static int64_t global_num_constraints()
static OptimizationProblemType ParseSolverTypeOrDie(const std::string &solver_id)
Parses the name of the solver and returns the correct optimization type or dies.
void MakeBoolVarArray(int nb, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of boolean variables.
bool VerifySolution(double tolerance, bool log_errors) const
Advanced usage: Verifies the correctness of the solution.
void Reset()
Advanced usage: resets extracted model to solve from scratch.
MPConstraint * constraint(int index) const
Returns the constraint at the given index.
MPVariable * LookupVariableOrNull(const std::string &var_name) const
Looks up a variable by name, and returns nullptr if it does not exist.
int64_t iterations() const
Returns the number of simplex iterations.
void SetStartingLpBasis(const std::vector< MPSolver::BasisStatus > &variable_statuses, const std::vector< MPSolver::BasisStatus > &constraint_statuses)
Advanced usage: Incrementality.
static bool SupportsProblemType(OptimizationProblemType problem_type)
Whether the given problem type is supported (this will depend on the targets that you linked).
static MPSolver * CreateSolver(const std::string &solver_id)
Recommended factory method to create a MPSolver instance, especially in non C++ languages.
MPVariable * MakeBoolVar(const std::string &name)
Creates a boolean variable.
void SetHint(std::vector< std::pair< const MPVariable *, double > > hint)
Sets a hint for solution.
double ComputeExactConditionNumber() const
Advanced usage: computes the exact condition number of the current scaled basis: L1norm(B) * L1norm(i...
ResultStatus
The status of solving the problem.
@ FEASIBLE
feasible, or stopped by limit.
@ NOT_SOLVED
not been solved yet.
@ INFEASIBLE
proven infeasible.
@ UNBOUNDED
proven unbounded.
@ ABNORMAL
abnormal, i.e., error of some kind.
@ MODEL_INVALID
the model is trivially invalid (NaN coefficients, etc).
static int64_t global_num_variables()
void MakeNumVarArray(int nb, double lb, double ub, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of continuous variables.
void MakeVarArray(int nb, double lb, double ub, bool integer, const std::string &name_prefix, std::vector< MPVariable * > *vars)
Creates an array of variables.
void * underlying_solver()
Advanced usage: returns the underlying solver.
OptimizationProblemType
The type of problems (LP or MIP) that will be solved and the underlying solver (GLOP,...
@ GLOP_LINEAR_PROGRAMMING
@ CPLEX_MIXED_INTEGER_PROGRAMMING
@ KNAPSACK_MIXED_INTEGER_PROGRAMMING
@ XPRESS_LINEAR_PROGRAMMING
@ GLPK_LINEAR_PROGRAMMING
@ CPLEX_LINEAR_PROGRAMMING
@ GUROBI_LINEAR_PROGRAMMING
@ XPRESS_MIXED_INTEGER_PROGRAMMING
@ GUROBI_MIXED_INTEGER_PROGRAMMING
@ BOP_INTEGER_PROGRAMMING
@ SCIP_MIXED_INTEGER_PROGRAMMING
@ PDLP_LINEAR_PROGRAMMING
@ SAT_INTEGER_PROGRAMMING
@ GLPK_MIXED_INTEGER_PROGRAMMING
@ CBC_MIXED_INTEGER_PROGRAMMING
bool SetSolverSpecificParametersAsString(const std::string ¶meters)
Advanced usage: pass solver specific parameters in text format.
absl::Status LoadSolutionFromProto(const MPSolutionResponse &response, double tolerance=std::numeric_limits< double >::infinity())
Load a solution encoded in a protocol buffer onto this solver for easy access via the MPSolver interf...
absl::Status SetNumThreads(int num_threads)
Sets the number of threads to use by the underlying solver.
const std::string & Name() const
Returns the name of the model set at construction.
std::string SolverVersion() const
Returns a string describing the underlying solver and its version.
bool SupportsCallbacks() const
void ExportModelToProto(MPModelProto *output_model) const
Exports model to protocol buffer.
void MakeIntVarArray(int nb, double lb, double ub, const std::string &name, std::vector< MPVariable * > *vars)
Creates an array of integer variables.
std::vector< double > ComputeConstraintActivities() const
Advanced usage: compute the "activities" of all constraints, which are the sums of their linear terms...
static double infinity()
Infinity.
static bool ParseSolverType(absl::string_view solver_id, OptimizationProblemType *type)
Parses the name of the solver.
int NumVariables() const
Returns the number of variables.
absl::Status ClampSolutionWithinBounds()
Resets values of out of bound variables to the corresponding bound and returns an error if any of the...
bool OwnsVariable(const MPVariable *var) const
int64_t nodes() const
Returns the number of branch-and-bound nodes evaluated during the solve.
void Clear()
Clears the objective (including the optimization direction), all variables and constraints.
MPVariable * variable(int index) const
Returns the variable at position index.
bool ExportModelAsLpFormat(bool obfuscate, std::string *model_str) const
Shortcuts to the homonymous MPModelProtoExporter methods, via exporting to a MPModelProto with Export...
void Write(const std::string &file_name)
Writes the model using the solver internal write function.
static void SolveWithProto(const MPModelRequest &model_request, MPSolutionResponse *response, std::atomic< bool > *interrupt=nullptr)
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.
void SetCallback(MPCallback *mp_callback)
MPSolverResponseStatus LoadModelFromProto(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
bool OutputIsEnabled() const
Controls (or queries) the amount of output produced by the underlying solver.
bool ExportModelAsMpsFormat(bool fixed_format, bool obfuscate, std::string *model_str) const
ABSL_MUST_USE_RESULT bool NextSolution()
Some solvers (MIP only, not LP) can produce multiple solutions to the problem.
MPVariable * MakeVar(double lb, double ub, bool integer, const std::string &name)
Creates a variable with the given bounds, integrality requirement and name.
MPConstraint * LookupConstraintOrNull(const std::string &constraint_name) const
Looks up a constraint by name, and returns nullptr if it does not exist.
MPVariable * MakeNumVar(double lb, double ub, const std::string &name)
Creates a continuous variable.
bool InterruptSolve()
Interrupts the Solve() execution to terminate processing if possible.
MPVariable * MakeIntVar(double lb, double ub, const std::string &name)
Creates an integer variable.
const MPObjective & Objective() const
Returns the objective object.
MPSolver(const std::string &name, OptimizationProblemType problem_type)
Create a solver with the given name and underlying solver backend.
ResultStatus Solve()
Solves the problem using the default parameter values.
void EnableOutput()
Enables solver logging.
void SuppressOutput()
Suppresses solver logging.
static bool SolverTypeSupportsInterruption(const MPModelRequest::SolverType solver)
MPSolverResponseStatus LoadModelFromProtoWithUniqueNamesOrDie(const MPModelProto &input_model, std::string *error_message)
Loads model from protocol buffer.
MPObjective * MutableObjective()
Returns the mutable objective object.
virtual OptimizationProblemType ProblemType() const
Returns the optimization problem type set at construction.
BasisStatus
Advanced usage: possible basis status values for a variable and the slack variable of a linear constr...
void SetTimeLimit(absl::Duration time_limit)
virtual ~MPSolverInterface()
double best_objective_bound() const
virtual void SetLpAlgorithm(int value)=0
virtual void SetIntegerParamToUnsupportedValue(MPSolverParameters::IntegerParam param, int value)
virtual void ExtractObjective()=0
void SetUnsupportedDoubleParam(MPSolverParameters::DoubleParam param)
virtual void ExtractNewVariables()=0
MPSolver::ResultStatus result_status_
static const int kDummyVariableIndex
void InvalidateSolutionSynchronization()
void SetMIPParameters(const MPSolverParameters ¶m)
int last_constraint_index_
virtual bool IsContinuous() const =0
virtual double ComputeExactConditionNumber() const
virtual void Write(const std::string &filename)
MPSolverInterface(MPSolver *const solver)
bool constraint_is_extracted(int ct_index) const
virtual void SetVariableBounds(int index, double lb, double ub)=0
virtual void SetPrimalTolerance(double value)=0
virtual void BranchingPriorityChangedForVariable(int var_index)
virtual void SetRelativeMipGap(double value)=0
double best_objective_bound_
virtual void SetOptimizationDirection(bool maximize)=0
virtual bool SetSolverSpecificParametersAsString(const std::string ¶meters)
virtual MPSolver::BasisStatus column_status(int variable_index) const =0
virtual void ExtractNewConstraints()=0
virtual MPSolver::BasisStatus row_status(int constraint_index) const =0
virtual std::string SolverVersion() const =0
virtual absl::Status SetNumThreads(int num_threads)
double objective_value() const
int last_variable_index() const
virtual void ClearConstraint(MPConstraint *const constraint)=0
bool CheckSolutionIsSynchronizedAndExists() const
bool CheckSolutionIsSynchronized() const
virtual bool CheckSolutionExists() const
virtual void SetObjectiveOffset(double value)=0
virtual void SetVariableInteger(int index, bool integer)=0
void ResetExtractionInformation()
virtual void ClearObjective()=0
bool variable_is_extracted(int var_index) const
virtual bool IsMIP() const =0
virtual void SetDualTolerance(double value)=0
virtual void SetPresolveMode(int value)=0
virtual void SetUnsupportedIntegerParam(MPSolverParameters::IntegerParam param)
virtual void SetCoefficient(MPConstraint *const constraint, const MPVariable *const variable, double new_value, double old_value)=0
virtual void SetObjectiveCoefficient(const MPVariable *const variable, double coefficient)=0
void SetDoubleParamToUnsupportedValue(MPSolverParameters::DoubleParam param, double value)
virtual void SetConstraintBounds(int index, double lb, double ub)=0
void SetCommonParameters(const MPSolverParameters ¶m)
SynchronizationStatus sync_status_
This class stores parameter settings for LP and MIP solvers.
static const double kDefaultRelativeMipGap
static const int kUnknownIntegerParamValue
void ResetIntegerParam(MPSolverParameters::IntegerParam param)
Sets an integer parameter to its default value (default value defined in MPSolverParameters if it exi...
void SetDoubleParam(MPSolverParameters::DoubleParam param, double value)
Sets a double parameter to a specific value.
IncrementalityValues
Advanced usage: Incrementality options.
@ INCREMENTALITY_OFF
Start solve from scratch.
@ INCREMENTALITY_ON
Reuse results from previous solve as much as the underlying solver allows.
@ SCALING_ON
Scaling is on.
@ SCALING_OFF
Scaling is off.
static const IncrementalityValues kDefaultIncrementality
void Reset()
Sets all parameters to their default value.
DoubleParam
Enumeration of parameters that take continuous values.
@ DUAL_TOLERANCE
Advanced usage: tolerance for dual feasibility of basic solutions.
@ PRIMAL_TOLERANCE
Advanced usage: tolerance for primal feasibility of basic solutions.
@ RELATIVE_MIP_GAP
Limit for relative MIP gap.
static const PresolveValues kDefaultPresolve
double GetDoubleParam(MPSolverParameters::DoubleParam param) const
Returns the value of a double parameter.
static const double kDefaultDualTolerance
static const double kUnknownDoubleParamValue
IntegerParam
Enumeration of parameters that take integer or categorical values.
@ LP_ALGORITHM
Algorithm to solve linear programs.
@ SCALING
Advanced usage: enable or disable matrix scaling.
@ PRESOLVE
Advanced usage: presolve mode.
@ INCREMENTALITY
Advanced usage: incrementality from one solve to the next.
@ BARRIER
Barrier algorithm.
PresolveValues
For each categorical parameter, enumeration of possible values.
@ PRESOLVE_ON
Presolve is on.
@ PRESOLVE_OFF
Presolve is off.
static const int kDefaultIntegerParamValue
static const double kDefaultPrimalTolerance
void SetIntegerParam(MPSolverParameters::IntegerParam param, int value)
Sets a integer parameter to a specific value.
static const double kDefaultDoubleParamValue
int GetIntegerParam(MPSolverParameters::IntegerParam param) const
Returns the value of an integer parameter.
MPSolverParameters()
The constructor sets all parameters to their default value.
void ResetDoubleParam(MPSolverParameters::DoubleParam param)
Sets a double parameter to its default value (default value defined in MPSolverParameters if it exist...
The class for variables of a Mathematical Programming (MP) model.
void SetBounds(double lb, double ub)
Sets both the lower and upper bounds.
double unrounded_solution_value() const
Advanced usage: unrounded solution value.
const std::string & name() const
Returns the name of the variable.
void set_solution_value(double value)
void SetBranchingPriority(int priority)
double ub() const
Returns the upper bound.
double reduced_cost() const
Advanced usage: returns the reduced cost of the variable in the current solution (only available for ...
void SetInteger(bool integer)
Sets the integrality requirement of the variable.
bool integer() const
Returns the integrality requirement of the variable.
int index() const
Returns the index of the variable in the MPSolver::variables_.
double lb() const
Returns the lower bound.
double solution_value() const
Returns the value of the variable in the current solution.
MPSolver::BasisStatus basis_status() const
Advanced usage: returns the basis status of the variable in the current solution (only available for ...
virtual std::string name() const
Object naming.
void Schedule(std::function< void()> closure)
SharedResponseManager * response
ABSL_FLAG(bool, verify_solution, false, "Systematically verify the solution when calling Solve()" ", and change the return value of Solve() to ABNORMAL if" " an error was detected.")
MPSolver::OptimizationProblemType problem_type
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
void STLDeleteElements(T *container)
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
const Collection::value_type::second_type & FindWithDefault(const Collection &collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
std::function< int64_t(const Model &)> Value(IntegerVariable v)
Collection of objects used to extend the Constraint Solver library.
MPSolverInterface * BuildGurobiInterface(bool mip, MPSolver *const solver)
MPSolverInterface * BuildSCIPInterface(MPSolver *const solver)
MPSolverInterface * BuildBopInterface(MPSolver *const solver)
constexpr double kDefaultPrimalTolerance
const absl::string_view ToString(MPSolver::OptimizationProblemType optimization_problem_type)
bool SolverTypeIsMip(MPModelRequest::SolverType solver_type)
MPSolverInterface * BuildCBCInterface(MPSolver *const solver)
bool AreWithinAbsoluteOrRelativeTolerances(FloatType x, FloatType y, FloatType relative_tolerance, FloatType absolute_tolerance)
absl::StatusOr< std::string > ExportModelAsMpsFormat(const MPModelProto &model, const MPModelExportOptions &options)
Outputs the current model (variables, constraints, objective) as a string encoded in MPS file format,...
bool AbslParseFlag(const absl::string_view text, MPSolver::OptimizationProblemType *solver_type, std::string *error)
absl::optional< LazyMutableCopy< MPModelProto > > ExtractValidMPModelOrPopulateResponseStatus(const MPModelRequest &request, MPSolutionResponse *response)
If the model is valid and non-empty, returns it (possibly after extracting the model_delta).
std::string FindErrorInMPModelProto(const MPModelProto &model, double abs_value_threshold, const bool accept_trivially_infeasible_bounds)
Returns an empty string iff the model is valid and not trivially infeasible.
MPSolverInterface * BuildSatInterface(MPSolver *const solver)
MPSolverInterface * BuildCLPInterface(MPSolver *const solver)
MPSolverInterface * BuildPdlpInterface(MPSolver *const solver)
constexpr NamedOptimizationProblemType kOptimizationProblemTypeNames[]
MPSolverInterface * BuildGLOPInterface(MPSolver *const solver)
bool GurobiIsCorrectlyInstalled()
absl::StatusOr< std::string > ExportModelAsLpFormat(const MPModelProto &model, const MPModelExportOptions &options)
Outputs the current model (variables, constraints, objective) as a string encoded in the so-called "C...
bool MPSolverResponseStatusIsRpcError(MPSolverResponseStatus status)
bool obfuscate
Obfuscates variable and constraint names.