39 const double kMinCutViolation = 1e-4;
42 double GetLiteralLpValue(
45 const IntegerEncoder* encoder) {
46 const IntegerVariable direct_view = encoder->GetLiteralView(lit);
48 return lp_values[direct_view];
50 const IntegerVariable opposite_view = encoder->GetLiteralView(lit.Negated());
52 return 1.0 - lp_values[opposite_view];
58 LinearConstraint GenerateKnapsackCutForCover(
59 const std::vector<IntegerVariable>& vars,
60 const std::vector<IntegerValue>& coeffs,
const IntegerValue upper_bound,
61 const IntegerTrail& integer_trail) {
62 CHECK_EQ(vars.size(), coeffs.size());
65 IntegerValue cut_upper_bound = IntegerValue(0);
66 IntegerValue max_coeff = coeffs[0];
68 IntegerValue slack = -upper_bound;
69 for (
int i = 0; i < vars.size(); ++i) {
70 const IntegerValue var_upper_bound =
71 integer_trail.LevelZeroUpperBound(vars[i]);
72 cut_upper_bound += var_upper_bound;
73 cut.vars.push_back(vars[i]);
74 cut.coeffs.push_back(IntegerValue(1));
75 max_coeff =
std::max(max_coeff, coeffs[i]);
76 slack += coeffs[i] * var_upper_bound;
78 CHECK_GT(slack, 0.0) <<
"Invalid cover for knapsack cut.";
79 cut_upper_bound -=
CeilRatio(slack, max_coeff);
81 cut.ub = cut_upper_bound;
82 VLOG(2) <<
"Generated Knapsack Constraint:" << cut.DebugString();
86 bool SolutionSatisfiesConstraint(
87 const LinearConstraint& constraint,
90 const double tolerance = 1e-6;
91 return (activity <= constraint.ub.value() + tolerance &&
92 activity >= constraint.lb.value() - tolerance)
97 bool SmallRangeAndAllCoefficientsMagnitudeAreTheSame(
98 const LinearConstraint& constraint, IntegerTrail* integer_trail) {
99 if (constraint.vars.empty())
return true;
101 const int64 magnitude = std::abs(constraint.coeffs[0].value());
102 for (
int i = 1; i < constraint.coeffs.size(); ++i) {
103 const IntegerVariable
var = constraint.vars[i];
104 if (integer_trail->LevelZeroUpperBound(
var) -
105 integer_trail->LevelZeroLowerBound(
var) >
109 if (std::abs(constraint.coeffs[i].value()) != magnitude) {
116 bool AllVarsTakeIntegerValue(
117 const std::vector<IntegerVariable> vars,
119 for (IntegerVariable
var : vars) {
120 if (std::abs(lp_values[
var] - std::round(lp_values[
var])) > 1e-6) {
136 int GetSmallestCoverSize(
const LinearConstraint& constraint,
137 const IntegerTrail& integer_trail) {
138 IntegerValue ub = constraint.ub;
139 std::vector<IntegerValue> sorted_terms;
140 for (
int i = 0; i < constraint.vars.size(); ++i) {
141 const IntegerValue coeff = constraint.coeffs[i];
142 const IntegerVariable
var = constraint.vars[i];
143 const IntegerValue var_ub = integer_trail.LevelZeroUpperBound(
var);
144 const IntegerValue var_lb = integer_trail.LevelZeroLowerBound(
var);
145 ub -= var_lb * coeff;
146 sorted_terms.push_back(coeff * (var_ub - var_lb));
148 std::sort(sorted_terms.begin(), sorted_terms.end(),
149 std::greater<IntegerValue>());
150 int smallest_cover_size = 0;
151 IntegerValue sorted_term_sum = IntegerValue(0);
152 while (sorted_term_sum <= ub &&
153 smallest_cover_size < constraint.vars.size()) {
154 sorted_term_sum += sorted_terms[smallest_cover_size++];
156 return smallest_cover_size;
159 bool ConstraintIsEligibleForLifting(
const LinearConstraint& constraint,
160 const IntegerTrail& integer_trail) {
161 for (
const IntegerVariable
var : constraint.vars) {
162 if (integer_trail.LevelZeroLowerBound(
var) != IntegerValue(0) ||
163 integer_trail.LevelZeroUpperBound(
var) != IntegerValue(1)) {
174 const std::vector<IntegerValue>& cut_vars_original_coefficients,
177 std::set<IntegerVariable> vars_in_cut;
178 for (IntegerVariable
var : cut->
vars) {
179 vars_in_cut.insert(
var);
182 std::vector<std::pair<IntegerValue, IntegerVariable>> non_zero_vars;
183 std::vector<std::pair<IntegerValue, IntegerVariable>> zero_vars;
184 for (
int i = 0; i < constraint.
vars.size(); ++i) {
185 const IntegerVariable
var = constraint.
vars[i];
190 if (vars_in_cut.find(
var) != vars_in_cut.end())
continue;
191 const IntegerValue coeff = constraint.
coeffs[i];
192 if (lp_values[
var] <= 1e-6) {
193 zero_vars.push_back({coeff,
var});
195 non_zero_vars.push_back({coeff,
var});
201 std::sort(non_zero_vars.rbegin(), non_zero_vars.rend());
202 std::sort(zero_vars.rbegin(), zero_vars.rend());
204 std::vector<std::pair<IntegerValue, IntegerVariable>> lifting_sequence(
205 std::move(non_zero_vars));
207 lifting_sequence.insert(lifting_sequence.end(), zero_vars.begin(),
211 std::vector<double> lifting_profits;
212 std::vector<double> lifting_weights;
213 for (
int i = 0; i < cut->
vars.size(); ++i) {
214 lifting_profits.push_back(cut->
coeffs[i].value());
215 lifting_weights.push_back(cut_vars_original_coefficients[i].
value());
219 bool is_lifted =
false;
220 bool is_solution_optimal =
false;
222 for (
auto entry : lifting_sequence) {
223 is_solution_optimal =
false;
224 const IntegerValue var_original_coeff = entry.first;
225 const IntegerVariable
var = entry.second;
226 const IntegerValue lifting_capacity = constraint.
ub - entry.first;
227 if (lifting_capacity <= IntegerValue(0))
continue;
228 knapsack_solver.
Init(lifting_profits, lifting_weights,
229 lifting_capacity.value());
235 const double knapsack_upper_bound =
237 const IntegerValue cut_coeff = cut->
ub - knapsack_upper_bound;
238 if (cut_coeff > IntegerValue(0)) {
241 cut->
coeffs.push_back(cut_coeff);
242 lifting_profits.push_back(cut_coeff.value());
243 lifting_weights.push_back(var_original_coeff.value());
253 IntegerValue ub = constraint.
ub;
255 for (
int i = 0; i < constraint.
vars.size(); ++i) {
256 const IntegerVariable
var = constraint.
vars[i];
258 const IntegerValue coeff = constraint.
coeffs[i];
259 if (var_ub.value() - lp_values[
var] <= 1.0 - kMinCutViolation) {
260 constraint_with_left_vars.
vars.push_back(
var);
261 constraint_with_left_vars.
coeffs.push_back(coeff);
265 ub -= coeff * var_lb;
268 constraint_with_left_vars.
ub = ub;
269 constraint_with_left_vars.
lb = constraint.
lb;
270 return constraint_with_left_vars;
275 IntegerValue term_sum = IntegerValue(0);
276 for (
int i = 0; i < constraint.
vars.size(); ++i) {
277 const IntegerVariable
var = constraint.
vars[i];
279 const IntegerValue coeff = constraint.
coeffs[i];
280 term_sum += coeff * var_ub;
282 if (term_sum <= constraint.
ub) {
283 VLOG(2) <<
"Filtered by cover filter";
293 std::vector<double> variable_upper_bound_distances;
294 for (
const IntegerVariable
var : preprocessed_constraint.
vars) {
296 variable_upper_bound_distances.push_back(var_ub.value() - lp_values[
var]);
299 const int smallest_cover_size =
300 GetSmallestCoverSize(preprocessed_constraint, integer_trail);
303 variable_upper_bound_distances.begin(),
304 variable_upper_bound_distances.begin() + smallest_cover_size - 1,
305 variable_upper_bound_distances.end());
306 double cut_lower_bound = 0.0;
307 for (
int i = 0; i < smallest_cover_size; ++i) {
308 cut_lower_bound += variable_upper_bound_distances[i];
310 if (cut_lower_bound >= 1.0 - kMinCutViolation) {
311 VLOG(2) <<
"Filtered by kappa heuristic";
320 std::sort(items.begin(), items.end(), std::greater<KnapsackItem>());
324 if (item.weight <= left_capacity) {
325 profit += item.profit;
326 left_capacity -= item.weight;
328 profit += (left_capacity / item.weight) * item.profit;
339 std::vector<KnapsackItem> items;
340 double capacity = -constraint.
ub.value() - 1.0;
341 double sum_variable_profit = 0;
342 for (
int i = 0; i < constraint.
vars.size(); ++i) {
343 const IntegerVariable
var = constraint.
vars[i];
346 const IntegerValue coeff = constraint.
coeffs[i];
348 item.
profit = var_ub.value() - lp_values[
var];
349 item.
weight = (coeff * (var_ub - var_lb)).value();
350 items.push_back(item);
352 sum_variable_profit += item.
profit;
357 if (sum_variable_profit - 1.0 + kMinCutViolation < 0.0)
return false;
360 const double knapsack_upper_bound =
362 if (knapsack_upper_bound < sum_variable_profit - 1.0 + kMinCutViolation) {
363 VLOG(2) <<
"Filtered by knapsack upper bound";
388 std::vector<LinearConstraint>* knapsack_constraints,
394 if (SmallRangeAndAllCoefficientsMagnitudeAreTheSame(constraint,
402 for (
int i = 0; i < constraint.
vars.size(); ++i) {
403 const IntegerVariable
var = constraint.
vars[i];
404 const IntegerValue coeff = constraint.
coeffs[i];
405 if (coeff > IntegerValue(0)) {
411 canonical_knapsack_form.
ub = constraint.
ub;
413 knapsack_constraints->push_back(canonical_knapsack_form);
420 for (
int i = 0; i < constraint.
vars.size(); ++i) {
421 const IntegerVariable
var = constraint.
vars[i];
422 const IntegerValue coeff = constraint.
coeffs[i];
423 if (coeff > IntegerValue(0)) {
429 canonical_knapsack_form.
ub = -constraint.
lb;
431 knapsack_constraints->push_back(canonical_knapsack_form);
437 const std::vector<LinearConstraint>& base_constraints,
438 const std::vector<IntegerVariable>& vars,
Model*
model) {
443 std::vector<LinearConstraint> knapsack_constraints;
451 if (constraint.vars.size() <= 2)
continue;
455 VLOG(1) <<
"#knapsack constraints: " << knapsack_constraints.size();
465 result.
generate_cuts = [implied_bounds_processor, knapsack_constraints, vars,
466 model, integer_trail](
473 if (AllVarsTakeIntegerValue(vars, lp_values))
return;
476 "Knapsack on demand cover cut generator");
477 int64 skipped_constraints = 0;
484 VLOG(2) <<
"Processing constraint: " << constraint.DebugString();
486 mutable_constraint = constraint;
488 lp_values, &mutable_constraint);
494 if (preprocessed_constraint.
vars.empty())
continue;
498 skipped_constraints++;
503 std::vector<double> profits;
504 profits.reserve(preprocessed_constraint.
vars.size());
507 std::vector<double> weights;
508 weights.reserve(preprocessed_constraint.
vars.size());
510 double capacity = -preprocessed_constraint.
ub.value() - 1.0;
516 double sum_variable_profit = 0;
520 for (
int i = 0; i < preprocessed_constraint.
vars.size(); ++i) {
521 const IntegerVariable
var = preprocessed_constraint.
vars[i];
525 const double variable_profit = var_ub - lp_values[
var];
526 profits.push_back(variable_profit);
528 sum_variable_profit += variable_profit;
531 weights.push_back(
weight);
536 std::vector<IntegerVariable> cut_vars;
537 std::vector<IntegerValue> cut_vars_original_coefficients;
539 VLOG(2) <<
"Knapsack size: " << profits.size();
543 const double time_limit_for_knapsack_solver =
549 bool is_solution_optimal =
false;
551 sum_variable_profit - 1.0 + kMinCutViolation);
555 auto time_limit_for_solver =
556 absl::make_unique<TimeLimit>(time_limit_for_knapsack_solver);
557 const double sum_of_distance_to_ub_for_vars_in_cover =
558 sum_variable_profit -
559 knapsack_solver.
Solve(time_limit_for_solver.get(),
560 &is_solution_optimal);
561 if (is_solution_optimal) {
562 VLOG(2) <<
"Knapsack Optimal solution found yay !";
564 if (time_limit_for_solver->LimitReached()) {
565 VLOG(1) <<
"Knapsack Solver run out of time limit.";
567 if (sum_of_distance_to_ub_for_vars_in_cover < 1.0 - kMinCutViolation) {
570 IntegerValue constraint_ub_for_cut = preprocessed_constraint.
ub;
571 std::set<IntegerVariable> vars_in_cut;
572 for (
int i = 0; i < preprocessed_constraint.
vars.size(); ++i) {
573 const IntegerVariable
var = preprocessed_constraint.
vars[i];
576 cut_vars.push_back(
var);
577 cut_vars_original_coefficients.push_back(
coefficient);
578 vars_in_cut.insert(
var);
585 cut_vars, cut_vars_original_coefficients, constraint_ub_for_cut,
589 bool is_lifted =
false;
590 if (ConstraintIsEligibleForLifting(cut, *integer_trail)) {
592 cut_vars_original_coefficients, *integer_trail,
598 CHECK(!SolutionSatisfiesConstraint(cut, lp_values));
599 manager->AddCut(cut, is_lifted ?
"LiftedKnapsack" :
"Knapsack",
603 if (skipped_constraints > 0) {
604 VLOG(2) <<
"Skipped constraints: " << skipped_constraints;
615 IntegerValue
GetFactorT(IntegerValue rhs_remainder, IntegerValue divisor,
616 IntegerValue max_t) {
618 return rhs_remainder == 0
624 IntegerValue rhs_remainder, IntegerValue divisor, IntegerValue t,
625 IntegerValue max_scaling) {
637 const IntegerValue size = divisor - rhs_remainder;
638 if (max_scaling == 1 || size == 1) {
642 return [t, divisor](IntegerValue coeff) {
645 }
else if (size <= max_scaling) {
646 return [size, rhs_remainder, t, divisor](IntegerValue coeff) {
648 const IntegerValue remainder = t * coeff -
ratio * divisor;
649 const IntegerValue diff = remainder - rhs_remainder;
652 }
else if (max_scaling.value() * rhs_remainder.value() < divisor) {
662 return [t, divisor, max_scaling](IntegerValue coeff) {
664 const IntegerValue remainder = t * coeff -
ratio * divisor;
665 const IntegerValue bucket =
FloorRatio(remainder * max_scaling, divisor);
666 return max_scaling *
ratio + bucket;
691 return [size, rhs_remainder, t, divisor, max_scaling](IntegerValue coeff) {
693 const IntegerValue remainder = t * coeff -
ratio * divisor;
694 const IntegerValue diff = remainder - rhs_remainder;
695 const IntegerValue bucket =
696 diff > 0 ?
CeilRatio(diff * (max_scaling - 1), size)
698 return max_scaling *
ratio + bucket;
711 const int size = lp_values.size();
712 if (size == 0)
return;
725 relevant_indices_.clear();
726 relevant_lp_values_.clear();
727 relevant_coeffs_.clear();
728 relevant_bound_diffs_.clear();
730 adjusted_coeffs_.clear();
733 IntegerValue max_magnitude(0);
734 for (
int i = 0; i < size; ++i) {
737 max_magnitude =
std::max(max_magnitude, magnitude);
743 bool overflow =
false;
744 change_sign_at_postprocessing_.assign(size,
false);
745 for (
int i = 0; i < size; ++i) {
746 if (cut->
coeffs[i] == 0)
continue;
751 double lp_value = lp_values[i];
754 const IntegerValue bound_diff =
755 IntegerValue(
CapSub(ub.value(), lb.value()));
768 const double lb_dist = std::abs(lp_value -
ToDouble(lb));
769 const double ub_dist = std::abs(lp_value -
ToDouble(ub));
772 if ((bias * lb_dist > ub_dist && cut->
coeffs[i] < 0) ||
773 (lb_dist > bias * ub_dist && cut->
coeffs[i] > 0)) {
774 change_sign_at_postprocessing_[i] =
true;
776 lp_value = -lp_value;
791 if (bound_diff == 0) {
792 cut->
coeffs[i] = IntegerValue(0);
796 if (std::abs(lp_value) > 1e-2) {
797 relevant_coeffs_.push_back(cut->
coeffs[i]);
798 relevant_indices_.push_back(i);
799 relevant_lp_values_.push_back(lp_value);
800 relevant_bound_diffs_.push_back(bound_diff);
801 divisors_.push_back(magnitude);
806 if (relevant_coeffs_.empty()) {
807 VLOG(2) <<
"Issue, nothing to cut.";
827 double best_scaled_violation = 0.01;
828 const IntegerValue remainder_threshold(max_magnitude / 1000);
839 if (overflow || max_magnitude >= threshold) {
840 VLOG(2) <<
"Issue, overflow.";
844 const IntegerValue max_t = threshold / max_magnitude;
855 const IntegerValue divisor_threshold = max_magnitude / 10;
856 for (
int i = 0; i < divisors_.size(); ++i) {
857 if (divisors_[i] <= divisor_threshold)
continue;
858 divisors_[new_size++] = divisors_[i];
860 divisors_.resize(new_size);
867 IntegerValue best_divisor(0);
868 for (
const IntegerValue divisor : divisors_) {
870 const IntegerValue initial_rhs_remainder =
872 if (initial_rhs_remainder <= remainder_threshold)
continue;
874 IntegerValue temp_ub = cut->
ub;
875 adjusted_coeffs_.clear();
892 const IntegerValue adjust_threshold =
893 (divisor - initial_rhs_remainder - 1) / IntegerValue(size);
894 if (adjust_threshold > 0) {
898 bool early_abort =
false;
899 double loss_lb = 0.0;
900 const double threshold =
ToDouble(initial_rhs_remainder);
902 for (
int i = 0; i < relevant_coeffs_.size(); ++i) {
904 const IntegerValue coeff = relevant_coeffs_[i];
905 const IntegerValue remainder =
906 CeilRatio(coeff, divisor) * divisor - coeff;
908 if (divisor - remainder <= initial_rhs_remainder) {
911 loss_lb +=
ToDouble(divisor - remainder) * relevant_lp_values_[i];
912 if (loss_lb >= threshold) {
919 const IntegerValue diff = relevant_bound_diffs_[i];
920 if (remainder > 0 && remainder <= adjust_threshold &&
921 CapProd(diff.value(), remainder.value()) <= adjust_threshold) {
922 temp_ub += remainder * diff;
923 adjusted_coeffs_.push_back({i, coeff + remainder});
927 if (early_abort)
continue;
931 const IntegerValue rhs_remainder =
932 temp_ub -
FloorRatio(temp_ub, divisor) * divisor;
933 if (rhs_remainder == 0)
continue;
936 rhs_remainder, divisor,
GetFactorT(rhs_remainder, divisor, max_t),
947 const double threshold = scaling *
ToDouble(rhs_remainder);
954 double violation = -
ToDouble(f(temp_ub));
955 double l2_norm = 0.0;
956 bool early_abort =
false;
957 int adjusted_coeffs_index = 0;
958 for (
int i = 0; i < relevant_coeffs_.size(); ++i) {
959 IntegerValue coeff = relevant_coeffs_[i];
962 if (adjusted_coeffs_index < adjusted_coeffs_.size() &&
963 adjusted_coeffs_[adjusted_coeffs_index].first == i) {
964 coeff = adjusted_coeffs_[adjusted_coeffs_index].second;
965 adjusted_coeffs_index++;
968 if (coeff == 0)
continue;
969 const IntegerValue new_coeff = f(coeff);
970 const double new_coeff_double =
ToDouble(new_coeff);
971 const double lp_value = relevant_lp_values_[i];
973 l2_norm += new_coeff_double * new_coeff_double;
974 violation += new_coeff_double * lp_value;
975 loss += (scaling *
ToDouble(coeff) - new_coeff_double) * lp_value;
976 if (loss >= threshold) {
981 if (early_abort)
continue;
985 violation /= sqrt(l2_norm);
986 if (violation > best_scaled_violation) {
987 best_scaled_violation = violation;
988 best_divisor = divisor;
992 if (best_divisor == 0) {
1002 const IntegerValue initial_rhs_remainder =
1004 const IntegerValue adjust_threshold =
1005 (best_divisor - initial_rhs_remainder - 1) / IntegerValue(size);
1006 if (adjust_threshold > 0) {
1007 for (
int i = 0; i < relevant_indices_.size(); ++i) {
1008 const int index = relevant_indices_[i];
1009 const IntegerValue diff = relevant_bound_diffs_[i];
1010 if (diff > adjust_threshold)
continue;
1014 const IntegerValue remainder =
1015 CeilRatio(coeff, best_divisor) * best_divisor - coeff;
1016 if (
CapProd(diff.value(), remainder.value()) <= adjust_threshold) {
1017 cut->
ub += remainder * diff;
1030 const IntegerValue rhs_remainder =
1032 IntegerValue factor_t =
GetFactorT(rhs_remainder, best_divisor, max_t);
1039 remainders_.clear();
1040 for (
int i = 0; i < size; ++i) {
1041 const IntegerValue coeff = cut->
coeffs[i];
1042 const IntegerValue r =
1043 coeff -
FloorRatio(coeff, best_divisor) * best_divisor;
1044 if (r > rhs_remainder) remainders_.push_back(r);
1047 if (remainders_.size() <= 100) {
1049 for (
const IntegerValue r : remainders_) {
1050 best_rs_.push_back(f(r));
1052 IntegerValue best_d = f(best_divisor);
1057 for (
const IntegerValue t :
1058 {IntegerValue(1),
GetFactorT(rhs_remainder, best_divisor, max_t)}) {
1059 for (IntegerValue s(2); s <= options.
max_scaling; ++s) {
1062 int num_strictly_better = 0;
1064 const IntegerValue d = g(best_divisor);
1065 for (
int i = 0; i < best_rs_.size(); ++i) {
1066 const IntegerValue temp = g(remainders_[i]);
1067 if (temp * best_d < best_rs_[i] * d)
break;
1068 if (temp * best_d > best_rs_[i] * d) num_strictly_better++;
1069 rs_.push_back(temp);
1071 if (rs_.size() == best_rs_.size() && num_strictly_better > 0) {
1083 cut->
ub = f(cut->
ub);
1088 num_lifted_booleans_ = 0;
1089 if (ib_processor !=
nullptr) {
1090 for (
int i = 0; i < size; ++i) {
1091 const IntegerValue coeff = cut->
coeffs[i];
1092 if (coeff == 0)
continue;
1094 IntegerVariable
var = cut->
vars[i];
1095 if (change_sign_at_postprocessing_[i]) {
1113 const IntegerValue coeff_b =
1116 if (coeff_b == 0)
continue;
1118 ++num_lifted_booleans_;
1120 tmp_terms_.push_back({info.
bool_var, coeff_b});
1122 tmp_terms_.push_back({info.
bool_var, -coeff_b});
1123 cut->
ub =
CapAdd(-coeff_b.value(), cut->
ub.value());
1132 for (
int i = 0; i < size; ++i) {
1133 IntegerValue coeff = cut->
coeffs[i];
1134 if (coeff == 0)
continue;
1136 if (coeff == 0)
continue;
1137 if (change_sign_at_postprocessing_[i]) {
1138 cut->
ub = IntegerValue(
1140 tmp_terms_.push_back({cut->
vars[i], -coeff});
1142 cut->
ub = IntegerValue(
1144 tmp_terms_.push_back({cut->
vars[i], coeff});
1158 const int base_size = lp_values.size();
1164 IntegerValue rhs = base_ct.
ub;
1165 IntegerValue sum_of_diff(0);
1166 IntegerValue max_base_magnitude(0);
1167 for (
int i = 0; i < base_size; ++i) {
1168 const IntegerValue coeff = base_ct.
coeffs[i];
1169 const IntegerValue positive_coeff =
IntTypeAbs(coeff);
1170 max_base_magnitude =
std::max(max_base_magnitude, positive_coeff);
1172 if (!
AddProductTo(positive_coeff, bound_diff, &sum_of_diff)) {
1175 const IntegerValue diff = positive_coeff * bound_diff;
1189 double activity = 0.0;
1191 std::sort(terms_.begin(), terms_.end(), [](
const Term&
a,
const Term&
b) {
1192 if (a.dist_to_max_value == b.dist_to_max_value) {
1194 return a.positive_coeff < b.positive_coeff;
1196 return a.dist_to_max_value <
b.dist_to_max_value;
1198 for (
int i = 0; i < terms_.size(); ++i) {
1199 const Term& term = terms_[i];
1200 activity += term.dist_to_max_value;
1209 if (activity > 1.0) {
1224 if (rhs >= 0)
return false;
1225 if (new_size == 0)
return false;
1233 terms_.resize(new_size);
1234 std::sort(terms_.begin(), terms_.end(), [](
const Term&
a,
const Term&
b) {
1235 if (a.positive_coeff == b.positive_coeff) {
1236 return a.dist_to_max_value > b.dist_to_max_value;
1238 return a.positive_coeff >
b.positive_coeff;
1240 in_cut_.assign(base_ct.vars.size(),
false);
1243 cut_.ub = IntegerValue(-1);
1244 IntegerValue max_coeff(0);
1245 for (
const Term term : terms_) {
1246 if (term.diff + rhs < 0) {
1250 in_cut_[term.index] =
true;
1251 max_coeff =
std::max(max_coeff, term.positive_coeff);
1252 cut_.vars.push_back(base_ct.vars[term.index]);
1253 if (base_ct.coeffs[term.index] > 0) {
1254 cut_.coeffs.push_back(IntegerValue(1));
1257 cut_.coeffs.push_back(IntegerValue(-1));
1266 if (max_coeff == 0)
return true;
1267 if (max_coeff < -rhs) {
1268 const IntegerValue m =
FloorRatio(-rhs - 1, max_coeff);
1269 rhs += max_coeff * m;
1288 const IntegerValue slack = -rhs;
1289 const IntegerValue remainder = max_coeff - slack;
1291 const IntegerValue max_scaling(
std::min(
1294 IntegerValue(1), max_scaling);
1296 const IntegerValue scaling = f(max_coeff);
1298 for (
int i = 0; i < cut_.coeffs.size(); ++i) cut_.coeffs[i] *= scaling;
1303 for (
int i = 0; i < base_size; ++i) {
1304 if (in_cut_[i])
continue;
1305 const IntegerValue positive_coeff =
IntTypeAbs(base_ct.coeffs[i]);
1306 const IntegerValue new_coeff = f(positive_coeff);
1307 if (new_coeff == 0)
continue;
1310 if (base_ct.coeffs[i] > 0) {
1312 cut_.coeffs.push_back(new_coeff);
1313 cut_.vars.push_back(base_ct.vars[i]);
1317 cut_.coeffs.push_back(-new_coeff);
1318 cut_.vars.push_back(base_ct.vars[i]);
1332 result.
vars = {z, x, y};
1336 [z, x, y, integer_trail](
1345 const int64 kMaxSafeInteger = (
int64{1} << 53) - 1;
1347 if (
CapProd(x_ub, y_ub) >= kMaxSafeInteger) {
1348 VLOG(3) <<
"Potential overflow in PositiveMultiplicationCutGenerator";
1352 const double x_lp_value = lp_values[x];
1353 const double y_lp_value = lp_values[y];
1354 const double z_lp_value = lp_values[z];
1362 auto try_add_above_cut = [manager, z_lp_value, x_lp_value, y_lp_value,
1363 x, y, z, &lp_values](
1365 if (-z_lp_value + x_lp_value * x_coeff + y_lp_value * y_coeff >=
1366 rhs + kMinCutViolation) {
1368 cut.
vars.push_back(z);
1369 cut.
coeffs.push_back(IntegerValue(-1));
1371 cut.
vars.push_back(x);
1372 cut.
coeffs.push_back(IntegerValue(x_coeff));
1375 cut.
vars.push_back(y);
1376 cut.
coeffs.push_back(IntegerValue(y_coeff));
1379 cut.
ub = IntegerValue(rhs);
1380 manager->AddCut(cut,
"PositiveProduct", lp_values);
1385 auto try_add_below_cut = [manager, z_lp_value, x_lp_value, y_lp_value,
1386 x, y, z, &lp_values](
1388 if (-z_lp_value + x_lp_value * x_coeff + y_lp_value * y_coeff <=
1389 rhs - kMinCutViolation) {
1391 cut.
vars.push_back(z);
1392 cut.
coeffs.push_back(IntegerValue(-1));
1394 cut.
vars.push_back(x);
1395 cut.
coeffs.push_back(IntegerValue(x_coeff));
1398 cut.
vars.push_back(y);
1399 cut.
coeffs.push_back(IntegerValue(y_coeff));
1401 cut.
lb = IntegerValue(rhs);
1403 manager->AddCut(cut,
"PositiveProduct", lp_values);
1414 try_add_above_cut(y_lb, x_lb, x_lb * y_lb);
1415 try_add_above_cut(y_ub, x_ub, x_ub * y_ub);
1416 try_add_below_cut(y_ub, x_lb, x_lb * y_ub);
1417 try_add_below_cut(y_lb, x_ub, x_ub * y_lb);
1426 result.
vars = {y, x};
1430 [y, x, integer_trail](
1436 if (x_lb == x_ub)
return;
1439 if (x_ub > (
int64{1} << 31))
return;
1442 const double y_lp_value = lp_values[y];
1443 const double x_lp_value = lp_values[x];
1448 const int64 y_lb = x_lb * x_lb;
1449 const int64 above_slope = x_ub + x_lb;
1450 const double max_lp_y = y_lb + above_slope * (x_lp_value - x_lb);
1451 if (y_lp_value >= max_lp_y + kMinCutViolation) {
1454 above_cut.
vars.push_back(y);
1455 above_cut.
coeffs.push_back(IntegerValue(1));
1456 above_cut.
vars.push_back(x);
1457 above_cut.
coeffs.push_back(IntegerValue(-above_slope));
1459 above_cut.
ub = IntegerValue(-x_lb * x_ub);
1460 manager->AddCut(above_cut,
"SquareUpper", lp_values);
1469 const int64 x_floor =
static_cast<int64>(std::floor(x_lp_value));
1470 const int64 below_slope = 2 * x_floor + 1;
1471 const double min_lp_y =
1472 below_slope * x_lp_value - x_floor - x_floor * x_floor;
1473 if (min_lp_y >= y_lp_value + kMinCutViolation) {
1477 below_cut.
vars.push_back(y);
1478 below_cut.
coeffs.push_back(IntegerValue(1));
1479 below_cut.
vars.push_back(x);
1480 below_cut.
coeffs.push_back(-IntegerValue(below_slope));
1481 below_cut.
lb = IntegerValue(-x_floor - x_floor * x_floor);
1483 manager->AddCut(below_cut,
"SquareLower", lp_values);
1490 void ImpliedBoundsProcessor::ProcessUpperBoundedConstraint(
1493 ProcessUpperBoundedConstraintWithSlackCreation(
1494 false, IntegerVariable(0), lp_values,
1499 ImpliedBoundsProcessor::GetCachedImpliedBoundInfo(IntegerVariable
var) {
1500 auto it = cache_.find(
var);
1501 if (it != cache_.end())
return it->second;
1506 ImpliedBoundsProcessor::ComputeBestImpliedBound(
1507 IntegerVariable
var,
1509 auto it = cache_.find(
var);
1510 if (it != cache_.end())
return it->second;
1511 BestImpliedBoundInfo result;
1512 const IntegerValue lb = integer_trail_->LevelZeroLowerBound(
var);
1514 implied_bounds_->GetImpliedBounds(
var)) {
1526 const IntegerValue diff = entry.lower_bound - lb;
1528 const double bool_lp_value = entry.is_positive
1529 ? lp_values[entry.literal_view]
1530 : 1.0 - lp_values[entry.literal_view];
1531 const double slack_lp_value =
1536 if (slack_lp_value < -1e-4) {
1537 LinearConstraint ib_cut;
1539 std::vector<std::pair<IntegerVariable, IntegerValue>> terms;
1540 if (entry.is_positive) {
1542 terms.push_back({entry.literal_view, diff});
1543 terms.push_back({
var, IntegerValue(-1)});
1547 terms.push_back({entry.literal_view, -diff});
1548 terms.push_back({
var, IntegerValue(-1)});
1549 ib_cut.ub = -entry.lower_bound;
1552 ib_cut_pool_.AddCut(std::move(ib_cut),
"IB", lp_values);
1558 if (slack_lp_value + 1e-4 < result.slack_lp_value ||
1559 (slack_lp_value < result.slack_lp_value + 1e-4 &&
1560 diff > result.bound_diff)) {
1561 result.bool_lp_value = bool_lp_value;
1562 result.slack_lp_value = slack_lp_value;
1564 result.bound_diff = diff;
1565 result.is_positive = entry.is_positive;
1566 result.bool_var = entry.literal_view;
1569 cache_[
var] = result;
1574 void ImpliedBoundsProcessor::SeparateSomeImpliedBoundCuts(
1576 for (
const IntegerVariable
var :
1577 implied_bounds_->VariablesWithImpliedBounds()) {
1579 ComputeBestImpliedBound(
var, lp_values);
1583 void ImpliedBoundsProcessor::ProcessUpperBoundedConstraintWithSlackCreation(
1584 bool substitute_only_inner_variables, IntegerVariable first_slack,
1588 IntegerValue new_ub = cut->
ub;
1589 bool changed =
false;
1592 int64 overflow_detection = 0;
1594 const int size = cut->
vars.size();
1595 for (
int i = 0; i < size; ++i) {
1596 IntegerVariable
var = cut->
vars[i];
1597 IntegerValue coeff = cut->
coeffs[i];
1619 const int old_size = tmp_terms_.size();
1622 bool keep_term =
false;
1636 if (substitute_only_inner_variables) {
1637 const IntegerValue lb = integer_trail_->LevelZeroLowerBound(
var);
1638 const IntegerValue ub = integer_trail_->LevelZeroUpperBound(
var);
1639 if (lp_values[
var] -
ToDouble(lb) < 1e-2) keep_term =
true;
1640 if (
ToDouble(ub) - lp_values[
var] < 1e-2) keep_term =
true;
1644 if (slack_infos ==
nullptr) {
1651 tmp_terms_.push_back({
var, coeff});
1654 const IntegerValue lb = integer_trail_->LevelZeroLowerBound(
var);
1655 const IntegerValue ub = integer_trail_->LevelZeroUpperBound(
var);
1660 slack_info.
ub = ub - lb;
1666 VLOG(2) <<
"Overflow";
1669 if (slack_infos !=
nullptr) {
1670 tmp_terms_.push_back({first_slack, coeff});
1674 slack_info.
terms.push_back({
var, IntegerValue(1)});
1677 slack_infos->push_back(slack_info);
1684 VLOG(2) <<
"Overflow";
1687 if (slack_infos !=
nullptr) {
1688 tmp_terms_.push_back({first_slack, coeff});
1692 slack_info.
terms.push_back({
var, IntegerValue(1)});
1695 slack_infos->push_back(slack_info);
1703 for (
int i = old_size; i < tmp_terms_.size(); ++i) {
1704 overflow_detection =
1705 CapAdd(overflow_detection, std::abs(tmp_terms_[i].second.value()));
1710 VLOG(2) <<
"Overflow";
1713 if (!changed)
return;
1724 bool ImpliedBoundsProcessor::DebugSlack(IntegerVariable first_slack,
1727 const std::vector<SlackInfo>& info) {
1729 IntegerValue new_ub = cut.
ub;
1730 for (
int i = 0; i < cut.
vars.size(); ++i) {
1732 if (cut.
vars[i] < first_slack) {
1733 tmp_terms_.push_back({cut.
vars[i], cut.
coeffs[i]});
1738 const IntegerValue multiplier = cut.
coeffs[i];
1739 const int index = (cut.
vars[i].value() - first_slack.value()) / 2;
1740 for (
const std::pair<IntegerVariable, IntegerValue>& term :
1741 info[
index].terms) {
1742 tmp_terms_.push_back({term.first, term.second * multiplier});
1744 new_ub -= multiplier * info[
index].offset;
1749 tmp_cut.
ub = new_ub;
1759 for (
int i = 0; i < initial_cut.
vars.size(); ++i) {
1760 tmp_terms_.push_back({initial_cut.
vars[i], initial_cut.
coeffs[i]});
1763 tmp_copy.
ub = new_ub;
1767 if (tmp_cut == tmp_copy)
return true;
1778 void TryToGenerateAllDiffCut(
1779 const std::vector<std::pair<double, IntegerVariable>>& sorted_vars_lp,
1784 std::vector<IntegerVariable> current_set_vars;
1786 for (
auto value_var : sorted_vars_lp) {
1787 sum += value_var.first;
1788 const IntegerVariable
var = value_var.second;
1793 current_set_vars.push_back(
var);
1794 const int64 required_min_sum =
1796 const int64 required_max_sum =
1798 if (sum < required_min_sum || sum > required_max_sum) {
1800 for (IntegerVariable
var : current_set_vars) {
1803 cut.
lb = IntegerValue(required_min_sum);
1804 cut.
ub = IntegerValue(required_max_sum);
1805 manager->
AddCut(cut,
"all_diff", lp_values);
1809 current_set_vars.clear();
1810 current_union =
Domain();
1818 const std::vector<IntegerVariable>& vars,
Model*
model) {
1824 [vars, integer_trail, trail](
1830 if (trail->CurrentDecisionLevel() > 0)
return;
1831 std::vector<std::pair<double, IntegerVariable>> sorted_vars;
1832 for (
const IntegerVariable
var : vars) {
1833 if (integer_trail->LevelZeroLowerBound(
var) ==
1834 integer_trail->LevelZeroUpperBound(
var)) {
1837 sorted_vars.push_back(std::make_pair(lp_values[
var],
var));
1839 std::sort(sorted_vars.begin(), sorted_vars.end());
1840 TryToGenerateAllDiffCut(sorted_vars, *integer_trail, lp_values,
1843 std::reverse(sorted_vars.begin(), sorted_vars.end());
1844 TryToGenerateAllDiffCut(sorted_vars, *integer_trail, lp_values,
1847 VLOG(1) <<
"Created all_diff cut generator of size: " << vars.size();
1853 IntegerValue MaxCornerDifference(
const IntegerVariable
var,
1854 const IntegerValue w1_i,
1855 const IntegerValue w2_i,
1856 const IntegerTrail& integer_trail) {
1857 const IntegerValue lb = integer_trail.LevelZeroLowerBound(
var);
1858 const IntegerValue ub = integer_trail.LevelZeroUpperBound(
var);
1859 return std::max((w2_i - w1_i) * lb, (w2_i - w1_i) * ub);
1868 IntegerValue MPlusCoefficient(
1869 const std::vector<IntegerVariable>& x_vars,
1870 const std::vector<LinearExpression>& exprs,
1872 const int max_index,
const IntegerTrail& integer_trail) {
1873 IntegerValue coeff = exprs[max_index].offset;
1876 for (
const IntegerVariable
var : x_vars) {
1877 const int target_index = variable_partition[
var];
1878 if (max_index != target_index) {
1879 coeff += MaxCornerDifference(
1890 double ComputeContribution(
1891 const IntegerVariable xi_var,
const std::vector<IntegerVariable>& z_vars,
1892 const std::vector<LinearExpression>& exprs,
1894 const IntegerTrail& integer_trail,
const int target_index) {
1896 CHECK_LT(target_index, exprs.size());
1897 const LinearExpression& target_expr = exprs[target_index];
1898 const double xi_value = lp_values[xi_var];
1900 double contrib = wt_i.value() * xi_value;
1901 for (
int expr_index = 0; expr_index < exprs.size(); ++expr_index) {
1902 if (expr_index == target_index)
continue;
1903 const LinearExpression& max_expr = exprs[expr_index];
1904 const double z_max_value = lp_values[z_vars[expr_index]];
1905 const IntegerValue corner_value = MaxCornerDifference(
1908 contrib += corner_value.value() * z_max_value;
1915 const IntegerVariable target,
const std::vector<LinearExpression>& exprs,
1916 const std::vector<IntegerVariable>& z_vars,
Model*
model) {
1918 std::vector<IntegerVariable> x_vars;
1919 result.
vars = {target};
1920 const int num_exprs = exprs.size();
1921 for (
int i = 0; i < num_exprs; ++i) {
1922 result.
vars.push_back(z_vars[i]);
1923 x_vars.insert(x_vars.end(), exprs[i].vars.begin(), exprs[i].vars.end());
1927 DCHECK(std::all_of(x_vars.begin(), x_vars.end(), [](IntegerVariable
var) {
1928 return VariableIsPositive(var);
1930 result.
vars.insert(result.
vars.end(), x_vars.begin(), x_vars.end());
1934 [x_vars, z_vars, target, num_exprs, exprs, integer_trail,
model](
1938 lp_values.size(), -1);
1940 lp_values.size(), std::numeric_limits<double>::infinity());
1941 for (
int expr_index = 0; expr_index < num_exprs; ++expr_index) {
1942 for (
const IntegerVariable
var : x_vars) {
1943 const double contribution = ComputeContribution(
1944 var, z_vars, exprs, lp_values, *integer_trail, expr_index);
1945 const double prev_contribution = variable_partition_contrib[
var];
1946 if (contribution < prev_contribution) {
1947 variable_partition[
var] = expr_index;
1948 variable_partition_contrib[
var] = contribution;
1955 double violation = lp_values[target];
1956 cut.
AddTerm(target, IntegerValue(-1));
1958 for (
const IntegerVariable xi_var : x_vars) {
1959 const int input_index = variable_partition[xi_var];
1962 if (coeff != IntegerValue(0)) {
1965 violation -= coeff.value() * lp_values[xi_var];
1967 for (
int expr_index = 0; expr_index < num_exprs; ++expr_index) {
1968 const IntegerVariable z_var = z_vars[expr_index];
1969 const IntegerValue z_coeff = MPlusCoefficient(
1970 x_vars, exprs, variable_partition, expr_index, *integer_trail);
1971 if (z_coeff != IntegerValue(0)) {
1974 violation -= z_coeff.value() * lp_values[z_var];
1976 if (violation > 1e-2) {
1977 manager->
AddCut(cut.
Build(),
"LinMax", lp_values);
1985 std::vector<IntegerVariable>* vars) {
1987 for (
int t = 0; t < helper->
NumTasks(); ++t) {
1989 vars->push_back(helper->
Starts()[t].var);
1992 vars->push_back(helper->
Sizes()[t].var);
1995 vars->push_back(helper->
Ends()[t].var);
2006 vars->push_back(direct_view);
2017 LinearConstraintManager*)>
2020 const std::vector<IntegerVariable>& demands,
2026 return [
capacity, demands, trail, integer_trail, helper,
model, cut_name,
2029 if (trail->CurrentDecisionLevel() > 0)
return;
2031 const auto demand_is_fixed = [integer_trail, &demands](
int i) {
2032 return demands.empty() || integer_trail->IsFixed(demands[i]);
2034 const auto demand_min = [integer_trail, &demands](
int i) {
2035 return demands.empty() ? IntegerValue(1)
2036 : integer_trail->LowerBound(demands[i]);
2038 const auto demand_max = [integer_trail, &demands](
int i) {
2039 return demands.empty() ? IntegerValue(1)
2040 : integer_trail->UpperBound(demands[i]);
2043 std::vector<int> active_intervals;
2044 for (
int i = 0; i < helper->NumTasks(); ++i) {
2045 if (!helper->IsAbsent(i) && demand_max(i) > 0 && helper->SizeMin(i) > 0) {
2046 active_intervals.push_back(i);
2050 if (active_intervals.size() < 2)
return;
2052 std::sort(active_intervals.begin(), active_intervals.end(),
2053 [helper](
int a,
int b) {
2054 return helper->StartMin(a) < helper->StartMin(b) ||
2055 (helper->StartMin(a) == helper->StartMin(b) &&
2056 helper->EndMax(a) < helper->EndMax(b));
2059 const IntegerValue capacity_max = integer_trail->UpperBound(
capacity);
2061 for (
int i1 = 0; i1 + 1 < active_intervals.size(); ++i1) {
2062 const int start_index = active_intervals[i1];
2063 DCHECK(!helper->IsAbsent(start_index));
2067 if (helper->StartMin(start_index) == processed_start) {
2070 processed_start = helper->StartMin(start_index);
2075 int end_index_of_max_violation = -1;
2076 double max_relative_violation = 1.01;
2077 IntegerValue span_of_max_violation(0);
2080 double energy_lp = 0.0;
2086 std::vector<int> residual_tasks(active_intervals.begin() + i1,
2087 active_intervals.end());
2089 residual_tasks.begin(), residual_tasks.end(),
2090 [&](
int a,
int b) { return helper->EndMax(a) < helper->EndMax(b); });
2095 for (
int i2 = 0; i2 < residual_tasks.size(); ++i2) {
2096 const int t = residual_tasks[i2];
2097 if (helper->IsPresent(t)) {
2098 if (demand_is_fixed(t)) {
2099 if (helper->SizeIsFixed(t)) {
2100 energy_lp +=
ToDouble(helper->SizeMin(t) * demand_min(t));
2102 energy_lp +=
ToDouble(demand_min(t)) *
2103 helper->Sizes()[t].LpValue(lp_values);
2105 }
else if (helper->SizeIsFixed(t)) {
2106 DCHECK(!demands.empty());
2107 energy_lp += lp_values[demands[t]] *
ToDouble(helper->SizeMin(t));
2109 DCHECK(!demands.empty());
2111 ToDouble(demand_min(t)) * helper->Sizes()[t].LpValue(lp_values);
2112 energy_lp += lp_values[demands[t]] *
ToDouble(helper->SizeMin(t));
2113 energy_lp -=
ToDouble(demand_min(t) * helper->SizeMin(t));
2116 energy_lp += GetLiteralLpValue(helper->PresenceLiteral(t), lp_values,
2118 ToDouble(helper->SizeMin(t) * demand_min(t));
2121 min_of_starts =
std::min(min_of_starts, helper->StartMin(t));
2122 max_of_ends =
std::max(max_of_ends, helper->EndMax(t));
2125 const double relative_violation =
2126 energy_lp /
ToDouble((max_of_ends - min_of_starts) * capacity_max);
2127 if (relative_violation > max_relative_violation) {
2128 end_index_of_max_violation = i2;
2129 max_relative_violation = relative_violation;
2130 span_of_max_violation = max_of_ends - min_of_starts;
2134 if (end_index_of_max_violation == -1)
continue;
2137 bool cut_generated =
true;
2138 bool has_opt_cuts =
false;
2139 bool has_quadratic_cuts =
false;
2145 for (
int i2 = 0; i2 <= end_index_of_max_violation; ++i2) {
2146 const int t = residual_tasks[i2];
2147 if (helper->IsPresent(t)) {
2148 if (demand_is_fixed(t)) {
2149 if (helper->SizeIsFixed(t)) {
2150 cut.
AddConstant(helper->SizeMin(t) * demand_min(t));
2152 cut.
AddTerm(helper->Sizes()[t], demand_min(t));
2154 }
else if (helper->SizeIsFixed(t)) {
2155 DCHECK(!demands.empty());
2156 cut.
AddTerm(demands[t], helper->SizeMin(t));
2158 DCHECK(!demands.empty());
2167 cut.
AddTerm(helper->Sizes()[t], demand_min(t));
2168 cut.
AddTerm(demands[t], helper->SizeMin(t));
2170 cut.
AddConstant(-helper->SizeMin(t) * demand_min(t));
2171 has_quadratic_cuts =
true;
2174 has_opt_cuts =
true;
2175 if (!helper->SizeIsFixed(t) || !demand_is_fixed(t)) {
2176 has_quadratic_cuts =
true;
2179 helper->SizeMin(t) * demand_min(t))) {
2180 cut_generated =
false;
2186 if (cut_generated) {
2187 std::string full_name = cut_name;
2188 if (has_opt_cuts) full_name.append(
"_opt");
2189 if (has_quadratic_cuts) full_name.append(
"_quad");
2191 manager->
AddCut(cut.
Build(), cut_name, lp_values);
2198 const std::vector<IntervalVariable>& intervals,
2199 const IntegerVariable
capacity,
const std::vector<IntegerVariable>& demands,
2205 model->TakeOwnership(helper);
2207 result.
vars = demands;
2217 const std::vector<IntervalVariable>& intervals,
2218 const IntegerVariable
capacity,
const std::vector<IntegerVariable>& demands,
2224 model->TakeOwnership(helper);
2226 result.
vars = demands;
2244 if (trail->CurrentDecisionLevel() > 0)
return;
2246 std::vector<Event> events;
2249 for (
int i = 0; i < helper->
NumTasks(); ++i) {
2258 e1.interval_index = i;
2260 e1.demand = demands[i];
2265 e2.positive =
false;
2266 events.push_back(e1);
2267 events.push_back(e2);
2273 std::sort(events.begin(), events.end(),
2274 [](
const Event i,
const Event j) {
2275 if (i.time == j.time) {
2276 if (i.positive == j.positive) {
2277 return i.interval_index < j.interval_index;
2281 return i.time < j.time;
2284 std::vector<Event> cut_events;
2285 bool added_positive_event =
false;
2286 for (
const Event& e : events) {
2288 added_positive_event =
true;
2289 cut_events.push_back(e);
2292 if (added_positive_event && cut_events.size() > 1) {
2294 bool cut_generated =
true;
2298 for (
const Event& cut_event : cut_events) {
2299 if (helper->
IsPresent(cut_event.interval_index)) {
2300 cut.
AddTerm(cut_event.demand, IntegerValue(1));
2304 integer_trail->LowerBound(cut_event.demand));
2305 if (!cut_generated)
break;
2308 if (cut_generated) {
2311 manager->
AddCut(cut.
Build(),
"Cumulative", lp_values);
2316 for (
int i = 0; i < cut_events.size(); ++i) {
2317 if (cut_events[i].interval_index == e.interval_index) {
2320 cut_events[new_size] = cut_events[i];
2323 cut_events.resize(new_size);
2324 added_positive_event =
false;
2331 const std::vector<IntervalVariable>& intervals,
Model*
model) {
2336 model->TakeOwnership(helper);
2341 "NoOverlapEnergy", helper,
2348 const std::vector<IntervalVariable>& intervals,
Model*
model) {
2353 model->TakeOwnership(helper);
2360 [trail, helper,
model](
2373 for (
int index1 = 0; index1 < helper->NumTasks(); ++index1) {
2374 if (!helper->IsPresent(index1))
continue;
2375 for (
int index2 = index1 + 1; index2 < helper->NumTasks(); ++index2) {
2376 if (!helper->IsPresent(index2))
continue;
2379 if (helper->EndMax(index1) <= helper->StartMin(index2) ||
2380 helper->EndMax(index2) <= helper->StartMin(index1)) {
2384 const bool interval_1_can_precede_2 =
2385 helper->EndMin(index1) <= helper->StartMax(index2);
2386 const bool interval_2_can_precede_1 =
2387 helper->EndMin(index2) <= helper->StartMax(index1);
2389 if (interval_1_can_precede_2 && !interval_2_can_precede_1) {
2393 cut.
AddTerm(helper->Ends()[index1], IntegerValue(1));
2394 cut.
AddTerm(helper->Starts()[index2], IntegerValue(-1));
2395 manager->
AddCut(cut.
Build(),
"NoOverlapPrecedence", lp_values);
2396 }
else if (interval_2_can_precede_1 && !interval_1_can_precede_2) {
2400 cut.
AddTerm(helper->Ends()[index2], IntegerValue(1));
2401 cut.
AddTerm(helper->Starts()[index1], IntegerValue(-1));
2402 manager->
AddCut(cut.
Build(),
"NoOverlapPrecedence", lp_values);
2411 const std::vector<IntegerVariable>& base_variables,
Model*
model) {
2414 std::vector<IntegerVariable> variables;
2415 std::vector<Literal> literals;
2416 absl::flat_hash_map<LiteralIndex, IntegerVariable> positive_map;
2417 absl::flat_hash_map<LiteralIndex, IntegerVariable> negative_map;
2420 for (
const IntegerVariable
var : base_variables) {
2421 if (integer_trail->LowerBound(
var) != IntegerValue(0))
continue;
2422 if (integer_trail->UpperBound(
var) != IntegerValue(1))
continue;
2423 const LiteralIndex literal_index = encoder->GetAssociatedLiteral(
2426 variables.push_back(
var);
2427 literals.push_back(
Literal(literal_index));
2428 positive_map[literal_index] =
var;
2433 result.
vars = variables;
2436 [variables, literals, implication_graph, positive_map, negative_map,
2439 std::vector<double> packed_values;
2440 for (
int i = 0; i < literals.size(); ++i) {
2441 packed_values.push_back(lp_values[variables[i]]);
2443 const std::vector<std::vector<Literal>> at_most_ones =
2444 implication_graph->GenerateAtMostOnesWithLargeWeight(literals,
2447 for (
const std::vector<Literal>& at_most_one : at_most_ones) {
2453 for (
const Literal l : at_most_one) {
2455 builder.
AddTerm(positive_map.at(l.Index()), IntegerValue(1));
2458 builder.
AddTerm(negative_map.at(l.Index()), IntegerValue(-1));
2463 manager->
AddCut(builder.
Build(),
"clique", lp_values);