26 #include "absl/container/btree_set.h" 45 const double kMinCutViolation = 1e-4;
48 double GetLiteralLpValue(
51 const IntegerEncoder* encoder) {
52 const IntegerVariable direct_view = encoder->GetLiteralView(lit);
54 return lp_values[direct_view];
56 const IntegerVariable opposite_view = encoder->GetLiteralView(lit.Negated());
58 return 1.0 - lp_values[opposite_view];
64 LinearConstraint GenerateKnapsackCutForCover(
65 const std::vector<IntegerVariable>& vars,
66 const std::vector<IntegerValue>& coeffs,
const IntegerValue
upper_bound,
67 const IntegerTrail& integer_trail) {
68 CHECK_EQ(vars.size(), coeffs.size());
71 IntegerValue cut_upper_bound = IntegerValue(0);
72 IntegerValue max_coeff = coeffs[0];
75 for (
int i = 0; i < vars.size(); ++i) {
76 const IntegerValue var_upper_bound =
77 integer_trail.LevelZeroUpperBound(vars[i]);
78 cut_upper_bound += var_upper_bound;
79 cut.vars.push_back(vars[i]);
80 cut.coeffs.push_back(IntegerValue(1));
81 max_coeff =
std::max(max_coeff, coeffs[i]);
82 slack += coeffs[i] * var_upper_bound;
84 CHECK_GT(slack, 0.0) <<
"Invalid cover for knapsack cut.";
85 cut_upper_bound -=
CeilRatio(slack, max_coeff);
87 cut.ub = cut_upper_bound;
88 VLOG(2) <<
"Generated Knapsack Constraint:" << cut.DebugString();
92 bool SolutionSatisfiesConstraint(
93 const LinearConstraint& constraint,
96 const double tolerance = 1e-6;
97 return (activity <=
ToDouble(constraint.ub) + tolerance &&
98 activity >=
ToDouble(constraint.lb) - tolerance)
103 bool SmallRangeAndAllCoefficientsMagnitudeAreTheSame(
104 const LinearConstraint& constraint, IntegerTrail* integer_trail) {
105 if (constraint.vars.empty())
return true;
107 const int64_t magnitude = std::abs(constraint.coeffs[0].value());
108 for (
int i = 1; i < constraint.coeffs.size(); ++i) {
109 const IntegerVariable
var = constraint.vars[i];
110 if (integer_trail->LevelZeroUpperBound(
var) -
111 integer_trail->LevelZeroLowerBound(
var) >
115 if (std::abs(constraint.coeffs[i].value()) != magnitude) {
122 bool AllVarsTakeIntegerValue(
123 const std::vector<IntegerVariable> vars,
125 for (IntegerVariable
var : vars) {
126 if (std::abs(lp_values[
var] - std::round(lp_values[
var])) > 1e-6) {
142 int GetSmallestCoverSize(
const LinearConstraint& constraint,
143 const IntegerTrail& integer_trail) {
144 IntegerValue ub = constraint.ub;
145 std::vector<IntegerValue> sorted_terms;
146 for (
int i = 0; i < constraint.vars.size(); ++i) {
147 const IntegerValue coeff = constraint.coeffs[i];
148 const IntegerVariable
var = constraint.vars[i];
149 const IntegerValue var_ub = integer_trail.LevelZeroUpperBound(
var);
150 const IntegerValue var_lb = integer_trail.LevelZeroLowerBound(
var);
151 ub -= var_lb * coeff;
152 sorted_terms.push_back(coeff * (var_ub - var_lb));
154 std::sort(sorted_terms.begin(), sorted_terms.end(),
155 std::greater<IntegerValue>());
156 int smallest_cover_size = 0;
157 IntegerValue sorted_term_sum = IntegerValue(0);
158 while (sorted_term_sum <= ub &&
159 smallest_cover_size < constraint.vars.size()) {
160 sorted_term_sum += sorted_terms[smallest_cover_size++];
162 return smallest_cover_size;
165 bool ConstraintIsEligibleForLifting(
const LinearConstraint& constraint,
166 const IntegerTrail& integer_trail) {
167 for (
const IntegerVariable
var : constraint.vars) {
168 if (integer_trail.LevelZeroLowerBound(
var) != IntegerValue(0) ||
169 integer_trail.LevelZeroUpperBound(
var) != IntegerValue(1)) {
180 const std::vector<IntegerValue>& cut_vars_original_coefficients,
183 std::set<IntegerVariable> vars_in_cut;
184 for (IntegerVariable
var : cut->
vars) {
185 vars_in_cut.insert(
var);
188 std::vector<std::pair<IntegerValue, IntegerVariable>> non_zero_vars;
189 std::vector<std::pair<IntegerValue, IntegerVariable>> zero_vars;
190 for (
int i = 0; i < constraint.
vars.size(); ++i) {
191 const IntegerVariable
var = constraint.
vars[i];
196 if (vars_in_cut.find(
var) != vars_in_cut.end())
continue;
197 const IntegerValue coeff = constraint.
coeffs[i];
198 if (lp_values[
var] <= 1e-6) {
199 zero_vars.push_back({coeff,
var});
201 non_zero_vars.push_back({coeff,
var});
207 std::sort(non_zero_vars.rbegin(), non_zero_vars.rend());
208 std::sort(zero_vars.rbegin(), zero_vars.rend());
210 std::vector<std::pair<IntegerValue, IntegerVariable>> lifting_sequence(
211 std::move(non_zero_vars));
213 lifting_sequence.insert(lifting_sequence.end(), zero_vars.begin(),
217 std::vector<double> lifting_profits;
218 std::vector<double> lifting_weights;
219 for (
int i = 0; i < cut->
vars.size(); ++i) {
221 lifting_weights.push_back(
ToDouble(cut_vars_original_coefficients[i]));
225 bool is_lifted =
false;
226 bool is_solution_optimal =
false;
228 for (
auto entry : lifting_sequence) {
229 is_solution_optimal =
false;
230 const IntegerValue var_original_coeff = entry.first;
231 const IntegerVariable
var = entry.second;
232 const IntegerValue lifting_capacity = constraint.
ub - entry.first;
233 if (lifting_capacity <= IntegerValue(0))
continue;
234 knapsack_solver.
Init(lifting_profits, lifting_weights,
241 const double knapsack_upper_bound =
243 const IntegerValue cut_coeff =
244 cut->
ub - static_cast<int64_t>(knapsack_upper_bound);
245 if (cut_coeff > IntegerValue(0)) {
248 cut->
coeffs.push_back(cut_coeff);
249 lifting_profits.push_back(
ToDouble(cut_coeff));
250 lifting_weights.push_back(
ToDouble(var_original_coeff));
260 IntegerValue ub = constraint.
ub;
262 for (
int i = 0; i < constraint.
vars.size(); ++i) {
263 const IntegerVariable
var = constraint.
vars[i];
265 const IntegerValue coeff = constraint.
coeffs[i];
266 if (
ToDouble(var_ub) - lp_values[
var] <= 1.0 - kMinCutViolation) {
267 constraint_with_left_vars.
vars.push_back(
var);
268 constraint_with_left_vars.
coeffs.push_back(coeff);
272 ub -= coeff * var_lb;
275 constraint_with_left_vars.
ub = ub;
276 constraint_with_left_vars.
lb = constraint.
lb;
277 return constraint_with_left_vars;
282 IntegerValue term_sum = IntegerValue(0);
283 for (
int i = 0; i < constraint.
vars.size(); ++i) {
284 const IntegerVariable
var = constraint.
vars[i];
286 const IntegerValue coeff = constraint.
coeffs[i];
287 term_sum += coeff * var_ub;
289 if (term_sum <= constraint.
ub) {
290 VLOG(2) <<
"Filtered by cover filter";
300 std::vector<double> variable_upper_bound_distances;
301 for (
const IntegerVariable
var : preprocessed_constraint.
vars) {
303 variable_upper_bound_distances.push_back(
ToDouble(var_ub) - lp_values[
var]);
306 const int smallest_cover_size =
307 GetSmallestCoverSize(preprocessed_constraint, integer_trail);
310 variable_upper_bound_distances.begin(),
311 variable_upper_bound_distances.begin() + smallest_cover_size - 1,
312 variable_upper_bound_distances.end());
313 double cut_lower_bound = 0.0;
314 for (
int i = 0; i < smallest_cover_size; ++i) {
315 cut_lower_bound += variable_upper_bound_distances[i];
317 if (cut_lower_bound >= 1.0 - kMinCutViolation) {
318 VLOG(2) <<
"Filtered by kappa heuristic";
327 std::sort(items.begin(), items.end(), std::greater<KnapsackItem>());
331 if (item.weight <= left_capacity) {
332 profit += item.profit;
333 left_capacity -= item.weight;
335 profit += (left_capacity / item.weight) * item.profit;
346 std::vector<KnapsackItem> items;
348 double sum_variable_profit = 0;
349 for (
int i = 0; i < constraint.
vars.size(); ++i) {
350 const IntegerVariable
var = constraint.
vars[i];
353 const IntegerValue coeff = constraint.
coeffs[i];
357 items.push_back(item);
359 sum_variable_profit += item.
profit;
364 if (sum_variable_profit - 1.0 + kMinCutViolation < 0.0)
return false;
367 const double knapsack_upper_bound =
369 if (knapsack_upper_bound < sum_variable_profit - 1.0 + kMinCutViolation) {
370 VLOG(2) <<
"Filtered by knapsack upper bound";
395 std::vector<LinearConstraint>* knapsack_constraints,
401 if (SmallRangeAndAllCoefficientsMagnitudeAreTheSame(constraint,
409 for (
int i = 0; i < constraint.
vars.size(); ++i) {
410 const IntegerVariable
var = constraint.
vars[i];
411 const IntegerValue coeff = constraint.
coeffs[i];
412 if (coeff > IntegerValue(0)) {
418 canonical_knapsack_form.
ub = constraint.
ub;
420 knapsack_constraints->push_back(canonical_knapsack_form);
427 for (
int i = 0; i < constraint.
vars.size(); ++i) {
428 const IntegerVariable
var = constraint.
vars[i];
429 const IntegerValue coeff = constraint.
coeffs[i];
430 if (coeff > IntegerValue(0)) {
436 canonical_knapsack_form.
ub = -constraint.
lb;
438 knapsack_constraints->push_back(canonical_knapsack_form);
445 const std::vector<LinearConstraint>& base_constraints,
446 const std::vector<IntegerVariable>& vars,
Model*
model) {
451 std::vector<LinearConstraint> knapsack_constraints;
459 if (constraint.vars.size() <= 2)
continue;
463 VLOG(1) <<
"#knapsack constraints: " << knapsack_constraints.size();
473 result.
generate_cuts = [implied_bounds_processor, knapsack_constraints, vars,
474 model, integer_trail](
481 if (AllVarsTakeIntegerValue(vars, lp_values))
return true;
484 "Knapsack on demand cover cut generator");
485 int64_t skipped_constraints = 0;
493 VLOG(2) <<
"Processing constraint: " << constraint.DebugString();
495 mutable_constraint = constraint;
497 lp_values, &mutable_constraint);
503 if (preprocessed_constraint.
vars.empty())
continue;
507 skipped_constraints++;
512 std::vector<double> profits;
513 profits.reserve(preprocessed_constraint.
vars.size());
516 std::vector<double> weights;
517 weights.reserve(preprocessed_constraint.
vars.size());
525 double sum_variable_profit = 0;
529 for (
int i = 0; i < preprocessed_constraint.
vars.size(); ++i) {
530 const IntegerVariable
var = preprocessed_constraint.
vars[i];
534 const double variable_profit = var_ub - lp_values[
var];
535 profits.push_back(variable_profit);
537 sum_variable_profit += variable_profit;
540 weights.push_back(
weight);
545 std::vector<IntegerVariable> cut_vars;
546 std::vector<IntegerValue> cut_vars_original_coefficients;
548 VLOG(2) <<
"Knapsack size: " << profits.size();
552 const double time_limit_for_knapsack_solver =
558 bool is_solution_optimal =
false;
560 sum_variable_profit - 1.0 + kMinCutViolation);
564 auto time_limit_for_solver =
565 absl::make_unique<TimeLimit>(time_limit_for_knapsack_solver);
566 const double sum_of_distance_to_ub_for_vars_in_cover =
567 sum_variable_profit -
568 knapsack_solver.
Solve(time_limit_for_solver.get(),
569 &is_solution_optimal);
570 if (is_solution_optimal) {
571 VLOG(2) <<
"Knapsack Optimal solution found yay !";
573 if (time_limit_for_solver->LimitReached()) {
574 VLOG(1) <<
"Knapsack Solver run out of time limit.";
576 if (sum_of_distance_to_ub_for_vars_in_cover < 1.0 - kMinCutViolation) {
579 IntegerValue constraint_ub_for_cut = preprocessed_constraint.
ub;
580 std::set<IntegerVariable> vars_in_cut;
581 for (
int i = 0; i < preprocessed_constraint.
vars.size(); ++i) {
582 const IntegerVariable
var = preprocessed_constraint.
vars[i];
585 cut_vars.push_back(
var);
586 cut_vars_original_coefficients.push_back(
coefficient);
587 vars_in_cut.insert(
var);
594 cut_vars, cut_vars_original_coefficients, constraint_ub_for_cut,
598 bool is_lifted =
false;
599 if (ConstraintIsEligibleForLifting(cut, *integer_trail)) {
601 cut_vars_original_coefficients, *integer_trail,
607 CHECK(!SolutionSatisfiesConstraint(cut, lp_values));
608 manager->AddCut(cut, is_lifted ?
"LiftedKnapsack" :
"Knapsack",
612 if (skipped_constraints > 0) {
613 VLOG(2) <<
"Skipped constraints: " << skipped_constraints;
625 IntegerValue
GetFactorT(IntegerValue rhs_remainder, IntegerValue divisor,
626 IntegerValue max_t) {
628 return rhs_remainder == 0
634 IntegerValue rhs_remainder, IntegerValue divisor, IntegerValue t,
635 IntegerValue max_scaling) {
649 const IntegerValue size = divisor - rhs_remainder;
650 if (max_scaling == 1 || size == 1) {
654 return [t, divisor](IntegerValue coeff) {
657 }
else if (size <= max_scaling) {
658 return [size, rhs_remainder, t, divisor](IntegerValue coeff) {
659 const IntegerValue t_coeff = t * coeff;
662 const IntegerValue diff = remainder - rhs_remainder;
665 }
else if (max_scaling.value() * rhs_remainder.value() < divisor) {
675 return [t, divisor, max_scaling](IntegerValue coeff) {
676 const IntegerValue t_coeff = t * coeff;
679 const IntegerValue bucket =
FloorRatio(remainder * max_scaling, divisor);
680 return max_scaling *
ratio + bucket;
705 return [size, rhs_remainder, t, divisor, max_scaling](IntegerValue coeff) {
706 const IntegerValue t_coeff = t * coeff;
709 const IntegerValue diff = remainder - rhs_remainder;
710 const IntegerValue bucket =
711 diff > 0 ?
CeilRatio(diff * (max_scaling - 1), size)
713 return max_scaling *
ratio + bucket;
726 const int size = lp_values.size();
727 if (size == 0)
return;
740 relevant_indices_.clear();
741 relevant_lp_values_.clear();
742 relevant_coeffs_.clear();
743 relevant_bound_diffs_.clear();
745 adjusted_coeffs_.clear();
748 IntegerValue max_magnitude(0);
749 for (
int i = 0; i < size; ++i) {
752 max_magnitude =
std::max(max_magnitude, magnitude);
758 bool overflow =
false;
759 change_sign_at_postprocessing_.assign(size,
false);
760 for (
int i = 0; i < size; ++i) {
761 if (cut->
coeffs[i] == 0)
continue;
766 double lp_value = lp_values[i];
769 const IntegerValue bound_diff =
770 IntegerValue(
CapSub(ub.value(), lb.value()));
782 const double lb_dist = std::abs(lp_value -
ToDouble(lb));
783 const double ub_dist = std::abs(lp_value -
ToDouble(ub));
786 if ((bias * lb_dist > ub_dist && cut->
coeffs[i] < 0) ||
787 (lb_dist > bias * ub_dist && cut->
coeffs[i] > 0)) {
788 change_sign_at_postprocessing_[i] =
true;
790 lp_value = -lp_value;
805 if (bound_diff == 0) {
806 cut->
coeffs[i] = IntegerValue(0);
810 if (std::abs(lp_value) > 1e-2) {
811 relevant_coeffs_.push_back(cut->
coeffs[i]);
812 relevant_indices_.push_back(i);
813 relevant_lp_values_.push_back(lp_value);
814 relevant_bound_diffs_.push_back(bound_diff);
815 divisors_.push_back(magnitude);
820 if (relevant_coeffs_.empty()) {
821 VLOG(2) <<
"Issue, nothing to cut.";
841 double best_scaled_violation = 0.01;
842 const IntegerValue remainder_threshold(max_magnitude / 1000);
853 if (overflow || max_magnitude >= threshold) {
854 VLOG(2) <<
"Issue, overflow.";
858 const IntegerValue max_t = threshold / max_magnitude;
869 const IntegerValue divisor_threshold = max_magnitude / 10;
870 for (
int i = 0; i < divisors_.size(); ++i) {
871 if (divisors_[i] <= divisor_threshold)
continue;
872 divisors_[new_size++] = divisors_[i];
874 divisors_.resize(new_size);
881 IntegerValue best_divisor(0);
882 for (
const IntegerValue divisor : divisors_) {
884 const IntegerValue initial_rhs_remainder =
886 if (initial_rhs_remainder <= remainder_threshold)
continue;
888 IntegerValue temp_ub = cut->
ub;
889 adjusted_coeffs_.clear();
906 const IntegerValue adjust_threshold =
907 (divisor - initial_rhs_remainder - 1) / IntegerValue(size);
908 if (adjust_threshold > 0) {
912 bool early_abort =
false;
913 double loss_lb = 0.0;
914 const double threshold =
ToDouble(initial_rhs_remainder);
916 for (
int i = 0; i < relevant_coeffs_.size(); ++i) {
918 const IntegerValue coeff = relevant_coeffs_[i];
919 const IntegerValue remainder =
920 CeilRatio(coeff, divisor) * divisor - coeff;
922 if (divisor - remainder <= initial_rhs_remainder) {
925 loss_lb +=
ToDouble(divisor - remainder) * relevant_lp_values_[i];
926 if (loss_lb >= threshold) {
933 const IntegerValue diff = relevant_bound_diffs_[i];
934 if (remainder > 0 && remainder <= adjust_threshold &&
935 CapProd(diff.value(), remainder.value()) <= adjust_threshold) {
936 temp_ub += remainder * diff;
937 adjusted_coeffs_.push_back({i, coeff + remainder});
941 if (early_abort)
continue;
945 const IntegerValue rhs_remainder =
946 temp_ub -
FloorRatio(temp_ub, divisor) * divisor;
947 if (rhs_remainder == 0)
continue;
950 rhs_remainder, divisor,
GetFactorT(rhs_remainder, divisor, max_t),
961 const double threshold = scaling *
ToDouble(rhs_remainder);
968 double violation = -
ToDouble(f(temp_ub));
969 double l2_norm = 0.0;
970 bool early_abort =
false;
971 int adjusted_coeffs_index = 0;
972 for (
int i = 0; i < relevant_coeffs_.size(); ++i) {
973 IntegerValue coeff = relevant_coeffs_[i];
976 if (adjusted_coeffs_index < adjusted_coeffs_.size() &&
977 adjusted_coeffs_[adjusted_coeffs_index].first == i) {
978 coeff = adjusted_coeffs_[adjusted_coeffs_index].second;
979 adjusted_coeffs_index++;
982 if (coeff == 0)
continue;
983 const IntegerValue new_coeff = f(coeff);
984 const double new_coeff_double =
ToDouble(new_coeff);
985 const double lp_value = relevant_lp_values_[i];
987 l2_norm += new_coeff_double * new_coeff_double;
988 violation += new_coeff_double * lp_value;
989 loss += (scaling *
ToDouble(coeff) - new_coeff_double) * lp_value;
990 if (loss >= threshold) {
995 if (early_abort)
continue;
999 violation /= sqrt(l2_norm);
1000 if (violation > best_scaled_violation) {
1001 best_scaled_violation = violation;
1002 best_divisor = divisor;
1006 if (best_divisor == 0) {
1016 const IntegerValue initial_rhs_remainder =
1018 const IntegerValue adjust_threshold =
1019 (best_divisor - initial_rhs_remainder - 1) / IntegerValue(size);
1020 if (adjust_threshold > 0) {
1021 for (
int i = 0; i < relevant_indices_.size(); ++i) {
1022 const int index = relevant_indices_[i];
1023 const IntegerValue diff = relevant_bound_diffs_[i];
1024 if (diff > adjust_threshold)
continue;
1028 const IntegerValue remainder =
1029 CeilRatio(coeff, best_divisor) * best_divisor - coeff;
1030 if (
CapProd(diff.value(), remainder.value()) <= adjust_threshold) {
1031 cut->
ub += remainder * diff;
1044 const IntegerValue rhs_remainder =
1046 IntegerValue factor_t =
GetFactorT(rhs_remainder, best_divisor, max_t);
1053 remainders_.clear();
1054 for (
int i = 0; i < size; ++i) {
1055 const IntegerValue coeff = cut->
coeffs[i];
1056 const IntegerValue r =
1057 coeff -
FloorRatio(coeff, best_divisor) * best_divisor;
1058 if (r > rhs_remainder) remainders_.push_back(r);
1061 if (remainders_.size() <= 100) {
1063 for (
const IntegerValue r : remainders_) {
1064 best_rs_.push_back(f(r));
1066 IntegerValue best_d = f(best_divisor);
1071 for (
const IntegerValue t :
1072 {IntegerValue(1),
GetFactorT(rhs_remainder, best_divisor, max_t)}) {
1073 for (IntegerValue s(2); s <= options.
max_scaling; ++s) {
1076 int num_strictly_better = 0;
1078 const IntegerValue d = g(best_divisor);
1079 for (
int i = 0; i < best_rs_.size(); ++i) {
1080 const IntegerValue temp = g(remainders_[i]);
1081 if (temp * best_d < best_rs_[i] * d)
break;
1082 if (temp * best_d > best_rs_[i] * d) num_strictly_better++;
1083 rs_.push_back(temp);
1085 if (rs_.size() == best_rs_.size() && num_strictly_better > 0) {
1097 cut->
ub = f(cut->
ub);
1102 num_lifted_booleans_ = 0;
1103 if (ib_processor !=
nullptr) {
1104 for (
int i = 0; i < size; ++i) {
1105 const IntegerValue coeff = cut->
coeffs[i];
1106 if (coeff == 0)
continue;
1108 IntegerVariable
var = cut->
vars[i];
1109 if (change_sign_at_postprocessing_[i]) {
1128 const IntegerValue coeff_b =
1131 if (coeff_b == 0)
continue;
1133 ++num_lifted_booleans_;
1135 tmp_terms_.push_back({info.
bool_var, coeff_b});
1137 tmp_terms_.push_back({info.
bool_var, -coeff_b});
1138 cut->
ub =
CapAdd(-coeff_b.value(), cut->
ub.value());
1147 for (
int i = 0; i < size; ++i) {
1148 IntegerValue coeff = cut->
coeffs[i];
1149 if (coeff == 0)
continue;
1151 if (coeff == 0)
continue;
1152 if (change_sign_at_postprocessing_[i]) {
1158 tmp_terms_.push_back({cut->
vars[i], -coeff});
1165 tmp_terms_.push_back({cut->
vars[i], coeff});
1179 const int base_size = lp_values.size();
1185 IntegerValue rhs = base_ct.
ub;
1186 IntegerValue sum_of_diff(0);
1187 IntegerValue max_base_magnitude(0);
1188 for (
int i = 0; i < base_size; ++i) {
1189 const IntegerValue coeff = base_ct.
coeffs[i];
1190 const IntegerValue positive_coeff =
IntTypeAbs(coeff);
1191 max_base_magnitude =
std::max(max_base_magnitude, positive_coeff);
1193 if (!
AddProductTo(positive_coeff, bound_diff, &sum_of_diff)) {
1196 const IntegerValue diff = positive_coeff * bound_diff;
1210 double activity = 0.0;
1212 std::sort(terms_.begin(), terms_.end(), [](
const Term&
a,
const Term&
b) {
1213 if (
a.dist_to_max_value ==
b.dist_to_max_value) {
1215 return a.positive_coeff <
b.positive_coeff;
1217 return a.dist_to_max_value <
b.dist_to_max_value;
1219 for (
int i = 0; i < terms_.size(); ++i) {
1220 const Term& term = terms_[i];
1221 activity += term.dist_to_max_value;
1230 if (activity > 1.0) {
1245 if (rhs >= 0)
return false;
1246 if (new_size == 0)
return false;
1254 terms_.resize(new_size);
1255 std::sort(terms_.begin(), terms_.end(), [](
const Term&
a,
const Term&
b) {
1256 if (
a.positive_coeff ==
b.positive_coeff) {
1257 return a.dist_to_max_value >
b.dist_to_max_value;
1259 return a.positive_coeff >
b.positive_coeff;
1261 in_cut_.assign(base_ct.
vars.size(),
false);
1264 cut_.
ub = IntegerValue(-1);
1265 IntegerValue max_coeff(0);
1266 for (
const Term term : terms_) {
1267 if (term.diff + rhs < 0) {
1271 in_cut_[term.index] =
true;
1272 max_coeff =
std::max(max_coeff, term.positive_coeff);
1273 cut_.
vars.push_back(base_ct.
vars[term.index]);
1274 if (base_ct.
coeffs[term.index] > 0) {
1275 cut_.
coeffs.push_back(IntegerValue(1));
1278 cut_.
coeffs.push_back(IntegerValue(-1));
1287 if (max_coeff == 0)
return true;
1288 if (max_coeff < -rhs) {
1289 const IntegerValue m =
FloorRatio(-rhs - 1, max_coeff);
1290 rhs += max_coeff * m;
1309 const IntegerValue slack = -rhs;
1310 const IntegerValue remainder = max_coeff - slack;
1312 const IntegerValue max_scaling(
std::min(
1315 IntegerValue(1), max_scaling);
1317 const IntegerValue scaling = f(max_coeff);
1319 for (
int i = 0; i < cut_.
coeffs.size(); ++i) cut_.
coeffs[i] *= scaling;
1324 for (
int i = 0; i < base_size; ++i) {
1325 if (in_cut_[i])
continue;
1327 const IntegerValue new_coeff = f(positive_coeff);
1328 if (new_coeff == 0)
continue;
1331 if (base_ct.
coeffs[i] > 0) {
1333 cut_.
coeffs.push_back(new_coeff);
1334 cut_.
vars.push_back(base_ct.
vars[i]);
1338 cut_.
coeffs.push_back(-new_coeff);
1339 cut_.
vars.push_back(base_ct.
vars[i]);
1351 int linearization_level,
1362 [z, x, y, linearization_level,
model, trail, integer_trail](
1365 if (trail->CurrentDecisionLevel() > 0 && linearization_level == 1) {
1374 const int64_t kMaxSafeInteger = (int64_t{1} << 53) - 1;
1376 if (
CapProd(x_ub, y_ub) >= kMaxSafeInteger) {
1377 VLOG(3) <<
"Potential overflow in PositiveMultiplicationCutGenerator";
1381 const double x_lp_value = x.LpValue(lp_values);
1382 const double y_lp_value = y.LpValue(lp_values);
1383 const double z_lp_value = z.
LpValue(lp_values);
1391 auto try_add_above_cut =
1392 [manager, z_lp_value, x_lp_value, y_lp_value, x, y, z,
model,
1393 &lp_values](int64_t x_coeff, int64_t y_coeff, int64_t rhs) {
1394 if (-z_lp_value + x_lp_value * x_coeff + y_lp_value * y_coeff >=
1395 rhs + kMinCutViolation) {
1398 cut.
AddTerm(z, IntegerValue(-1));
1399 if (x_coeff != 0) cut.
AddTerm(x, IntegerValue(x_coeff));
1400 if (y_coeff != 0) cut.
AddTerm(y, IntegerValue(y_coeff));
1401 manager->AddCut(cut.
Build(),
"PositiveProduct", lp_values);
1406 auto try_add_below_cut =
1407 [manager, z_lp_value, x_lp_value, y_lp_value, x, y, z,
model,
1408 &lp_values](int64_t x_coeff, int64_t y_coeff, int64_t rhs) {
1409 if (-z_lp_value + x_lp_value * x_coeff + y_lp_value * y_coeff <=
1410 rhs - kMinCutViolation) {
1413 cut.
AddTerm(z, IntegerValue(-1));
1414 if (x_coeff != 0) cut.
AddTerm(x, IntegerValue(x_coeff));
1415 if (y_coeff != 0) cut.
AddTerm(y, IntegerValue(y_coeff));
1416 manager->AddCut(cut.
Build(),
"PositiveProduct", lp_values);
1427 try_add_above_cut(y_lb, x_lb, x_lb * y_lb);
1428 try_add_above_cut(y_ub, x_ub, x_ub * y_ub);
1429 try_add_below_cut(y_ub, x_lb, x_lb * y_ub);
1430 try_add_below_cut(y_lb, x_ub, x_ub * y_lb);
1446 [y, x, linearization_level, trail, integer_trail,
model](
1449 if (trail->CurrentDecisionLevel() > 0 && linearization_level == 1) {
1452 const int64_t x_ub = integer_trail->LevelZeroUpperBound(x).value();
1453 const int64_t x_lb = integer_trail->LevelZeroLowerBound(x).value();
1455 if (x_lb == x_ub)
return true;
1458 if (x_ub > (int64_t{1} << 31))
return true;
1461 const double y_lp_value = y.
LpValue(lp_values);
1462 const double x_lp_value = x.LpValue(lp_values);
1467 const int64_t y_lb = x_lb * x_lb;
1468 const int64_t above_slope = x_ub + x_lb;
1469 const double max_lp_y = y_lb + above_slope * (x_lp_value - x_lb);
1470 if (y_lp_value >= max_lp_y + kMinCutViolation) {
1473 IntegerValue(-x_lb * x_ub));
1474 above_cut.
AddTerm(y, IntegerValue(1));
1475 above_cut.
AddTerm(x, IntegerValue(-above_slope));
1476 manager->AddCut(above_cut.
Build(),
"SquareUpper", lp_values);
1485 const int64_t x_floor = static_cast<int64_t>(std::floor(x_lp_value));
1486 const int64_t below_slope = 2 * x_floor + 1;
1487 const double min_lp_y =
1488 below_slope * x_lp_value - x_floor - x_floor * x_floor;
1489 if (min_lp_y >= y_lp_value + kMinCutViolation) {
1493 model, IntegerValue(-x_floor - x_floor * x_floor),
1495 below_cut.
AddTerm(y, IntegerValue(1));
1496 below_cut.
AddTerm(x, -IntegerValue(below_slope));
1497 manager->AddCut(below_cut.
Build(),
"SquareLower", lp_values);
1509 false, IntegerVariable(0), lp_values,
1515 auto it = cache_.find(
var);
1516 if (it != cache_.end())
return it->second;
1521 ImpliedBoundsProcessor::ComputeBestImpliedBound(
1522 IntegerVariable
var,
1524 auto it = cache_.find(
var);
1525 if (it != cache_.end())
return it->second;
1526 BestImpliedBoundInfo result;
1541 const IntegerValue diff = entry.lower_bound - lb;
1543 const double bool_lp_value = entry.is_positive
1544 ? lp_values[entry.literal_view]
1545 : 1.0 - lp_values[entry.literal_view];
1546 const double slack_lp_value =
1551 if (slack_lp_value < -1e-4) {
1552 LinearConstraint ib_cut;
1554 std::vector<std::pair<IntegerVariable, IntegerValue>> terms;
1555 if (entry.is_positive) {
1557 terms.push_back({entry.literal_view, diff});
1558 terms.push_back({
var, IntegerValue(-1)});
1562 terms.push_back({entry.literal_view, -diff});
1563 terms.push_back({
var, IntegerValue(-1)});
1564 ib_cut.ub = -entry.lower_bound;
1567 ib_cut_pool_.
AddCut(std::move(ib_cut),
"IB", lp_values);
1573 if (slack_lp_value + 1e-4 < result.slack_lp_value ||
1574 (slack_lp_value < result.slack_lp_value + 1e-4 &&
1575 diff > result.bound_diff)) {
1576 result.bool_lp_value = bool_lp_value;
1577 result.slack_lp_value = slack_lp_value;
1579 result.bound_diff = diff;
1580 result.is_positive = entry.is_positive;
1581 result.bool_var = entry.literal_view;
1584 cache_[
var] = result;
1591 for (
const IntegerVariable
var :
1594 ComputeBestImpliedBound(
var, lp_values);
1599 bool substitute_only_inner_variables, IntegerVariable first_slack,
1602 if (cache_.empty())
return;
1604 IntegerValue new_ub = cut->
ub;
1605 bool changed =
false;
1608 int64_t overflow_detection = 0;
1610 const int size = cut->
vars.size();
1611 for (
int i = 0; i < size; ++i) {
1612 IntegerVariable
var = cut->
vars[i];
1613 IntegerValue coeff = cut->
coeffs[i];
1626 const int old_size = tmp_terms_.size();
1629 bool keep_term =
false;
1643 if (substitute_only_inner_variables) {
1646 if (lp_values[
var] -
ToDouble(lb) < 1e-2) keep_term =
true;
1647 if (
ToDouble(ub) - lp_values[
var] < 1e-2) keep_term =
true;
1651 if (slack_infos ==
nullptr) {
1658 tmp_terms_.push_back({
var, coeff});
1667 slack_info.
ub = ub - lb;
1673 VLOG(2) <<
"Overflow";
1676 if (slack_infos !=
nullptr) {
1677 tmp_terms_.push_back({first_slack, coeff});
1681 slack_info.
terms.push_back({
var, IntegerValue(1)});
1684 slack_infos->push_back(slack_info);
1691 VLOG(2) <<
"Overflow";
1694 if (slack_infos !=
nullptr) {
1695 tmp_terms_.push_back({first_slack, coeff});
1699 slack_info.
terms.push_back({
var, IntegerValue(1)});
1702 slack_infos->push_back(slack_info);
1710 for (
int i = old_size; i < tmp_terms_.size(); ++i) {
1711 overflow_detection =
1712 CapAdd(overflow_detection, std::abs(tmp_terms_[i].second.value()));
1717 VLOG(2) <<
"Overflow";
1720 if (!changed)
return;
1734 const std::vector<SlackInfo>& info) {
1736 IntegerValue new_ub = cut.
ub;
1737 for (
int i = 0; i < cut.
vars.size(); ++i) {
1739 if (cut.
vars[i] < first_slack) {
1740 tmp_terms_.push_back({cut.
vars[i], cut.
coeffs[i]});
1745 const IntegerValue multiplier = cut.
coeffs[i];
1746 const int index = (cut.
vars[i].value() - first_slack.value()) / 2;
1747 for (
const std::pair<IntegerVariable, IntegerValue>& term :
1748 info[
index].terms) {
1749 tmp_terms_.push_back({term.first, term.second * multiplier});
1751 new_ub -= multiplier * info[
index].offset;
1756 tmp_cut.
ub = new_ub;
1766 for (
int i = 0; i < initial_cut.
vars.size(); ++i) {
1767 tmp_terms_.push_back({initial_cut.
vars[i], initial_cut.
coeffs[i]});
1770 tmp_copy.
ub = new_ub;
1774 if (tmp_cut == tmp_copy)
return true;
1785 int64_t SumOfKMinValues(
const absl::btree_set<int64_t>& values,
int k) {
1788 for (
const int64_t
value : values) {
1790 if (++count >= k)
return sum;
1795 void TryToGenerateAllDiffCut(
1796 const std::vector<std::pair<double, AffineExpression>>& sorted_exprs_lp,
1797 const IntegerTrail& integer_trail,
1799 LinearConstraintManager* manager, Model*
model) {
1800 std::vector<AffineExpression> current_set_exprs;
1801 const int num_exprs = sorted_exprs_lp.size();
1802 absl::btree_set<int64_t> min_values;
1803 absl::btree_set<int64_t> negated_max_values;
1805 for (
auto value_expr : sorted_exprs_lp) {
1806 sum += value_expr.first;
1807 const AffineExpression expr = value_expr.second;
1808 if (integer_trail.IsFixed(expr)) {
1809 const int64_t
value = integer_trail.FixedValue(expr).value();
1810 min_values.insert(
value);
1811 negated_max_values.insert(-
value);
1814 const int64_t coeff = expr.coeff.value();
1815 const int64_t constant = expr.constant.value();
1816 for (
const int64_t
value :
1817 integer_trail.InitialVariableDomain(expr.var).Values()) {
1819 min_values.insert(
value * coeff + constant);
1821 negated_max_values.insert(-(
value * coeff + constant));
1823 if (++count >= num_exprs)
break;
1827 for (
const int64_t
value :
1828 integer_trail.InitialVariableDomain(expr.var).Negation().Values()) {
1830 negated_max_values.insert(
value * coeff - constant);
1832 min_values.insert(-
value * coeff + constant);
1834 if (++count >= num_exprs)
break;
1837 current_set_exprs.push_back(expr);
1838 const int64_t required_min_sum =
1839 SumOfKMinValues(min_values, current_set_exprs.size());
1840 const int64_t required_max_sum =
1841 -SumOfKMinValues(negated_max_values, current_set_exprs.size());
1842 if (sum < required_min_sum || sum > required_max_sum) {
1843 LinearConstraintBuilder cut(
model, IntegerValue(required_min_sum),
1844 IntegerValue(required_max_sum));
1845 for (AffineExpression expr : current_set_exprs) {
1846 cut.AddTerm(expr, IntegerValue(1));
1848 manager->AddCut(cut.Build(),
"all_diff", lp_values);
1852 current_set_exprs.clear();
1854 negated_max_values.clear();
1862 const std::vector<AffineExpression>& exprs,
Model*
model) {
1867 if (!integer_trail->
IsFixed(expr)) {
1868 result.
vars.push_back(expr.var);
1875 [exprs, integer_trail, trail,
model](
1881 if (trail->CurrentDecisionLevel() > 0)
return true;
1882 std::vector<std::pair<double, AffineExpression>> sorted_exprs;
1884 if (integer_trail->LevelZeroLowerBound(expr) ==
1885 integer_trail->LevelZeroUpperBound(expr)) {
1888 sorted_exprs.push_back(std::make_pair(expr.LpValue(lp_values), expr));
1890 std::sort(sorted_exprs.begin(), sorted_exprs.end(),
1891 [](std::pair<double, AffineExpression>&
a,
1892 const std::pair<double, AffineExpression>&
b) {
1893 return a.first <
b.first;
1895 TryToGenerateAllDiffCut(sorted_exprs, *integer_trail, lp_values,
1898 std::reverse(sorted_exprs.begin(), sorted_exprs.end());
1899 TryToGenerateAllDiffCut(sorted_exprs, *integer_trail, lp_values,
1903 VLOG(1) <<
"Created all_diff cut generator of size: " << exprs.size();
1909 IntegerValue MaxCornerDifference(
const IntegerVariable
var,
1910 const IntegerValue w1_i,
1911 const IntegerValue w2_i,
1912 const IntegerTrail& integer_trail) {
1913 const IntegerValue lb = integer_trail.LevelZeroLowerBound(
var);
1914 const IntegerValue ub = integer_trail.LevelZeroUpperBound(
var);
1915 return std::max((w2_i - w1_i) * lb, (w2_i - w1_i) * ub);
1924 IntegerValue MPlusCoefficient(
1925 const std::vector<IntegerVariable>& x_vars,
1926 const std::vector<LinearExpression>& exprs,
1928 const int max_index,
const IntegerTrail& integer_trail) {
1929 IntegerValue coeff = exprs[max_index].offset;
1932 for (
const IntegerVariable
var : x_vars) {
1933 const int target_index = variable_partition[
var];
1934 if (max_index != target_index) {
1935 coeff += MaxCornerDifference(
1946 double ComputeContribution(
1947 const IntegerVariable xi_var,
const std::vector<IntegerVariable>& z_vars,
1948 const std::vector<LinearExpression>& exprs,
1950 const IntegerTrail& integer_trail,
const int target_index) {
1952 CHECK_LT(target_index, exprs.size());
1953 const LinearExpression& target_expr = exprs[target_index];
1954 const double xi_value = lp_values[xi_var];
1956 double contrib =
ToDouble(wt_i) * xi_value;
1957 for (
int expr_index = 0; expr_index < exprs.size(); ++expr_index) {
1958 if (expr_index == target_index)
continue;
1959 const LinearExpression& max_expr = exprs[expr_index];
1960 const double z_max_value = lp_values[z_vars[expr_index]];
1961 const IntegerValue corner_value = MaxCornerDifference(
1964 contrib +=
ToDouble(corner_value) * z_max_value;
1971 const IntegerVariable target,
const std::vector<LinearExpression>& exprs,
1972 const std::vector<IntegerVariable>& z_vars,
Model*
model) {
1974 std::vector<IntegerVariable> x_vars;
1975 result.
vars = {target};
1976 const int num_exprs = exprs.size();
1977 for (
int i = 0; i < num_exprs; ++i) {
1978 result.
vars.push_back(z_vars[i]);
1979 x_vars.insert(x_vars.end(), exprs[i].vars.begin(), exprs[i].vars.end());
1983 DCHECK(std::all_of(x_vars.begin(), x_vars.end(), [](IntegerVariable
var) {
1986 result.
vars.insert(result.
vars.end(), x_vars.begin(), x_vars.end());
1990 [x_vars, z_vars, target, num_exprs, exprs, integer_trail,
model](
1994 lp_values.size(), -1);
1996 lp_values.size(), std::numeric_limits<double>::infinity());
1997 for (
int expr_index = 0; expr_index < num_exprs; ++expr_index) {
1998 for (
const IntegerVariable
var : x_vars) {
1999 const double contribution = ComputeContribution(
2000 var, z_vars, exprs, lp_values, *integer_trail, expr_index);
2001 const double prev_contribution = variable_partition_contrib[
var];
2002 if (contribution < prev_contribution) {
2003 variable_partition[
var] = expr_index;
2004 variable_partition_contrib[
var] = contribution;
2011 double violation = lp_values[target];
2012 cut.
AddTerm(target, IntegerValue(-1));
2014 for (
const IntegerVariable xi_var : x_vars) {
2015 const int input_index = variable_partition[xi_var];
2018 if (coeff != IntegerValue(0)) {
2021 violation -=
ToDouble(coeff) * lp_values[xi_var];
2023 for (
int expr_index = 0; expr_index < num_exprs; ++expr_index) {
2024 const IntegerVariable z_var = z_vars[expr_index];
2025 const IntegerValue z_coeff = MPlusCoefficient(
2026 x_vars, exprs, variable_partition, expr_index, *integer_trail);
2027 if (z_coeff != IntegerValue(0)) {
2030 violation -=
ToDouble(z_coeff) * lp_values[z_var];
2032 if (violation > 1e-2) {
2033 manager->AddCut(cut.
Build(),
"LinMax", lp_values);
2042 IntegerValue EvaluateMaxAffine(
2043 const std::vector<std::pair<IntegerValue, IntegerValue>>& affines,
2046 for (
const auto& p : affines) {
2047 y =
std::max(y, x * p.first + p.second);
2056 const std::vector<std::pair<IntegerValue, IntegerValue>>& affines,
2060 const IntegerValue x_max = integer_trail->LevelZeroUpperBound(
var);
2062 const IntegerValue y_at_min = EvaluateMaxAffine(affines, x_min);
2063 const IntegerValue y_at_max = EvaluateMaxAffine(affines, x_max);
2067 const IntegerValue delta_x = x_max - x_min;
2068 const IntegerValue delta_y = y_at_max - y_at_min;
2073 const IntegerValue rhs = delta_x * y_at_min - delta_y * x_min;
2081 VLOG(2) <<
"Linear constraint can cause overflow: " <<
ct;
2092 std::vector<std::pair<IntegerValue, IntegerValue>> affines,
2101 [target,
var, affines, cut_name, integer_trail,
model](
2104 if (integer_trail->IsFixed(
var))
return true;
2106 cut_name, lp_values);
2113 const std::vector<IntegerVariable>& base_variables,
Model*
model) {
2116 std::vector<IntegerVariable> variables;
2117 std::vector<Literal> literals;
2118 absl::flat_hash_map<LiteralIndex, IntegerVariable> positive_map;
2119 absl::flat_hash_map<LiteralIndex, IntegerVariable> negative_map;
2122 for (
const IntegerVariable
var : base_variables) {
2123 if (integer_trail->LowerBound(
var) != IntegerValue(0))
continue;
2124 if (integer_trail->UpperBound(
var) != IntegerValue(1))
continue;
2125 const LiteralIndex literal_index = encoder->GetAssociatedLiteral(
2128 variables.push_back(
var);
2129 literals.push_back(
Literal(literal_index));
2130 positive_map[literal_index] =
var;
2135 result.
vars = variables;
2138 [variables, literals, implication_graph, positive_map, negative_map,
2141 std::vector<double> packed_values;
2142 for (
int i = 0; i < literals.size(); ++i) {
2143 packed_values.push_back(lp_values[variables[i]]);
2145 const std::vector<std::vector<Literal>> at_most_ones =
2146 implication_graph->GenerateAtMostOnesWithLargeWeight(literals,
2149 for (
const std::vector<Literal>& at_most_one : at_most_ones) {
2156 for (
const Literal l : at_most_one) {
2157 if (positive_map.contains(l.Index())) {
2158 builder.
AddTerm(positive_map.at(l.Index()), IntegerValue(1));
2161 builder.
AddTerm(negative_map.at(l.Index()), IntegerValue(-1));
2166 manager->AddCut(builder.
Build(),
"clique", lp_values);
void set_node_limit(const int64_t node_limit)
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
std::vector< std::pair< IntegerVariable, IntegerValue > > terms
int64_t CapSub(int64_t x, int64_t y)
bool CanBeFilteredUsingKnapsackUpperBound(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &lp_values, const IntegerTrail &integer_trail)
void AddTerm(IntegerVariable var, IntegerValue coeff)
#define CHECK_GE(val1, val2)
Class that owns everything related to a particular optimization model.
ModelSharedTimeLimit * time_limit
std::vector< IntegerValue > coeffs
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
LinearConstraint BuildMaxAffineUpConstraint(const LinearExpression &target, IntegerVariable var, const std::vector< std::pair< IntegerValue, IntegerValue >> &affines, Model *model)
#define CHECK_GT(val1, val2)
#define VLOG(verboselevel)
std::vector< double > lower_bounds
std::vector< IntegerVariable > vars
CutGenerator CreateMaxAffineCutGenerator(LinearExpression target, IntegerVariable var, std::vector< std::pair< IntegerValue, IntegerValue >> affines, const std::string cut_name, Model *model)
LinearConstraint GetPreprocessedLinearConstraint(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &lp_values, const IntegerTrail &integer_trail)
void AddLinearExpression(const LinearExpression &expr)
void AddTerm(IntegerVariable var, IntegerValue coeff)
double LpValue(const absl::StrongVector< IntegerVariable, double > &lp_values) const
int64_t CapProd(int64_t x, int64_t y)
bool LiftKnapsackCut(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &lp_values, const std::vector< IntegerValue > &cut_vars_original_coefficients, const IntegerTrail &integer_trail, TimeLimit *time_limit, LinearConstraint *cut)
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
void MakeAllCoefficientsPositive(LinearConstraint *constraint)
void ComputeCut(RoundingOptions options, const std::vector< double > &lp_values, const std::vector< IntegerValue > &lower_bounds, const std::vector< IntegerValue > &upper_bounds, ImpliedBoundsProcessor *ib_processor, LinearConstraint *cut)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
void ProcessUpperBoundedConstraint(const absl::StrongVector< IntegerVariable, double > &lp_values, LinearConstraint *cut)
CutGenerator CreatePositiveMultiplicationCutGenerator(AffineExpression z, AffineExpression x, AffineExpression y, int linearization_level, Model *model)
double ComputeActivity(const LinearConstraint &constraint, const absl::StrongVector< IntegerVariable, double > &values)
double Solve(TimeLimit *time_limit, bool *is_solution_optimal)
bool AddProductTo(IntegerValue a, IntegerValue b, IntegerValue *result)
std::vector< IntegerVariable > vars
std::vector< IntegerVariable > vars
#define CHECK_LT(val1, val2)
double ToDouble(IntegerValue value)
IntegerVariable PositiveVariable(IntegerVariable i)
BestImpliedBoundInfo GetCachedImpliedBoundInfo(IntegerVariable var)
void DivideByGCD(LinearConstraint *constraint)
CutGenerator CreateLinMaxCutGenerator(const IntegerVariable target, const std::vector< LinearExpression > &exprs, const std::vector< IntegerVariable > &z_vars, Model *model)
LiteralIndex NegatedIndex() const
CutGenerator CreateKnapsackCoverCutGenerator(const std::vector< LinearConstraint > &base_constraints, const std::vector< IntegerVariable > &vars, Model *model)
int64_t CapAdd(int64_t x, int64_t y)
bool CanBeFilteredUsingCutLowerBound(const LinearConstraint &preprocessed_constraint, const absl::StrongVector< IntegerVariable, double > &lp_values, const IntegerTrail &integer_trail)
#define DCHECK_NE(val1, val2)
const std::vector< IntegerVariable > & VariablesWithImpliedBounds() const
CutGenerator CreateAllDifferentCutGenerator(const std::vector< AffineExpression > &exprs, Model *model)
bool ConstraintIsTriviallyTrue(const LinearConstraint &constraint, const IntegerTrail &integer_trail)
CutGenerator CreateSquareCutGenerator(AffineExpression y, AffineExpression x, int linearization_level, Model *model)
IntegerValue LevelZeroUpperBound(IntegerVariable var) const
CutGenerator CreateCliqueCutGenerator(const std::vector< IntegerVariable > &base_variables, Model *model)
bool DebugSlack(IntegerVariable first_slack, const LinearConstraint &initial_cut, const LinearConstraint &cut, const std::vector< SlackInfo > &info)
bool VariableIsPositive(IntegerVariable i)
#define DCHECK_GE(val1, val2)
bool ValidateLinearConstraintForOverflow(const LinearConstraint &constraint, const IntegerTrail &integer_trail)
void ProcessUpperBoundedConstraintWithSlackCreation(bool substitute_only_inner_variables, IntegerVariable first_slack, const absl::StrongVector< IntegerVariable, double > &lp_values, LinearConstraint *cut, std::vector< SlackInfo > *slack_infos)
void RecomputeCacheAndSeparateSomeImpliedBoundCuts(const absl::StrongVector< IntegerVariable, double > &lp_values)
#define CHECK_EQ(val1, val2)
IntegerValue PositiveRemainder(IntegerValue dividend, IntegerValue positive_divisor)
bool best_solution(int item_id) const
IntegerValue GetFactorT(IntegerValue rhs_remainder, IntegerValue divisor, IntegerValue max_t)
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
#define DCHECK(condition)
const std::vector< ImpliedBoundEntry > & GetImpliedBounds(IntegerVariable var)
IntegerValue FloorRatio(IntegerValue dividend, IntegerValue positive_divisor)
bool LimitReached()
Returns true when the external limit is true, or the deterministic time is over the deterministic lim...
std::function< IntegerValue(IntegerValue)> GetSuperAdditiveRoundingFunction(IntegerValue rhs_remainder, IntegerValue divisor, IntegerValue t, IntegerValue max_scaling)
void AddConstant(IntegerValue value)
void CleanTermsAndFillConstraint(std::vector< std::pair< IntegerVariable, IntegerValue >> *terms, ClassWithVarsAndCoeffs *output)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
void ConvertToKnapsackForm(const LinearConstraint &constraint, std::vector< LinearConstraint > *knapsack_constraints, IntegerTrail *integer_trail)
void AddCut(LinearConstraint ct, const std::string &name, const absl::StrongVector< IntegerVariable, double > &lp_solution)
void set_solution_upper_bound_threshold(const double solution_upper_bound_threshold)
Collection of objects used to extend the Constraint Solver library.
void Init(const std::vector< double > &profits, const std::vector< double > &weights, const double capacity)
const IntegerVariable kNoIntegerVariable(-1)
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)
std::vector< double > upper_bounds
const LiteralIndex kNoLiteralIndex(-1)
std::function< bool(const absl::StrongVector< IntegerVariable, double > &lp_values, LinearConstraintManager *manager)> generate_cuts
double GetKnapsackUpperBound(std::vector< KnapsackItem > items, const double capacity)
IntType IntTypeAbs(IntType t)
void MakeAllVariablesPositive(LinearConstraint *constraint)
std::string DebugString() const
IntegerValue LevelZeroLowerBound(IntegerVariable var) const
bool TrySimpleKnapsack(const LinearConstraint base_ct, const std::vector< double > &lp_values, const std::vector< IntegerValue > &lower_bounds, const std::vector< IntegerValue > &upper_bounds)
bool CanFormValidKnapsackCover(const LinearConstraint &preprocessed_constraint, const absl::StrongVector< IntegerVariable, double > &lp_values, const IntegerTrail &integer_trail)
#define CHECK_NE(val1, val2)
void RemoveZeroTerms(LinearConstraint *constraint)
bool IsFixed(IntegerVariable i) const
#define DCHECK_LT(val1, val2)
IntegerValue GetCoefficientOfPositiveVar(const IntegerVariable var, const LinearExpression &expr)