OR-Tools  9.2
optimization.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <algorithm>
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <deque>
21 #include <limits>
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <utility>
27 
28 #include "absl/random/bit_gen_ref.h"
29 #include "absl/random/random.h"
30 #include "absl/strings/str_cat.h"
31 #include "absl/strings/str_format.h"
32 #include "ortools/base/cleanup.h"
33 #include "ortools/base/int_type.h"
34 #include "ortools/base/logging.h"
35 #include "ortools/base/macros.h"
36 #include "ortools/base/map_util.h"
37 #include "ortools/base/stl_util.h"
38 #include "ortools/base/timer.h"
39 #if !defined(__PORTABLE_PLATFORM__) && defined(USE_SCIP)
42 #endif // __PORTABLE_PLATFORM__
45 #include "ortools/sat/encoding.h"
49 #include "ortools/sat/util.h"
51 
52 namespace operations_research {
53 namespace sat {
54 
55 namespace {
56 
57 // Used to log messages to stdout or to the normal logging framework according
58 // to the given LogBehavior value.
59 class Logger {
60  public:
61  explicit Logger(LogBehavior v) : use_stdout_(v == STDOUT_LOG) {}
62  void Log(const std::string& message) {
63  if (use_stdout_) {
64  absl::PrintF("%s\n", message);
65  } else {
66  LOG(INFO) << message;
67  }
68  }
69 
70  private:
71  bool use_stdout_;
72 };
73 
74 // Outputs the current objective value in the cnf output format.
75 // Note that this function scale the given objective.
76 std::string CnfObjectiveLine(const LinearBooleanProblem& problem,
77  Coefficient objective) {
78  const double scaled_objective =
79  AddOffsetAndScaleObjectiveValue(problem, objective);
80  return absl::StrFormat("o %d", static_cast<int64_t>(scaled_objective));
81 }
82 
83 struct LiteralWithCoreIndex {
84  LiteralWithCoreIndex(Literal l, int i) : literal(l), core_index(i) {}
85  Literal literal;
87 };
88 
89 // Deletes the given indices from a vector. The given indices must be sorted in
90 // increasing order. The order of the non-deleted entries in the vector is
91 // preserved.
92 template <typename Vector>
93 void DeleteVectorIndices(const std::vector<int>& indices, Vector* v) {
94  int new_size = 0;
95  int indices_index = 0;
96  for (int i = 0; i < v->size(); ++i) {
97  if (indices_index < indices.size() && i == indices[indices_index]) {
98  ++indices_index;
99  } else {
100  (*v)[new_size] = (*v)[i];
101  ++new_size;
102  }
103  }
104  v->resize(new_size);
105 }
106 
107 // In the Fu & Malik algorithm (or in WPM1), when two cores overlap, we
108 // artificially introduce symmetries. More precisely:
109 //
110 // The picture below shows two cores with index 0 and 1, with one blocking
111 // variable per '-' and with the variables ordered from left to right (by their
112 // assumptions index). The blocking variables will be the one added to "relax"
113 // the core for the next iteration.
114 //
115 // 1: -------------------------------
116 // 0: ------------------------------------
117 //
118 // The 2 following assignment of the blocking variables are equivalent.
119 // Remember that exactly one blocking variable per core must be assigned to 1.
120 //
121 // 1: ----------------------1--------
122 // 0: --------1---------------------------
123 //
124 // and
125 //
126 // 1: ---------------------------1---
127 // 0: ---1--------------------------------
128 //
129 // This class allows to add binary constraints excluding the second possibility.
130 // Basically, each time a new core is added, if two of its blocking variables
131 // (b1, b2) have the same assumption index of two blocking variables from
132 // another core (c1, c2), then we forbid the assignment c1 true and b2 true.
133 //
134 // Reference: C Ansótegui, ML Bonet, J Levy, "Sat-based maxsat algorithms",
135 // Artificial Intelligence, 2013 - Elsevier.
136 class FuMalikSymmetryBreaker {
137  public:
138  FuMalikSymmetryBreaker() {}
139 
140  // Must be called before a new core is processed.
141  void StartResolvingNewCore(int new_core_index) {
142  literal_by_core_.resize(new_core_index);
143  for (int i = 0; i < new_core_index; ++i) {
144  literal_by_core_[i].clear();
145  }
146  }
147 
148  // This should be called for each blocking literal b of the new core. The
149  // assumption_index identify the soft clause associated to the given blocking
150  // literal. Note that between two StartResolvingNewCore() calls,
151  // ProcessLiteral() is assumed to be called with different assumption_index.
152  //
153  // Changing the order of the calls will not change the correctness, but will
154  // change the symmetry-breaking clause produced.
155  //
156  // Returns a set of literals which can't be true at the same time as b (under
157  // symmetry breaking).
158  std::vector<Literal> ProcessLiteral(int assumption_index, Literal b) {
159  if (assumption_index >= info_by_assumption_index_.size()) {
160  info_by_assumption_index_.resize(assumption_index + 1);
161  }
162 
163  // Compute the function result.
164  // info_by_assumption_index_[assumption_index] will contain all the pairs
165  // (blocking_literal, core) of the previous resolved cores at the same
166  // assumption index as b.
167  std::vector<Literal> result;
168  for (LiteralWithCoreIndex data :
169  info_by_assumption_index_[assumption_index]) {
170  // literal_by_core_ will contain all the blocking literal of a given core
171  // with an assumption_index that was used in one of the ProcessLiteral()
172  // calls since the last StartResolvingNewCore().
173  //
174  // Note that there can be only one such literal by core, so we will not
175  // add duplicates.
176  result.insert(result.end(), literal_by_core_[data.core_index].begin(),
177  literal_by_core_[data.core_index].end());
178  }
179 
180  // Update the internal data structure.
181  for (LiteralWithCoreIndex data :
182  info_by_assumption_index_[assumption_index]) {
183  literal_by_core_[data.core_index].push_back(data.literal);
184  }
185  info_by_assumption_index_[assumption_index].push_back(
186  LiteralWithCoreIndex(b, literal_by_core_.size()));
187  return result;
188  }
189 
190  // Deletes the given assumption indices.
191  void DeleteIndices(const std::vector<int>& indices) {
192  DeleteVectorIndices(indices, &info_by_assumption_index_);
193  }
194 
195  // This is only used in WPM1 to forget all the information related to a given
196  // assumption_index.
197  void ClearInfo(int assumption_index) {
198  CHECK_LE(assumption_index, info_by_assumption_index_.size());
199  info_by_assumption_index_[assumption_index].clear();
200  }
201 
202  // This is only used in WPM1 when a new assumption_index is created.
203  void AddInfo(int assumption_index, Literal b) {
204  CHECK_GE(assumption_index, info_by_assumption_index_.size());
205  info_by_assumption_index_.resize(assumption_index + 1);
206  info_by_assumption_index_[assumption_index].push_back(
207  LiteralWithCoreIndex(b, literal_by_core_.size()));
208  }
209 
210  private:
211  std::vector<std::vector<LiteralWithCoreIndex>> info_by_assumption_index_;
212  std::vector<std::vector<Literal>> literal_by_core_;
213 
214  DISALLOW_COPY_AND_ASSIGN(FuMalikSymmetryBreaker);
215 };
216 
217 } // namespace
218 
220  std::vector<Literal>* core) {
221  if (solver->IsModelUnsat()) return;
222  std::set<LiteralIndex> moved_last;
223  std::vector<Literal> candidate(core->begin(), core->end());
224 
225  solver->Backtrack(0);
226  solver->SetAssumptionLevel(0);
227  if (!solver->FinishPropagation()) return;
228  while (!limit->LimitReached()) {
229  // We want each literal in candidate to appear last once in our propagation
230  // order. We want to do that while maximizing the reutilization of the
231  // current assignment prefix, that is minimizing the number of
232  // decision/progagation we need to perform.
233  const int target_level = MoveOneUnprocessedLiteralLast(
234  moved_last, solver->CurrentDecisionLevel(), &candidate);
235  if (target_level == -1) break;
236  solver->Backtrack(target_level);
237  while (!solver->IsModelUnsat() && !limit->LimitReached() &&
238  solver->CurrentDecisionLevel() < candidate.size()) {
239  const Literal decision = candidate[solver->CurrentDecisionLevel()];
240  if (solver->Assignment().LiteralIsTrue(decision)) {
241  candidate.erase(candidate.begin() + solver->CurrentDecisionLevel());
242  continue;
243  } else if (solver->Assignment().LiteralIsFalse(decision)) {
244  // This is a "weird" API to get the subset of decisions that caused
245  // this literal to be false with reason analysis.
246  solver->EnqueueDecisionAndBacktrackOnConflict(decision);
247  candidate = solver->GetLastIncompatibleDecisions();
248  break;
249  } else {
250  solver->EnqueueDecisionAndBackjumpOnConflict(decision);
251  }
252  }
253  if (candidate.empty() || solver->IsModelUnsat()) return;
254  moved_last.insert(candidate.back().Index());
255  }
256 
257  solver->Backtrack(0);
258  solver->SetAssumptionLevel(0);
259  if (candidate.size() < core->size()) {
260  VLOG(1) << "minimization " << core->size() << " -> " << candidate.size();
261  core->assign(candidate.begin(), candidate.end());
262  }
263 }
264 
265 // This algorithm works by exploiting the unsat core returned by the SAT solver
266 // when the problem is UNSAT. It starts by trying to solve the decision problem
267 // where all the objective variables are set to their value with minimal cost,
268 // and relax in each step some of these fixed variables until the problem
269 // becomes satisfiable.
271  const LinearBooleanProblem& problem,
272  SatSolver* solver,
273  std::vector<bool>* solution) {
274  Logger logger(log);
275  FuMalikSymmetryBreaker symmetry;
276 
277  // blocking_clauses will contains a set of clauses that are currently added to
278  // the initial problem.
279  //
280  // Initially, each clause just contains a literal associated to an objective
281  // variable with non-zero cost. Setting all these literals to true will lead
282  // to the lowest possible objective.
283  //
284  // During the algorithm, "blocking" literals will be added to each clause.
285  // Moreover each clause will contain an extra "assumption" literal stored in
286  // the separate assumptions vector (in its negated form).
287  //
288  // The meaning of a given clause will always be:
289  // If the assumption literal and all blocking literals are false, then the
290  // "objective" literal (which is the first one in the clause) must be true.
291  // When the "objective" literal is true, its variable (which have a non-zero
292  // cost) is set to the value that minimize the objective cost.
293  //
294  // ex: If a variable "x" as a cost of 3, its cost contribution is smaller when
295  // it is set to false (since it will contribute to zero instead of 3).
296  std::vector<std::vector<Literal>> blocking_clauses;
297  std::vector<Literal> assumptions;
298 
299  // Initialize blocking_clauses and assumptions.
300  const LinearObjective& objective = problem.objective();
301  CHECK_GT(objective.coefficients_size(), 0);
302  const Coefficient unique_objective_coeff(std::abs(objective.coefficients(0)));
303  for (int i = 0; i < objective.literals_size(); ++i) {
304  CHECK_EQ(std::abs(objective.coefficients(i)), unique_objective_coeff)
305  << "The basic Fu & Malik algorithm needs constant objective coeffs.";
306  const Literal literal(objective.literals(i));
307 
308  // We want to minimize the cost when this literal is true.
309  const Literal min_literal =
310  objective.coefficients(i) > 0 ? literal.Negated() : literal;
311  blocking_clauses.push_back(std::vector<Literal>(1, min_literal));
312 
313  // Note that initialy, we do not create any extra variables.
314  assumptions.push_back(min_literal);
315  }
316 
317  // Print the number of variable with a non-zero cost.
318  logger.Log(absl::StrFormat("c #weights:%u #vars:%d #constraints:%d",
319  assumptions.size(), problem.num_variables(),
320  problem.constraints_size()));
321 
322  // Starts the algorithm. Each loop will solve the problem under the given
323  // assumptions, and if unsat, will relax exactly one of the objective
324  // variables (from the unsat core) to be in its "costly" state. When the
325  // algorithm terminates, the number of iterations is exactly the minimal
326  // objective value.
327  for (int iter = 0;; ++iter) {
328  const SatSolver::Status result =
329  solver->ResetAndSolveWithGivenAssumptions(assumptions);
330  if (result == SatSolver::FEASIBLE) {
331  ExtractAssignment(problem, *solver, solution);
332  Coefficient objective = ComputeObjectiveValue(problem, *solution);
333  logger.Log(CnfObjectiveLine(problem, objective));
334  return SatSolver::FEASIBLE;
335  }
336  if (result != SatSolver::ASSUMPTIONS_UNSAT) return result;
337 
338  // The interesting case: we have an unsat core.
339  //
340  // We need to add new "blocking" variables b_i for all the objective
341  // variable appearing in the core. Moreover, we will only relax as little
342  // as possible (to not miss the optimal), so we will enforce that the sum
343  // of the b_i is exactly one.
344  std::vector<Literal> core = solver->GetLastIncompatibleDecisions();
345  MinimizeCore(solver, &core);
346  solver->Backtrack(0);
347 
348  // Print the search progress.
349  logger.Log(absl::StrFormat("c iter:%d core:%u", iter, core.size()));
350 
351  // Special case for a singleton core.
352  if (core.size() == 1) {
353  // Find the index of the "objective" variable that need to be fixed in
354  // its "costly" state.
355  const int index =
356  std::find(assumptions.begin(), assumptions.end(), core[0]) -
357  assumptions.begin();
358  CHECK_LT(index, assumptions.size());
359 
360  // Fix it. We also fix all the associated blocking variables if any.
361  if (!solver->AddUnitClause(core[0].Negated())) {
362  return SatSolver::INFEASIBLE;
363  }
364  for (Literal b : blocking_clauses[index]) {
365  if (!solver->AddUnitClause(b.Negated())) return SatSolver::INFEASIBLE;
366  }
367 
368  // Erase this entry from the current "objective"
369  std::vector<int> to_delete(1, index);
370  DeleteVectorIndices(to_delete, &assumptions);
371  DeleteVectorIndices(to_delete, &blocking_clauses);
372  symmetry.DeleteIndices(to_delete);
373  } else {
374  symmetry.StartResolvingNewCore(iter);
375 
376  // We will add 2 * |core.size()| variables.
377  const int old_num_variables = solver->NumVariables();
378  if (core.size() == 2) {
379  // Special case. If core.size() == 2, we can use only one blocking
380  // variable (the other one beeing its negation). This actually do happen
381  // quite often in practice, so it is worth it.
382  solver->SetNumVariables(old_num_variables + 3);
383  } else {
384  solver->SetNumVariables(old_num_variables + 2 * core.size());
385  }
386 
387  // Temporary vectors for the constraint (sum new blocking variable == 1).
388  std::vector<LiteralWithCoeff> at_most_one_constraint;
389  std::vector<Literal> at_least_one_constraint;
390 
391  // This will be set to false if the problem becomes unsat while adding a
392  // new clause. This is unlikely, but may be possible.
393  bool ok = true;
394 
395  // Loop over the core.
396  int index = 0;
397  for (int i = 0; i < core.size(); ++i) {
398  // Since the assumptions appear in order in the core, we can find the
399  // relevant "objective" variable efficiently with a simple linear scan
400  // in the assumptions vector (done with index).
401  index =
402  std::find(assumptions.begin() + index, assumptions.end(), core[i]) -
403  assumptions.begin();
404  CHECK_LT(index, assumptions.size());
405 
406  // The new blocking and assumption variables for this core entry.
407  const Literal a(BooleanVariable(old_num_variables + i), true);
408  Literal b(BooleanVariable(old_num_variables + core.size() + i), true);
409  if (core.size() == 2) {
410  b = Literal(BooleanVariable(old_num_variables + 2), true);
411  if (i == 1) b = b.Negated();
412  }
413 
414  // Symmetry breaking clauses.
415  for (Literal l : symmetry.ProcessLiteral(index, b)) {
416  ok &= solver->AddBinaryClause(l.Negated(), b.Negated());
417  }
418 
419  // Note(user): There is more than one way to encode the algorithm in
420  // SAT. Here we "delete" the old blocking clause and add a new one. In
421  // the WPM1 algorithm below, the blocking clause is decomposed into
422  // 3-SAT and we don't need to delete anything.
423 
424  // First, fix the old "assumption" variable to false, which has the
425  // effect of deleting the old clause from the solver.
426  if (assumptions[index].Variable() >= problem.num_variables()) {
427  CHECK(solver->AddUnitClause(assumptions[index].Negated()));
428  }
429 
430  // Add the new blocking variable.
431  blocking_clauses[index].push_back(b);
432 
433  // Add the new clause to the solver. Temporary including the
434  // assumption, but removing it right afterwards.
435  blocking_clauses[index].push_back(a);
436  ok &= solver->AddProblemClause(blocking_clauses[index]);
437  blocking_clauses[index].pop_back();
438 
439  // For the "== 1" constraint on the blocking literals.
440  at_most_one_constraint.push_back(LiteralWithCoeff(b, 1.0));
441  at_least_one_constraint.push_back(b);
442 
443  // The new assumption variable replace the old one.
444  assumptions[index] = a.Negated();
445  }
446 
447  // Add the "<= 1" side of the "== 1" constraint.
448  ok &= solver->AddLinearConstraint(false, Coefficient(0), true,
449  Coefficient(1.0),
450  &at_most_one_constraint);
451 
452  // TODO(user): The algorithm does not really need the >= 1 side of this
453  // constraint. Initial investigation shows that it doesn't really help,
454  // but investigate more.
455  if (/* DISABLES CODE */ (false)) {
456  ok &= solver->AddProblemClause(at_least_one_constraint);
457  }
458 
459  if (!ok) {
460  LOG(INFO) << "Infeasible while adding a clause.";
461  return SatSolver::INFEASIBLE;
462  }
463  }
464  }
465 }
466 
468  const LinearBooleanProblem& problem,
469  SatSolver* solver,
470  std::vector<bool>* solution) {
471  Logger logger(log);
472  FuMalikSymmetryBreaker symmetry;
473 
474  // The current lower_bound on the cost.
475  // It will be correct after the initialization.
476  Coefficient lower_bound(static_cast<int64_t>(problem.objective().offset()));
478 
479  // The assumption literals and their associated cost.
480  std::vector<Literal> assumptions;
481  std::vector<Coefficient> costs;
482  std::vector<Literal> reference;
483 
484  // Initialization.
485  const LinearObjective& objective = problem.objective();
486  CHECK_GT(objective.coefficients_size(), 0);
487  for (int i = 0; i < objective.literals_size(); ++i) {
488  const Literal literal(objective.literals(i));
489  const Coefficient coeff(objective.coefficients(i));
490 
491  // We want to minimize the cost when the assumption is true.
492  // Note that initially, we do not create any extra variables.
493  if (coeff > 0) {
494  assumptions.push_back(literal.Negated());
495  costs.push_back(coeff);
496  } else {
497  assumptions.push_back(literal);
498  costs.push_back(-coeff);
499  lower_bound += coeff;
500  }
501  }
502  reference = assumptions;
503 
504  // This is used by the "stratified" approach.
505  Coefficient stratified_lower_bound =
506  *std::max_element(costs.begin(), costs.end());
507 
508  // Print the number of variables with a non-zero cost.
509  logger.Log(absl::StrFormat("c #weights:%u #vars:%d #constraints:%d",
510  assumptions.size(), problem.num_variables(),
511  problem.constraints_size()));
512 
513  for (int iter = 0;; ++iter) {
514  // This is called "hardening" in the literature.
515  // Basically, we know that there is only hardening_threshold weight left
516  // to distribute, so any assumption with a greater cost than this can never
517  // be false. We fix it instead of treating it as an assumption.
518  solver->Backtrack(0);
519  const Coefficient hardening_threshold = upper_bound - lower_bound;
520  CHECK_GE(hardening_threshold, 0);
521  std::vector<int> to_delete;
522  int num_above_threshold = 0;
523  for (int i = 0; i < assumptions.size(); ++i) {
524  if (costs[i] > hardening_threshold) {
525  if (!solver->AddUnitClause(assumptions[i])) {
526  return SatSolver::INFEASIBLE;
527  }
528  to_delete.push_back(i);
529  ++num_above_threshold;
530  } else {
531  // This impact the stratification heuristic.
532  if (solver->Assignment().LiteralIsTrue(assumptions[i])) {
533  to_delete.push_back(i);
534  }
535  }
536  }
537  if (!to_delete.empty()) {
538  logger.Log(absl::StrFormat("c fixed %u assumptions, %d with cost > %d",
539  to_delete.size(), num_above_threshold,
540  hardening_threshold.value()));
541  DeleteVectorIndices(to_delete, &assumptions);
542  DeleteVectorIndices(to_delete, &costs);
543  DeleteVectorIndices(to_delete, &reference);
544  symmetry.DeleteIndices(to_delete);
545  }
546 
547  // This is the "stratification" part.
548  // Extract the assumptions with a cost >= stratified_lower_bound.
549  std::vector<Literal> assumptions_subset;
550  for (int i = 0; i < assumptions.size(); ++i) {
551  if (costs[i] >= stratified_lower_bound) {
552  assumptions_subset.push_back(assumptions[i]);
553  }
554  }
555 
556  const SatSolver::Status result =
557  solver->ResetAndSolveWithGivenAssumptions(assumptions_subset);
558  if (result == SatSolver::FEASIBLE) {
559  // If not all assumptions were taken, continue with a lower stratified
560  // bound. Otherwise we have an optimal solution!
561  //
562  // TODO(user): Try more advanced variant where the bound is lowered by
563  // more than this minimal amount.
564  const Coefficient old_lower_bound = stratified_lower_bound;
565  for (Coefficient cost : costs) {
566  if (cost < old_lower_bound) {
567  if (stratified_lower_bound == old_lower_bound ||
568  cost > stratified_lower_bound) {
569  stratified_lower_bound = cost;
570  }
571  }
572  }
573 
574  ExtractAssignment(problem, *solver, solution);
575  DCHECK(IsAssignmentValid(problem, *solution));
576  const Coefficient objective_offset(
577  static_cast<int64_t>(problem.objective().offset()));
578  const Coefficient objective = ComputeObjectiveValue(problem, *solution);
579  if (objective + objective_offset < upper_bound) {
580  logger.Log(CnfObjectiveLine(problem, objective));
581  upper_bound = objective + objective_offset;
582  }
583 
584  if (stratified_lower_bound < old_lower_bound) continue;
585  return SatSolver::FEASIBLE;
586  }
587  if (result != SatSolver::ASSUMPTIONS_UNSAT) return result;
588 
589  // The interesting case: we have an unsat core.
590  //
591  // We need to add new "blocking" variables b_i for all the objective
592  // variables appearing in the core. Moreover, we will only relax as little
593  // as possible (to not miss the optimal), so we will enforce that the sum
594  // of the b_i is exactly one.
595  std::vector<Literal> core = solver->GetLastIncompatibleDecisions();
596  MinimizeCore(solver, &core);
597  solver->Backtrack(0);
598 
599  // Compute the min cost of all the assertions in the core.
600  // The lower bound will be updated by that much.
601  Coefficient min_cost = kCoefficientMax;
602  {
603  int index = 0;
604  for (int i = 0; i < core.size(); ++i) {
605  index =
606  std::find(assumptions.begin() + index, assumptions.end(), core[i]) -
607  assumptions.begin();
608  CHECK_LT(index, assumptions.size());
609  min_cost = std::min(min_cost, costs[index]);
610  }
611  }
612  lower_bound += min_cost;
613 
614  // Print the search progress.
615  logger.Log(absl::StrFormat(
616  "c iter:%d core:%u lb:%d min_cost:%d strat:%d", iter, core.size(),
617  lower_bound.value(), min_cost.value(), stratified_lower_bound.value()));
618 
619  // This simple line helps a lot on the packup-wpms instances!
620  //
621  // TODO(user): That was because of a bug before in the way
622  // stratified_lower_bound was decremented, not sure it helps that much now.
623  if (min_cost > stratified_lower_bound) {
624  stratified_lower_bound = min_cost;
625  }
626 
627  // Special case for a singleton core.
628  if (core.size() == 1) {
629  // Find the index of the "objective" variable that need to be fixed in
630  // its "costly" state.
631  const int index =
632  std::find(assumptions.begin(), assumptions.end(), core[0]) -
633  assumptions.begin();
634  CHECK_LT(index, assumptions.size());
635 
636  // Fix it.
637  if (!solver->AddUnitClause(core[0].Negated())) {
638  return SatSolver::INFEASIBLE;
639  }
640 
641  // Erase this entry from the current "objective".
642  std::vector<int> to_delete(1, index);
643  DeleteVectorIndices(to_delete, &assumptions);
644  DeleteVectorIndices(to_delete, &costs);
645  DeleteVectorIndices(to_delete, &reference);
646  symmetry.DeleteIndices(to_delete);
647  } else {
648  symmetry.StartResolvingNewCore(iter);
649 
650  // We will add 2 * |core.size()| variables.
651  const int old_num_variables = solver->NumVariables();
652  if (core.size() == 2) {
653  // Special case. If core.size() == 2, we can use only one blocking
654  // variable (the other one beeing its negation). This actually do happen
655  // quite often in practice, so it is worth it.
656  solver->SetNumVariables(old_num_variables + 3);
657  } else {
658  solver->SetNumVariables(old_num_variables + 2 * core.size());
659  }
660 
661  // Temporary vectors for the constraint (sum new blocking variable == 1).
662  std::vector<LiteralWithCoeff> at_most_one_constraint;
663  std::vector<Literal> at_least_one_constraint;
664 
665  // This will be set to false if the problem becomes unsat while adding a
666  // new clause. This is unlikely, but may be possible.
667  bool ok = true;
668 
669  // Loop over the core.
670  int index = 0;
671  for (int i = 0; i < core.size(); ++i) {
672  // Since the assumptions appear in order in the core, we can find the
673  // relevant "objective" variable efficiently with a simple linear scan
674  // in the assumptions vector (done with index).
675  index =
676  std::find(assumptions.begin() + index, assumptions.end(), core[i]) -
677  assumptions.begin();
678  CHECK_LT(index, assumptions.size());
679 
680  // The new blocking and assumption variables for this core entry.
681  const Literal a(BooleanVariable(old_num_variables + i), true);
682  Literal b(BooleanVariable(old_num_variables + core.size() + i), true);
683  if (core.size() == 2) {
684  b = Literal(BooleanVariable(old_num_variables + 2), true);
685  if (i == 1) b = b.Negated();
686  }
687 
688  // a false & b false => previous assumptions (which was false).
689  const Literal old_a = assumptions[index];
690  ok &= solver->AddTernaryClause(a, b, old_a);
691 
692  // Optional. Also add the two implications a => x and b => x where x is
693  // the negation of the previous assumption variable.
694  ok &= solver->AddBinaryClause(a.Negated(), old_a.Negated());
695  ok &= solver->AddBinaryClause(b.Negated(), old_a.Negated());
696 
697  // Optional. Also add the implication a => not(b).
698  ok &= solver->AddBinaryClause(a.Negated(), b.Negated());
699 
700  // This is the difference with the Fu & Malik algorithm.
701  // If the soft clause protected by old_a has a cost greater than
702  // min_cost then:
703  // - its cost is disminished by min_cost.
704  // - an identical clause with cost min_cost is artificially added to
705  // the problem.
706  CHECK_GE(costs[index], min_cost);
707  if (costs[index] == min_cost) {
708  // The new assumption variable replaces the old one.
709  assumptions[index] = a.Negated();
710 
711  // Symmetry breaking clauses.
712  for (Literal l : symmetry.ProcessLiteral(index, b)) {
713  ok &= solver->AddBinaryClause(l.Negated(), b.Negated());
714  }
715  } else {
716  // Since the cost of the given index changes, we need to start a new
717  // "equivalence" class for the symmetry breaking algo and clear the
718  // old one.
719  symmetry.AddInfo(assumptions.size(), b);
720  symmetry.ClearInfo(index);
721 
722  // Reduce the cost of the old assumption.
723  costs[index] -= min_cost;
724 
725  // We add the new assumption with a cost of min_cost.
726  //
727  // Note(user): I think it is nice that these are added after old_a
728  // because assuming old_a will implies all the derived assumptions to
729  // true, and thus they will never appear in a core until old_a is not
730  // an assumption anymore.
731  assumptions.push_back(a.Negated());
732  costs.push_back(min_cost);
733  reference.push_back(reference[index]);
734  }
735 
736  // For the "<= 1" constraint on the blocking literals.
737  // Note(user): we don't add the ">= 1" side because it is not needed for
738  // the correctness and it doesn't seems to help.
739  at_most_one_constraint.push_back(LiteralWithCoeff(b, 1.0));
740 
741  // Because we have a core, we know that at least one of the initial
742  // problem variables must be true. This seems to help a bit.
743  //
744  // TODO(user): Experiment more.
745  at_least_one_constraint.push_back(reference[index].Negated());
746  }
747 
748  // Add the "<= 1" side of the "== 1" constraint.
749  ok &= solver->AddLinearConstraint(false, Coefficient(0), true,
750  Coefficient(1.0),
751  &at_most_one_constraint);
752 
753  // Optional. Add the ">= 1" constraint on the initial problem variables.
754  ok &= solver->AddProblemClause(at_least_one_constraint);
755 
756  if (!ok) {
757  LOG(INFO) << "Unsat while adding a clause.";
758  return SatSolver::INFEASIBLE;
759  }
760  }
761  }
762 }
763 
765  LogBehavior log, const LinearBooleanProblem& problem, int num_times,
766  absl::BitGenRef random, SatSolver* solver, std::vector<bool>* solution) {
767  Logger logger(log);
768  const SatParameters initial_parameters = solver->parameters();
769 
770  SatParameters parameters = initial_parameters;
772 
773  // We start with a low conflict limit and increase it until we are able to
774  // solve the problem at least once. After this, the limit stays the same.
775  int max_number_of_conflicts = 5;
777 
780  Coefficient best(min_seen);
781  for (int i = 0; i < num_times; ++i) {
782  solver->Backtrack(0);
784 
785  parameters.set_max_number_of_conflicts(max_number_of_conflicts);
788  solver->SetParameters(parameters);
789  solver->ResetDecisionHeuristic();
790 
791  const bool use_obj = absl::Bernoulli(random, 1.0 / 4);
792  if (use_obj) UseObjectiveForSatAssignmentPreference(problem, solver);
793 
794  const SatSolver::Status result = solver->Solve();
795  if (result == SatSolver::INFEASIBLE) {
796  // If the problem is INFEASIBLE after we over-constrained the objective,
797  // then we found an optimal solution, otherwise, even the decision problem
798  // is INFEASIBLE.
799  if (best == kCoefficientMax) return SatSolver::INFEASIBLE;
800  return SatSolver::FEASIBLE;
801  }
802  if (result == SatSolver::LIMIT_REACHED) {
803  // We augment the number of conflict until we have one feasible solution.
804  if (best == kCoefficientMax) ++max_number_of_conflicts;
806  continue;
807  }
808 
809  CHECK_EQ(result, SatSolver::FEASIBLE);
810  std::vector<bool> candidate;
811  ExtractAssignment(problem, *solver, &candidate);
812  CHECK(IsAssignmentValid(problem, candidate));
813  const Coefficient objective = ComputeObjectiveValue(problem, candidate);
814  if (objective < best) {
815  *solution = candidate;
816  best = objective;
817  logger.Log(CnfObjectiveLine(problem, objective));
818 
819  // Overconstrain the objective.
820  solver->Backtrack(0);
821  if (!AddObjectiveConstraint(problem, false, Coefficient(0), true,
822  objective - 1, solver)) {
823  return SatSolver::FEASIBLE;
824  }
825  }
826  min_seen = std::min(min_seen, objective);
827  max_seen = std::max(max_seen, objective);
828 
829  logger.Log(absl::StrCat(
830  "c ", objective.value(), " [", min_seen.value(), ", ", max_seen.value(),
831  "] objective_preference: ", use_obj ? "true" : "false", " ",
833  }
834 
835  // Retore the initial parameter (with an updated time limit).
836  parameters = initial_parameters;
838  solver->SetParameters(parameters);
840 }
841 
843  const LinearBooleanProblem& problem,
844  SatSolver* solver,
845  std::vector<bool>* solution) {
846  Logger logger(log);
847 
848  // This has a big positive impact on most problems.
850 
851  Coefficient objective = kCoefficientMax;
852  if (!solution->empty()) {
853  CHECK(IsAssignmentValid(problem, *solution));
854  objective = ComputeObjectiveValue(problem, *solution);
855  }
856  while (true) {
857  if (objective != kCoefficientMax) {
858  // Over constrain the objective.
859  solver->Backtrack(0);
860  if (!AddObjectiveConstraint(problem, false, Coefficient(0), true,
861  objective - 1, solver)) {
862  return SatSolver::FEASIBLE;
863  }
864  }
865 
866  // Solve the problem.
867  const SatSolver::Status result = solver->Solve();
869  if (result == SatSolver::INFEASIBLE) {
870  if (objective == kCoefficientMax) return SatSolver::INFEASIBLE;
871  return SatSolver::FEASIBLE;
872  }
873  if (result == SatSolver::LIMIT_REACHED) {
875  }
876 
877  // Extract the new best solution.
878  CHECK_EQ(result, SatSolver::FEASIBLE);
879  ExtractAssignment(problem, *solver, solution);
880  CHECK(IsAssignmentValid(problem, *solution));
881  const Coefficient old_objective = objective;
882  objective = ComputeObjectiveValue(problem, *solution);
883  CHECK_LT(objective, old_objective);
884  logger.Log(CnfObjectiveLine(problem, objective));
885  }
886 }
887 
889  LogBehavior log, const LinearBooleanProblem& problem, SatSolver* solver,
890  std::vector<bool>* solution) {
891  Logger logger(log);
892  std::deque<EncodingNode> repository;
893 
894  // Create one initial node per variables with cost.
895  Coefficient offset(0);
896  std::vector<EncodingNode*> nodes =
897  CreateInitialEncodingNodes(problem.objective(), &offset, &repository);
898 
899  // This algorithm only work with weights of the same magnitude.
900  CHECK(!nodes.empty());
901  const Coefficient reference = nodes.front()->weight();
902  for (const EncodingNode* n : nodes) CHECK_EQ(n->weight(), reference);
903 
904  // Initialize the current objective.
905  Coefficient objective = kCoefficientMax;
907  if (!solution->empty()) {
908  CHECK(IsAssignmentValid(problem, *solution));
909  objective = ComputeObjectiveValue(problem, *solution);
910  upper_bound = objective + offset;
911  }
912 
913  // Print the number of variables with a non-zero cost.
914  logger.Log(absl::StrFormat("c #weights:%u #vars:%d #constraints:%d",
915  nodes.size(), problem.num_variables(),
916  problem.constraints_size()));
917 
918  // Create the sorter network.
919  solver->Backtrack(0);
920  EncodingNode* root =
921  MergeAllNodesWithDeque(upper_bound, nodes, solver, &repository);
922  logger.Log(absl::StrFormat("c encoding depth:%d", root->depth()));
923 
924  while (true) {
925  if (objective != kCoefficientMax) {
926  // Over constrain the objective by fixing the variable index - 1 of the
927  // root node to 0.
928  const int index = offset.value() + objective.value();
929  if (index == 0) return SatSolver::FEASIBLE;
930  solver->Backtrack(0);
931  if (!solver->AddUnitClause(root->literal(index - 1).Negated())) {
932  return SatSolver::FEASIBLE;
933  }
934  }
935 
936  // Solve the problem.
937  const SatSolver::Status result = solver->Solve();
939  if (result == SatSolver::INFEASIBLE) {
940  if (objective == kCoefficientMax) return SatSolver::INFEASIBLE;
941  return SatSolver::FEASIBLE;
942  }
944 
945  // Extract the new best solution.
946  CHECK_EQ(result, SatSolver::FEASIBLE);
947  ExtractAssignment(problem, *solver, solution);
948  CHECK(IsAssignmentValid(problem, *solution));
949  const Coefficient old_objective = objective;
950  objective = ComputeObjectiveValue(problem, *solution);
951  CHECK_LT(objective, old_objective);
952  logger.Log(CnfObjectiveLine(problem, objective));
953  }
954 }
955 
957  LogBehavior log, const LinearBooleanProblem& problem, SatSolver* solver,
958  std::vector<bool>* solution) {
959  Logger logger(log);
960  SatParameters parameters = solver->parameters();
961 
962  // Create one initial nodes per variables with cost.
963  Coefficient offset(0);
964  std::deque<EncodingNode> repository;
965  std::vector<EncodingNode*> nodes =
966  CreateInitialEncodingNodes(problem.objective(), &offset, &repository);
967 
968  // Initialize the bounds.
969  // This is in term of number of variables not at their minimal value.
972  if (!solution->empty()) {
973  CHECK(IsAssignmentValid(problem, *solution));
974  upper_bound = ComputeObjectiveValue(problem, *solution) + offset;
975  }
976 
977  // Print the number of variables with a non-zero cost.
978  logger.Log(absl::StrFormat("c #weights:%u #vars:%d #constraints:%d",
979  nodes.size(), problem.num_variables(),
980  problem.constraints_size()));
981 
982  // This is used by the "stratified" approach.
983  Coefficient stratified_lower_bound(0);
986  // In this case, we initialize it to the maximum assumption weights.
987  for (EncodingNode* n : nodes) {
988  stratified_lower_bound = std::max(stratified_lower_bound, n->weight());
989  }
990  }
991 
992  // Start the algorithm.
993  int max_depth = 0;
994  std::string previous_core_info = "";
995  for (int iter = 0;; ++iter) {
996  const std::vector<Literal> assumptions = ReduceNodesAndExtractAssumptions(
997  upper_bound, stratified_lower_bound, &lower_bound, &nodes, solver);
998  if (assumptions.empty()) return SatSolver::FEASIBLE;
999 
1000  // Display the progress.
1001  const std::string gap_string =
1003  ? ""
1004  : absl::StrFormat(" gap:%d", (upper_bound - lower_bound).value());
1005  logger.Log(
1006  absl::StrFormat("c iter:%d [%s] lb:%d%s assumptions:%u depth:%d", iter,
1007  previous_core_info,
1008  lower_bound.value() - offset.value() +
1009  static_cast<int64_t>(problem.objective().offset()),
1010  gap_string, nodes.size(), max_depth));
1011 
1012  // Solve under the assumptions.
1013  const SatSolver::Status result =
1014  solver->ResetAndSolveWithGivenAssumptions(assumptions);
1015  if (result == SatSolver::FEASIBLE) {
1016  // Extract the new solution and save it if it is the best found so far.
1017  std::vector<bool> temp_solution;
1018  ExtractAssignment(problem, *solver, &temp_solution);
1019  CHECK(IsAssignmentValid(problem, temp_solution));
1020  const Coefficient obj = ComputeObjectiveValue(problem, temp_solution);
1021  if (obj + offset < upper_bound) {
1022  *solution = temp_solution;
1023  logger.Log(CnfObjectiveLine(problem, obj));
1024  upper_bound = obj + offset;
1025  }
1026 
1027  // If not all assumptions were taken, continue with a lower stratified
1028  // bound. Otherwise we have an optimal solution.
1029  stratified_lower_bound =
1030  MaxNodeWeightSmallerThan(nodes, stratified_lower_bound);
1031  if (stratified_lower_bound > 0) continue;
1032  return SatSolver::FEASIBLE;
1033  }
1034  if (result != SatSolver::ASSUMPTIONS_UNSAT) return result;
1035 
1036  // We have a new core.
1037  std::vector<Literal> core = solver->GetLastIncompatibleDecisions();
1038  if (parameters.minimize_core()) MinimizeCore(solver, &core);
1039 
1040  // Compute the min weight of all the nodes in the core.
1041  // The lower bound will be increased by that much.
1042  const Coefficient min_weight = ComputeCoreMinWeight(nodes, core);
1043  previous_core_info =
1044  absl::StrFormat("core:%u mw:%d", core.size(), min_weight.value());
1045 
1046  // Increase stratified_lower_bound according to the parameters.
1047  if (stratified_lower_bound < min_weight &&
1050  stratified_lower_bound = min_weight;
1051  }
1052 
1053  ProcessCore(core, min_weight, &repository, &nodes, solver);
1054  max_depth = std::max(max_depth, nodes.back()->depth());
1055  }
1056 }
1057 
1059  IntegerVariable objective_var,
1060  const std::function<void()>& feasible_solution_observer, Model* model) {
1061  SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
1062  IntegerTrail* integer_trail = model->GetOrCreate<IntegerTrail>();
1063  const SatParameters& parameters = *(model->GetOrCreate<SatParameters>());
1064 
1065  // Simple linear scan algorithm to find the optimal.
1066  while (true) {
1068  if (result != SatSolver::FEASIBLE) return result;
1069 
1070  // The objective is the current lower bound of the objective_var.
1071  const IntegerValue objective = integer_trail->LowerBound(objective_var);
1072 
1073  // We have a solution!
1074  if (feasible_solution_observer != nullptr) {
1075  feasible_solution_observer();
1076  }
1078  return SatSolver::LIMIT_REACHED;
1079  }
1080 
1081  // Restrict the objective.
1082  sat_solver->Backtrack(0);
1083  if (!integer_trail->Enqueue(
1084  IntegerLiteral::LowerOrEqual(objective_var, objective - 1), {},
1085  {})) {
1086  return SatSolver::INFEASIBLE;
1087  }
1088  }
1089 }
1090 
1092  IntegerVariable objective_var,
1093  const std::function<void()>& feasible_solution_observer, Model* model) {
1094  const SatParameters old_params = *model->GetOrCreate<SatParameters>();
1095  SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
1096  IntegerTrail* integer_trail = model->GetOrCreate<IntegerTrail>();
1097  IntegerEncoder* integer_encoder = model->GetOrCreate<IntegerEncoder>();
1098 
1099  // Set the requested conflict limit.
1100  {
1101  SatParameters new_params = old_params;
1102  new_params.set_max_number_of_conflicts(
1103  old_params.binary_search_num_conflicts());
1104  *model->GetOrCreate<SatParameters>() = new_params;
1105  }
1106 
1107  // The assumption (objective <= value) for values in
1108  // [unknown_min, unknown_max] reached the conflict limit.
1109  bool loop = true;
1110  IntegerValue unknown_min = integer_trail->UpperBound(objective_var);
1111  IntegerValue unknown_max = integer_trail->LowerBound(objective_var);
1112  while (loop) {
1113  sat_solver->Backtrack(0);
1114  const IntegerValue lb = integer_trail->LowerBound(objective_var);
1115  const IntegerValue ub = integer_trail->UpperBound(objective_var);
1116  unknown_min = std::min(unknown_min, ub);
1117  unknown_max = std::max(unknown_max, lb);
1118 
1119  // We first refine the lower bound and then the upper bound.
1120  IntegerValue target;
1121  if (lb < unknown_min) {
1122  target = lb + (unknown_min - lb) / 2;
1123  } else if (unknown_max < ub) {
1124  target = ub - (ub - unknown_max) / 2;
1125  } else {
1126  VLOG(1) << "Binary-search, done.";
1127  break;
1128  }
1129  VLOG(1) << "Binary-search, objective: [" << lb << "," << ub << "]"
1130  << " tried: [" << unknown_min << "," << unknown_max << "]"
1131  << " target: obj<=" << target;
1132  SatSolver::Status result;
1133  if (target < ub) {
1134  const Literal assumption = integer_encoder->GetOrCreateAssociatedLiteral(
1135  IntegerLiteral::LowerOrEqual(objective_var, target));
1136  result = ResetAndSolveIntegerProblem({assumption}, model);
1137  } else {
1138  result = ResetAndSolveIntegerProblem({}, model);
1139  }
1140 
1141  switch (result) {
1142  case SatSolver::INFEASIBLE: {
1143  loop = false;
1144  break;
1145  }
1147  // Update the objective lower bound.
1148  sat_solver->Backtrack(0);
1149  if (!integer_trail->Enqueue(
1150  IntegerLiteral::GreaterOrEqual(objective_var, target + 1), {},
1151  {})) {
1152  loop = false;
1153  }
1154  break;
1155  }
1156  case SatSolver::FEASIBLE: {
1157  // The objective is the current lower bound of the objective_var.
1158  const IntegerValue objective = integer_trail->LowerBound(objective_var);
1159  if (feasible_solution_observer != nullptr) {
1160  feasible_solution_observer();
1161  }
1162 
1163  // We have a solution, restrict the objective upper bound to only look
1164  // for better ones now.
1165  sat_solver->Backtrack(0);
1166  if (!integer_trail->Enqueue(
1167  IntegerLiteral::LowerOrEqual(objective_var, objective - 1), {},
1168  {})) {
1169  loop = false;
1170  }
1171  break;
1172  }
1173  case SatSolver::LIMIT_REACHED: {
1174  unknown_min = std::min(target, unknown_min);
1175  unknown_max = std::max(target, unknown_max);
1176  break;
1177  }
1178  }
1179  }
1180 
1181  sat_solver->Backtrack(0);
1182  *model->GetOrCreate<SatParameters>() = old_params;
1183 }
1184 
1185 namespace {
1186 
1187 // If the given model is unsat under the given assumptions, returns one or more
1188 // non-overlapping set of assumptions, each set making the problem infeasible on
1189 // its own (the cores).
1190 //
1191 // In presence of weights, we "generalize" the notions of disjoints core using
1192 // the WCE idea describe in "Weight-Aware Core Extraction in SAT-Based MaxSAT
1193 // solving" Jeremias Berg And Matti Jarvisalo.
1194 //
1195 // The returned status can be either:
1196 // - ASSUMPTIONS_UNSAT if the set of returned core perfectly cover the given
1197 // assumptions, in this case, we don't bother trying to find a SAT solution
1198 // with no assumptions.
1199 // - FEASIBLE if after finding zero or more core we have a solution.
1200 // - LIMIT_REACHED if we reached the time-limit before one of the two status
1201 // above could be decided.
1202 //
1203 // TODO(user): There is many way to combine the WCE and stratification
1204 // heuristics. I didn't had time to properly compare the different approach. See
1205 // the WCE papers for some ideas, but there is many more ways to try to find a
1206 // lot of core at once and try to minimize the minimum weight of each of the
1207 // cores.
1208 SatSolver::Status FindCores(std::vector<Literal> assumptions,
1209  std::vector<IntegerValue> assumption_weights,
1210  IntegerValue stratified_threshold, Model* model,
1211  std::vector<std::vector<Literal>>* cores) {
1212  cores->clear();
1213  SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
1214  TimeLimit* limit = model->GetOrCreate<TimeLimit>();
1215  do {
1216  if (limit->LimitReached()) return SatSolver::LIMIT_REACHED;
1217 
1218  const SatSolver::Status result =
1219  ResetAndSolveIntegerProblem(assumptions, model);
1220  if (result != SatSolver::ASSUMPTIONS_UNSAT) return result;
1221  std::vector<Literal> core = sat_solver->GetLastIncompatibleDecisions();
1222  if (sat_solver->parameters().minimize_core()) {
1223  MinimizeCoreWithPropagation(limit, sat_solver, &core);
1224  }
1225  if (core.empty()) return sat_solver->UnsatStatus();
1226  cores->push_back(core);
1227  if (!sat_solver->parameters().find_multiple_cores()) break;
1228 
1229  // Recover the original indices of the assumptions that are part of the
1230  // core.
1231  std::vector<int> indices;
1232  {
1233  std::set<Literal> temp(core.begin(), core.end());
1234  for (int i = 0; i < assumptions.size(); ++i) {
1235  if (gtl::ContainsKey(temp, assumptions[i])) {
1236  indices.push_back(i);
1237  }
1238  }
1239  }
1240 
1241  // Remove min_weight from the weights of all the assumptions in the core.
1242  //
1243  // TODO(user): push right away the objective bound by that much? This should
1244  // be better in a multi-threading context as we can share more quickly the
1245  // better bound.
1246  IntegerValue min_weight = assumption_weights[indices.front()];
1247  for (const int i : indices) {
1248  min_weight = std::min(min_weight, assumption_weights[i]);
1249  }
1250  for (const int i : indices) {
1251  assumption_weights[i] -= min_weight;
1252  }
1253 
1254  // Remove from assumptions all the one with a new weight smaller than the
1255  // current stratification threshold and see if we can find another core.
1256  int new_size = 0;
1257  for (int i = 0; i < assumptions.size(); ++i) {
1258  if (assumption_weights[i] < stratified_threshold) continue;
1259  assumptions[new_size] = assumptions[i];
1260  assumption_weights[new_size] = assumption_weights[i];
1261  ++new_size;
1262  }
1263  assumptions.resize(new_size);
1264  assumption_weights.resize(new_size);
1265  } while (!assumptions.empty());
1267 }
1268 
1269 // Slightly different algo than FindCores() which aim to extract more cores, but
1270 // not necessarily non-overlaping ones.
1271 SatSolver::Status FindMultipleCoresForMaxHs(
1272  std::vector<Literal> assumptions, Model* model,
1273  std::vector<std::vector<Literal>>* cores) {
1274  cores->clear();
1275  SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
1276  TimeLimit* limit = model->GetOrCreate<TimeLimit>();
1277  do {
1278  if (limit->LimitReached()) return SatSolver::LIMIT_REACHED;
1279 
1280  const SatSolver::Status result =
1281  ResetAndSolveIntegerProblem(assumptions, model);
1282  if (result != SatSolver::ASSUMPTIONS_UNSAT) return result;
1283  std::vector<Literal> core = sat_solver->GetLastIncompatibleDecisions();
1284  if (sat_solver->parameters().minimize_core()) {
1285  MinimizeCoreWithPropagation(limit, sat_solver, &core);
1286  }
1287  CHECK(!core.empty());
1288  cores->push_back(core);
1289  if (!sat_solver->parameters().find_multiple_cores()) break;
1290 
1291  // Pick a random literal from the core and remove it from the set of
1292  // assumptions.
1293  CHECK(!core.empty());
1294  auto* random = model->GetOrCreate<ModelRandomGenerator>();
1295  const Literal random_literal =
1296  core[absl::Uniform<int>(*random, 0, core.size())];
1297  for (int i = 0; i < assumptions.size(); ++i) {
1298  if (assumptions[i] == random_literal) {
1299  std::swap(assumptions[i], assumptions.back());
1300  assumptions.pop_back();
1301  break;
1302  }
1303  }
1304  } while (!assumptions.empty());
1306 }
1307 
1308 } // namespace
1309 
1311  IntegerVariable objective_var,
1312  const std::vector<IntegerVariable>& variables,
1313  const std::vector<IntegerValue>& coefficients,
1314  std::function<void()> feasible_solution_observer, Model* model)
1315  : parameters_(model->GetOrCreate<SatParameters>()),
1316  sat_solver_(model->GetOrCreate<SatSolver>()),
1317  time_limit_(model->GetOrCreate<TimeLimit>()),
1318  integer_trail_(model->GetOrCreate<IntegerTrail>()),
1319  integer_encoder_(model->GetOrCreate<IntegerEncoder>()),
1320  model_(model),
1321  objective_var_(objective_var),
1322  feasible_solution_observer_(std::move(feasible_solution_observer)) {
1323  CHECK_EQ(variables.size(), coefficients.size());
1324  for (int i = 0; i < variables.size(); ++i) {
1325  if (coefficients[i] > 0) {
1326  terms_.push_back({variables[i], coefficients[i]});
1327  } else if (coefficients[i] < 0) {
1328  terms_.push_back({NegationOf(variables[i]), -coefficients[i]});
1329  } else {
1330  continue; // coefficients[i] == 0
1331  }
1332  terms_.back().depth = 0;
1333  }
1334 
1335  // This is used by the "stratified" approach. We will only consider terms with
1336  // a weight not lower than this threshold. The threshold will decrease as the
1337  // algorithm progress.
1338  stratification_threshold_ = parameters_->max_sat_stratification() ==
1340  ? IntegerValue(1)
1341  : kMaxIntegerValue;
1342 }
1343 
1344 bool CoreBasedOptimizer::ProcessSolution() {
1345  // We don't assume that objective_var is linked with its linear term, so
1346  // we recompute the objective here.
1347  IntegerValue objective(0);
1348  for (ObjectiveTerm& term : terms_) {
1349  const IntegerValue value = integer_trail_->LowerBound(term.var);
1350  objective += term.weight * value;
1351 
1352  // Also keep in term.cover_ub the minimum value for term.var that we have
1353  // seens amongst all the feasible solutions found so far.
1354  term.cover_ub = std::min(term.cover_ub, value);
1355  }
1356 
1357  // We use the level zero upper bound of the objective to indicate an upper
1358  // limit for the solution objective we are looking for. Again, because the
1359  // objective_var is not assumed to be linked, it could take any value in the
1360  // current solution.
1361  if (objective > integer_trail_->LevelZeroUpperBound(objective_var_)) {
1362  return true;
1363  }
1364 
1365  if (feasible_solution_observer_ != nullptr) {
1366  feasible_solution_observer_();
1367  }
1368  if (parameters_->stop_after_first_solution()) {
1369  stop_ = true;
1370  }
1371 
1372  // Constrain objective_var. This has a better result when objective_var is
1373  // used in an LP relaxation for instance.
1374  sat_solver_->Backtrack(0);
1375  sat_solver_->SetAssumptionLevel(0);
1376  return integer_trail_->Enqueue(
1377  IntegerLiteral::LowerOrEqual(objective_var_, objective - 1), {}, {});
1378 }
1379 
1380 bool CoreBasedOptimizer::PropagateObjectiveBounds() {
1381  // We assumes all terms (modulo stratification) at their lower-bound.
1382  bool some_bound_were_tightened = true;
1383  while (some_bound_were_tightened) {
1384  some_bound_were_tightened = false;
1385  if (!sat_solver_->ResetToLevelZero()) return false;
1386 
1387  // Compute implied lb.
1388  IntegerValue implied_objective_lb(0);
1389  for (ObjectiveTerm& term : terms_) {
1390  const IntegerValue var_lb = integer_trail_->LowerBound(term.var);
1391  term.old_var_lb = var_lb;
1392  implied_objective_lb += term.weight * var_lb.value();
1393  }
1394 
1395  // Update the objective lower bound with our current bound.
1396  if (implied_objective_lb > integer_trail_->LowerBound(objective_var_)) {
1397  if (!integer_trail_->Enqueue(IntegerLiteral::GreaterOrEqual(
1398  objective_var_, implied_objective_lb),
1399  {}, {})) {
1400  return false;
1401  }
1402 
1403  some_bound_were_tightened = true;
1404  }
1405 
1406  // The gap is used to propagate the upper-bound of all variable that are
1407  // in the current objective (Exactly like done in the propagation of a
1408  // linear constraint with the slack). When this fix a variable to its
1409  // lower bound, it is called "hardening" in the max-sat literature. This
1410  // has a really beneficial effect on some weighted max-sat problems like
1411  // the haplotyping-pedigrees ones.
1412  const IntegerValue gap =
1413  integer_trail_->UpperBound(objective_var_) - implied_objective_lb;
1414 
1415  for (const ObjectiveTerm& term : terms_) {
1416  if (term.weight == 0) continue;
1417  const IntegerValue var_lb = integer_trail_->LowerBound(term.var);
1418  const IntegerValue var_ub = integer_trail_->UpperBound(term.var);
1419  if (var_lb == var_ub) continue;
1420 
1421  // Hardening. This basically just propagate the implied upper bound on
1422  // term.var from the current best solution. Note that the gap is
1423  // non-negative and the weight positive here. The test is done in order
1424  // to avoid any integer overflow provided (ub - lb) do not overflow, but
1425  // this is a precondition in our cp-model.
1426  if (gap / term.weight < var_ub - var_lb) {
1427  some_bound_were_tightened = true;
1428  const IntegerValue new_ub = var_lb + gap / term.weight;
1429  CHECK_LT(new_ub, var_ub);
1430  CHECK(!integer_trail_->IsCurrentlyIgnored(term.var));
1431  if (!integer_trail_->Enqueue(
1432  IntegerLiteral::LowerOrEqual(term.var, new_ub), {}, {})) {
1433  return false;
1434  }
1435  }
1436  }
1437  }
1438  return true;
1439 }
1440 
1441 // A basic algorithm is to take the next one, or at least the next one
1442 // that invalidate the current solution. But to avoid corner cases for
1443 // problem with a lot of terms all with different objective weights (in
1444 // which case we will kind of introduce only one assumption per loop
1445 // which is little), we use an heuristic and take the 90% percentile of
1446 // the unique weights not yet included.
1447 //
1448 // TODO(user): There is many other possible heuristics here, and I
1449 // didn't have the time to properly compare them.
1450 void CoreBasedOptimizer::ComputeNextStratificationThreshold() {
1451  std::vector<IntegerValue> weights;
1452  for (ObjectiveTerm& term : terms_) {
1453  if (term.weight >= stratification_threshold_) continue;
1454  if (term.weight == 0) continue;
1455 
1456  const IntegerValue var_lb = integer_trail_->LevelZeroLowerBound(term.var);
1457  const IntegerValue var_ub = integer_trail_->LevelZeroUpperBound(term.var);
1458  if (var_lb == var_ub) continue;
1459 
1460  weights.push_back(term.weight);
1461  }
1462  if (weights.empty()) {
1463  stratification_threshold_ = IntegerValue(0);
1464  return;
1465  }
1466 
1468  stratification_threshold_ =
1469  weights[static_cast<int>(std::floor(0.9 * weights.size()))];
1470 }
1471 
1472 bool CoreBasedOptimizer::CoverOptimization() {
1473  // We set a fix deterministic time limit per all sub-solve and skip to the
1474  // next core if the sum of the subsolve is also over this limit.
1475  constexpr double max_dtime_per_core = 0.5;
1476  const double old_time_limit = parameters_->max_deterministic_time();
1477  parameters_->set_max_deterministic_time(max_dtime_per_core);
1478  auto cleanup = ::absl::MakeCleanup([old_time_limit, this]() {
1479  parameters_->set_max_deterministic_time(old_time_limit);
1480  });
1481 
1482  for (const ObjectiveTerm& term : terms_) {
1483  // We currently skip the initial objective terms as there could be many
1484  // of them. TODO(user): provide an option to cover-optimize them? I
1485  // fear that this will slow down the solver too much though.
1486  if (term.depth == 0) continue;
1487 
1488  // Find out the true lower bound of var. This is called "cover
1489  // optimization" in some of the max-SAT literature. It can helps on some
1490  // problem families and hurt on others, but the overall impact is
1491  // positive.
1492  const IntegerVariable var = term.var;
1493  IntegerValue best =
1494  std::min(term.cover_ub, integer_trail_->UpperBound(var));
1495 
1496  // Note(user): this can happen in some corner case because each time we
1497  // find a solution, we constrain the objective to be smaller than it, so
1498  // it is possible that a previous best is now infeasible.
1499  if (best <= integer_trail_->LowerBound(var)) continue;
1500 
1501  // Compute the global deterministic time for this core cover
1502  // optimization.
1503  const double deterministic_limit =
1504  time_limit_->GetElapsedDeterministicTime() + max_dtime_per_core;
1505 
1506  // Simple linear scan algorithm to find the optimal of var.
1507  SatSolver::Status result;
1508  while (best > integer_trail_->LowerBound(var)) {
1509  const Literal assumption = integer_encoder_->GetOrCreateAssociatedLiteral(
1510  IntegerLiteral::LowerOrEqual(var, best - 1));
1511  result = ResetAndSolveIntegerProblem({assumption}, model_);
1512  if (result != SatSolver::FEASIBLE) break;
1513 
1514  best = integer_trail_->LowerBound(var);
1515  VLOG(1) << "cover_opt var:" << var << " domain:["
1516  << integer_trail_->LevelZeroLowerBound(var) << "," << best << "]";
1517  if (!ProcessSolution()) return false;
1518  if (!sat_solver_->ResetToLevelZero()) return false;
1519  if (stop_ ||
1520  time_limit_->GetElapsedDeterministicTime() > deterministic_limit) {
1521  break;
1522  }
1523  }
1524  if (result == SatSolver::INFEASIBLE) return false;
1525  if (result == SatSolver::ASSUMPTIONS_UNSAT) {
1526  // TODO(user): If we improve the lower bound of var, we should check
1527  // if our global lower bound reached our current best solution in
1528  // order to abort early if the optimal is proved.
1529  if (!integer_trail_->Enqueue(IntegerLiteral::GreaterOrEqual(var, best),
1530  {}, {})) {
1531  return false;
1532  }
1533  }
1534  }
1535 
1536  if (!PropagateObjectiveBounds()) return false;
1537  return true;
1538 }
1539 
1541  // TODO(user): The core is returned in the same order as the assumptions,
1542  // so we don't really need this map, we could just do a linear scan to
1543  // recover which node are part of the core. This however needs to be properly
1544  // unit tested before usage.
1545  std::map<LiteralIndex, int> literal_to_term_index;
1546 
1547  // Start the algorithm.
1548  stop_ = false;
1549  while (true) {
1550  // TODO(user): This always resets the solver to level zero.
1551  // Because of that we don't resume a solve in "chunk" perfectly. Fix.
1552  if (!PropagateObjectiveBounds()) return SatSolver::INFEASIBLE;
1553 
1554  // Bulk cover optimization.
1555  //
1556  // TODO(user): If the search is aborted during this phase and we solve in
1557  // "chunk", we don't resume perfectly from where it was. Fix.
1558  if (parameters_->cover_optimization()) {
1559  if (!CoverOptimization()) return SatSolver::INFEASIBLE;
1560  if (stop_) return SatSolver::LIMIT_REACHED;
1561  }
1562 
1563  // We assumes all terms (modulo stratification) at their lower-bound.
1564  std::vector<int> term_indices;
1565  std::vector<IntegerLiteral> integer_assumptions;
1566  std::vector<IntegerValue> assumption_weights;
1567  IntegerValue objective_offset(0);
1568  bool some_assumptions_were_skipped = false;
1569  for (int i = 0; i < terms_.size(); ++i) {
1570  const ObjectiveTerm term = terms_[i];
1571 
1572  // TODO(user): These can be simply removed from the list.
1573  if (term.weight == 0) continue;
1574 
1575  // Skip fixed terms.
1576  // We still keep them around for a proper lower-bound computation.
1577  //
1578  // TODO(user): we could keep an objective offset instead.
1579  const IntegerValue var_lb = integer_trail_->LowerBound(term.var);
1580  const IntegerValue var_ub = integer_trail_->UpperBound(term.var);
1581  if (var_lb == var_ub) {
1582  objective_offset += term.weight * var_lb.value();
1583  continue;
1584  }
1585 
1586  // Only consider the terms above the threshold.
1587  if (term.weight >= stratification_threshold_) {
1588  integer_assumptions.push_back(
1589  IntegerLiteral::LowerOrEqual(term.var, var_lb));
1590  assumption_weights.push_back(term.weight);
1591  term_indices.push_back(i);
1592  } else {
1593  some_assumptions_were_skipped = true;
1594  }
1595  }
1596 
1597  // No assumptions with the current stratification? use the next one.
1598  if (term_indices.empty() && some_assumptions_were_skipped) {
1599  ComputeNextStratificationThreshold();
1600  continue;
1601  }
1602 
1603  // If there is only one or two assumptions left, we switch the algorithm.
1604  if (term_indices.size() <= 2 && !some_assumptions_were_skipped) {
1605  VLOG(1) << "Switching to linear scan...";
1606  if (!already_switched_to_linear_scan_) {
1607  already_switched_to_linear_scan_ = true;
1608  std::vector<IntegerVariable> constraint_vars;
1609  std::vector<int64_t> constraint_coeffs;
1610  for (const int index : term_indices) {
1611  constraint_vars.push_back(terms_[index].var);
1612  constraint_coeffs.push_back(terms_[index].weight.value());
1613  }
1614  constraint_vars.push_back(objective_var_);
1615  constraint_coeffs.push_back(-1);
1616  model_->Add(WeightedSumLowerOrEqual(constraint_vars, constraint_coeffs,
1617  -objective_offset.value()));
1618  }
1619 
1621  objective_var_, feasible_solution_observer_, model_);
1622  }
1623 
1624  // Display the progress.
1625  if (VLOG_IS_ON(1)) {
1626  int max_depth = 0;
1627  for (const ObjectiveTerm& term : terms_) {
1628  max_depth = std::max(max_depth, term.depth);
1629  }
1630  const int64_t lb = integer_trail_->LowerBound(objective_var_).value();
1631  const int64_t ub = integer_trail_->UpperBound(objective_var_).value();
1632  const int gap =
1633  lb == ub
1634  ? 0
1635  : static_cast<int>(std::ceil(
1636  100.0 * (ub - lb) / std::max(std::abs(ub), std::abs(lb))));
1637  VLOG(1) << absl::StrCat("unscaled_next_obj_range:[", lb, ",", ub,
1638  "]"
1639  " gap:",
1640  gap, "%", " assumptions:", term_indices.size(),
1641  " strat:", stratification_threshold_.value(),
1642  " depth:", max_depth);
1643  }
1644 
1645  // Convert integer_assumptions to Literals.
1646  std::vector<Literal> assumptions;
1647  literal_to_term_index.clear();
1648  for (int i = 0; i < integer_assumptions.size(); ++i) {
1649  assumptions.push_back(integer_encoder_->GetOrCreateAssociatedLiteral(
1650  integer_assumptions[i]));
1651 
1652  // Tricky: In some rare case, it is possible that the same literal
1653  // correspond to more that one assumptions. In this case, we can just
1654  // pick one of them when converting back a core to term indices.
1655  //
1656  // TODO(user): We can probably be smarter about the cost of the
1657  // assumptions though.
1658  literal_to_term_index[assumptions.back().Index()] = term_indices[i];
1659  }
1660 
1661  // Solve under the assumptions.
1662  //
1663  // TODO(user): If the "search" is interrupted while computing cores, we
1664  // currently do not resume it flawlessly. We however add any cores we found
1665  // before aborting.
1666  std::vector<std::vector<Literal>> cores;
1667  const SatSolver::Status result =
1668  FindCores(assumptions, assumption_weights, stratification_threshold_,
1669  model_, &cores);
1670  if (result == SatSolver::INFEASIBLE) return SatSolver::INFEASIBLE;
1671  if (result == SatSolver::FEASIBLE) {
1672  if (!ProcessSolution()) return SatSolver::INFEASIBLE;
1673  if (stop_) return SatSolver::LIMIT_REACHED;
1674  if (cores.empty()) {
1675  ComputeNextStratificationThreshold();
1676  if (stratification_threshold_ == 0) return SatSolver::INFEASIBLE;
1677  continue;
1678  }
1679  }
1680 
1681  // Process the cores by creating new variables and transferring the minimum
1682  // weight of each core to it.
1683  if (!sat_solver_->ResetToLevelZero()) return SatSolver::INFEASIBLE;
1684  for (const std::vector<Literal>& core : cores) {
1685  // This just increase the lower-bound of the corresponding node, which
1686  // should already be done by the solver.
1687  if (core.size() == 1) continue;
1688 
1689  // Compute the min weight of all the terms in the core. The lower bound
1690  // will be increased by that much because at least one assumption in the
1691  // core must be true. This is also why we can start at 1 for new_var_lb.
1692  bool ignore_this_core = false;
1693  IntegerValue min_weight = kMaxIntegerValue;
1694  IntegerValue max_weight(0);
1695  IntegerValue new_var_lb(1);
1696  IntegerValue new_var_ub(0);
1697  int new_depth = 0;
1698  for (const Literal lit : core) {
1699  const int index = gtl::FindOrDie(literal_to_term_index, lit.Index());
1700 
1701  // When this happen, the core is now trivially "minimized" by the new
1702  // bound on this variable, so there is no point in adding it.
1703  if (terms_[index].old_var_lb <
1704  integer_trail_->LowerBound(terms_[index].var)) {
1705  ignore_this_core = true;
1706  break;
1707  }
1708 
1709  const IntegerValue weight = terms_[index].weight;
1710  min_weight = std::min(min_weight, weight);
1711  max_weight = std::max(max_weight, weight);
1712  new_depth = std::max(new_depth, terms_[index].depth + 1);
1713  new_var_lb += integer_trail_->LowerBound(terms_[index].var);
1714  new_var_ub += integer_trail_->UpperBound(terms_[index].var);
1715  }
1716  if (ignore_this_core) continue;
1717 
1718  VLOG(1) << absl::StrFormat(
1719  "core:%u weight:[%d,%d] domain:[%d,%d] depth:%d", core.size(),
1720  min_weight.value(), max_weight.value(), new_var_lb.value(),
1721  new_var_ub.value(), new_depth);
1722 
1723  // We will "transfer" min_weight from all the variables of the core
1724  // to a new variable.
1725  const IntegerVariable new_var =
1726  integer_trail_->AddIntegerVariable(new_var_lb, new_var_ub);
1727  terms_.push_back({new_var, min_weight, new_depth});
1728  terms_.back().cover_ub = new_var_ub;
1729 
1730  // Sum variables in the core <= new_var.
1731  {
1732  std::vector<IntegerVariable> constraint_vars;
1733  std::vector<int64_t> constraint_coeffs;
1734  for (const Literal lit : core) {
1735  const int index = gtl::FindOrDie(literal_to_term_index, lit.Index());
1736  terms_[index].weight -= min_weight;
1737  constraint_vars.push_back(terms_[index].var);
1738  constraint_coeffs.push_back(1);
1739  }
1740  constraint_vars.push_back(new_var);
1741  constraint_coeffs.push_back(-1);
1742  model_->Add(
1743  WeightedSumLowerOrEqual(constraint_vars, constraint_coeffs, 0));
1744  }
1745  }
1746 
1747  // Abort if we reached the time limit. Note that we still add any cores we
1748  // found in case the solve is splitted in "chunk".
1749  if (result == SatSolver::LIMIT_REACHED) return result;
1750  }
1751 }
1752 
1753 // TODO(user): take the MPModelRequest or MPModelProto directly, so that we can
1754 // have initial constraints!
1755 //
1756 // TODO(user): remove code duplication with MinimizeWithCoreAndLazyEncoding();
1758  const ObjectiveDefinition& objective_definition,
1759  const std::function<void()>& feasible_solution_observer, Model* model) {
1760 #if !defined(__PORTABLE_PLATFORM__) && defined(USE_SCIP)
1761 
1762  IntegerVariable objective_var = objective_definition.objective_var;
1763  std::vector<IntegerVariable> variables = objective_definition.vars;
1764  std::vector<IntegerValue> coefficients = objective_definition.coeffs;
1765 
1766  SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
1767  IntegerTrail* integer_trail = model->GetOrCreate<IntegerTrail>();
1768  IntegerEncoder* integer_encoder = model->GetOrCreate<IntegerEncoder>();
1769 
1770  // This will be called each time a feasible solution is found.
1771  const auto process_solution = [&]() {
1772  // We don't assume that objective_var is linked with its linear term, so
1773  // we recompute the objective here.
1774  IntegerValue objective(0);
1775  for (int i = 0; i < variables.size(); ++i) {
1776  objective +=
1777  coefficients[i] * IntegerValue(model->Get(Value(variables[i])));
1778  }
1779  if (objective > integer_trail->UpperBound(objective_var)) return true;
1780 
1781  if (feasible_solution_observer != nullptr) {
1782  feasible_solution_observer();
1783  }
1784 
1785  // Constrain objective_var. This has a better result when objective_var is
1786  // used in an LP relaxation for instance.
1787  sat_solver->Backtrack(0);
1788  sat_solver->SetAssumptionLevel(0);
1789  if (!integer_trail->Enqueue(
1790  IntegerLiteral::LowerOrEqual(objective_var, objective - 1), {},
1791  {})) {
1792  return false;
1793  }
1794  return true;
1795  };
1796 
1797  // This is the "generalized" hitting set problem we will solve. Each time
1798  // we find a core, a new constraint will be added to this problem.
1799  MPModelRequest request;
1800  request.set_solver_specific_parameters("limits/gap = 0");
1802 
1803  MPModelProto& hs_model = *request.mutable_model();
1804  const int num_variables_in_objective = variables.size();
1805  for (int i = 0; i < num_variables_in_objective; ++i) {
1806  if (coefficients[i] < 0) {
1807  variables[i] = NegationOf(variables[i]);
1808  coefficients[i] = -coefficients[i];
1809  }
1810  const IntegerVariable var = variables[i];
1811  MPVariableProto* var_proto = hs_model.add_variable();
1812  var_proto->set_lower_bound(integer_trail->LowerBound(var).value());
1813  var_proto->set_upper_bound(integer_trail->UpperBound(var).value());
1814  var_proto->set_objective_coefficient(coefficients[i].value());
1815  var_proto->set_is_integer(true);
1816  }
1817 
1819 
1820  // This is used by the "stratified" approach. We will only consider terms with
1821  // a weight not lower than this threshold. The threshold will decrease as the
1822  // algorithm progress.
1823  IntegerValue stratified_threshold = kMaxIntegerValue;
1824 
1825  // TODO(user): The core is returned in the same order as the assumptions,
1826  // so we don't really need this map, we could just do a linear scan to
1827  // recover which node are part of the core.
1828  std::map<LiteralIndex, std::vector<int>> assumption_to_indices;
1829 
1830  // New Booleans variable in the MIP model to represent X >= cte.
1831  std::map<std::pair<int, double>, int> created_var;
1832 
1833  const SatParameters& parameters = *(model->GetOrCreate<SatParameters>());
1834 
1835  // Start the algorithm.
1836  SatSolver::Status result;
1837  for (int iter = 0;; ++iter) {
1838  // TODO(user): Even though we keep the same solver, currently the solve is
1839  // not really done incrementally. It might be hard to improve though.
1840  //
1841  // TODO(user): deal with time limit.
1844 
1845  const IntegerValue mip_objective(
1846  static_cast<int64_t>(std::round(response.objective_value())));
1847  VLOG(1) << "constraints: " << hs_model.constraint_size()
1848  << " variables: " << hs_model.variable_size() << " hs_lower_bound: "
1849  << objective_definition.ScaleIntegerObjective(mip_objective)
1850  << " strat: " << stratified_threshold;
1851 
1852  // Update the objective lower bound with our current bound.
1853  //
1854  // Note(user): This is not needed for correctness, but it might cause
1855  // more propagation and is nice to have for reporting/logging purpose.
1856  if (!integer_trail->Enqueue(
1857  IntegerLiteral::GreaterOrEqual(objective_var, mip_objective), {},
1858  {})) {
1859  result = SatSolver::INFEASIBLE;
1860  break;
1861  }
1862 
1863  sat_solver->Backtrack(0);
1864  sat_solver->SetAssumptionLevel(0);
1865  std::vector<Literal> assumptions;
1866  assumption_to_indices.clear();
1867  IntegerValue next_stratified_threshold(0);
1868  for (int i = 0; i < num_variables_in_objective; ++i) {
1869  const IntegerValue hs_value(
1870  static_cast<int64_t>(response.variable_value(i)));
1871  if (hs_value == integer_trail->UpperBound(variables[i])) continue;
1872 
1873  // Only consider the terms above the threshold.
1874  if (coefficients[i] < stratified_threshold) {
1875  next_stratified_threshold =
1876  std::max(next_stratified_threshold, coefficients[i]);
1877  } else {
1878  // It is possible that different variables have the same associated
1879  // literal. So we do need to consider this case.
1880  assumptions.push_back(integer_encoder->GetOrCreateAssociatedLiteral(
1881  IntegerLiteral::LowerOrEqual(variables[i], hs_value)));
1882  assumption_to_indices[assumptions.back().Index()].push_back(i);
1883  }
1884  }
1885 
1886  // No assumptions with the current stratified_threshold? use the new one.
1887  if (assumptions.empty() && next_stratified_threshold > 0) {
1888  CHECK_LT(next_stratified_threshold, stratified_threshold);
1889  stratified_threshold = next_stratified_threshold;
1890  --iter; // "false" iteration, the lower bound does not increase.
1891  continue;
1892  }
1893 
1894  // TODO(user): we could also randomly shuffle the assumptions to find more
1895  // cores for only one MIP solve.
1896  //
1897  // TODO(user): Use the real weights and exploit the extra cores.
1898  std::vector<std::vector<Literal>> cores;
1899  result = FindMultipleCoresForMaxHs(assumptions, model, &cores);
1900  if (result == SatSolver::FEASIBLE) {
1901  if (!process_solution()) return SatSolver::INFEASIBLE;
1903  return SatSolver::LIMIT_REACHED;
1904  }
1905  if (cores.empty()) {
1906  // If not all assumptions were taken, continue with a lower stratified
1907  // bound. Otherwise we have an optimal solution.
1908  stratified_threshold = next_stratified_threshold;
1909  if (stratified_threshold == 0) break;
1910  --iter; // "false" iteration, the lower bound does not increase.
1911  continue;
1912  }
1913  } else if (result != SatSolver::ASSUMPTIONS_UNSAT) {
1914  break;
1915  }
1916 
1917  sat_solver->Backtrack(0);
1918  sat_solver->SetAssumptionLevel(0);
1919  for (const std::vector<Literal>& core : cores) {
1920  if (core.size() == 1) {
1921  for (const int index :
1922  gtl::FindOrDie(assumption_to_indices, core.front().Index())) {
1924  integer_trail->LowerBound(variables[index]).value());
1925  }
1926  continue;
1927  }
1928 
1929  // Add the corresponding constraint to hs_model.
1930  MPConstraintProto* ct = hs_model.add_constraint();
1931  ct->set_lower_bound(1.0);
1932  for (const Literal lit : core) {
1933  for (const int index :
1934  gtl::FindOrDie(assumption_to_indices, lit.Index())) {
1935  const double lb = integer_trail->LowerBound(variables[index]).value();
1936  const double hs_value = response.variable_value(index);
1937  if (hs_value == lb) {
1938  ct->add_var_index(index);
1939  ct->add_coefficient(1.0);
1940  ct->set_lower_bound(ct->lower_bound() + lb);
1941  } else {
1942  const std::pair<int, double> key = {index, hs_value};
1943  if (!gtl::ContainsKey(created_var, key)) {
1944  const int new_var_index = hs_model.variable_size();
1945  created_var[key] = new_var_index;
1946 
1947  MPVariableProto* new_var = hs_model.add_variable();
1948  new_var->set_lower_bound(0);
1949  new_var->set_upper_bound(1);
1950  new_var->set_is_integer(true);
1951 
1952  // (new_var == 1) => x > hs_value.
1953  // (x - lb) - (hs_value - lb + 1) * new_var >= 0.
1954  MPConstraintProto* implication = hs_model.add_constraint();
1955  implication->set_lower_bound(lb);
1956  implication->add_var_index(index);
1957  implication->add_coefficient(1.0);
1958  implication->add_var_index(new_var_index);
1959  implication->add_coefficient(lb - hs_value - 1);
1960  }
1961  ct->add_var_index(gtl::FindOrDieNoPrint(created_var, key));
1962  ct->add_coefficient(1.0);
1963  }
1964  }
1965  }
1966  }
1967  }
1968 
1969  return result;
1970 #else // !__PORTABLE_PLATFORM__ && USE_SCIP
1971  LOG(FATAL) << "Not supported.";
1972 #endif // !__PORTABLE_PLATFORM__ && USE_SCIP
1973 }
1974 
1975 } // namespace sat
1976 } // namespace operations_research
SatSolver::Status SolveWithFuMalik(LogBehavior log, const LinearBooleanProblem &problem, SatSolver *solver, std::vector< bool > *solution)
bool AddObjectiveConstraint(const LinearBooleanProblem &problem, bool use_lower_bound, Coefficient lower_bound, bool use_upper_bound, Coefficient upper_bound, SatSolver *solver)
void SetParameters(const SatParameters &parameters)
Definition: sat_solver.cc:116
#define CHECK(condition)
Definition: base/logging.h:495
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:106
void RandomizeDecisionHeuristic(absl::BitGenRef random, SatParameters *parameters)
Definition: sat/util.cc:38
const Collection::value_type::second_type & FindOrDieNoPrint(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition: map_util.h:216
SatSolver::Status SolveWithCardinalityEncoding(LogBehavior log, const LinearBooleanProblem &problem, SatSolver *solver, std::vector< bool > *solution)
IntegerVariable AddIntegerVariable(IntegerValue lower_bound, IntegerValue upper_bound)
Definition: integer.cc:622
const SatParameters & parameters() const
Definition: sat_solver.cc:111
double AddOffsetAndScaleObjectiveValue(const LinearBooleanProblem &problem, Coefficient v)
std::function< void(Model *)> WeightedSumLowerOrEqual(const std::vector< IntegerVariable > &vars, const VectorInt &coefficients, int64_t upper_bound)
Definition: integer_expr.h:361
int64_t min
Definition: alldiff_cst.cc:139
const Coefficient kCoefficientMax(std::numeric_limits< Coefficient::ValueType >::max())
#define CHECK_GE(val1, val2)
Definition: base/logging.h:706
const int FATAL
Definition: log_severity.h:32
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
ModelSharedTimeLimit * time_limit
std::vector< Literal > GetLastIncompatibleDecisions()
Definition: sat_solver.cc:1273
void SetAssumptionLevel(int assumption_level)
Definition: sat_solver.cc:963
#define CHECK_GT(val1, val2)
Definition: base/logging.h:707
static IntegerLiteral LowerOrEqual(IntegerVariable i, IntegerValue bound)
Definition: integer.h:1383
EncodingNode * MergeAllNodesWithDeque(Coefficient upper_bound, const std::vector< EncodingNode * > &nodes, SatSolver *solver, std::deque< EncodingNode > *repository)
Definition: encoding.cc:264
#define VLOG(verboselevel)
Definition: base/logging.h:983
SatSolver::Status MinimizeWithHittingSetAndLazyEncoding(const ObjectiveDefinition &objective_definition, const std::function< void()> &feasible_solution_observer, Model *model)
IntegerValue LowerBound(IntegerVariable i) const
Definition: integer.h:1435
bool LiteralIsFalse(Literal literal) const
Definition: sat_base.h:149
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Definition: id_map.h:263
bool LiteralIsTrue(Literal literal) const
Definition: sat_base.h:152
const ::operations_research::sat::LinearObjective & objective() const
int EnqueueDecisionAndBacktrackOnConflict(Literal true_literal)
Definition: sat_solver.cc:862
#define LOG(severity)
Definition: base/logging.h:420
void MinimizeCore(SatSolver *solver, std::vector< Literal > *core)
Definition: sat_solver.cc:2553
GRBmodel * model
static constexpr SolverType SCIP_MIXED_INTEGER_PROGRAMMING
SatSolver::Status SolveWithRandomParameters(LogBehavior log, const LinearBooleanProblem &problem, int num_times, absl::BitGenRef random, SatSolver *solver, std::vector< bool > *solution)
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
Definition: stl_util.h:58
void SetNumVariables(int num_variables)
Definition: sat_solver.cc:65
bool AddUnitClause(Literal true_literal)
Definition: sat_solver.cc:165
void set_solver_specific_parameters(ArgT0 &&arg0, ArgT... args)
CoreBasedOptimizer(IntegerVariable objective_var, const std::vector< IntegerVariable > &variables, const std::vector< IntegerValue > &coefficients, std::function< void()> feasible_solution_observer, Model *model)
bool AddBinaryClause(Literal a, Literal b)
Definition: sat_solver.cc:181
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
void ExtractAssignment(const LinearBooleanProblem &problem, const SatSolver &solver, std::vector< bool > *assignment)
int64_t b
ABSL_MUST_USE_RESULT bool Enqueue(IntegerLiteral i_lit, absl::Span< const Literal > literal_reason, absl::Span< const IntegerLiteral > integer_reason)
Definition: integer.cc:1027
Coefficient ComputeCoreMinWeight(const std::vector< EncodingNode * > &nodes, const std::vector< Literal > &core)
Definition: encoding.cc:419
std::function< int64_t(const Model &)> Value(IntegerVariable v)
Definition: integer.h:1673
#define CHECK_LT(val1, val2)
Definition: base/logging.h:705
double ScaleIntegerObjective(IntegerValue value) const
bool AddTernaryClause(Literal a, Literal b, Literal c)
Definition: sat_solver.cc:192
int64_t max
Definition: alldiff_cst.cc:140
absl::Cleanup< absl::decay_t< Callback > > MakeCleanup(Callback &&callback)
Definition: cleanup.h:120
::operations_research::MPConstraintProto * add_constraint()
double upper_bound
int64_t weight
Definition: pack.cc:510
SatSolver::Status MinimizeIntegerVariableWithLinearScanAndLazyEncoding(IntegerVariable objective_var, const std::function< void()> &feasible_solution_observer, Model *model)
Literal GetOrCreateAssociatedLiteral(IntegerLiteral i_lit)
Definition: integer.cc:220
int EnqueueDecisionAndBackjumpOnConflict(Literal true_literal)
Definition: sat_solver.cc:500
int core_index
Definition: optimization.cc:86
#define CHECK_LE(val1, val2)
Definition: base/logging.h:704
double lower_bound
IntegerValue LevelZeroUpperBound(IntegerVariable var) const
Definition: integer.h:1524
SatSolver::Status SolveWithLinearScan(LogBehavior log, const LinearBooleanProblem &problem, SatSolver *solver, std::vector< bool > *solution)
bool ContainsKey(const Collection &collection, const Key &key)
Definition: map_util.h:200
const Collection::value_type::second_type & FindOrDie(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition: map_util.h:206
bool IsAssignmentValid(const LinearBooleanProblem &problem, const std::vector< bool > &assignment)
int index
Definition: pack.cc:509
void Backtrack(int target_level)
Definition: sat_solver.cc:889
bool AddLinearConstraint(bool use_lower_bound, Coefficient lower_bound, bool use_upper_bound, Coefficient upper_bound, std::vector< LiteralWithCoeff > *cst)
Definition: sat_solver.cc:300
SharedResponseManager * response
Status ResetAndSolveWithGivenAssumptions(const std::vector< Literal > &assumptions)
Definition: sat_solver.cc:948
std::string message
Definition: trace.cc:398
Coefficient ComputeObjectiveValue(const LinearBooleanProblem &problem, const std::vector< bool > &assignment)
static constexpr MaxSatStratificationAlgorithm STRATIFICATION_NONE
Coefficient MaxNodeWeightSmallerThan(const std::vector< EncodingNode * > &nodes, Coefficient upper_bound)
Definition: encoding.cc:434
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:702
::operations_research::MPModelProto * mutable_model()
SatSolver::Status SolveWithCardinalityEncodingAndCore(LogBehavior log, const LinearBooleanProblem &problem, SatSolver *solver, std::vector< bool > *solution)
int64_t cost
std::vector< EncodingNode * > CreateInitialEncodingNodes(const std::vector< Literal > &literals, const std::vector< Coefficient > &coeffs, Coefficient *offset, std::deque< EncodingNode > *repository)
Definition: encoding.cc:303
std::vector< IntegerVariable > NegationOf(const std::vector< IntegerVariable > &vars)
Definition: integer.cc:30
const VariablesAssignment & Assignment() const
Definition: sat_solver.h:363
Literal literal(int i) const
Definition: encoding.h:79
#define DCHECK(condition)
Definition: base/logging.h:889
SatSolver::Status SolveIntegerProblem(Model *model)
double GetElapsedDeterministicTime() const
Returns the elapsed deterministic time since the construction of this object.
Definition: time_limit.h:261
SatSolver::Status ResetAndSolveIntegerProblem(const std::vector< Literal > &assumptions, Model *model)
T Add(std::function< T(Model *)> f)
This makes it possible to have a nicer API on the client side, and it allows both of these forms:
Definition: sat/model.h:81
bool LimitReached()
Returns true when the external limit is true, or the deterministic time is over the deterministic lim...
Definition: time_limit.h:534
std::tuple< int64_t, int64_t, const double > Coefficient
bool IsCurrentlyIgnored(IntegerVariable i) const
Definition: integer.h:698
::operations_research::MPVariableProto * add_variable()
std::function< int64_t(const Model &)> LowerBound(IntegerVariable v)
Definition: integer.h:1653
std::string ProtobufShortDebugString(const P &message)
::operations_research::MPVariableProto * mutable_variable(int index)
void RestrictObjectiveDomainWithBinarySearch(IntegerVariable objective_var, const std::function< void()> &feasible_solution_observer, Model *model)
IntegerValue UpperBound(IntegerVariable i) const
Definition: integer.h:1439
SatSolver::Status SolveWithWPM1(LogBehavior log, const LinearBooleanProblem &problem, SatSolver *solver, std::vector< bool > *solution)
bool AddProblemClause(absl::Span< const Literal > literals)
Definition: sat_solver.cc:204
Collection of objects used to extend the Constraint Solver library.
void UseObjectiveForSatAssignmentPreference(const LinearBooleanProblem &problem, SatSolver *solver)
static IntegerLiteral GreaterOrEqual(IntegerVariable i, IntegerValue bound)
Definition: integer.h:1377
absl::Span< const double > coefficients
SatParameters parameters
IntVar * var
Definition: expr_array.cc:1874
void ProcessCore(const std::vector< Literal > &core, Coefficient min_weight, std::deque< EncodingNode > *repository, std::vector< EncodingNode * > *nodes, SatSolver *solver)
Definition: encoding.cc:446
::operations_research::sat::SatParameters_MaxSatStratificationAlgorithm max_sat_stratification() const
int MoveOneUnprocessedLiteralLast(const std::set< LiteralIndex > &processed, int relevant_prefix_size, std::vector< Literal > *literals)
Definition: sat/util.cc:190
void set_solver_type(::operations_research::MPModelRequest_SolverType value)
#define VLOG_IS_ON(verboselevel)
Definition: vlog_is_on.h:44
int nodes
static constexpr MaxSatStratificationAlgorithm STRATIFICATION_DESCENT
static void SolveWithProto(const MPModelRequest &model_request, MPSolutionResponse *response, std::atomic< bool > *interrupt=nullptr)
Solves the model encoded by a MPModelRequest protocol buffer and fills the solution encoded as a MPSo...
IntegerValue LevelZeroLowerBound(IntegerVariable var) const
Definition: integer.h:1519
int64_t value
Literal literal
Definition: optimization.cc:85
void MinimizeCoreWithPropagation(TimeLimit *limit, SatSolver *solver, std::vector< Literal > *core)
#define CHECK_NE(val1, val2)
Definition: base/logging.h:703
const Constraint * ct
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:29
const int INFO
Definition: log_severity.h:31
std::vector< Literal > ReduceNodesAndExtractAssumptions(Coefficient upper_bound, Coefficient stratified_lower_bound, Coefficient *lower_bound, std::vector< EncodingNode * > *nodes, SatSolver *solver)
Definition: encoding.cc:367
int64_t a
static constexpr MaxSatStratificationAlgorithm STRATIFICATION_ASCENT