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