24#include "absl/log/check.h"
25#include "absl/strings/str_cat.h"
26#include "absl/types/span.h"
30#include "ortools/glop/parameters.pb.h"
31#include "ortools/linear_solver/linear_solver.pb.h"
36#include "ortools/sat/boolean_problem.pb.h"
37#include "ortools/sat/cp_model.pb.h"
40#include "ortools/sat/sat_parameters.pb.h"
54using operations_research::MPConstraintProto;
55using operations_research::MPModelProto;
56using operations_research::MPVariableProto;
60void ScaleConstraint(
const std::vector<double>& var_scaling,
61 MPConstraintProto* mp_constraint) {
62 const int num_terms = mp_constraint->coefficient_size();
63 for (
int i = 0;
i < num_terms; ++
i) {
64 const int var_index = mp_constraint->var_index(
i);
65 mp_constraint->set_coefficient(
66 i, mp_constraint->coefficient(
i) / var_scaling[
var_index]);
70void ApplyVarScaling(
const std::vector<double>& var_scaling,
71 MPModelProto* mp_model) {
72 const int num_variables = mp_model->variable_size();
73 for (
int i = 0;
i < num_variables; ++
i) {
74 const double scaling = var_scaling[
i];
75 const MPVariableProto& mp_var = mp_model->variable(
i);
76 const double old_lb = mp_var.lower_bound();
77 const double old_ub = mp_var.upper_bound();
78 const double old_obj = mp_var.objective_coefficient();
79 mp_model->mutable_variable(
i)->set_lower_bound(old_lb * scaling);
80 mp_model->mutable_variable(
i)->set_upper_bound(old_ub * scaling);
81 mp_model->mutable_variable(
i)->set_objective_coefficient(old_obj / scaling);
85 for (MPConstraintProto& mp_constraint : *mp_model->mutable_constraint()) {
86 ScaleConstraint(var_scaling, &mp_constraint);
88 for (MPGeneralConstraintProto& general_constraint :
89 *mp_model->mutable_general_constraint()) {
90 switch (general_constraint.general_constraint_case()) {
91 case MPGeneralConstraintProto::kIndicatorConstraint:
92 ScaleConstraint(var_scaling,
93 general_constraint.mutable_indicator_constraint()
94 ->mutable_constraint());
96 case MPGeneralConstraintProto::kAndConstraint:
97 case MPGeneralConstraintProto::kOrConstraint:
102 LOG(FATAL) <<
"Scaling unsupported for general constraint of type "
103 << general_constraint.general_constraint_case();
111 MPModelProto* mp_model) {
112 const int num_variables = mp_model->variable_size();
113 std::vector<double> var_scaling(num_variables, 1.0);
114 for (
int i = 0;
i < num_variables; ++
i) {
115 if (mp_model->variable(
i).is_integer())
continue;
116 if (max_bound == std::numeric_limits<double>::infinity()) {
117 var_scaling[
i] = scaling;
120 const double lb = mp_model->variable(
i).lower_bound();
121 const double ub = mp_model->variable(
i).upper_bound();
122 const double magnitude = std::max(std::abs(lb), std::abs(ub));
123 if (magnitude == 0 || magnitude > max_bound)
continue;
124 var_scaling[
i] = std::min(scaling, max_bound / magnitude);
126 ApplyVarScaling(var_scaling, mp_model);
134 const double initial_x =
x;
137 int64_t current_q = 1;
139 while (current_q < limit) {
140 const double q =
static_cast<double>(current_q);
141 const double qx = q * initial_x;
142 const double qtolerance = q * tolerance;
143 if (std::abs(qx - std::round(qx)) < qtolerance) {
147 const double floored_x = std::floor(
x);
148 if (floored_x >=
static_cast<double>(std::numeric_limits<int64_t>::max())) {
151 const int64_t new_q =
152 CapAdd(prev_q,
CapProd(
static_cast<int64_t
>(floored_x), current_q));
166double GetIntegralityMultiplier(
const MPModelProto& mp_model,
167 absl::Span<const double> var_scaling,
int var,
169 DCHECK(!mp_model.variable(
var).is_integer());
170 const MPConstraintProto&
ct = mp_model.constraint(
ct_index);
171 double multiplier = 1.0;
172 double var_coeff = 0.0;
173 const double max_multiplier = 1e4;
174 for (
int i = 0;
i <
ct.var_index().
size(); ++
i) {
175 if (
var ==
ct.var_index(
i)) {
176 var_coeff =
ct.coefficient(
i);
180 DCHECK(mp_model.variable(
ct.var_index(
i)).is_integer());
184 multiplier *
ct.coefficient(
i) / var_scaling[
ct.var_index(
i)];
187 if (multiplier == 0 || multiplier > max_multiplier)
return 0.0;
189 DCHECK_NE(var_coeff, 0.0);
192 for (
const double bound : {
ct.lower_bound(),
ct.upper_bound()}) {
193 if (!std::isfinite(
bound))
continue;
194 if (std::abs(std::round(
bound * multiplier) -
bound * multiplier) >
195 tolerance * multiplier) {
199 return std::abs(multiplier * var_coeff);
205 MPModelProto* mp_model,
207 const int num_variables = mp_model->variable_size();
208 const double tolerance = params.mip_wanted_precision();
209 int64_t num_changes = 0;
210 for (
int i = 0;
i < num_variables; ++
i) {
211 const MPVariableProto& mp_var = mp_model->variable(
i);
212 if (!mp_var.is_integer())
continue;
214 const double lb = mp_var.lower_bound();
215 const double new_lb = std::isfinite(lb) ? std::ceil(lb - tolerance) : lb;
218 mp_model->mutable_variable(
i)->set_lower_bound(new_lb);
221 const double ub = mp_var.upper_bound();
222 const double new_ub = std::isfinite(ub) ? std::floor(ub + tolerance) : ub;
225 mp_model->mutable_variable(
i)->set_upper_bound(new_ub);
228 if (new_ub < new_lb) {
229 SOLVER_LOG(logger,
"Empty domain for integer variable #",
i,
": [", lb,
239 const int num_variables = mp_model->variable_size();
240 int64_t num_variable_bounds_pushed_to_infinity = 0;
241 const double infinity = std::numeric_limits<double>::infinity();
242 for (
int i = 0;
i < num_variables; ++
i) {
243 MPVariableProto* mp_var = mp_model->mutable_variable(
i);
244 const double lb = mp_var->lower_bound();
245 if (std::isfinite(lb) && lb < -max_magnitude) {
246 ++num_variable_bounds_pushed_to_infinity;
247 mp_var->set_lower_bound(-infinity);
250 const double ub = mp_var->upper_bound();
251 if (std::isfinite(ub) && ub > max_magnitude) {
252 ++num_variable_bounds_pushed_to_infinity;
253 mp_var->set_upper_bound(infinity);
257 if (num_variable_bounds_pushed_to_infinity > 0) {
258 SOLVER_LOG(logger,
"Pushed ", num_variable_bounds_pushed_to_infinity,
259 " variable bounds to +/-infinity");
262 const int num_constraints = mp_model->constraint_size();
263 int64_t num_constraint_bounds_pushed_to_infinity = 0;
265 for (
int i = 0;
i < num_constraints; ++
i) {
266 MPConstraintProto* mp_ct = mp_model->mutable_constraint(
i);
267 const double lb = mp_ct->lower_bound();
268 if (std::isfinite(lb) && lb < -max_magnitude) {
269 ++num_constraint_bounds_pushed_to_infinity;
270 mp_ct->set_lower_bound(-infinity);
273 const double ub = mp_ct->upper_bound();
274 if (std::isfinite(ub) && ub > max_magnitude) {
275 ++num_constraint_bounds_pushed_to_infinity;
276 mp_ct->set_upper_bound(infinity);
280 for (
int i = 0;
i < mp_model->general_constraint_size(); ++
i) {
281 if (mp_model->general_constraint(
i).general_constraint_case() !=
282 MPGeneralConstraintProto::kIndicatorConstraint) {
286 MPConstraintProto* mp_ct = mp_model->mutable_general_constraint(
i)
287 ->mutable_indicator_constraint()
288 ->mutable_constraint();
289 const double lb = mp_ct->lower_bound();
290 if (std::isfinite(lb) && lb < -max_magnitude) {
291 ++num_constraint_bounds_pushed_to_infinity;
292 mp_ct->set_lower_bound(-infinity);
295 const double ub = mp_ct->upper_bound();
296 if (std::isfinite(ub) && ub > max_magnitude) {
297 ++num_constraint_bounds_pushed_to_infinity;
298 mp_ct->set_upper_bound(infinity);
302 if (num_constraint_bounds_pushed_to_infinity > 0) {
303 SOLVER_LOG(logger,
"Pushed ", num_constraint_bounds_pushed_to_infinity,
304 " constraint bounds to +/-infinity");
312 double max_dropped = 0.0;
313 const double drop = params.mip_drop_tolerance();
314 const int num_variables = mp_model->variable_size();
315 for (
int i = 0;
i < num_variables; ++
i) {
316 MPVariableProto*
var = mp_model->mutable_variable(
i);
317 if (
var->lower_bound() != 0.0 && std::abs(
var->lower_bound()) < drop) {
319 max_dropped = std::max(max_dropped, std::abs(
var->lower_bound()));
320 var->set_lower_bound(0.0);
322 if (
var->upper_bound() != 0.0 && std::abs(
var->upper_bound()) < drop) {
324 max_dropped = std::max(max_dropped, std::abs(
var->upper_bound()));
325 var->set_upper_bound(0.0);
328 const int num_constraints = mp_model->constraint_size();
329 for (
int i = 0;
i < num_constraints; ++
i) {
330 MPConstraintProto*
ct = mp_model->mutable_constraint(
i);
331 if (
ct->lower_bound() != 0.0 && std::abs(
ct->lower_bound()) < drop) {
333 max_dropped = std::max(max_dropped, std::abs(
ct->lower_bound()));
334 ct->set_lower_bound(0.0);
336 if (
ct->upper_bound() != 0.0 && std::abs(
ct->upper_bound()) < drop) {
338 max_dropped = std::max(max_dropped, std::abs(
ct->upper_bound()));
339 ct->set_upper_bound(0.0);
342 if (num_dropped > 0) {
343 SOLVER_LOG(logger,
"Set to zero ", num_dropped,
344 " variable or constraint bounds with largest magnitude ",
351 std::vector<double> max_bounds(num_variables);
352 for (
int i = 0;
i < num_variables; ++
i) {
353 double value = std::abs(mp_model->variable(
i).lower_bound());
354 value = std::max(
value, std::abs(mp_model->variable(
i).upper_bound()));
355 value = std::min(
value, params.mip_max_bound());
361 double largest_removed = 0.0;
368 int64_t num_removed = 0;
369 for (
int c = 0; c < num_constraints; ++c) {
370 MPConstraintProto*
ct = mp_model->mutable_constraint(c);
372 const int size =
ct->var_index().size();
373 if (
size == 0)
continue;
374 const double threshold =
375 params.mip_wanted_precision() /
static_cast<double>(
size);
376 for (
int i = 0;
i <
size; ++
i) {
377 const int var =
ct->var_index(
i);
378 const double coeff =
ct->coefficient(
i);
379 if (std::abs(coeff) * max_bounds[
var] < threshold) {
380 if (max_bounds[
var] != 0) {
381 largest_removed = std::max(largest_removed, std::abs(coeff));
385 ct->set_var_index(new_size,
var);
386 ct->set_coefficient(new_size, coeff);
389 num_removed +=
size - new_size;
390 ct->mutable_var_index()->Truncate(new_size);
391 ct->mutable_coefficient()->Truncate(new_size);
395 if (num_variables > 0) {
396 const double threshold =
397 params.mip_wanted_precision() /
static_cast<double>(num_variables);
398 for (
int var = 0;
var < num_variables; ++
var) {
399 const double coeff = mp_model->variable(
var).objective_coefficient();
400 if (coeff == 0.0)
continue;
401 if (std::abs(coeff) * max_bounds[
var] < threshold) {
403 if (max_bounds[
var] != 0) {
404 largest_removed = std::max(largest_removed, std::abs(coeff));
406 mp_model->mutable_variable(
var)->clear_objective_coefficient();
411 if (num_removed > 0) {
413 " near zero terms with largest magnitude of ", largest_removed,
419 const MPModelProto& mp_model,
422 for (
const MPGeneralConstraintProto& general_constraint :
423 mp_model.general_constraint()) {
424 switch (general_constraint.general_constraint_case()) {
425 case MPGeneralConstraintProto::kIndicatorConstraint:
427 case MPGeneralConstraintProto::kAndConstraint:
429 case MPGeneralConstraintProto::kOrConstraint:
432 SOLVER_LOG(logger,
"General constraints of type ",
433 general_constraint.general_constraint_case(),
434 " are not supported.");
440 const double threshold = params.mip_max_valid_magnitude();
441 const int num_variables = mp_model.variable_size();
442 for (
int i = 0;
i < num_variables; ++
i) {
443 const MPVariableProto&
var = mp_model.variable(
i);
444 if ((std::isfinite(
var.lower_bound()) &&
445 std::abs(
var.lower_bound()) > threshold) ||
446 (std::isfinite(
var.upper_bound()) &&
447 std::abs(
var.upper_bound()) > threshold)) {
448 SOLVER_LOG(logger,
"Variable bounds are too large [",
var.lower_bound(),
449 ",",
var.upper_bound(),
"]");
452 if (std::abs(
var.objective_coefficient()) > threshold) {
453 SOLVER_LOG(logger,
"Objective coefficient is too large: ",
454 var.objective_coefficient());
460 const int num_constraints = mp_model.constraint_size();
461 for (
int c = 0; c < num_constraints; ++c) {
462 const MPConstraintProto&
ct = mp_model.constraint(c);
463 if ((std::isfinite(
ct.lower_bound()) &&
464 std::abs(
ct.lower_bound()) > threshold) ||
465 (std::isfinite(
ct.upper_bound()) &&
466 std::abs(
ct.upper_bound()) > threshold)) {
467 SOLVER_LOG(logger,
"Constraint bounds are too large [",
ct.lower_bound(),
468 ",",
ct.upper_bound(),
"]");
471 for (
const double coeff :
ct.coefficient()) {
472 if (std::abs(coeff) > threshold) {
473 SOLVER_LOG(logger,
"Constraint coefficient is too large: ", coeff);
484 const int num_variables = mp_model->variable_size();
485 std::vector<double> var_scaling(num_variables, 1.0);
487 int initial_num_integers = 0;
488 for (
int i = 0;
i < num_variables; ++
i) {
489 if (mp_model->variable(
i).is_integer()) ++initial_num_integers;
491 VLOG(1) <<
"Initial num integers: " << initial_num_integers;
494 const double tolerance = 1e-6;
495 std::vector<int> constraint_queue;
497 const int num_constraints = mp_model->constraint_size();
498 std::vector<int> constraint_to_num_non_integer(num_constraints, 0);
499 std::vector<std::vector<int>> var_to_constraints(num_variables);
500 for (
int i = 0;
i < num_constraints; ++
i) {
501 const MPConstraintProto& mp_constraint = mp_model->constraint(
i);
503 for (
const int var : mp_constraint.var_index()) {
504 if (!mp_model->variable(
var).is_integer()) {
505 var_to_constraints[
var].push_back(
i);
506 constraint_to_num_non_integer[
i]++;
509 if (constraint_to_num_non_integer[
i] == 1) {
510 constraint_queue.push_back(
i);
513 VLOG(1) <<
"Initial constraint queue: " << constraint_queue.size() <<
" / "
516 int num_detected = 0;
517 double max_scaling = 0.0;
518 auto scale_and_mark_as_integer = [&](
int var,
double scaling)
mutable {
520 CHECK(!mp_model->variable(
var).is_integer());
521 CHECK_EQ(var_scaling[
var], 1.0);
522 if (scaling != 1.0) {
523 VLOG(2) <<
"Scaled " <<
var <<
" by " << scaling;
527 max_scaling = std::max(max_scaling, scaling);
531 var_scaling[
var] = scaling;
532 mp_model->mutable_variable(
var)->set_is_integer(
true);
535 for (
const int ct_index : var_to_constraints[
var]) {
536 constraint_to_num_non_integer[
ct_index]--;
537 if (constraint_to_num_non_integer[
ct_index] == 1) {
538 constraint_queue.push_back(
ct_index);
543 int num_fail_due_to_rhs = 0;
544 int num_fail_due_to_large_multiplier = 0;
545 int num_processed_constraints = 0;
546 while (!constraint_queue.empty()) {
547 const int top_ct_index = constraint_queue.back();
548 constraint_queue.pop_back();
552 if (constraint_to_num_non_integer[top_ct_index] == 0)
continue;
555 const MPConstraintProto&
ct = mp_model->constraint(top_ct_index);
556 if (
ct.lower_bound() + tolerance <
ct.upper_bound())
continue;
558 ++num_processed_constraints;
570 double multiplier = 1.0;
571 const double max_multiplier = 1e4;
573 for (
int i = 0;
i <
ct.var_index().
size(); ++
i) {
574 if (!mp_model->variable(
ct.var_index(
i)).is_integer()) {
577 var_coeff =
ct.coefficient(
i);
582 multiplier *
ct.coefficient(
i) / var_scaling[
ct.var_index(
i)];
585 if (multiplier == 0 || multiplier > max_multiplier) {
591 if (multiplier == 0 || multiplier > max_multiplier) {
592 ++num_fail_due_to_large_multiplier;
597 const double rhs =
ct.lower_bound();
598 if (std::abs(std::round(rhs * multiplier) - rhs * multiplier) >
599 tolerance * multiplier) {
600 ++num_fail_due_to_rhs;
610 double best_scaling = std::abs(var_coeff * multiplier);
611 for (
const int ct_index : var_to_constraints[
var]) {
612 if (
ct_index == top_ct_index)
continue;
613 if (constraint_to_num_non_integer[
ct_index] != 1)
continue;
616 const MPConstraintProto&
ct = mp_model->constraint(top_ct_index);
617 if (
ct.lower_bound() + tolerance <
ct.upper_bound())
continue;
619 const double multiplier = GetIntegralityMultiplier(
621 if (multiplier != 0.0 && multiplier < best_scaling) {
622 best_scaling = multiplier;
626 scale_and_mark_as_integer(
var, best_scaling);
634 int num_in_inequalities = 0;
635 int num_to_be_handled = 0;
636 for (
int var = 0;
var < num_variables; ++
var) {
637 if (mp_model->variable(
var).is_integer())
continue;
640 if (var_to_constraints[
var].empty())
continue;
643 for (
const int ct_index : var_to_constraints[
var]) {
644 if (constraint_to_num_non_integer[
ct_index] != 1) {
651 std::vector<double> scaled_coeffs;
652 for (
const int ct_index : var_to_constraints[
var]) {
653 const double multiplier = GetIntegralityMultiplier(
655 if (multiplier == 0.0) {
659 scaled_coeffs.push_back(multiplier);
668 double scaling = scaled_coeffs[0];
669 for (
const double c : scaled_coeffs) {
670 scaling = std::min(scaling, c);
672 CHECK_GT(scaling, 0.0);
673 for (
const double c : scaled_coeffs) {
674 const double fraction = c / scaling;
675 if (std::abs(std::round(fraction) - fraction) > tolerance) {
687 for (
const double bound : {mp_model->variable(
var).lower_bound(),
688 mp_model->variable(
var).upper_bound()}) {
689 if (!std::isfinite(
bound))
continue;
690 if (std::abs(std::round(
bound * scaling) -
bound * scaling) >
691 tolerance * scaling) {
703 ++num_in_inequalities;
704 scale_and_mark_as_integer(
var, scaling);
706 VLOG(1) <<
"num_new_integer: " << num_detected
707 <<
" num_processed_constraints: " << num_processed_constraints
708 <<
" num_rhs_fail: " << num_fail_due_to_rhs
709 <<
" num_multiplier_fail: " << num_fail_due_to_large_multiplier;
711 if (num_to_be_handled > 0) {
712 SOLVER_LOG(logger,
"Missed ", num_to_be_handled,
713 " potential implied integer.");
716 const int num_integers = initial_num_integers + num_detected;
717 SOLVER_LOG(logger,
"Num integers: ", num_integers,
"/", num_variables,
718 " (implied: ", num_detected,
719 " in_inequalities: ", num_in_inequalities,
720 " max_scaling: ", max_scaling,
")",
721 (num_integers == num_variables ?
" [IP] " :
" [MIP] "));
723 ApplyVarScaling(var_scaling, mp_model);
730struct ConstraintScaler {
732 ConstraintProto* AddConstraint(
const MPModelProto& mp_model,
733 const MPConstraintProto& mp_constraint,
734 CpModelProto* cp_model);
750ConstraintProto* ConstraintScaler::AddConstraint(
751 const MPModelProto& mp_model,
const MPConstraintProto& mp_constraint,
752 CpModelProto* cp_model) {
753 if (mp_constraint.lower_bound() == -kInfinity &&
754 mp_constraint.upper_bound() == kInfinity) {
758 auto* constraint = cp_model->add_constraints();
759 if (
keep_names) constraint->set_name(mp_constraint.name());
760 auto* arg = constraint->mutable_linear();
768 const int num_coeffs = mp_constraint.coefficient_size();
769 for (
int i = 0;
i < num_coeffs; ++
i) {
770 const auto& var_proto = cp_model->variables(mp_constraint.var_index(i));
771 const int64_t lb = var_proto.domain(0);
772 const int64_t ub = var_proto.domain(var_proto.domain_size() - 1);
773 if (lb == 0 && ub == 0)
continue;
775 const double coeff = mp_constraint.coefficient(i);
776 if (coeff == 0.0)
continue;
784 double relative_coeff_error;
785 double scaled_sum_error;
789 if (scaling_factor == 0.0) {
793 LOG(DFATAL) <<
"Scaling factor of zero while scaling constraint: "
805 const double scaled_value =
coefficients[
i] * scaling_factor;
806 const int64_t
value =
static_cast<int64_t
>(std::round(scaled_value)) / gcd;
809 arg->add_coeffs(
value);
829 const Fractional scaled_lb = std::ceil(lb * scaling_factor);
830 if (lb == kInfinity || scaled_lb >= std::numeric_limits<int64_t>::max()) {
832 arg->add_domain(std::numeric_limits<int64_t>::max());
833 }
else if (lb == -kInfinity ||
834 scaled_lb <= std::numeric_limits<int64_t>::min()) {
835 arg->add_domain(std::numeric_limits<int64_t>::min());
837 arg->add_domain(
CeilRatio(IntegerValue(
static_cast<int64_t
>(scaled_lb)),
842 const Fractional scaled_ub = std::floor(ub * scaling_factor);
843 if (ub == -kInfinity || scaled_ub <= std::numeric_limits<int64_t>::min()) {
845 arg->add_domain(std::numeric_limits<int64_t>::min());
846 }
else if (ub == kInfinity ||
847 scaled_ub >= std::numeric_limits<int64_t>::max()) {
848 arg->add_domain(std::numeric_limits<int64_t>::max());
850 arg->add_domain(
FloorRatio(IntegerValue(
static_cast<int64_t
>(scaled_ub)),
859double FindFractionalScaling(absl::Span<const double>
coefficients,
861 double multiplier = 1.0;
864 multiplier * tolerance);
865 if (multiplier == 0.0)
break;
875 absl::Span<const double>
upper_bounds, int64_t max_absolute_activity,
876 double wanted_absolute_activity_precision,
double* relative_coeff_error,
877 double* scaled_sum_error) {
881 if (scaling_factor == 0.0)
return scaling_factor;
891 double x = std::min(scaling_factor, 1.0);
892 for (;
x <= scaling_factor;
x *= 2) {
894 relative_coeff_error, scaled_sum_error);
895 if (*scaled_sum_error < wanted_absolute_activity_precision *
x)
break;
898 if (
x == scaling_factor)
break;
901 DCHECK(std::isfinite(scaling_factor));
911 const double integer_factor = FindFractionalScaling(
coefficients, 1e-8);
912 DCHECK(std::isfinite(integer_factor));
913 if (integer_factor != 0 && integer_factor < scaling_factor) {
914 double local_relative_coeff_error;
915 double local_scaled_sum_error;
917 integer_factor, &local_relative_coeff_error,
918 &local_scaled_sum_error);
919 if (local_scaled_sum_error * scaling_factor <=
920 *scaled_sum_error * integer_factor ||
921 local_scaled_sum_error <
922 wanted_absolute_activity_precision * integer_factor) {
923 *relative_coeff_error = local_relative_coeff_error;
924 *scaled_sum_error = local_scaled_sum_error;
925 scaling_factor = integer_factor;
929 DCHECK(std::isfinite(scaling_factor));
930 return scaling_factor;
934 const MPModelProto& mp_model,
935 CpModelProto* cp_model,
937 CHECK(cp_model !=
nullptr);
939 cp_model->set_name(mp_model.name());
953 const int64_t kMaxVariableBound =
954 static_cast<int64_t
>(params.mip_max_bound());
956 int num_truncated_bounds = 0;
957 int num_small_domains = 0;
958 const int64_t kSmallDomainSize = 1000;
959 const double kWantedPrecision = params.mip_wanted_precision();
962 const int num_variables = mp_model.variable_size();
963 const bool keep_names = !params.ignore_names();
964 for (
int i = 0;
i < num_variables; ++
i) {
965 const MPVariableProto& mp_var = mp_model.variable(
i);
966 IntegerVariableProto* cp_var = cp_model->add_variables();
967 if (
keep_names) cp_var->set_name(mp_var.name());
975 if (mp_var.lower_bound() >
static_cast<double>(kMaxVariableBound) ||
976 mp_var.upper_bound() <
static_cast<double>(-kMaxVariableBound)) {
977 SOLVER_LOG(logger,
"Error: variable ", mp_var,
978 " is outside [-mip_max_bound..mip_max_bound]");
983 for (
const bool lower : {
true,
false}) {
984 const double bound =
lower ? mp_var.lower_bound() : mp_var.upper_bound();
985 if (std::abs(
bound) + kWantedPrecision >=
986 static_cast<double>(kMaxVariableBound)) {
987 ++num_truncated_bounds;
988 cp_var->add_domain(
bound < 0 ? -kMaxVariableBound : kMaxVariableBound);
994 static_cast<int64_t
>(
lower ? std::ceil(
bound - kWantedPrecision)
995 : std::floor(
bound + kWantedPrecision)));
998 if (cp_var->domain(0) > cp_var->domain(1)) {
999 LOG(WARNING) <<
"Variable #" <<
i <<
" cannot take integer value. "
1006 if (!mp_var.is_integer()) {
1007 const double diff = mp_var.upper_bound() - mp_var.lower_bound();
1008 if (diff > kWantedPrecision && diff < kSmallDomainSize) {
1009 ++num_small_domains;
1014 if (num_truncated_bounds > 0) {
1015 SOLVER_LOG(logger,
"Warning: ", num_truncated_bounds,
1016 " bounds were truncated to ", kMaxVariableBound,
".");
1018 if (num_small_domains > 0) {
1019 SOLVER_LOG(logger,
"Warning: ", num_small_domains,
1020 " continuous variable domain with fewer than ", kSmallDomainSize,
1024 ConstraintScaler scaler;
1025 const int64_t kScalingTarget = int64_t{1}
1026 << params.mip_max_activity_exponent();
1027 scaler.wanted_precision = kWantedPrecision;
1028 scaler.scaling_target = kScalingTarget;
1032 for (
const MPConstraintProto& mp_constraint : mp_model.constraint()) {
1033 scaler.AddConstraint(mp_model, mp_constraint, cp_model);
1035 for (
const MPGeneralConstraintProto& general_constraint :
1036 mp_model.general_constraint()) {
1037 switch (general_constraint.general_constraint_case()) {
1038 case MPGeneralConstraintProto::kIndicatorConstraint: {
1039 const auto& indicator_constraint =
1040 general_constraint.indicator_constraint();
1041 const MPConstraintProto& mp_constraint =
1042 indicator_constraint.constraint();
1043 ConstraintProto*
ct =
1044 scaler.AddConstraint(mp_model, mp_constraint, cp_model);
1045 if (
ct ==
nullptr)
continue;
1048 const int var = indicator_constraint.var_index();
1049 const int value = indicator_constraint.var_value();
1053 case MPGeneralConstraintProto::kAndConstraint: {
1054 const auto& and_constraint = general_constraint.and_constraint();
1055 const std::string&
name = general_constraint.name();
1057 ConstraintProto* ct_pos = cp_model->add_constraints();
1058 ct_pos->set_name(
name.empty() ?
"" : absl::StrCat(
name,
"_pos"));
1059 ct_pos->add_enforcement_literal(and_constraint.resultant_var_index());
1060 *ct_pos->mutable_bool_and()->mutable_literals() =
1061 and_constraint.var_index();
1063 ConstraintProto* ct_neg = cp_model->add_constraints();
1064 ct_neg->set_name(
name.empty() ?
"" : absl::StrCat(
name,
"_neg"));
1065 ct_neg->add_enforcement_literal(
1066 NegatedRef(and_constraint.resultant_var_index()));
1067 for (
const int var_index : and_constraint.var_index()) {
1072 case MPGeneralConstraintProto::kOrConstraint: {
1073 const auto& or_constraint = general_constraint.or_constraint();
1074 const std::string&
name = general_constraint.name();
1076 ConstraintProto* ct_pos = cp_model->add_constraints();
1077 ct_pos->set_name(
name.empty() ?
"" : absl::StrCat(
name,
"_pos"));
1078 ct_pos->add_enforcement_literal(or_constraint.resultant_var_index());
1079 *ct_pos->mutable_bool_or()->mutable_literals() =
1080 or_constraint.var_index();
1082 ConstraintProto* ct_neg = cp_model->add_constraints();
1083 ct_neg->set_name(
name.empty() ?
"" : absl::StrCat(
name,
"_neg"));
1084 ct_neg->add_enforcement_literal(
1085 NegatedRef(or_constraint.resultant_var_index()));
1086 for (
const int var_index : or_constraint.var_index()) {
1092 LOG(ERROR) <<
"Can't convert general constraints of type "
1093 << general_constraint.general_constraint_case()
1094 <<
" to CpModelProto.";
1100 SOLVER_LOG(logger,
"Maximum constraint coefficient relative error: ",
1101 scaler.max_relative_coeff_error);
1102 SOLVER_LOG(logger,
"Maximum constraint worst-case activity error: ",
1103 scaler.max_absolute_rhs_error,
1104 (scaler.max_absolute_rhs_error > params.mip_check_precision()
1105 ?
" [Potentially IMPRECISE]"
1107 SOLVER_LOG(logger,
"Constraint scaling factor range: [",
1108 scaler.min_scaling_factor,
", ", scaler.max_scaling_factor,
"]");
1113 auto* float_objective = cp_model->mutable_floating_point_objective();
1114 float_objective->set_maximize(mp_model.maximize());
1115 float_objective->set_offset(mp_model.objective_offset());
1116 for (
int i = 0;
i < num_variables; ++
i) {
1117 const MPVariableProto& mp_var = mp_model.variable(
i);
1118 if (mp_var.objective_coefficient() != 0.0) {
1119 float_objective->add_vars(
i);
1120 float_objective->add_coeffs(mp_var.objective_coefficient());
1125 if (float_objective->offset() == 0 && float_objective->vars().empty()) {
1126 cp_model->clear_floating_point_objective();
1133int AppendSumOfLiteral(absl::Span<const int> literals, MPConstraintProto* out) {
1135 for (
const int ref : literals) {
1137 out->add_coefficient(1);
1138 out->add_var_index(ref);
1140 out->add_coefficient(-1);
1151 MPModelProto* output) {
1152 CHECK(output !=
nullptr);
1156 const int num_vars =
input.variables().size();
1157 for (
int v = 0; v < num_vars; ++v) {
1158 if (
input.variables(v).domain().size() != 2) {
1159 VLOG(1) <<
"Cannot convert "
1164 MPVariableProto*
var = output->add_variable();
1165 var->set_is_integer(
true);
1166 var->set_lower_bound(
input.variables(v).domain(0));
1167 var->set_upper_bound(
input.variables(v).domain(1));
1171 if (
input.has_objective()) {
1172 double factor =
input.objective().scaling_factor();
1173 if (factor == 0.0) factor = 1.0;
1174 const int num_terms =
input.objective().vars().size();
1175 for (
int i = 0;
i < num_terms; ++
i) {
1176 const int var =
input.objective().vars(
i);
1177 if (
var < 0)
return false;
1178 CHECK_EQ(output->variable(
var).objective_coefficient(), 0.0);
1179 output->mutable_variable(
var)->set_objective_coefficient(
1180 factor *
input.objective().coeffs(
i));
1182 output->set_objective_offset(factor *
input.objective().offset());
1184 output->set_maximize(
true);
1186 }
else if (
input.has_floating_point_objective()) {
1187 const int num_terms =
input.floating_point_objective().vars().size();
1188 for (
int i = 0;
i < num_terms; ++
i) {
1189 const int var =
input.floating_point_objective().vars(
i);
1190 if (
var < 0)
return false;
1191 CHECK_EQ(output->variable(
var).objective_coefficient(), 0.0);
1192 output->mutable_variable(
var)->set_objective_coefficient(
1193 input.floating_point_objective().coeffs(
i));
1195 output->set_objective_offset(
input.floating_point_objective().offset());
1197 if (output->objective_offset() == 0.0) {
1198 output->clear_objective_offset();
1202 const int num_constraints =
input.constraints().size();
1203 std::vector<int> tmp_literals;
1204 for (
int c = 0; c < num_constraints; ++c) {
1205 const ConstraintProto&
ct =
input.constraints(c);
1206 if (!
ct.enforcement_literal().empty() &&
1207 (
ct.constraint_case() != ConstraintProto::kBoolAnd &&
1208 ct.constraint_case() != ConstraintProto::kLinear)) {
1213 switch (
ct.constraint_case()) {
1214 case ConstraintProto::kExactlyOne: {
1215 MPConstraintProto* out = output->add_constraint();
1216 const int shift = AppendSumOfLiteral(
ct.exactly_one().literals(), out);
1217 out->set_lower_bound(1 - shift);
1218 out->set_upper_bound(1 - shift);
1221 case ConstraintProto::kAtMostOne: {
1222 MPConstraintProto* out = output->add_constraint();
1223 const int shift = AppendSumOfLiteral(
ct.at_most_one().literals(), out);
1224 out->set_lower_bound(-kInfinity);
1225 out->set_upper_bound(1 - shift);
1228 case ConstraintProto::kBoolOr: {
1229 MPConstraintProto* out = output->add_constraint();
1230 const int shift = AppendSumOfLiteral(
ct.bool_or().literals(), out);
1231 out->set_lower_bound(1 - shift);
1232 out->set_upper_bound(kInfinity);
1235 case ConstraintProto::kBoolAnd: {
1236 tmp_literals.clear();
1237 for (
const int ref :
ct.enforcement_literal()) {
1240 for (
const int ref :
ct.bool_and().literals()) {
1241 MPConstraintProto* out = output->add_constraint();
1242 tmp_literals.push_back(ref);
1243 const int shift = AppendSumOfLiteral(tmp_literals, out);
1244 out->set_lower_bound(1 - shift);
1245 out->set_upper_bound(kInfinity);
1246 tmp_literals.pop_back();
1250 case ConstraintProto::kLinear: {
1251 if (
ct.linear().domain().size() != 2) {
1252 VLOG(1) <<
"Cannot convert constraint: "
1258 int64_t min_activity = 0;
1259 int64_t max_activity = 0;
1260 const int num_terms =
ct.linear().vars().size();
1261 for (
int i = 0;
i < num_terms; ++
i) {
1262 const int var =
ct.linear().vars(
i);
1263 if (
var < 0)
return false;
1264 DCHECK_EQ(
input.variables(
var).domain().size(), 2);
1265 const int64_t coeff =
ct.linear().coeffs(
i);
1267 min_activity += coeff *
input.variables(
var).domain(0);
1268 max_activity += coeff *
input.variables(
var).domain(1);
1270 min_activity += coeff *
input.variables(
var).domain(1);
1271 max_activity += coeff *
input.variables(
var).domain(0);
1275 if (
ct.enforcement_literal().empty()) {
1276 MPConstraintProto* out_ct = output->add_constraint();
1277 if (min_activity <
ct.linear().domain(0)) {
1278 out_ct->set_lower_bound(
ct.linear().domain(0));
1280 out_ct->set_lower_bound(-kInfinity);
1282 if (max_activity >
ct.linear().domain(1)) {
1283 out_ct->set_upper_bound(
ct.linear().domain(1));
1285 out_ct->set_upper_bound(kInfinity);
1287 for (
int i = 0;
i < num_terms; ++
i) {
1288 const int var =
ct.linear().vars(
i);
1289 if (
var < 0)
return false;
1290 out_ct->add_var_index(
var);
1291 out_ct->add_coefficient(
ct.linear().coeffs(
i));
1296 std::vector<MPConstraintProto*> out_cts;
1297 if (
ct.linear().domain(1) < max_activity) {
1298 MPConstraintProto* high_out_ct = output->add_constraint();
1299 high_out_ct->set_lower_bound(-kInfinity);
1300 int64_t ub =
ct.linear().domain(1);
1301 const int64_t coeff = max_activity -
ct.linear().domain(1);
1302 for (
const int lit :
ct.enforcement_literal()) {
1305 high_out_ct->add_var_index(
lit);
1306 high_out_ct->add_coefficient(coeff);
1310 high_out_ct->add_coefficient(-coeff);
1313 high_out_ct->set_upper_bound(ub);
1314 out_cts.push_back(high_out_ct);
1316 if (
ct.linear().domain(0) > min_activity) {
1317 MPConstraintProto* low_out_ct = output->add_constraint();
1318 low_out_ct->set_upper_bound(kInfinity);
1319 int64_t lb =
ct.linear().domain(0);
1320 int64_t coeff = min_activity -
ct.linear().domain(0);
1321 for (
const int lit :
ct.enforcement_literal()) {
1324 low_out_ct->add_var_index(
lit);
1325 low_out_ct->add_coefficient(coeff);
1329 low_out_ct->add_coefficient(-coeff);
1332 low_out_ct->set_lower_bound(lb);
1333 out_cts.push_back(low_out_ct);
1335 for (MPConstraintProto* out_ct : out_cts) {
1336 for (
int i = 0;
i < num_terms; ++
i) {
1337 const int var =
ct.linear().vars(
i);
1338 if (
var < 0)
return false;
1339 out_ct->add_var_index(
var);
1340 out_ct->add_coefficient(
ct.linear().coeffs(
i));
1355 const std::vector<std::pair<int, double>>& objective,
1356 double objective_offset,
bool maximize,
1359 cp_model->clear_objective();
1366 double min_magnitude = std::numeric_limits<double>::infinity();
1367 double max_magnitude = 0.0;
1368 double l1_norm = 0.0;
1369 for (
const auto& [
var, coeff] : objective) {
1370 const auto& var_proto = cp_model->variables(
var);
1371 const int64_t lb = var_proto.domain(0);
1372 const int64_t ub = var_proto.domain(var_proto.domain_size() - 1);
1374 if (lb != 0) objective_offset += lb * coeff;
1382 min_magnitude = std::min(min_magnitude, std::abs(coeff));
1383 max_magnitude = std::max(max_magnitude, std::abs(coeff));
1384 l1_norm += std::abs(coeff);
1387 if (
coefficients.empty() && objective_offset == 0.0)
return true;
1390 const double average_magnitude =
1392 SOLVER_LOG(logger,
"[Scaling] Floating point objective has ",
1393 coefficients.size(),
" terms with magnitude in [", min_magnitude,
1394 ", ", max_magnitude,
"] average = ", average_magnitude);
1398 const int64_t max_absolute_activity = int64_t{1}
1399 << params.mip_max_activity_exponent();
1401 std::max(params.mip_wanted_precision(), params.absolute_gap_limit());
1403 double relative_coeff_error;
1404 double scaled_sum_error;
1408 if (scaling_factor == 0.0) {
1409 LOG(ERROR) <<
"Scaling factor of zero while scaling objective! This "
1410 "likely indicate an infinite coefficient in the objective.";
1417 SOLVER_LOG(logger,
"[Scaling] Objective coefficient relative error: ",
1418 relative_coeff_error);
1419 SOLVER_LOG(logger,
"[Scaling] Objective worst-case absolute error: ",
1420 scaled_sum_error / scaling_factor);
1422 "[Scaling] Objective scaling factor: ", scaling_factor / gcd);
1426 "[Scaling] Warning: the worst-case absolute error is greater "
1427 "than the wanted precision (",
1429 "). Try to increase mip_max_activity_exponent (default = ",
1430 params.mip_max_activity_exponent(),
1431 ") or reduced your variables range and/or objective "
1432 "coefficient. We will continue the solve, but the final "
1433 "objective value might be off.");
1439 auto* objective_proto = cp_model->mutable_objective();
1440 const int64_t mult = maximize ? -1 : 1;
1441 objective_proto->set_offset(objective_offset * scaling_factor / gcd * mult);
1442 objective_proto->set_scaling_factor(1.0 / scaling_factor * gcd * mult);
1444 const int64_t
value =
1445 static_cast<int64_t
>(std::round(
coefficients[
i] * scaling_factor)) /
1449 objective_proto->add_coeffs(
value * mult);
1453 if (scaled_sum_error == 0.0) {
1454 objective_proto->set_scaling_was_exact(
true);
1461 LinearBooleanProblem* problem) {
1462 CHECK(problem !=
nullptr);
1464 problem->set_name(mp_model.name());
1465 const int num_variables = mp_model.variable_size();
1466 problem->set_num_variables(num_variables);
1470 for (
int var_id(0); var_id < num_variables; ++var_id) {
1471 const MPVariableProto& mp_var = mp_model.variable(var_id);
1472 problem->add_var_names(mp_var.name());
1477 bool is_binary = mp_var.is_integer();
1481 if (lb <= -1.0) is_binary =
false;
1482 if (ub >= 2.0) is_binary =
false;
1485 if (lb <= 0.0 && ub >= 1.0) {
1487 }
else if (lb <= 1.0 && ub >= 1.0) {
1489 LinearBooleanConstraint* constraint = problem->add_constraints();
1490 constraint->set_lower_bound(1);
1491 constraint->set_upper_bound(1);
1492 constraint->add_literals(var_id + 1);
1493 constraint->add_coefficients(1);
1494 }
else if (lb <= 0.0 && ub >= 0.0) {
1496 LinearBooleanConstraint* constraint = problem->add_constraints();
1497 constraint->set_lower_bound(0);
1498 constraint->set_upper_bound(0);
1499 constraint->add_literals(var_id + 1);
1500 constraint->add_coefficients(1);
1509 LOG(WARNING) <<
"The variable #" << var_id <<
" with name "
1510 << mp_var.name() <<
" is not binary. " <<
"lb: " << lb
1517 const int64_t kInt64Max = std::numeric_limits<int64_t>::max();
1518 double max_relative_error = 0.0;
1519 double max_bound_error = 0.0;
1521 double relative_error = 0.0;
1522 double scaling_factor = 0.0;
1526 for (
const MPConstraintProto& mp_constraint : mp_model.constraint()) {
1527 LinearBooleanConstraint* constraint = problem->add_constraints();
1528 constraint->set_name(mp_constraint.name());
1532 const int num_coeffs = mp_constraint.coefficient_size();
1533 for (
int i = 0;
i < num_coeffs; ++
i) {
1540 max_relative_error = std::max(relative_error, max_relative_error);
1543 double bound_error = 0.0;
1544 for (
int i = 0;
i < num_coeffs; ++
i) {
1545 const double scaled_value = mp_constraint.coefficient(
i) * scaling_factor;
1546 bound_error += std::abs(round(scaled_value) - scaled_value);
1547 const int64_t
value =
static_cast<int64_t
>(round(scaled_value)) / gcd;
1549 constraint->add_literals(mp_constraint.var_index(
i) + 1);
1550 constraint->add_coefficients(
value);
1553 max_bound_error = std::max(max_bound_error, bound_error);
1560 const Fractional lb = mp_constraint.lower_bound();
1561 if (lb != -kInfinity) {
1562 if (lb * scaling_factor >
static_cast<double>(kInt64Max)) {
1563 LOG(WARNING) <<
"A constraint is trivially unsatisfiable.";
1566 if (lb * scaling_factor > -
static_cast<double>(kInt64Max)) {
1568 constraint->set_lower_bound(
1569 static_cast<int64_t
>(round(lb * scaling_factor - bound_error)) /
1573 const Fractional ub = mp_constraint.upper_bound();
1574 if (ub != kInfinity) {
1575 if (ub * scaling_factor < -
static_cast<double>(kInt64Max)) {
1576 LOG(WARNING) <<
"A constraint is trivially unsatisfiable.";
1579 if (ub * scaling_factor <
static_cast<double>(kInt64Max)) {
1581 constraint->set_upper_bound(
1582 static_cast<int64_t
>(round(ub * scaling_factor + bound_error)) /
1589 LOG(INFO) <<
"Maximum constraint relative error: " << max_relative_error;
1590 LOG(INFO) <<
"Maximum constraint bound error: " << max_bound_error;
1595 for (
int var_id = 0; var_id < num_variables; ++var_id) {
1596 const MPVariableProto& mp_var = mp_model.variable(var_id);
1597 coefficients.push_back(mp_var.objective_coefficient());
1602 max_relative_error = std::max(relative_error, max_relative_error);
1605 LOG(INFO) <<
"objective relative error: " << relative_error;
1606 LOG(INFO) <<
"objective scaling factor: " << scaling_factor / gcd;
1608 LinearObjective* objective = problem->mutable_objective();
1609 objective->set_offset(mp_model.objective_offset() * scaling_factor / gcd);
1613 objective->set_scaling_factor(1.0 / scaling_factor * gcd);
1614 for (
int var_id = 0; var_id < num_variables; ++var_id) {
1615 const MPVariableProto& mp_var = mp_model.variable(var_id);
1616 const int64_t
value =
1617 static_cast<int64_t
>(
1618 round(mp_var.objective_coefficient() * scaling_factor)) /
1621 objective->add_literals(var_id + 1);
1622 objective->add_coefficients(
value);
1630 const double kRelativeTolerance = 1e-8;
1631 if (max_relative_error > kRelativeTolerance) {
1632 LOG(WARNING) <<
"The relative error during double -> int64_t conversion "
1642 for (
int i = 0;
i < problem.num_variables(); ++
i) {
1649 if (problem.var_names_size() != 0) {
1650 CHECK_EQ(problem.var_names_size(), problem.num_variables());
1651 for (
int i = 0;
i < problem.num_variables(); ++
i) {
1656 for (
const LinearBooleanConstraint& constraint : problem.constraints()) {
1660 for (
int i = 0;
i < constraint.literals_size(); ++
i) {
1661 const int literal = constraint.literals(
i);
1662 const double coeff = constraint.coefficients(
i);
1663 const ColIndex variable_index = ColIndex(abs(
literal) - 1);
1673 constraint.has_lower_bound() ? constraint.lower_bound() - sum
1675 constraint.has_upper_bound() ? constraint.upper_bound() - sum
1682 const LinearObjective& objective = problem.objective();
1683 const double scaling_factor = objective.scaling_factor();
1684 for (
int i = 0;
i < objective.literals_size(); ++
i) {
1685 const int literal = objective.literals(
i);
1686 const double coeff =
1687 static_cast<double>(objective.coefficients(
i)) * scaling_factor;
1688 const ColIndex variable_index = ColIndex(abs(
literal) - 1);
1704 const CpModelProto& model_proto_with_floating_point_objective,
1705 const CpObjectiveProto& integer_objective,
1706 const int64_t inner_integer_objective_lower_bound) {
1709 const CpModelProto&
proto = model_proto_with_floating_point_objective;
1711 const auto& domain =
proto.variables(
i).domain();
1713 static_cast<double>(domain[domain.size() - 1]));
1718 const FloatObjectiveProto& float_obj =
proto.floating_point_objective();
1721 for (
int i = 0;
i < float_obj.vars().
size(); ++
i) {
1722 const glop::ColIndex
col(float_obj.vars(
i));
1730 ct,
static_cast<double>(inner_integer_objective_lower_bound),
1731 std::numeric_limits<double>::infinity());
1732 for (
int i = 0;
i < integer_objective.vars().
size(); ++
i) {
1734 static_cast<double>(integer_objective.coeffs(
i)));
1742 glop::GlopParameters glop_parameters;
1743 glop_parameters.set_max_number_of_iterations(100 *
proto.variables().size());
1744 glop_parameters.set_change_status_to_imprecise(
false);
1752 return float_obj.maximize() ? std::numeric_limits<double>::infinity()
1753 : -std::numeric_limits<double>::infinity();
A full-fledged linear programming solver.
Fractional GetObjectiveValue() const
Returns the objective value of the solution with its offset and scaling.
void SetParameters(const GlopParameters ¶meters)
ABSL_MUST_USE_RESULT ProblemStatus Solve(const LinearProgram &lp)
const DenseRow & objective_coefficients() const
Returns the objective coefficients (or cost) of variables as a row vector.
void Clear()
Clears, i.e. reset the object to its initial value.
void SetConstraintName(RowIndex row, absl::string_view name)
@ 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.
void SetVariableName(ColIndex col, absl::string_view name)
RowIndex CreateNewConstraint()
ColIndex CreateNewVariable()
void SetMaximizationProblem(bool maximize)
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
absl::Span< const double > coefficients
constexpr double kInfinity
Infinity for type Fractional.
ProblemStatus
Different statuses for a given problem.
IntegerValue FloorRatio(IntegerValue dividend, IntegerValue positive_divisor)
double FindBestScalingAndComputeErrors(const std::vector< double > &coefficients, absl::Span< const double > lower_bounds, absl::Span< const double > upper_bounds, int64_t max_absolute_activity, double wanted_absolute_activity_precision, double *relative_coeff_error, double *scaled_sum_error)
bool RefIsPositive(int ref)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
bool ConvertMPModelProtoToCpModelProto(const SatParameters ¶ms, const MPModelProto &mp_model, CpModelProto *cp_model, SolverLogger *logger)
int64_t FindRationalFactor(double x, int64_t limit, double tolerance)
void ConvertBooleanProblemToLinearProgram(const LinearBooleanProblem &problem, glop::LinearProgram *lp)
Converts a Boolean optimization problem to its lp formulation.
bool ScaleAndSetObjective(const SatParameters ¶ms, const std::vector< std::pair< int, double > > &objective, double objective_offset, bool maximize, CpModelProto *cp_model, SolverLogger *logger)
bool MakeBoundsOfIntegerVariablesInteger(const SatParameters ¶ms, MPModelProto *mp_model, SolverLogger *logger)
void ChangeLargeBoundsToInfinity(double max_magnitude, MPModelProto *mp_model, SolverLogger *logger)
void ChangeOptimizationDirection(LinearBooleanProblem *problem)
std::vector< double > DetectImpliedIntegers(MPModelProto *mp_model, SolverLogger *logger)
void RemoveNearZeroTerms(const SatParameters ¶ms, MPModelProto *mp_model, SolverLogger *logger)
bool ConvertBinaryMPModelProtoToBooleanProblem(const MPModelProto &mp_model, LinearBooleanProblem *problem)
double ComputeTrueObjectiveLowerBound(const CpModelProto &model_proto_with_floating_point_objective, const CpObjectiveProto &integer_objective, const int64_t inner_integer_objective_lower_bound)
bool ConvertCpModelProtoToMPModelProto(const CpModelProto &input, MPModelProto *output)
std::vector< double > ScaleContinuousVariables(double scaling, double max_bound, MPModelProto *mp_model)
int NegatedRef(int ref)
Small utility functions to deal with negative variable/literal references.
bool MPModelProtoValidationBeforeConversion(const SatParameters ¶ms, const MPModelProto &mp_model, SolverLogger *logger)
In SWIG mode, we don't want anything besides these top-level includes.
int64_t CapAdd(int64_t x, int64_t y)
double GetBestScalingOfDoublesToInt64(absl::Span< const double > input, absl::Span< const double > lb, absl::Span< const double > ub, int64_t max_absolute_sum)
std::string ProtobufShortDebugString(const P &message)
int64_t CapProd(int64_t x, int64_t y)
void ComputeScalingErrors(absl::Span< const double > input, absl::Span< const double > lb, absl::Span< const double > ub, double scaling_factor, double *max_relative_coeff_error, double *max_scaled_sum_error)
std::string ProtobufDebugString(const P &message)
int64_t ComputeGcdOfRoundedDoubles(absl::Span< const double > x, double scaling_factor)
static int input(yyscan_t yyscanner)
double max_scaling_factor
double max_relative_coeff_error
std::vector< double > lower_bounds
std::vector< int > var_indices
std::vector< double > upper_bounds
double min_scaling_factor
double max_absolute_rhs_error
#define SOLVER_LOG(logger,...)