24#include "absl/cleanup/cleanup.h"
25#include "absl/container/btree_map.h"
26#include "absl/container/btree_set.h"
27#include "absl/container/flat_hash_set.h"
28#include "absl/log/check.h"
29#include "absl/log/log.h"
30#include "absl/log/vlog_is_on.h"
31#include "absl/strings/str_cat.h"
32#include "absl/strings/str_format.h"
33#include "absl/types/span.h"
59 std::vector<Literal>* core) {
61 absl::btree_set<LiteralIndex> moved_last;
62 std::vector<Literal> candidate(core->begin(), core->end());
74 if (target_level == -1)
break;
93 moved_last.insert(candidate.back().Index());
98 if (candidate.size() < core->size()) {
99 VLOG(1) <<
"minimization with propag " << core->size() <<
" -> "
103 absl::flat_hash_set<LiteralIndex> set;
104 for (
const Literal l : candidate) set.insert(l.Index());
106 for (
const Literal l : *core) {
107 if (set.contains(l.Index())) {
108 (*core)[new_size++] = l;
111 core->resize(new_size);
116 std::vector<Literal>* core) {
120 if (core->size() > 100 || core->size() == 1)
return;
125 const int old_size = core->size();
126 std::vector<Literal> assumptions;
127 absl::flat_hash_set<LiteralIndex> removed_once;
137 for (
int i = core->size(); --
i >= 0;) {
139 const auto [_, inserted] = removed_once.insert(l.
Index());
145 if (to_remove == -1)
break;
148 for (
int i = 0;
i < core->size(); ++
i) {
149 if (
i == to_remove)
continue;
150 assumptions.push_back((*core)[
i]);
160 if (core->size() < old_size) {
161 VLOG(1) <<
"minimization with search " << old_size <<
" -> "
180 {assumption}, 1'000);
198 std::vector<Literal>* core) {
200 for (
const Literal lit : *core) {
207 (*core)[new_size++] = lit;
209 core->resize(new_size);
213 IntegerVariable objective_var,
214 const std::function<
void()>& feasible_solution_observer,
Model* model) {
227 const IntegerValue objective = integer_trail->LowerBound(objective_var);
230 if (feasible_solution_observer !=
nullptr) {
231 feasible_solution_observer();
233 if (parameters.stop_after_first_solution()) {
239 if (!integer_trail->Enqueue(
248 IntegerVariable objective_var,
249 const std::function<
void()>& feasible_solution_observer,
Model* model) {
266 IntegerValue unknown_min = integer_trail->UpperBound(objective_var);
267 IntegerValue unknown_max = integer_trail->LowerBound(objective_var);
269 sat_solver->Backtrack(0);
270 const IntegerValue lb = integer_trail->LowerBound(objective_var);
271 const IntegerValue ub = integer_trail->UpperBound(objective_var);
272 unknown_min = std::min(unknown_min, ub);
273 unknown_max = std::max(unknown_max, lb);
277 if (lb < unknown_min) {
278 target = lb + (unknown_min - lb) / 2;
279 }
else if (unknown_max < ub) {
280 target = ub - (ub - unknown_max) / 2;
282 VLOG(1) <<
"Binary-search, done.";
285 VLOG(1) <<
"Binary-search, objective: [" << lb <<
"," << ub <<
"]"
286 <<
" tried: [" << unknown_min <<
"," << unknown_max <<
"]"
287 <<
" target: obj<=" << target;
290 const Literal assumption = integer_encoder->GetOrCreateAssociatedLiteral(
304 sat_solver->Backtrack(0);
305 if (!integer_trail->Enqueue(
314 const IntegerValue objective = integer_trail->LowerBound(objective_var);
315 if (feasible_solution_observer !=
nullptr) {
316 feasible_solution_observer();
321 sat_solver->Backtrack(0);
322 if (!integer_trail->Enqueue(
330 unknown_min = std::min(target, unknown_min);
331 unknown_max = std::max(target, unknown_max);
337 sat_solver->Backtrack(0);
365 std::vector<IntegerValue> assumption_weights,
366 IntegerValue stratified_threshold,
Model* model,
367 std::vector<std::vector<Literal>>* cores) {
369 SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
377 std::vector<Literal> core = sat_solver->GetLastIncompatibleDecisions();
378 if (sat_solver->parameters().core_minimization_level() > 0) {
381 if (core.size() == 1) {
382 if (!sat_solver->AddUnitClause(core[0].Negated())) {
386 if (core.empty())
return sat_solver->UnsatStatus();
387 cores->push_back(core);
388 if (!sat_solver->parameters().find_multiple_cores())
break;
392 std::vector<int> indices;
394 absl::btree_set<Literal> temp(core.begin(), core.end());
395 for (
int i = 0;
i < assumptions.size(); ++
i) {
396 if (temp.contains(assumptions[
i])) {
397 indices.push_back(
i);
407 IntegerValue min_weight = assumption_weights[indices.front()];
408 for (
const int i : indices) {
409 min_weight = std::min(min_weight, assumption_weights[
i]);
411 for (
const int i : indices) {
412 assumption_weights[
i] -= min_weight;
418 for (
int i = 0;
i < assumptions.size(); ++
i) {
419 if (assumption_weights[
i] < stratified_threshold)
continue;
420 assumptions[new_size] = assumptions[
i];
421 assumption_weights[new_size] = assumption_weights[
i];
424 assumptions.resize(new_size);
425 assumption_weights.resize(new_size);
426 }
while (!assumptions.empty());
433 IntegerVariable objective_var, absl::Span<const IntegerVariable> variables,
434 absl::Span<const IntegerValue> coefficients,
435 std::function<
void()> feasible_solution_observer,
Model* model)
437 sat_solver_(model->GetOrCreate<
SatSolver>()),
439 time_limit_(model->GetOrCreate<
TimeLimit>()),
444 objective_var_(objective_var),
445 feasible_solution_observer_(
std::move(feasible_solution_observer)) {
446 CHECK_EQ(variables.size(), coefficients.size());
447 for (
int i = 0;
i < variables.size(); ++
i) {
448 if (coefficients[
i] > 0) {
449 terms_.push_back({variables[
i], coefficients[
i]});
450 }
else if (coefficients[
i] < 0) {
451 terms_.push_back({
NegationOf(variables[
i]), -coefficients[
i]});
455 terms_.back().depth = 0;
461 stratification_threshold_ = parameters_->max_sat_stratification() ==
467bool CoreBasedOptimizer::ProcessSolution() {
470 IntegerValue objective(0);
471 for (ObjectiveTerm& term : terms_) {
472 const IntegerValue value = integer_trail_->
LowerBound(term.var);
473 objective += term.weight * value;
477 term.cover_ub = std::min(term.cover_ub, value);
487 if (feasible_solution_observer_ !=
nullptr) {
488 feasible_solution_observer_();
490 if (parameters_->stop_after_first_solution()) {
496 sat_solver_->Backtrack(0);
497 sat_solver_->SetAssumptionLevel(0);
498 return integer_trail_->Enqueue(
502bool CoreBasedOptimizer::PropagateObjectiveBounds() {
504 bool some_bound_were_tightened =
true;
505 while (some_bound_were_tightened) {
506 some_bound_were_tightened =
false;
507 if (!sat_solver_->ResetToLevelZero())
return false;
508 if (time_limit_->LimitReached())
return true;
511 IntegerValue implied_objective_lb(0);
512 for (ObjectiveTerm& term : terms_) {
513 const IntegerValue var_lb = integer_trail_->LowerBound(term.var);
514 term.old_var_lb = var_lb;
515 implied_objective_lb += term.weight * var_lb.value();
519 if (implied_objective_lb > integer_trail_->LowerBound(objective_var_)) {
521 objective_var_, implied_objective_lb),
526 some_bound_were_tightened =
true;
535 const IntegerValue gap =
536 integer_trail_->UpperBound(objective_var_) - implied_objective_lb;
538 for (
const ObjectiveTerm& term : terms_) {
539 if (term.weight == 0)
continue;
540 const IntegerValue var_lb = integer_trail_->LowerBound(term.var);
541 const IntegerValue var_ub = integer_trail_->UpperBound(term.var);
542 if (var_lb == var_ub)
continue;
549 if (gap / term.weight < var_ub - var_lb) {
550 some_bound_were_tightened =
true;
551 const IntegerValue new_ub = var_lb + gap / term.weight;
552 DCHECK_LT(new_ub, var_ub);
553 if (!integer_trail_->Enqueue(
572void CoreBasedOptimizer::ComputeNextStratificationThreshold() {
573 std::vector<IntegerValue> weights;
574 for (ObjectiveTerm& term : terms_) {
575 if (term.weight >= stratification_threshold_)
continue;
576 if (term.weight == 0)
continue;
578 const IntegerValue var_lb = integer_trail_->LevelZeroLowerBound(term.var);
579 const IntegerValue var_ub = integer_trail_->LevelZeroUpperBound(term.var);
580 if (var_lb == var_ub)
continue;
582 weights.push_back(term.weight);
584 if (weights.empty()) {
585 stratification_threshold_ = IntegerValue(0);
590 stratification_threshold_ =
591 weights[
static_cast<int>(std::floor(0.9 * weights.size()))];
594bool CoreBasedOptimizer::CoverOptimization() {
595 if (!sat_solver_->ResetToLevelZero())
return false;
599 constexpr double max_dtime_per_core = 0.5;
600 const double old_time_limit = parameters_->max_deterministic_time();
601 parameters_->set_max_deterministic_time(max_dtime_per_core);
602 auto cleanup = ::absl::MakeCleanup([old_time_limit,
this]() {
603 parameters_->set_max_deterministic_time(old_time_limit);
606 for (
const ObjectiveTerm& term : terms_) {
610 if (term.depth == 0)
continue;
616 const IntegerVariable var = term.var;
618 std::min(term.cover_ub, integer_trail_->UpperBound(var));
623 if (best <= integer_trail_->
LowerBound(var))
continue;
627 const double deterministic_limit =
628 time_limit_->GetElapsedDeterministicTime() + max_dtime_per_core;
632 while (best > integer_trail_->LowerBound(var)) {
633 const Literal assumption = integer_encoder_->GetOrCreateAssociatedLiteral(
638 best = integer_trail_->LowerBound(var);
639 VLOG(1) <<
"cover_opt var:" << var <<
" domain:["
640 << integer_trail_->LevelZeroLowerBound(var) <<
"," << best <<
"]";
641 if (!ProcessSolution())
return false;
642 if (!sat_solver_->ResetToLevelZero())
return false;
644 time_limit_->GetElapsedDeterministicTime() > deterministic_limit) {
650 if (!sat_solver_->ResetToLevelZero())
return false;
662 return PropagateObjectiveBounds();
666 absl::Span<const Literal> literals, absl::Span<const IntegerVariable> vars,
667 absl::Span<const Coefficient> coefficients, Coefficient offset) {
681 for (
int i = 0;
i < literals.size(); ++
i) {
682 CHECK_GT(coefficients[
i], 0);
688 CHECK_EQ(vars.size(), coefficients.size());
689 for (
int i = 0;
i < vars.size(); ++
i) {
690 CHECK_GT(coefficients[
i], 0);
691 const IntegerVariable var = vars[
i];
692 const IntegerValue var_lb = integer_trail_->LowerBound(var);
693 const IntegerValue var_ub = integer_trail_->UpperBound(var);
694 if (var_ub - var_lb == 1) {
695 const Literal lit = integer_encoder_->GetOrCreateAssociatedLiteral(
703 const int ub =
static_cast<int>(var_ub.value() - var_lb.value());
706 [var, var_lb,
this](
int x) {
707 return integer_encoder_->GetOrCreateAssociatedLiteral(
709 var_lb + IntegerValue(x + 1)));
718 Coefficient lower_bound(0);
721 Coefficient stratified_lower_bound(0);
722 if (parameters_->max_sat_stratification() !=
725 stratified_lower_bound = std::max(stratified_lower_bound, n->weight());
731 std::string previous_core_info =
"";
732 for (
int iter = 0;;) {
739 const Coefficient upper_bound(
740 integer_trail_->UpperBound(objective_var_).value() - offset.value());
743 const IntegerValue new_obj_lb(lower_bound.value() + offset.value());
744 if (new_obj_lb > integer_trail_->LowerBound(objective_var_)) {
745 if (!integer_trail_->Enqueue(
754 const int num_bools = sat_solver_->NumVariables();
755 const int num_fixed = sat_solver_->NumFixedVariables();
757 absl::StrFormat(
"bool_%s (num_cores=%d [%s] a=%u d=%d "
758 "fixed=%d/%d clauses=%s)",
759 model_->Name(), iter, previous_core_info,
760 encoder.
nodes().size(), max_depth, num_fixed,
762 new_obj_lb, integer_trail_->LevelZeroUpperBound(objective_var_));
765 if (parameters_->cover_optimization() && encoder.
nodes().size() > 1) {
769 previous_core_info =
"cover";
777 const Coefficient gap = upper_bound - lower_bound;
778 if (stratified_lower_bound > (gap + 2) / 2) {
779 stratified_lower_bound = (gap + 2) / 2;
781 std::vector<Literal> assumptions;
785 if (!assumptions.empty())
break;
787 stratified_lower_bound =
789 if (stratified_lower_bound > 0)
continue;
795 VLOG(2) <<
"[Core] #nodes " << encoder.
nodes().size()
796 <<
" #assumptions:" << assumptions.size()
797 <<
" stratification:" << stratified_lower_bound <<
" gap:" << gap;
816 stratified_lower_bound =
818 if (stratified_lower_bound > 0)
continue;
824 std::vector<Literal> core = sat_solver_->GetLastIncompatibleDecisions();
825 if (parameters_->core_minimization_level() > 0) {
828 if (parameters_->core_minimization_level() > 1) {
839 absl::StrFormat(
"size:%u mw:%d", core.size(), min_weight.value());
843 if (!encoder.
ProcessCore(core, min_weight, gap, &previous_core_info)) {
846 max_depth = std::max(max_depth, encoder.
nodes().back()->depth());
853 std::vector<Coefficient>* coefficients,
854 Coefficient* offset) {
856 std::vector<std::pair<LiteralIndex, Coefficient>> pairs;
857 const int size = literals->size();
858 for (
int i = 0;
i < size; ++
i) {
859 pairs.push_back({(*literals)[
i].Index(), (*coefficients)[
i]});
861 std::sort(pairs.begin(), pairs.end());
865 for (
const auto& [index, coeff] : pairs) {
867 if (pairs[new_size - 1].first == index) {
868 pairs[new_size - 1].second += coeff;
870 }
else if (pairs[new_size - 1].first ==
Literal(index).NegatedIndex()) {
872 pairs[new_size - 1].second -= coeff;
877 pairs[new_size++] = {index, coeff};
879 pairs.resize(new_size);
883 coefficients->clear();
884 for (
const auto& [index, coeff] : pairs) {
886 literals->push_back(
Literal(index));
887 coefficients->push_back(coeff);
888 }
else if (coeff < 0) {
891 literals->push_back(
Literal(index).Negated());
892 coefficients->push_back(-coeff);
897void CoreBasedOptimizer::PresolveObjectiveWithAtMostOne(
898 std::vector<Literal>* literals, std::vector<Coefficient>* coefficients,
899 Coefficient* offset) {
917 std::vector<Literal> candidates;
918 const int num_terms = literals->size();
919 for (
int i = 0;
i < num_terms; ++
i) {
920 const Literal lit = (*literals)[
i];
921 const Coefficient coeff = (*coefficients)[
i];
926 weights[lit] = coeff;
928 candidates.push_back(lit.Negated());
929 is_candidate[lit.NegatedIndex()] =
true;
932 int num_at_most_ones = 0;
933 Coefficient overall_lb_increase(0);
935 std::vector<Literal> at_most_one;
936 std::vector<std::pair<Literal, Coefficient>> new_obj_terms;
938 for (
const Literal root : candidates) {
939 if (weights[root.NegatedIndex()] == 0)
continue;
940 if (implications_->
WorkDone() > 1e8)
continue;
943 CHECK_EQ(weights[root], 0);
949 {root}, is_candidate, preferences);
950 if (at_most_one.size() <= 1)
continue;
956 Coefficient max_coeff(0);
957 Coefficient lb_increase(0);
958 for (
const Literal lit : at_most_one) {
959 const Coefficient coeff = weights[lit.NegatedIndex()];
960 lb_increase += coeff;
961 max_coeff = std::max(max_coeff, coeff);
963 lb_increase -= max_coeff;
965 *offset += lb_increase;
966 overall_lb_increase += lb_increase;
968 for (
const Literal lit : at_most_one) {
969 is_candidate[lit] =
false;
970 const Coefficient new_weight = max_coeff - weights[lit.NegatedIndex()];
971 CHECK_EQ(weights[lit], 0);
972 weights[lit] = new_weight;
973 weights[lit.NegatedIndex()] = 0;
974 if (new_weight > 0) {
978 is_candidate[lit.NegatedIndex()] =
true;
983 const Literal new_lit(sat_solver_->NewBooleanVariable(),
true);
984 new_obj_terms.push_back({new_lit, max_coeff});
987 at_most_one.push_back(new_lit);
988 sat_solver_->AddProblemClause(at_most_one);
989 is_candidate.resize(implications_->literal_size(),
false);
990 preferences.
resize(implications_->literal_size(), 1.0);
993 if (overall_lb_increase > 0) {
995 model_->GetOrCreate<SharedResponseManager>()->UpdateInnerObjectiveBounds(
996 absl::StrFormat(
"am1_presolve (num_literals=%d num_am1=%d "
997 "increase=%lld work_done=%lld)",
998 (
int)candidates.size(), num_at_most_ones,
999 overall_lb_increase.value(), implications_->WorkDone()),
1000 IntegerValue(offset->value()),
1001 integer_trail_->LevelZeroUpperBound(objective_var_));
1006 coefficients->clear();
1007 for (
const Literal root : candidates) {
1008 if (weights[root] > 0) {
1009 CHECK_EQ(weights[root.NegatedIndex()], 0);
1010 literals->push_back(root);
1011 coefficients->push_back(weights[root]);
1013 if (weights[root.NegatedIndex()] > 0) {
1014 CHECK_EQ(weights[root], 0);
1015 literals->push_back(root.Negated());
1016 coefficients->push_back(weights[root.NegatedIndex()]);
1019 for (
const auto& [lit, coeff] : new_obj_terms) {
1020 literals->push_back(lit);
1021 coefficients->push_back(coeff);
1030 if (!parameters_->interleave_search()) {
1031 Coefficient offset(0);
1032 std::vector<Literal> literals;
1033 std::vector<IntegerVariable> vars;
1034 std::vector<Coefficient> coefficients;
1035 bool all_booleans =
true;
1036 IntegerValue range(0);
1037 for (
const ObjectiveTerm& term : terms_) {
1038 const IntegerVariable var = term.var;
1039 const IntegerValue coeff = term.weight;
1040 const IntegerValue lb = integer_trail_->LowerBound(var);
1041 const IntegerValue ub = integer_trail_->UpperBound(var);
1042 offset += Coefficient((lb * coeff).value());
1043 if (lb == ub)
continue;
1045 vars.push_back(var);
1046 coefficients.push_back(Coefficient(coeff.value()));
1048 literals.push_back(integer_encoder_->GetOrCreateAssociatedLiteral(
1051 all_booleans =
false;
1070 PresolveObjectiveWithAtMostOne(&literals, &coefficients, &offset);
1082 absl::btree_map<LiteralIndex, int> literal_to_term_index;
1096 if (parameters_->cover_optimization()) {
1102 std::vector<int> term_indices;
1103 std::vector<IntegerLiteral> integer_assumptions;
1104 std::vector<IntegerValue> assumption_weights;
1105 IntegerValue objective_offset(0);
1106 bool some_assumptions_were_skipped =
false;
1107 for (
int i = 0;
i < terms_.size(); ++
i) {
1108 const ObjectiveTerm term = terms_[
i];
1111 if (term.weight == 0)
continue;
1117 const IntegerValue var_lb = integer_trail_->LowerBound(term.var);
1118 const IntegerValue var_ub = integer_trail_->UpperBound(term.var);
1119 if (var_lb == var_ub) {
1120 objective_offset += term.weight * var_lb.value();
1125 if (term.weight >= stratification_threshold_) {
1126 integer_assumptions.push_back(
1128 assumption_weights.push_back(term.weight);
1129 term_indices.push_back(
i);
1131 some_assumptions_were_skipped =
true;
1136 if (term_indices.empty() && some_assumptions_were_skipped) {
1137 ComputeNextStratificationThreshold();
1142 if (term_indices.size() <= 2 && !some_assumptions_were_skipped) {
1143 VLOG(1) <<
"Switching to linear scan...";
1144 if (!already_switched_to_linear_scan_) {
1145 already_switched_to_linear_scan_ =
true;
1146 std::vector<IntegerVariable> constraint_vars;
1147 std::vector<int64_t> constraint_coeffs;
1148 for (
const int index : term_indices) {
1149 constraint_vars.push_back(terms_[index].var);
1150 constraint_coeffs.push_back(terms_[index].weight.value());
1152 constraint_vars.push_back(objective_var_);
1153 constraint_coeffs.push_back(-1);
1155 -objective_offset.value()));
1159 objective_var_, feasible_solution_observer_, model_);
1163 if (VLOG_IS_ON(1)) {
1165 for (
const ObjectiveTerm& term : terms_) {
1166 max_depth = std::max(max_depth, term.depth);
1168 const int64_t lb = integer_trail_->LowerBound(objective_var_).value();
1169 const int64_t ub = integer_trail_->UpperBound(objective_var_).value();
1173 :
static_cast<int>(std::ceil(
1174 100.0 * (ub - lb) / std::max(std::abs(ub), std::abs(lb))));
1175 VLOG(1) << absl::StrCat(
"unscaled_next_obj_range:[", lb,
",", ub,
1178 gap,
"%",
" assumptions:", term_indices.size(),
1179 " strat:", stratification_threshold_.value(),
1180 " depth:", max_depth,
1181 " bool: ", sat_solver_->NumVariables());
1185 std::vector<Literal> assumptions;
1186 literal_to_term_index.clear();
1187 for (
int i = 0;
i < integer_assumptions.size(); ++
i) {
1188 assumptions.push_back(integer_encoder_->GetOrCreateAssociatedLiteral(
1189 integer_assumptions[
i]));
1197 literal_to_term_index[assumptions.back()] = term_indices[
i];
1205 std::vector<std::vector<Literal>> cores;
1207 FindCores(assumptions, assumption_weights, stratification_threshold_,
1213 if (cores.empty()) {
1214 ComputeNextStratificationThreshold();
1223 for (
const std::vector<Literal>& core : cores) {
1226 if (core.size() == 1) {
1227 if (!sat_solver_->AddUnitClause(core[0].Negated())) {
1236 bool ignore_this_core =
false;
1238 IntegerValue max_weight(0);
1239 IntegerValue new_var_lb(1);
1240 IntegerValue new_var_ub(0);
1242 for (
const Literal lit : core) {
1243 const int index = literal_to_term_index.at(lit.Index());
1247 if (terms_[index].old_var_lb <
1248 integer_trail_->LowerBound(terms_[index].var)) {
1249 ignore_this_core =
true;
1253 const IntegerValue weight = terms_[index].weight;
1254 min_weight = std::min(min_weight, weight);
1255 max_weight = std::max(max_weight, weight);
1256 new_depth = std::max(new_depth, terms_[index].depth + 1);
1257 new_var_lb += integer_trail_->LowerBound(terms_[index].var);
1258 new_var_ub += integer_trail_->UpperBound(terms_[index].var);
1260 if (ignore_this_core)
continue;
1262 VLOG(1) << absl::StrFormat(
1263 "core:%u weight:[%d,%d] domain:[%d,%d] depth:%d", core.size(),
1264 min_weight.value(), max_weight.value(), new_var_lb.value(),
1265 new_var_ub.value(), new_depth);
1269 const IntegerVariable new_var =
1270 integer_trail_->AddIntegerVariable(new_var_lb, new_var_ub);
1271 terms_.push_back({new_var, min_weight, new_depth});
1272 terms_.back().cover_ub = new_var_ub;
1276 std::vector<IntegerVariable> constraint_vars;
1277 std::vector<int64_t> constraint_coeffs;
1278 for (
const Literal lit : core) {
1279 const int index = literal_to_term_index.at(lit.Index());
1280 terms_[index].weight -= min_weight;
1281 constraint_vars.push_back(terms_[index].var);
1282 constraint_coeffs.push_back(1);
1284 constraint_vars.push_back(new_var);
1285 constraint_coeffs.push_back(-1);
bool Contains(int64_t value) const
void EnableLogging(bool enable)
bool LoggingIsEnabled() const
Returns true iff logging is enabled.
std::vector< Literal > ExpandAtMostOneWithWeight(absl::Span< const Literal > at_most_one, const util_intops::StrongVector< LiteralIndex, bool > &can_be_included, const util_intops::StrongVector< LiteralIndex, double > &expanded_lp_values)
Same as ExpandAtMostOne() but try to maximize the weight in the clique.
int64_t literal_size() const
SatSolver::Status OptimizeWithSatEncoding(absl::Span< const Literal > literals, absl::Span< const IntegerVariable > vars, absl::Span< const Coefficient > coefficients, Coefficient offset)
SatSolver::Status Optimize()
CoreBasedOptimizer(IntegerVariable objective_var, absl::Span< const IntegerVariable > variables, absl::Span< const IntegerValue > coefficients, std::function< void()> feasible_solution_observer, Model *model)
static EncodingNode GenericNode(int lb, int ub, std::function< Literal(int x)> create_lit, Coefficient weight)
static EncodingNode LiteralNode(Literal l, Coefficient weight)
An helper class to share the code used by the different kind of search.
IntegerValue LowerBound(IntegerVariable i) const
Returns the current lower/upper bound of the given integer variable.
const Domain & InitialVariableDomain(IntegerVariable var) const
LiteralIndex Index() const
std::vector< EncodingNode * > * mutable_nodes()
void AddBaseNode(EncodingNode node)
const std::vector< EncodingNode * > & nodes() const
bool ProcessCore(absl::Span< const Literal > core, Coefficient min_weight, Coefficient gap, std::string *info)
void set_max_number_of_conflicts(::int64_t value)
::int32_t binary_search_num_conflicts() const
static constexpr MaxSatStratificationAlgorithm STRATIFICATION_NONE
Status EnqueueDecisionAndBacktrackOnConflict(Literal true_literal, int *first_propagation_index=nullptr)
SolverLogger * mutable_logger()
Hack to allow to temporarily disable logging if it is enabled.
void NotifyThatModelIsUnsat()
std::vector< Literal > GetLastIncompatibleDecisions()
int EnqueueDecisionAndBackjumpOnConflict(Literal true_literal)
bool ModelIsUnsat() const
void Backtrack(int target_level)
ABSL_MUST_USE_RESULT bool Propagate()
void SetAssumptionLevel(int assumption_level)
ABSL_MUST_USE_RESULT bool AddUnitClause(Literal true_literal)
Status ResetAndSolveWithGivenAssumptions(const std::vector< Literal > &assumptions, int64_t max_number_of_conflicts=-1)
const VariablesAssignment & Assignment() const
int CurrentDecisionLevel() const
ABSL_MUST_USE_RESULT bool ResetToLevelZero()
ABSL_MUST_USE_RESULT bool FinishPropagation()
bool LiteralIsAssigned(Literal literal) const
bool LiteralIsFalse(Literal literal) const
bool LiteralIsTrue(Literal literal) const
void resize(size_type new_size)
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
std::vector< Literal > ExtractAssumptions(Coefficient stratified_lower_bound, const std::vector< EncodingNode * > &nodes, SatSolver *solver)
std::function< int64_t(const Model &)> LowerBound(IntegerVariable v)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
Returns the vector of the negated variables.
void ReduceNodes(Coefficient upper_bound, Coefficient *lower_bound, std::vector< EncodingNode * > *nodes, SatSolver *solver)
void MinimizeCoreWithPropagation(TimeLimit *limit, SatSolver *solver, std::vector< Literal > *core)
void FilterAssignedLiteral(const VariablesAssignment &assignment, std::vector< Literal > *core)
A core cannot be all true.
Coefficient ComputeCoreMinWeight(const std::vector< EncodingNode * > &nodes, absl::Span< const Literal > core)
void MinimizeCoreWithSearch(TimeLimit *limit, SatSolver *solver, std::vector< Literal > *core)
void PresolveBooleanLinearExpression(std::vector< Literal > *literals, std::vector< Coefficient > *coefficients, Coefficient *offset)
std::string FormatCounter(int64_t num)
Prints a positive number with separators for easier reading (ex: 1'348'065).
bool ProbeLiteral(Literal assumption, SatSolver *solver)
SatSolver::Status MinimizeIntegerVariableWithLinearScanAndLazyEncoding(IntegerVariable objective_var, const std::function< void()> &feasible_solution_observer, Model *model)
int MoveOneUnprocessedLiteralLast(const absl::btree_set< LiteralIndex > &processed, int relevant_prefix_size, std::vector< Literal > *literals)
SatSolver::Status ResetAndSolveIntegerProblem(const std::vector< Literal > &assumptions, Model *model)
void RestrictObjectiveDomainWithBinarySearch(IntegerVariable objective_var, const std::function< void()> &feasible_solution_observer, Model *model)
Coefficient MaxNodeWeightSmallerThan(const std::vector< EncodingNode * > &nodes, Coefficient upper_bound)
std::function< void(Model *)> WeightedSumLowerOrEqual(absl::Span< const IntegerVariable > vars, const VectorInt &coefficients, int64_t upper_bound)
Weighted sum <= constant.
In SWIG mode, we don't want anything besides these top-level includes.
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)
static IntegerLiteral LowerOrEqual(IntegerVariable i, IntegerValue bound)