OR-Tools  9.6
sat_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
14 // This file implements a SAT solver.
15 // see http://en.wikipedia.org/wiki/Boolean_satisfiability_problem
16 // for more detail.
17 // TODO(user): Expand.
18 
19 #ifndef OR_TOOLS_SAT_SAT_SOLVER_H_
20 #define OR_TOOLS_SAT_SAT_SOLVER_H_
21 
22 #include <cstdint>
23 #include <functional>
24 #include <limits>
25 #include <memory>
26 #include <ostream>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "absl/container/flat_hash_map.h"
32 #include "absl/strings/string_view.h"
33 #include "absl/types/span.h"
34 #include "ortools/base/hash.h"
36 #include "ortools/base/logging.h"
37 #include "ortools/base/macros.h"
38 #include "ortools/base/timer.h"
39 #include "ortools/sat/clause.h"
41 #include "ortools/sat/model.h"
43 #include "ortools/sat/restart.h"
44 #include "ortools/sat/sat_base.h"
46 #include "ortools/sat/sat_parameters.pb.h"
47 #include "ortools/util/bitset.h"
48 #include "ortools/util/logging.h"
49 #include "ortools/util/stats.h"
52 
53 namespace operations_research {
54 namespace sat {
55 
56 // A constant used by the EnqueueDecision*() API.
57 const int kUnsatTrailIndex = -1;
58 
59 // The main SAT solver.
60 // It currently implements the CDCL algorithm. See
61 // http://en.wikipedia.org/wiki/Conflict_Driven_Clause_Learning
62 class SatSolver {
63  public:
64  SatSolver();
65  explicit SatSolver(Model* model);
66  ~SatSolver();
67 
68  // TODO(user): Remove. This is temporary for accessing the model deep within
69  // some old code that didn't use the Model object.
70  Model* model() { return model_; }
71 
72  // Parameters management. Note that calling SetParameters() will reset the
73  // value of many heuristics. For instance:
74  // - The restart strategy will be reinitialized.
75  // - The random seed and random generator will be reset to the value given in
76  // parameters.
77  // - The global TimeLimit singleton will be reset and time will be
78  // counted from this call.
79  void SetParameters(const SatParameters& parameters);
80  const SatParameters& parameters() const;
81 
82  // Increases the number of variables of the current problem.
83  //
84  // TODO(user): Rename to IncreaseNumVariablesTo() until we support removing
85  // variables...
86  void SetNumVariables(int num_variables);
87  int NumVariables() const { return num_variables_.value(); }
88  BooleanVariable NewBooleanVariable() {
89  const int num_vars = NumVariables();
90 
91  // We need to be able to encode the variable as a literal.
92  CHECK_LT(2 * num_vars, std::numeric_limits<int32_t>::max());
93  SetNumVariables(num_vars + 1);
94  return BooleanVariable(num_vars);
95  }
96 
97  // Fixes a variable so that the given literal is true. This can be used to
98  // solve a subproblem where some variables are fixed. Note that it is more
99  // efficient to add such unit clause before all the others.
100  // Returns false if the problem is detected to be UNSAT.
101  bool AddUnitClause(Literal true_literal);
102 
103  // Same as AddProblemClause() below, but for small clauses.
106 
107  // Adds a clause to the problem. Returns false if the problem is detected to
108  // be UNSAT.
109  // If is_safe is false, we will do some basic presolving like removing
110  // duplicate literals.
111  //
112  // TODO(user): Rename this to AddClause(), also get rid of the specialized
113  // AddUnitClause(), AddBinaryClause() and AddTernaryClause() since they
114  // just end up calling this?
115  bool AddProblemClause(absl::Span<const Literal> literals,
116  bool is_safe = true);
117 
118  // Adds a pseudo-Boolean constraint to the problem. Returns false if the
119  // problem is detected to be UNSAT. If the constraint is always true, this
120  // detects it and does nothing.
121  //
122  // Note(user): There is an optimization if the same constraint is added
123  // consecutively (even if the bounds are different). This is particularly
124  // useful for an optimization problem when we want to constrain the objective
125  // of the problem more and more. Just re-adding such constraint is relatively
126  // efficient.
127  //
128  // OVERFLOW: The sum of the absolute value of all the coefficients
129  // in the constraint must not overflow. This is currently CHECKed().
130  // TODO(user): Instead of failing, implement an error handling code.
131  bool AddLinearConstraint(bool use_lower_bound, Coefficient lower_bound,
132  bool use_upper_bound, Coefficient upper_bound,
133  std::vector<LiteralWithCoeff>* cst);
134 
135  // Returns true if the model is UNSAT. Note that currently the status is
136  // "sticky" and once this happen, nothing else can be done with the solver.
137  //
138  // Thanks to this function, a client can safely ignore the return value of any
139  // Add*() functions. If one of them return false, then ModelIsUnsat() will
140  // return true.
141  bool ModelIsUnsat() const { return model_is_unsat_; }
142 
143  // TODO(user): remove this function.
144  bool IsModelUnsat() const { return model_is_unsat_; } // DEPRECATED
145 
146  // Adds and registers the given propagator with the sat solver. Note that
147  // during propagation, they will be called in the order they were added.
148  void AddPropagator(SatPropagator* propagator);
149  void AddLastPropagator(SatPropagator* propagator);
150  void TakePropagatorOwnership(std::unique_ptr<SatPropagator> propagator) {
151  owned_propagators_.push_back(std::move(propagator));
152  }
153 
154  // Wrapper around the same functions in SatDecisionPolicy.
155  //
156  // TODO(user): Clean this up by making clients directly talk to
157  // SatDecisionPolicy.
159  decision_policy_->SetAssignmentPreference(literal, weight);
160  }
161  std::vector<std::pair<Literal, double>> AllPreferences() const {
162  return decision_policy_->AllPreferences();
163  }
165  return decision_policy_->ResetDecisionHeuristic();
166  }
168  const std::vector<std::pair<Literal, double>>& prefs) {
169  decision_policy_->ResetDecisionHeuristic();
170  for (const std::pair<Literal, double>& p : prefs) {
171  decision_policy_->SetAssignmentPreference(p.first, p.second);
172  }
173  }
174 
175  // Solves the problem and returns its status.
176  // An empty problem is considered to be SAT.
177  //
178  // Note that the conflict limit applies only to this function and starts
179  // counting from the time it is called.
180  //
181  // This will restart from the current solver configuration. If a previous call
182  // to Solve() was interrupted by a conflict or time limit, calling this again
183  // will resume the search exactly as it would have continued.
184  //
185  // Note that this will use the TimeLimit singleton, so the time limit
186  // will be counted since the last time TimeLimit was reset, not from
187  // the start of this function.
188  enum Status {
193  };
194  Status Solve();
195 
196  // Same as Solve(), but with a given time limit. Note that this will not
197  // update the TimeLimit singleton, but only the passed object instead.
199 
200  // Simple interface to solve a problem under the given assumptions. This
201  // simply ask the solver to solve a problem given a set of variables fixed to
202  // a given value (the assumptions). Compared to simply calling AddUnitClause()
203  // and fixing the variables once and for all, this allow to backtrack over the
204  // assumptions and thus exploit the incrementally between subsequent solves.
205  //
206  // This function backtrack over all the current decision, tries to enqueue the
207  // given assumptions, sets the assumption level accordingly and finally calls
208  // Solve().
209  //
210  // If, given these assumptions, the model is UNSAT, this returns the
211  // ASSUMPTIONS_UNSAT status. INFEASIBLE is reserved for the case where the
212  // model is proven to be unsat without any assumptions.
213  //
214  // If ASSUMPTIONS_UNSAT is returned, it is possible to get a "core" of unsat
215  // assumptions by calling GetLastIncompatibleDecisions().
217  const std::vector<Literal>& assumptions);
218 
219  // Changes the assumption level. All the decisions below this level will be
220  // treated as assumptions by the next Solve(). Note that this may impact some
221  // heuristics, like the LBD value of a clause.
222  void SetAssumptionLevel(int assumption_level);
223 
224  // Returns the current assumption level. Note that if a solve was done since
225  // the last SetAssumptionLevel(), then the returned level may be lower than
226  // the one that was set. This is because some assumptions may now be
227  // consequences of others before them due to the newly learned clauses.
228  int AssumptionLevel() const { return assumption_level_; }
229 
230  // This can be called just after SolveWithAssumptions() returned
231  // ASSUMPTION_UNSAT or after EnqueueDecisionAndBacktrackOnConflict() leaded
232  // to a conflict. It returns a subsequence (in the correct order) of the
233  // previously enqueued decisions that cannot be taken together without making
234  // the problem UNSAT.
235  std::vector<Literal> GetLastIncompatibleDecisions();
236 
237  // Advanced usage. The next 3 functions allow to drive the search from outside
238  // the solver.
239 
240  // Takes a new decision (the given true_literal must be unassigned) and
241  // propagates it. Returns the trail index of the first newly propagated
242  // literal. If there is a conflict and the problem is detected to be UNSAT,
243  // returns kUnsatTrailIndex.
244  //
245  // Important: In the presence of assumptions, this also returns
246  // kUnsatTrailIndex on ASSUMPTION_UNSAT. One can know the difference with
247  // IsModelUnsat().
248  //
249  // A client can determine if there is a conflict by checking if the
250  // CurrentDecisionLevel() was increased by 1 or not.
251  //
252  // If there is a conflict, the given decision is not applied and:
253  // - The conflict is learned.
254  // - The decisions are potentially backtracked to the first decision that
255  // propagates more variables because of the newly learned conflict.
256  // - The returned value is equal to trail_->Index() after this backtracking
257  // and just before the new propagation (due to the conflict) which is also
258  // performed by this function.
260 
261  // This function starts by calling EnqueueDecisionAndBackjumpOnConflict(). If
262  // there is no conflict, it stops there. Otherwise, it tries to reapply all
263  // the decisions that were backjumped over until the first one that can't be
264  // taken because it is incompatible. Note that during this process, more
265  // conflicts may happen and the trail may be backtracked even further.
266  //
267  // In any case, the new decisions stack will be the largest valid "prefix"
268  // of the old stack. Note that decisions that are now consequence of the ones
269  // before them will no longer be decisions.
270  //
271  // Returns INFEASIBLE if the model was proven infeasible, ASSUMPTION_UNSAT if
272  // the current decision and the one we are trying to take are not compatible
273  // together and FEASIBLE if all decisions are taken.
274  //
275  // Note(user): This function can be called with an already assigned literal.
277  Literal true_literal, int* first_propagation_index = nullptr);
278 
279  // Tries to enqueue the given decision and performs the propagation.
280  // Returns true if no conflict occurred. Otherwise, returns false and restores
281  // the solver to the state just before this was called.
282  //
283  // Note(user): With this function, the solver doesn't learn anything.
284  bool EnqueueDecisionIfNotConflicting(Literal true_literal);
285 
286  // Restores the state to the given target decision level. The decision at that
287  // level and all its propagation will not be undone. But all the trail after
288  // this will be cleared. Calling this with 0 will revert all the decisions and
289  // only the fixed variables will be left on the trail.
290  void Backtrack(int target_level);
291 
292  // Advanced usage. This is meant to restore the solver to a "proper" state
293  // after a solve was interrupted due to a limit reached.
294  //
295  // Without assumption (i.e. if AssumptionLevel() is 0), this will revert all
296  // decisions and make sure that all the fixed literals are propagated. In
297  // presence of assumptions, this will either backtrack to the assumption level
298  // or re-enqueue any assumptions that may have been backtracked over due to
299  // conflits resolution. In both cases, the propagation is finished.
300  //
301  // Note that this may prove the model to be UNSAT or ASSUMPTION_UNSAT in which
302  // case it will return false.
304 
305  // Advanced usage. Finish the progation if it was interrupted. Note that this
306  // might run into conflict and will propagate again until a fixed point is
307  // reached or the model was proven UNSAT. Returns IsModelUnsat().
308  bool FinishPropagation();
309 
310  // Like Backtrack(0) but make sure the propagation is finished and return
311  // false if unsat was detected. This also removes any assumptions level.
312  bool ResetToLevelZero();
313 
314  // Changes the assumptions level and the current solver assumptions. Returns
315  // false if the model is UNSAT or ASSUMPTION_UNSAT, true otherwise.
316  //
317  // This uses the "new" assumptions handling, where all assumptions are
318  // enqueued at once at decision level 1 before we start to propagate. This has
319  // many advantages. In particular, because we propagate with the binary
320  // implications first, if we ever have assumption => not(other_assumptions) we
321  // are guaranteed to find it and returns a core of size 2.
322  //
323  // Paper: "Speeding Up Assumption-Based SAT", Randy Hickey and Fahiem Bacchus
324  // http://www.maxhs.org/docs/Hickey-Bacchus2019_Chapter_SpeedingUpAssumption-BasedSAT.pdf
325  bool ResetWithGivenAssumptions(const std::vector<Literal>& assumptions);
326 
327  // Advanced usage. If the decision level is smaller than the assumption level,
328  // this will try to reapply all assumptions. Returns true if this was doable,
329  // otherwise returns false in which case the model is either UNSAT or
330  // ASSUMPTION_UNSAT.
332 
333  // Helper functions to get the correct status when one of the functions above
334  // returns false.
335  Status UnsatStatus() const {
337  }
338 
339  // Extract the current problem clauses. The Output type must support the two
340  // functions:
341  // - void AddBinaryClause(Literal a, Literal b);
342  // - void AddClause(absl::Span<const Literal> clause);
343  //
344  // TODO(user): also copy the removable clauses?
345  template <typename Output>
346  void ExtractClauses(Output* out) {
347  CHECK(!IsModelUnsat());
348  Backtrack(0);
349  if (!FinishPropagation()) return;
350 
351  // It is important to process the newly fixed variables, so they are not
352  // present in the clauses we export.
353  if (num_processed_fixed_variables_ < trail_->Index()) {
355  }
356  clauses_propagator_->DeleteRemovedClauses();
357 
358  // Note(user): Putting the binary clauses first help because the presolver
359  // currently process the clauses in order.
360  out->SetNumVariables(NumVariables());
361  binary_implication_graph_->ExtractAllBinaryClauses(out);
362  for (SatClause* clause : clauses_propagator_->AllClausesInCreationOrder()) {
363  if (!clauses_propagator_->IsRemovable(clause)) {
364  out->AddClause(clause->AsSpan());
365  }
366  }
367  }
368 
369  // Functions to manage the set of learned binary clauses.
370  // Only clauses added/learned when TrackBinaryClause() is true are managed.
371  void TrackBinaryClauses(bool value) { track_binary_clauses_ = value; }
372  bool AddBinaryClauses(const std::vector<BinaryClause>& clauses);
373  const std::vector<BinaryClause>& NewlyAddedBinaryClauses();
375 
376  struct Decision {
377  Decision() {}
378  Decision(int i, Literal l) : trail_index(i), literal(l) {}
379  int trail_index = 0;
381  };
382 
383  // Note that the Decisions() vector is always of size NumVariables(), and that
384  // only the first CurrentDecisionLevel() entries have a meaning.
385  const std::vector<Decision>& Decisions() const { return decisions_; }
386  int CurrentDecisionLevel() const { return current_decision_level_; }
387  const Trail& LiteralTrail() const { return *trail_; }
388  const VariablesAssignment& Assignment() const { return trail_->Assignment(); }
389 
390  // Some statistics since the creation of the solver.
391  int64_t num_branches() const;
392  int64_t num_failures() const;
393  int64_t num_propagations() const;
394 
395  // Note that we count the number of backtrack to level zero from a positive
396  // level. Those can corresponds to actual restarts, or conflicts that learn
397  // unit clauses or any other reason that trigger such backtrack.
398  int64_t num_restarts() const;
399 
400  // A deterministic number that should be correlated with the time spent in
401  // the Solve() function. The order of magnitude should be close to the time
402  // in seconds.
403  double deterministic_time() const;
404 
405  // Only used for debugging. Save the current assignment in debug_assignment_.
406  // The idea is that if we know that a given assignment is satisfiable, then
407  // all the learned clauses or PB constraints must be satisfiable by it. In
408  // debug mode, and after this is called, all the learned clauses are tested to
409  // satisfy this saved assignment.
410  void SaveDebugAssignment();
411 
412  // Returns true iff the loaded problem only contains clauses.
413  bool ProblemIsPureSat() const { return problem_is_pure_sat_; }
414 
415  void SetDratProofHandler(DratProofHandler* drat_proof_handler) {
416  drat_proof_handler_ = drat_proof_handler;
417  clauses_propagator_->SetDratProofHandler(drat_proof_handler_);
418  binary_implication_graph_->SetDratProofHandler(drat_proof_handler_);
419  }
420 
421  // This function is here to deal with the case where a SAT/CP model is found
422  // to be trivially UNSAT while the user is constructing the model. Instead of
423  // having to test the status of all the lines adding a constraint, one can
424  // just check if the solver is not UNSAT once the model is constructed. Note
425  // that we usually log a warning on the first constraint that caused a
426  // "trival" unsatisfiability.
427  void NotifyThatModelIsUnsat() { model_is_unsat_ = true; }
428 
429  // Adds a clause at any level of the tree and propagate any new deductions.
430  // Returns false if the model becomes UNSAT. Important: We currently do not
431  // support adding a clause that is already falsified at a positive decision
432  // level. Doing that will cause a check fail.
433  //
434  // TODO(user): Backjump and propagate on a falsified clause? this is currently
435  // not needed.
436  bool AddClauseDuringSearch(absl::Span<const Literal> literals);
437 
438  // Performs propagation of the recently enqueued elements.
439  // Mainly visible for testing.
440  bool Propagate();
441 
442  // This must be called at level zero. It will spend the given num decision and
443  // use propagation to try to minimize some clauses from the database.
444  void MinimizeSomeClauses(int decisions_budget);
445 
446  // Sets the export function to the shared clauses manager.
447  void SetShareBinaryClauseCallback(const std::function<void(Literal, Literal)>&
448  shared_binary_clauses_callback) {
449  shared_binary_clauses_callback_ = shared_binary_clauses_callback;
450  }
451 
452  // Advance the given time limit with all the deterministic time that was
453  // elapsed since last call.
455  const double current = deterministic_time();
457  current - deterministic_time_at_last_advanced_time_limit_);
458  deterministic_time_at_last_advanced_time_limit_ = current;
459  }
460 
461  // Simplifies the problem when new variables are assigned at level 0.
463 
464  int64_t NumFixedVariables() const {
465  if (!decisions_.empty()) return decisions_[0].trail_index;
466  CHECK_EQ(CurrentDecisionLevel(), 0);
467  return trail_->Index();
468  }
469 
470  private:
471  // Calls Propagate() and returns true if no conflict occurred. Otherwise,
472  // learns the conflict, backtracks, enqueues the consequence of the learned
473  // conflict and returns false.
474  //
475  // When handling assumptions, this might return false without backtracking
476  // in case of ASSUMPTIONS_UNSAT.
477  bool PropagateAndStopAfterOneConflictResolution();
478 
479  // All Solve() functions end up calling this one.
480  Status SolveInternal(TimeLimit* time_limit);
481 
482  // Adds a binary clause to the BinaryImplicationGraph and to the
483  // BinaryClauseManager when track_binary_clauses_ is true.
484  //
485  // If export_clause is true, then we will also export_clause that to a
486  // potential shared_binary_clauses_callback_.
487  void AddBinaryClauseInternal(Literal a, Literal b, bool export_clause);
488 
489  // See SaveDebugAssignment(). Note that these functions only consider the
490  // variables at the time the debug_assignment_ was saved. If new variables
491  // were added since that time, they will be considered unassigned.
492  bool ClauseIsValidUnderDebugAssignment(
493  const std::vector<Literal>& clause) const;
494  bool PBConstraintIsValidUnderDebugAssignment(
495  const std::vector<LiteralWithCoeff>& cst, const Coefficient rhs) const;
496 
497  // Logs the given status if parameters_.log_search_progress() is true.
498  // Also returns it.
499  Status StatusWithLog(Status status);
500 
501  // Main function called from SolveWithAssumptions() or from Solve() with an
502  // assumption_level of 0 (meaning no assumptions).
503  Status SolveInternal(int assumption_level);
504 
505  // Applies the previous decisions (which are still on decisions_), in order,
506  // starting from the one at the current decision level. Stops at the one at
507  // decisions_[level] or on the first decision already propagated to "false"
508  // and thus incompatible.
509  //
510  // Note that during this process, conflicts may arise which will lead to
511  // backjumps. In this case, we will simply keep reapplying decisions from the
512  // last one backtracked over and so on.
513  //
514  // Returns FEASIBLE if no conflict occurred, INFEASIBLE if the model was
515  // proven unsat and ASSUMPTION_UNSAT otherwise. In the last case the first non
516  // taken old decision will be propagated to false by the ones before.
517  //
518  // first_propagation_index will be filled with the trail index of the first
519  // newly propagated literal, or with -1 if INFEASIBLE is returned.
520  Status ReapplyDecisionsUpTo(int level,
521  int* first_propagation_index = nullptr);
522 
523  // Returns false if the thread memory is over the limit.
524  bool IsMemoryLimitReached() const;
525 
526  // Sets model_is_unsat_ to true and return false.
527  bool SetModelUnsat();
528 
529  // Returns the decision level of a given variable.
530  int DecisionLevel(BooleanVariable var) const {
531  return trail_->Info(var).level;
532  }
533 
534  // Returns the relevant pointer if the given variable was propagated by the
535  // constraint in question. This is used to bump the activity of the learned
536  // clauses or pb constraints.
537  SatClause* ReasonClauseOrNull(BooleanVariable var) const;
538  UpperBoundedLinearConstraint* ReasonPbConstraintOrNull(
539  BooleanVariable var) const;
540 
541  // This does one step of a pseudo-Boolean resolution:
542  // - The variable var has been assigned to l at a given trail_index.
543  // - The reason for var propagates it to l.
544  // - The conflict propagates it to not(l)
545  // The goal of the operation is to combine the two constraints in order to
546  // have a new conflict at a lower trail_index.
547  //
548  // Returns true if the reason for var was a normal clause. In this case,
549  // the *slack is updated to its new value.
550  bool ResolvePBConflict(BooleanVariable var,
551  MutableUpperBoundedLinearConstraint* conflict,
552  Coefficient* slack);
553 
554  // Returns true iff the clause is the reason for an assigned variable.
555  //
556  // TODO(user): With our current data structures, we could also return true
557  // for clauses that were just used as a reason (like just before an untrail).
558  // This may be beneficial, but should properly be defined so that we can
559  // have the same behavior if we change the implementation.
560  bool ClauseIsUsedAsReason(SatClause* clause) const {
561  const BooleanVariable var = clause->PropagatedLiteral().Variable();
562  return trail_->Info(var).trail_index < trail_->Index() &&
563  (*trail_)[trail_->Info(var).trail_index].Variable() == var &&
564  ReasonClauseOrNull(var) == clause;
565  }
566 
567  // Add a problem clause. The clause is assumed to be "cleaned", that is no
568  // duplicate variables (not strictly required) and not empty.
569  bool AddProblemClauseInternal(absl::Span<const Literal> literals);
570 
571  // This is used by all the Add*LinearConstraint() functions. It detects
572  // infeasible/trivial constraints or clause constraints and takes the proper
573  // action.
574  bool AddLinearConstraintInternal(const std::vector<LiteralWithCoeff>& cst,
575  Coefficient rhs, Coefficient max_value);
576 
577  // Makes sure a pseudo boolean constraint is in canonical form.
578  void CanonicalizeLinear(std::vector<LiteralWithCoeff>* cst,
579  Coefficient* bound_shift, Coefficient* max_value);
580 
581  // Adds a learned clause to the problem. This should be called after
582  // Backtrack(). The backtrack is such that after it is applied, all the
583  // literals of the learned close except one will be false. Thus the last one
584  // will be implied True. This function also Enqueue() the implied literal.
585  //
586  // Returns the LBD of the clause.
587  int AddLearnedClauseAndEnqueueUnitPropagation(
588  const std::vector<Literal>& literals, bool is_redundant);
589 
590  // Creates a new decision which corresponds to setting the given literal to
591  // True and Enqueue() this change.
592  void EnqueueNewDecision(Literal literal);
593 
594  // Returns true if everything has been propagated.
595  //
596  // TODO(user): This test is fast but not exhaustive, especially regarding the
597  // integer propagators. Fix.
598  bool PropagationIsDone() const;
599 
600  // Update the propagators_ list with the relevant propagators.
601  void InitializePropagators();
602 
603  // Unrolls the trail until a given point. This unassign the assigned variables
604  // and add them to the priority queue with the correct weight.
605  void Untrail(int target_trail_index);
606 
607  // Output to the DRAT proof handler any newly fixed variables.
608  void ProcessNewlyFixedVariablesForDratProof();
609 
610  // Returns the maximum trail_index of the literals in the given clause.
611  // All the literals must be assigned. Returns -1 if the clause is empty.
612  int ComputeMaxTrailIndex(absl::Span<const Literal> clause) const;
613 
614  // Computes what is known as the first UIP (Unique implication point) conflict
615  // clause starting from the failing clause. For a definition of UIP and a
616  // comparison of the different possible conflict clause computation, see the
617  // reference below.
618  //
619  // The conflict will have only one literal at the highest decision level, and
620  // this literal will always be the first in the conflict vector.
621  //
622  // L Zhang, CF Madigan, MH Moskewicz, S Malik, "Efficient conflict driven
623  // learning in a boolean satisfiability solver" Proceedings of the 2001
624  // IEEE/ACM international conference on Computer-aided design, Pages 279-285.
625  // http://www.cs.tau.ac.il/~msagiv/courses/ATP/iccad2001_final.pdf
626  void ComputeFirstUIPConflict(
627  int max_trail_index, std::vector<Literal>* conflict,
628  std::vector<Literal>* reason_used_to_infer_the_conflict,
629  std::vector<SatClause*>* subsumed_clauses);
630 
631  // Fills literals with all the literals in the reasons of the literals in the
632  // given input. The output vector will have no duplicates and will not contain
633  // the literals already present in the input.
634  void ComputeUnionOfReasons(const std::vector<Literal>& input,
635  std::vector<Literal>* literals);
636 
637  // Do the full pseudo-Boolean constraint analysis. This calls multiple
638  // time ResolvePBConflict() on the current conflict until we have a conflict
639  // that allow us to propagate more at a lower decision level. This level
640  // is the one returned in backjump_level.
641  void ComputePBConflict(int max_trail_index, Coefficient initial_slack,
642  MutableUpperBoundedLinearConstraint* conflict,
643  int* backjump_level);
644 
645  // Applies some heuristics to a conflict in order to minimize its size and/or
646  // replace literals by other literals from lower decision levels. The first
647  // function choose which one of the other functions to call depending on the
648  // parameters.
649  //
650  // Precondidtion: is_marked_ should be set to true for all the variables of
651  // the conflict. It can also contains false non-conflict variables that
652  // are implied by the negation of the 1-UIP conflict literal.
653  void MinimizeConflict(
654  std::vector<Literal>* conflict,
655  std::vector<Literal>* reason_used_to_infer_the_conflict);
656  void MinimizeConflictExperimental(std::vector<Literal>* conflict);
657  void MinimizeConflictSimple(std::vector<Literal>* conflict);
658  void MinimizeConflictRecursively(std::vector<Literal>* conflict);
659 
660  // Utility function used by MinimizeConflictRecursively().
661  bool CanBeInferedFromConflictVariables(BooleanVariable variable);
662 
663  // To be used in DCHECK(). Verifies some property of the conflict clause:
664  // - There is an unique literal with the highest decision level.
665  // - This literal appears in the first position.
666  // - All the other literals are of smaller decision level.
667  // - Ther is no literal with a decision level of zero.
668  bool IsConflictValid(const std::vector<Literal>& literals);
669 
670  // Given the learned clause after a conflict, this computes the correct
671  // backtrack level to call Backtrack() with.
672  int ComputeBacktrackLevel(const std::vector<Literal>& literals);
673 
674  // The LBD (Literal Blocks Distance) is the number of different decision
675  // levels at which the literals of the clause were assigned. Note that we
676  // ignore the decision level 0 whereas the definition in the paper below
677  // doesn't:
678  //
679  // G. Audemard, L. Simon, "Predicting Learnt Clauses Quality in Modern SAT
680  // Solver" in Twenty-first International Joint Conference on Artificial
681  // Intelligence (IJCAI'09), july 2009.
682  // http://www.ijcai.org/papers09/Papers/IJCAI09-074.pdf
683  //
684  // IMPORTANT: All the literals of the clause must be assigned, and the first
685  // literal must be of the highest decision level. This will be the case for
686  // all the reason clauses.
687  template <typename LiteralList>
688  int ComputeLbd(const LiteralList& literals);
689 
690  // Checks if we need to reduce the number of learned clauses and do
691  // it if needed. Also updates the learned clause limit for the next cleanup.
692  void CleanClauseDatabaseIfNeeded();
693 
694  // Activity management for clauses. This work the same way at the ones for
695  // variables, but with different parameters.
696  void BumpReasonActivities(const std::vector<Literal>& literals);
697  void BumpClauseActivity(SatClause* clause);
698  void RescaleClauseActivities(double scaling_factor);
699  void UpdateClauseActivityIncrement();
700 
701  std::string DebugString(const SatClause& clause) const;
702  std::string StatusString(Status status) const;
703  std::string RunningStatisticsString() const;
704 
705  // Marks as "non-deletable" all clauses that were used to infer the given
706  // variable. The variable must be currently assigned.
707  void KeepAllClauseUsedToInfer(BooleanVariable variable);
708 
709  // Use propagation to try to minimize the given clause. This is really similar
710  // to MinimizeCoreWithPropagation(). It must be called when the current
711  // decision level is zero. Note that because this do a small tree search, it
712  // will impact the variable/clauses activities and may add new conflicts.
713  void TryToMinimizeClause(SatClause* clause);
714 
715  // This is used by the old non-model constructor.
716  Model* model_;
717  std::unique_ptr<Model> owned_model_;
718 
719  BooleanVariable num_variables_ = BooleanVariable(0);
720 
721  // Internal propagators. We keep them here because we need more than the
722  // SatPropagator interface for them.
723  BinaryImplicationGraph* binary_implication_graph_;
724  LiteralWatchers* clauses_propagator_;
725  PbConstraints* pb_constraints_;
726 
727  // Ordered list of propagators used by Propagate()/Untrail().
728  std::vector<SatPropagator*> propagators_;
729  std::vector<SatPropagator*> non_empty_propagators_;
730 
731  // Ordered list of propagators added with AddPropagator().
732  std::vector<SatPropagator*> external_propagators_;
733  SatPropagator* last_propagator_ = nullptr;
734 
735  // For the old, non-model interface.
736  std::vector<std::unique_ptr<SatPropagator>> owned_propagators_;
737 
738  // Keep track of all binary clauses so they can be exported.
739  bool track_binary_clauses_;
740  BinaryClauseManager binary_clauses_;
741 
742  // Pointers to singleton Model objects.
743  Trail* trail_;
744  TimeLimit* time_limit_;
745  SatParameters* parameters_;
746  RestartPolicy* restart_;
747  SatDecisionPolicy* decision_policy_;
748  SolverLogger* logger_;
749 
750  // Used for debugging only. See SaveDebugAssignment().
751  VariablesAssignment debug_assignment_;
752 
753  // The stack of decisions taken by the solver. They are stored in [0,
754  // current_decision_level_). The vector is of size num_variables_ so it can
755  // store all the decisions. This is done this way because in some situation we
756  // need to remember the previously taken decisions after a backtrack.
757  int current_decision_level_ = 0;
758  std::vector<Decision> decisions_;
759 
760  // The trail index after the last Backtrack() call or before the last
761  // EnqueueNewDecision() call.
762  int last_decision_or_backtrack_trail_index_ = 0;
763 
764  // The assumption level. See SolveWithAssumptions().
765  int assumption_level_ = 0;
766  std::vector<Literal> assumptions_;
767 
768  // The size of the trail when ProcessNewlyFixedVariables() was last called.
769  // Note that the trail contains only fixed literals (that is literals of
770  // decision levels 0) before this point.
771  int num_processed_fixed_variables_ = 0;
772  double deterministic_time_of_last_fixed_variables_cleanup_ = 0.0;
773 
774  // Used in ProcessNewlyFixedVariablesForDratProof().
775  int drat_num_processed_fixed_variables_ = 0;
776 
777  // Tracks various information about the solver progress.
778  struct Counters {
779  int64_t num_branches = 0;
780  int64_t num_failures = 0;
781  int64_t num_restarts = 0;
782 
783  // Minimization stats.
784  int64_t num_minimizations = 0;
785  int64_t num_literals_removed = 0;
786 
787  // PB constraints.
788  int64_t num_learned_pb_literals = 0;
789 
790  // Clause learning /deletion stats.
791  int64_t num_literals_learned = 0;
792  int64_t num_literals_forgotten = 0;
793  int64_t num_subsumed_clauses = 0;
794 
795  // TryToMinimizeClause() stats.
796  int64_t minimization_num_clauses = 0;
797  int64_t minimization_num_decisions = 0;
798  int64_t minimization_num_true = 0;
799  int64_t minimization_num_subsumed = 0;
800  int64_t minimization_num_removed_literals = 0;
801  };
802  Counters counters_;
803 
804  // Solver information.
805  WallTimer timer_;
806 
807  // This is set to true if the model is found to be UNSAT when adding new
808  // constraints.
809  bool model_is_unsat_ = false;
810 
811  // Increment used to bump the variable activities.
812  double clause_activity_increment_;
813 
814  // This counter is decremented each time we learn a clause that can be
815  // deleted. When it reaches zero, a clause cleanup is triggered.
816  int num_learned_clause_before_cleanup_ = 0;
817 
818  // Temporary members used during conflict analysis.
819  SparseBitset<BooleanVariable> is_marked_;
820  SparseBitset<BooleanVariable> is_independent_;
821  SparseBitset<BooleanVariable> tmp_mark_;
822  std::vector<int> min_trail_index_per_level_;
823 
824  // Temporary members used by CanBeInferedFromConflictVariables().
825  std::vector<BooleanVariable> dfs_stack_;
826  std::vector<BooleanVariable> variable_to_process_;
827 
828  // Temporary member used when adding clauses.
829  std::vector<Literal> literals_scratchpad_;
830 
831  // A boolean vector used to temporarily mark decision levels.
832  DEFINE_STRONG_INDEX_TYPE(SatDecisionLevel);
833  SparseBitset<SatDecisionLevel> is_level_marked_;
834 
835  // Temporary vectors used by EnqueueDecisionAndBackjumpOnConflict().
836  std::vector<Literal> learned_conflict_;
837  std::vector<Literal> reason_used_to_infer_the_conflict_;
838  std::vector<Literal> extra_reason_literals_;
839  std::vector<SatClause*> subsumed_clauses_;
840 
841  // When true, temporarily disable the deletion of clauses that are not needed
842  // anymore. This is a hack for TryToMinimizeClause() because we use
843  // propagation in this function which might trigger a clause database
844  // deletion, but we still want the pointer to the clause we wants to minimize
845  // to be valid until the end of that function.
846  bool block_clause_deletion_ = false;
847 
848  // "cache" to avoid inspecting many times the same reason during conflict
849  // analysis.
850  VariableWithSameReasonIdentifier same_reason_identifier_;
851 
852  // Boolean used to include/exclude constraints from the core computation.
853  bool is_relevant_for_core_computation_;
854 
855  // The current pseudo-Boolean conflict used in PB conflict analysis.
856  MutableUpperBoundedLinearConstraint pb_conflict_;
857 
858  // The deterministic time when the time limit was updated.
859  // As the deterministic time in the time limit has to be advanced manually,
860  // it is necessary to keep track of the last time the time was advanced.
861  double deterministic_time_at_last_advanced_time_limit_ = 0;
862 
863  // This is true iff the loaded problem only contains clauses.
864  bool problem_is_pure_sat_;
865 
866  DratProofHandler* drat_proof_handler_;
867 
868  mutable StatsGroup stats_;
869 
870  std::function<void(Literal, Literal)> shared_binary_clauses_callback_ =
871  nullptr;
872 
873  DISALLOW_COPY_AND_ASSIGN(SatSolver);
874 };
875 
876 // Tries to minimize the given UNSAT core with a really simple heuristic.
877 // The idea is to remove literals that are consequences of others in the core.
878 // We already know that in the initial order, no literal is propagated by the
879 // one before it, so we just look for propagation in the reverse order.
880 //
881 // Important: The given SatSolver must be the one that just produced the given
882 // core.
883 //
884 // TODO(user): One should use MinimizeCoreWithPropagation() instead.
885 void MinimizeCore(SatSolver* solver, std::vector<Literal>* core);
886 
887 // ============================================================================
888 // Model based functions.
889 //
890 // TODO(user): move them in another file, and unit-test them.
891 // ============================================================================
892 
893 inline std::function<void(Model*)> BooleanLinearConstraint(
894  int64_t lower_bound, int64_t upper_bound,
895  std::vector<LiteralWithCoeff>* cst) {
896  return [=](Model* model) {
897  model->GetOrCreate<SatSolver>()->AddLinearConstraint(
898  /*use_lower_bound=*/true, Coefficient(lower_bound),
899  /*use_upper_bound=*/true, Coefficient(upper_bound), cst);
900  };
901 }
902 
903 inline std::function<void(Model*)> CardinalityConstraint(
904  int64_t lower_bound, int64_t upper_bound,
905  const std::vector<Literal>& literals) {
906  return [=](Model* model) {
907  std::vector<LiteralWithCoeff> cst;
908  cst.reserve(literals.size());
909  for (int i = 0; i < literals.size(); ++i) {
910  cst.emplace_back(literals[i], 1);
911  }
912  model->GetOrCreate<SatSolver>()->AddLinearConstraint(
913  /*use_lower_bound=*/true, Coefficient(lower_bound),
914  /*use_upper_bound=*/true, Coefficient(upper_bound), &cst);
915  };
916 }
917 
918 inline std::function<void(Model*)> ExactlyOneConstraint(
919  const std::vector<Literal>& literals) {
920  return [=](Model* model) {
921  std::vector<LiteralWithCoeff> cst;
922  cst.reserve(literals.size());
923  for (const Literal l : literals) {
924  cst.emplace_back(l, Coefficient(1));
925  }
926  model->GetOrCreate<SatSolver>()->AddLinearConstraint(
927  /*use_lower_bound=*/true, Coefficient(1),
928  /*use_upper_bound=*/true, Coefficient(1), &cst);
929  };
930 }
931 
932 inline std::function<void(Model*)> AtMostOneConstraint(
933  const std::vector<Literal>& literals) {
934  return [=](Model* model) {
935  std::vector<LiteralWithCoeff> cst;
936  cst.reserve(literals.size());
937  for (const Literal l : literals) {
938  cst.emplace_back(l, Coefficient(1));
939  }
940  model->GetOrCreate<SatSolver>()->AddLinearConstraint(
941  /*use_lower_bound=*/false, Coefficient(0),
942  /*use_upper_bound=*/true, Coefficient(1), &cst);
943  };
944 }
945 
946 inline std::function<void(Model*)> ClauseConstraint(
947  absl::Span<const Literal> literals) {
948  return [=](Model* model) {
949  model->GetOrCreate<SatSolver>()->AddProblemClause(literals,
950  /*is_safe=*/false);
951  };
952 }
953 
954 // a => b.
955 inline std::function<void(Model*)> Implication(Literal a, Literal b) {
956  return [=](Model* model) {
957  model->GetOrCreate<SatSolver>()->AddBinaryClause(a.Negated(), b);
958  };
959 }
960 
961 // a == b.
962 inline std::function<void(Model*)> Equality(Literal a, Literal b) {
963  return [=](Model* model) {
964  model->GetOrCreate<SatSolver>()->AddBinaryClause(a.Negated(), b);
965  model->GetOrCreate<SatSolver>()->AddBinaryClause(a, b.Negated());
966  };
967 }
968 
969 // r <=> (at least one literal is true). This is a reified clause.
970 inline std::function<void(Model*)> ReifiedBoolOr(
971  const std::vector<Literal>& literals, Literal r) {
972  return [=](Model* model) {
973  std::vector<Literal> clause;
974  for (const Literal l : literals) {
975  model->Add(Implication(l, r)); // l => r.
976  clause.push_back(l);
977  }
978 
979  // All false => r false.
980  clause.push_back(r.Negated());
981  model->Add(ClauseConstraint(clause));
982  };
983 }
984 
985 // enforcement_literals => clause.
986 inline std::function<void(Model*)> EnforcedClause(
987  absl::Span<const Literal> enforcement_literals,
988  absl::Span<const Literal> clause) {
989  return [=](Model* model) {
990  std::vector<Literal> tmp;
991  for (const Literal l : enforcement_literals) {
992  tmp.push_back(l.Negated());
993  }
994  for (const Literal l : clause) {
995  tmp.push_back(l);
996  }
997  model->Add(ClauseConstraint(tmp));
998  };
999 }
1000 
1001 // r <=> (all literals are true).
1002 //
1003 // Note(user): we could have called ReifiedBoolOr() with everything negated.
1004 inline std::function<void(Model*)> ReifiedBoolAnd(
1005  const std::vector<Literal>& literals, Literal r) {
1006  return [=](Model* model) {
1007  std::vector<Literal> clause;
1008  for (const Literal l : literals) {
1009  model->Add(Implication(r, l)); // r => l.
1010  clause.push_back(l.Negated());
1011  }
1012 
1013  // All true => r true.
1014  clause.push_back(r);
1015  model->Add(ClauseConstraint(clause));
1016  };
1017 }
1018 
1019 // r <=> (a <= b).
1020 inline std::function<void(Model*)> ReifiedBoolLe(Literal a, Literal b,
1021  Literal r) {
1022  return [=](Model* model) {
1023  // r <=> (a <= b) is the same as r <=> not(a=1 and b=0).
1024  // So r <=> a=0 OR b=1.
1025  model->Add(ReifiedBoolOr({a.Negated(), b}, r));
1026  };
1027 }
1028 
1029 // This checks that the variable is fixed.
1030 inline std::function<int64_t(const Model&)> Value(Literal l) {
1031  return [=](const Model& model) {
1032  const Trail* trail = model.Get<Trail>();
1033  CHECK(trail->Assignment().VariableIsAssigned(l.Variable()));
1034  return trail->Assignment().LiteralIsTrue(l);
1035  };
1036 }
1037 
1038 // This checks that the variable is fixed.
1039 inline std::function<int64_t(const Model&)> Value(BooleanVariable b) {
1040  return [=](const Model& model) {
1041  const Trail* trail = model.Get<Trail>();
1042  CHECK(trail->Assignment().VariableIsAssigned(b));
1043  return trail->Assignment().LiteralIsTrue(Literal(b, true));
1044  };
1045 }
1046 
1047 // This can be used to enumerate all the solutions. After each SAT call to
1048 // Solve(), calling this will reset the solver and exclude the current solution
1049 // so that the next call to Solve() will give a new solution or UNSAT is there
1050 // is no more new solutions.
1051 inline std::function<void(Model*)> ExcludeCurrentSolutionAndBacktrack() {
1052  return [=](Model* model) {
1053  SatSolver* sat_solver = model->GetOrCreate<SatSolver>();
1054 
1055  // Note that we only exclude the current decisions, which is an efficient
1056  // way to not get the same SAT assignment.
1057  const int current_level = sat_solver->CurrentDecisionLevel();
1058  std::vector<Literal> clause_to_exclude_solution;
1059  clause_to_exclude_solution.reserve(current_level);
1060  for (int i = 0; i < current_level; ++i) {
1061  clause_to_exclude_solution.push_back(
1062  sat_solver->Decisions()[i].literal.Negated());
1063  }
1064  sat_solver->Backtrack(0);
1065  model->Add(ClauseConstraint(clause_to_exclude_solution));
1066  };
1067 }
1068 
1069 // Returns a string representation of a SatSolver::Status.
1071 inline std::ostream& operator<<(std::ostream& os, SatSolver::Status status) {
1072  os << SatStatusString(status);
1073  return os;
1074 }
1075 
1076 } // namespace sat
1077 } // namespace operations_research
1078 
1079 #endif // OR_TOOLS_SAT_SAT_SOLVER_H_
int64_t max
Definition: alldiff_cst.cc:140
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 AdvanceDeterministicTime(double deterministic_duration)
Advances the deterministic time.
Definition: time_limit.h:226
void ExtractAllBinaryClauses(Output *out) const
Definition: clause.h:654
void SetDratProofHandler(DratProofHandler *drat_proof_handler)
Definition: clause.h:675
BooleanVariable Variable() const
Definition: sat_base.h:86
const std::vector< SatClause * > & AllClausesInCreationOrder() const
Definition: clause.h:214
void SetDratProofHandler(DratProofHandler *drat_proof_handler)
Definition: clause.h:242
bool IsRemovable(SatClause *const clause) const
Definition: clause.h:222
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
std::vector< std::pair< Literal, double > > AllPreferences() const
void SetAssignmentPreference(Literal literal, double weight)
const Trail & LiteralTrail() const
Definition: sat_solver.h:387
std::vector< std::pair< Literal, double > > AllPreferences() const
Definition: sat_solver.h:161
bool AddLinearConstraint(bool use_lower_bound, Coefficient lower_bound, bool use_upper_bound, Coefficient upper_bound, std::vector< LiteralWithCoeff > *cst)
Definition: sat_solver.cc:354
bool EnqueueDecisionIfNotConflicting(Literal true_literal)
Definition: sat_solver.cc:989
void SetNumVariables(int num_variables)
Definition: sat_solver.cc:86
bool AddTernaryClause(Literal a, Literal b, Literal c)
Definition: sat_solver.cc:194
void AddLastPropagator(SatPropagator *propagator)
Definition: sat_solver.cc:456
const SatParameters & parameters() const
Definition: sat_solver.cc:132
void ResetDecisionHeuristicAndSetAllPreferences(const std::vector< std::pair< Literal, double >> &prefs)
Definition: sat_solver.h:167
bool AddClauseDuringSearch(absl::Span< const Literal > literals)
Definition: sat_solver.cc:158
Status SolveWithTimeLimit(TimeLimit *time_limit)
Definition: sat_solver.cc:1083
Status ResetAndSolveWithGivenAssumptions(const std::vector< Literal > &assumptions)
Definition: sat_solver.cc:1058
void AddPropagator(SatPropagator *propagator)
Definition: sat_solver.cc:448
BooleanVariable NewBooleanVariable()
Definition: sat_solver.h:88
const std::vector< BinaryClause > & NewlyAddedBinaryClauses()
Definition: sat_solver.cc:1043
bool AddBinaryClauses(const std::vector< BinaryClause > &clauses)
Definition: sat_solver.cc:1033
void SetAssumptionLevel(int assumption_level)
Definition: sat_solver.cc:1071
void AdvanceDeterministicTime(TimeLimit *limit)
Definition: sat_solver.h:454
void SetShareBinaryClauseCallback(const std::function< void(Literal, Literal)> &shared_binary_clauses_callback)
Definition: sat_solver.h:447
void SetDratProofHandler(DratProofHandler *drat_proof_handler)
Definition: sat_solver.h:415
void MinimizeSomeClauses(int decisions_budget)
Definition: sat_solver.cc:1361
void SetAssignmentPreference(Literal literal, double weight)
Definition: sat_solver.h:158
const VariablesAssignment & Assignment() const
Definition: sat_solver.h:388
int EnqueueDecisionAndBackjumpOnConflict(Literal true_literal)
Definition: sat_solver.cc:547
void SetParameters(const SatParameters &parameters)
Definition: sat_solver.cc:137
bool AddBinaryClause(Literal a, Literal b)
Definition: sat_solver.cc:190
void Backtrack(int target_level)
Definition: sat_solver.cc:1004
bool AddProblemClause(absl::Span< const Literal > literals, bool is_safe=true)
Definition: sat_solver.cc:203
std::vector< Literal > GetLastIncompatibleDecisions()
Definition: sat_solver.cc:1386
void TakePropagatorOwnership(std::unique_ptr< SatPropagator > propagator)
Definition: sat_solver.h:150
bool ResetWithGivenAssumptions(const std::vector< Literal > &assumptions)
Definition: sat_solver.cc:598
Status EnqueueDecisionAndBacktrackOnConflict(Literal true_literal, int *first_propagation_index=nullptr)
Definition: sat_solver.cc:975
const std::vector< Decision > & Decisions() const
Definition: sat_solver.h:385
bool AddUnitClause(Literal true_literal)
Definition: sat_solver.cc:186
const AssignmentInfo & Info(BooleanVariable var) const
Definition: sat_base.h:403
const VariablesAssignment & Assignment() const
Definition: sat_base.h:402
bool VariableIsAssigned(BooleanVariable var) const
Definition: sat_base.h:172
bool LiteralIsTrue(Literal literal) const
Definition: sat_base.h:164
int64_t b
int64_t a
SharedClausesManager * clauses
ModelSharedTimeLimit * time_limit
int64_t value
IntVar * var
Definition: expr_array.cc:1874
absl::Status status
Definition: g_gurobi.cc:41
GRBmodel * model
std::tuple< int64_t, int64_t, const double > Coefficient
std::function< void(Model *)> ReifiedBoolLe(Literal a, Literal b, Literal r)
Definition: sat_solver.h:1020
std::function< void(Model *)> ExcludeCurrentSolutionAndBacktrack()
Definition: sat_solver.h:1051
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
Definition: cp_model.cc:88
std::function< void(Model *)> ClauseConstraint(absl::Span< const Literal > literals)
Definition: sat_solver.h:946
std::function< void(Model *)> ExactlyOneConstraint(const std::vector< Literal > &literals)
Definition: sat_solver.h:918
std::function< int64_t(const Model &)> Value(IntegerVariable v)
Definition: integer.h:1795
std::function< void(Model *)> EnforcedClause(absl::Span< const Literal > enforcement_literals, absl::Span< const Literal > clause)
Definition: sat_solver.h:986
void MinimizeCore(SatSolver *solver, std::vector< Literal > *core)
Definition: sat_solver.cc:2666
std::string SatStatusString(SatSolver::Status status)
Definition: sat_solver.cc:2649
std::function< void(Model *)> Implication(const std::vector< Literal > &enforcement_literals, IntegerLiteral i)
Definition: integer.h:1845
std::function< void(Model *)> BooleanLinearConstraint(int64_t lower_bound, int64_t upper_bound, std::vector< LiteralWithCoeff > *cst)
Definition: sat_solver.h:893
std::function< void(Model *)> ReifiedBoolAnd(const std::vector< Literal > &literals, Literal r)
Definition: sat_solver.h:1004
std::function< void(Model *)> Equality(IntegerVariable v, int64_t value)
Definition: integer.h:1832
std::function< void(Model *)> AtMostOneConstraint(const std::vector< Literal > &literals)
Definition: sat_solver.h:932
std::function< void(Model *)> CardinalityConstraint(int64_t lower_bound, int64_t upper_bound, const std::vector< Literal > &literals)
Definition: sat_solver.h:903
const int kUnsatTrailIndex
Definition: sat_solver.h:57
std::function< void(Model *)> ReifiedBoolOr(const std::vector< Literal > &literals, Literal r)
Definition: sat_solver.h:970
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:88
int64_t weight
Definition: pack.cc:510
static int input(yyscan_t yyscanner)
IntVar * upper_bound
Definition: routing.cc:1087
IntVar * lower_bound
Definition: routing.cc:1086