OR-Tools  8.2
bop_ls.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 defines the needed classes to efficiently perform Local Search in
15 // Bop.
16 // Local Search is a technique used to locally improve an existing solution by
17 // flipping a limited number of variables. To be successful the produced
18 // solution has to satisfy all constraints of the problem and improve the
19 // objective cost.
20 //
21 // The class BopLocalSearchOptimizer is the only public interface for Local
22 // Search in Bop. For unit-testing purposes this file also contains the four
23 // internal classes AssignmentAndConstraintFeasibilityMaintainer,
24 // OneFlipConstraintRepairer, SatWrapper and LocalSearchAssignmentIterator.
25 // They are implementation details and should not be used outside of bop_ls.
26 
27 #ifndef OR_TOOLS_BOP_BOP_LS_H_
28 #define OR_TOOLS_BOP_BOP_LS_H_
29 
30 #include <array>
31 
32 #include "absl/container/flat_hash_map.h"
33 #include "absl/container/flat_hash_set.h"
34 #include "absl/random/random.h"
35 #include "ortools/base/hash.h"
36 #include "ortools/base/random.h"
38 #include "ortools/bop/bop_base.h"
40 #include "ortools/bop/bop_types.h"
42 #include "ortools/sat/sat_solver.h"
43 
44 namespace operations_research {
45 namespace bop {
46 
47 // This class is used to ease the connection with the SAT solver.
48 //
49 // TODO(user): remove? the meat of the logic is used in just one place, so I am
50 // not sure having this extra layer improve the readability.
51 class SatWrapper {
52  public:
53  explicit SatWrapper(sat::SatSolver* sat_solver);
54 
55  // Returns the current state of the solver propagation trail.
56  std::vector<sat::Literal> FullSatTrail() const;
57 
58  // Returns true if the problem is UNSAT.
59  // Note that an UNSAT problem might not be marked as UNSAT at first because
60  // the SAT solver is not able to prove it; After some decisions / learned
61  // conflicts, the SAT solver might be able to prove UNSAT and so this will
62  // return true.
63  bool IsModelUnsat() const { return sat_solver_->IsModelUnsat(); }
64 
65  // Return the current solver VariablesAssignment.
67  return sat_solver_->Assignment();
68  }
69 
70  // Applies the decision that makes the given literal true and returns the
71  // number of decisions to backtrack due to conflicts if any.
72  // Two cases:
73  // - No conflicts: Returns 0 and fills the propagated_literals with the
74  // literals that have been propagated due to the decision including the
75  // the decision itself.
76  // - Conflicts: Returns the number of decisions to backtrack (the current
77  // decision included, i.e. returned value > 0) and fills the
78  // propagated_literals with the literals that the conflicts propagated.
79  // Note that the decision variable should not be already assigned in SAT.
80  int ApplyDecision(sat::Literal decision_literal,
81  std::vector<sat::Literal>* propagated_literals);
82 
83  // Backtracks the last decision if any.
84  void BacktrackOneLevel();
85 
86  // Bactracks all the decisions.
87  void BacktrackAll();
88 
89  // Extracts any new information learned during the search.
90  void ExtractLearnedInfo(LearnedInfo* info);
91 
92  // Returns a deterministic number that should be correlated with the time
93  // spent in the SAT wrapper. The order of magnitude should be close to the
94  // time in seconds.
95  double deterministic_time() const;
96 
97  private:
98  sat::SatSolver* sat_solver_;
99  DISALLOW_COPY_AND_ASSIGN(SatWrapper);
100 };
101 
102 // Forward declaration.
103 class LocalSearchAssignmentIterator;
104 
105 // This class defines a Local Search optimizer. The goal is to find a new
106 // solution with a better cost than the given solution by iterating on all
107 // assignments that can be reached in max_num_decisions decisions or less.
108 // The bop parameter max_number_of_explored_assignments_per_try_in_ls can be
109 // used to specify the number of new assignments to iterate on each time the
110 // method Optimize() is called. Limiting that parameter allows to reduce the
111 // time spent in the Optimize() method at once, and still explore all the
112 // reachable assignments (if Optimize() is called enough times).
113 // Note that due to propagation, the number of variables with a different value
114 // in the new solution can be greater than max_num_decisions.
116  public:
117  LocalSearchOptimizer(const std::string& name, int max_num_decisions,
118  sat::SatSolver* sat_propagator);
119  ~LocalSearchOptimizer() override;
120 
121  private:
122  bool ShouldBeRun(const ProblemState& problem_state) const override;
123  Status Optimize(const BopParameters& parameters,
124  const ProblemState& problem_state, LearnedInfo* learned_info,
125  TimeLimit* time_limit) override;
126 
127  int64 state_update_stamp_;
128 
129  // Maximum number of decisions the Local Search can take.
130  // Note that there is no limit on the number of changed variables due to
131  // propagation.
132  const int max_num_decisions_;
133 
134  // A wrapper around the given sat_propagator.
135  SatWrapper sat_wrapper_;
136 
137  // Iterator on all reachable assignments.
138  // Note that this iterator is only reset when Synchronize() is called, i.e.
139  // the iterator continues its iteration of the next assignments each time
140  // Optimize() is called until everything is explored or a solution is found.
141  std::unique_ptr<LocalSearchAssignmentIterator> assignment_iterator_;
142 };
143 
144 //------------------------------------------------------------------------------
145 // Implementation details. The declarations of those utility classes are in
146 // the .h for testing reasons.
147 //------------------------------------------------------------------------------
148 
149 // Maintains some information on a sparse set of integers in [0, n). More
150 // specifically this class:
151 // - Allows to dynamically add/remove element from the set.
152 // - Has a backtracking support.
153 // - Maintains the number of elements in the set.
154 // - Maintains a superset of the elements of the set that contains all the
155 // modified elements.
156 template <typename IntType>
158  public:
160 
161  // Prepares the class for integers in [0, n) and initializes the set to the
162  // empty one. Note that this run in O(n). Once resized, it is better to call
163  // BacktrackAll() instead of this to clear the set.
164  void ClearAndResize(IntType n);
165 
166  // Changes the state of the given integer i to be either inside or outside the
167  // set. Important: this should only be called with the opposite state of the
168  // current one, otherwise size() will not be correct.
169  void ChangeState(IntType i, bool should_be_inside);
170 
171  // Returns the current number of elements in the set.
172  // Note that this is not its maximum size n.
173  int size() const { return size_; }
174 
175  // Returns a superset of the current set of integers.
176  const std::vector<IntType>& Superset() const { return stack_; }
177 
178  // BacktrackOneLevel() backtracks to the state the class was in when the
179  // last AddBacktrackingLevel() was called. BacktrackAll() just restore the
180  // class to its state just after the last ClearAndResize().
181  void AddBacktrackingLevel();
182  void BacktrackOneLevel();
183  void BacktrackAll();
184 
185  private:
186  int size_;
187 
188  // Contains the elements whose status has been changed at least once.
189  std::vector<IntType> stack_;
190  std::vector<bool> in_stack_;
191 
192  // Used for backtracking. Contains the size_ and the stack_.size() at the time
193  // of each call to AddBacktrackingLevel() that is not yet backtracked over.
194  std::vector<int> saved_sizes_;
195  std::vector<int> saved_stack_sizes_;
196 };
197 
198 // A simple and efficient class to hash a given set of integers in [0, n).
199 // It uses O(n) memory and produces a good hash (random linear function).
200 template <typename IntType>
202  public:
203  NonOrderedSetHasher() : random_("Random seed") {}
204 
205  // Initializes the NonOrderedSetHasher to hash sets of integer in [0, n).
206  void Initialize(int size) {
207  hashes_.resize(size);
208  for (IntType i(0); i < size; ++i) {
209  hashes_[i] = random_.Rand64();
210  }
211  }
212 
213  // Ignores the given set element in all subsequent hash computation. Note that
214  // this will be reset by the next call to Initialize().
215  void IgnoreElement(IntType e) { hashes_[e] = 0; }
216 
217  // Returns the hash of the given set. The hash is independent of the set
218  // order, but there must be no duplicate element in the set. This uses a
219  // simple random linear function which has really good hashing properties.
220  uint64 Hash(const std::vector<IntType>& set) const {
221  uint64 hash = 0;
222  for (const IntType i : set) hash ^= hashes_[i];
223  return hash;
224  }
225 
226  // The hash of a set is simply the XOR of all its elements. This allows
227  // to compute an hash incrementally or without the need of a vector<>.
228  uint64 Hash(IntType e) const { return hashes_[e]; }
229 
230  // Returns true if Initialize() has been called with a non-zero size.
231  bool IsInitialized() const { return !hashes_.empty(); }
232 
233  private:
234  MTRandom random_;
236 };
237 
238 // This class is used to incrementally maintain an assignment and the
239 // feasibility of the constraints of a given LinearBooleanProblem.
240 //
241 // The current assignment is initialized using a feasible reference solution,
242 // i.e. the reference solution satisfies all the constraints of the problem.
243 // The current assignment is updated using the Assign() method.
244 //
245 // Note that the current assignment is not a solution in the sense that it
246 // might not be feasible, ie. violates some constraints.
247 //
248 // The assignment can be accessed at any time using Assignment().
249 // The set of infeasible constraints can be accessed at any time using
250 // PossiblyInfeasibleConstraints().
251 //
252 // Note that this class is reversible, i.e. it is possible to backtrack to
253 // previously added backtracking levels.
254 // levels. Consider for instance variable a, b, c, and d.
255 // Method called Assigned after method call
256 // 1- Assign({a, b}) a b
257 // 2- AddBacktrackingLevel() a b |
258 // 3- Assign({c}) a b | c
259 // 4- Assign({d}) a b | c d
260 // 5- BacktrackOneLevel() a b
261 // 6- Assign({c}) a b c
262 // 7- BacktrackOneLevel()
264  public:
265  // Note that the constraint indices used in this class are not the same as
266  // the one used in the given LinearBooleanProblem here.
268  const sat::LinearBooleanProblem& problem);
269 
270  // When we construct the problem, we treat the objective as one constraint.
271  // This is the index of this special "objective" constraint.
272  static const ConstraintIndex kObjectiveConstraint;
273 
274  // Sets a new reference solution and reverts all internal structures to their
275  // initial state. Note that the reference solution has to be feasible.
276  void SetReferenceSolution(const BopSolution& reference_solution);
277 
278  // Behaves exactly like SetReferenceSolution() where the passed reference
279  // is the current assignment held by this class. Note that the current
280  // assignment must be feasible (i.e. IsFeasible() is true).
282 
283  // Assigns all literals. That updates the assignment, the constraint values,
284  // and the infeasible constraints.
285  // Note that the assignment of those literals can be reverted thanks to
286  // AddBacktrackingLevel() and BacktrackOneLevel().
287  // Note that a variable can't be assigned twice, even for the same literal.
288  void Assign(const std::vector<sat::Literal>& literals);
289 
290  // Adds a new backtracking level to specify the state that will be restored
291  // by BacktrackOneLevel().
292  // See the example in the class comment.
293  void AddBacktrackingLevel();
294 
295  // Backtracks internal structures to the previous level defined by
296  // AddBacktrackingLevel(). As a consequence the state will be exactly as
297  // before the previous call to AddBacktrackingLevel().
298  // Note that backtracking the initial state has no effect.
299  void BacktrackOneLevel();
300  void BacktrackAll();
301 
302  // This returns the list of literal that appear in exactly all the current
303  // infeasible constraints (ignoring the objective) and correspond to a flip in
304  // a good direction for all the infeasible constraint. Performing this flip
305  // may repair the problem without any propagations.
306  //
307  // Important: The returned reference is only valid until the next
308  // PotentialOneFlipRepairs() call.
309  const std::vector<sat::Literal>& PotentialOneFlipRepairs();
310 
311  // Returns true if there is no infeasible constraint in the current state.
312  bool IsFeasible() const { return infeasible_constraint_set_.size() == 0; }
313 
314  // Returns the *exact* number of infeasible constraints.
315  // Note that PossiblyInfeasibleConstraints() will potentially return a larger
316  // number of constraints.
318  return infeasible_constraint_set_.size();
319  }
320 
321  // Returns a superset of all the infeasible constraints in the current state.
322  const std::vector<ConstraintIndex>& PossiblyInfeasibleConstraints() const {
323  return infeasible_constraint_set_.Superset();
324  }
325 
326  // Returns the number of constraints of the problem, objective included,
327  // i.e. the number of constraint in the problem + 1.
328  size_t NumConstraints() const { return constraint_lower_bounds_.size(); }
329 
330  // Returns the value of the var in the assignment.
331  // As the assignment is initialized with the reference solution, if the
332  // variable has not been assigned through Assign(), the returned value is
333  // the value of the variable in the reference solution.
334  bool Assignment(VariableIndex var) const { return assignment_.Value(var); }
335 
336  // Returns the current assignment.
337  const BopSolution& reference() const { return reference_; }
338 
339  // Returns the lower bound of the constraint.
340  int64 ConstraintLowerBound(ConstraintIndex constraint) const {
341  return constraint_lower_bounds_[constraint];
342  }
343 
344  // Returns the upper bound of the constraint.
345  int64 ConstraintUpperBound(ConstraintIndex constraint) const {
346  return constraint_upper_bounds_[constraint];
347  }
348 
349  // Returns the value of the constraint. The value is computed using the
350  // variable values in the assignment. Note that a constraint is feasible iff
351  // its value is between its two bounds (inclusive).
352  int64 ConstraintValue(ConstraintIndex constraint) const {
353  return constraint_values_[constraint];
354  }
355 
356  // Returns true if the given constraint is currently feasible.
357  bool ConstraintIsFeasible(ConstraintIndex constraint) const {
358  const int64 value = ConstraintValue(constraint);
359  return value >= ConstraintLowerBound(constraint) &&
360  value <= ConstraintUpperBound(constraint);
361  }
362 
363  std::string DebugString() const;
364 
365  private:
366  // This is lazily called by PotentialOneFlipRepairs() once.
367  void InitializeConstraintSetHasher();
368 
369  // This is used by PotentialOneFlipRepairs(). It encodes a ConstraintIndex
370  // together with a "repair" direction depending on the bound that make a
371  // constraint infeasible. An "up" direction means that the constraint activity
372  // is lower than the lower bound and we need to make the activity move up to
373  // fix the infeasibility.
374  DEFINE_INT_TYPE(ConstraintIndexWithDirection, int32);
375  ConstraintIndexWithDirection FromConstraintIndex(ConstraintIndex index,
376  bool up) const {
377  return ConstraintIndexWithDirection(2 * index.value() + (up ? 1 : 0));
378  }
379 
380  // Over constrains the objective cost by the given delta. This should only be
381  // called on a feasible reference solution and a fully backtracked state.
382  void MakeObjectiveConstraintInfeasible(int delta);
383 
384  // Local structure to represent the sparse matrix by variable used for fast
385  // update of the contraint values.
386  struct ConstraintEntry {
387  ConstraintEntry(ConstraintIndex c, int64 w) : constraint(c), weight(w) {}
388  ConstraintIndex constraint;
389  int64 weight;
390  };
391 
392  absl::StrongVector<VariableIndex,
394  by_variable_matrix_;
395  absl::StrongVector<ConstraintIndex, int64> constraint_lower_bounds_;
396  absl::StrongVector<ConstraintIndex, int64> constraint_upper_bounds_;
397 
398  BopSolution assignment_;
399  BopSolution reference_;
400 
402  BacktrackableIntegerSet<ConstraintIndex> infeasible_constraint_set_;
403 
404  // This contains the list of variable flipped in assignment_.
405  // flipped_var_trail_backtrack_levels_[i-1] is the index in flipped_var_trail_
406  // of the first variable flipped after the i-th AddBacktrackingLevel() call.
407  std::vector<int> flipped_var_trail_backtrack_levels_;
408  std::vector<VariableIndex> flipped_var_trail_;
409 
410  // Members used by PotentialOneFlipRepairs().
411  std::vector<sat::Literal> tmp_potential_repairs_;
412  NonOrderedSetHasher<ConstraintIndexWithDirection> constraint_set_hasher_;
413  absl::flat_hash_map<uint64, std::vector<sat::Literal>>
414  hash_to_potential_repairs_;
415 
416  DISALLOW_COPY_AND_ASSIGN(AssignmentAndConstraintFeasibilityMaintainer);
417 };
418 
419 // This class is an utility class used to select which infeasible constraint to
420 // repair and identify one variable to flip to actually repair the constraint.
421 // A constraint 'lb <= sum_i(w_i * x_i) <= ub', with 'lb' the lower bound,
422 // 'ub' the upper bound, 'w_i' the weight of the i-th term and 'x_i' the
423 // boolean variable appearing in the i-th term, is infeasible for a given
424 // assignment iff its value 'sum_i(w_i * x_i)' is outside of the bounds.
425 // Repairing-a-constraint-in-one-flip means making the constraint feasible by
426 // just flipping the value of one unassigned variable of the current assignment
427 // from the AssignmentAndConstraintFeasibilityMaintainer.
428 // For performance reasons, the pairs weight / variable (w_i, x_i) are stored
429 // in a sparse manner as a vector of terms (w_i, x_i). In the following the
430 // TermIndex term_index refers to the position of the term in the vector.
432  public:
433  // Note that the constraint indices used in this class follow the same
434  // convention as the one used in the
435  // AssignmentAndConstraintFeasibilityMaintainer.
436  //
437  // TODO(user): maybe merge the two classes? maintaining this implicit indices
438  // convention between the two classes sounds like a bad idea.
440  const sat::LinearBooleanProblem& problem,
442  const sat::VariablesAssignment& sat_assignment);
443 
444  static const ConstraintIndex kInvalidConstraint;
445  static const TermIndex kInitTerm;
446  static const TermIndex kInvalidTerm;
447 
448  // Returns the index of a constraint to repair. This will always return the
449  // index of a constraint that can be repaired in one flip if there is one.
450  // Note however that if there is only one possible candidate, it will be
451  // returned without checking that it can indeed be repaired in one flip.
452  // This is because the later check can be expensive, and is not needed in our
453  // context.
454  ConstraintIndex ConstraintToRepair() const;
455 
456  // Returns the index of the next term which repairs the constraint when the
457  // value of its variable is flipped. This method explores terms with an
458  // index strictly greater than start_term_index and then terms with an index
459  // smaller than or equal to init_term_index if any.
460  // Returns kInvalidTerm when no reparing terms are found.
461  //
462  // Note that if init_term_index == start_term_index, then all the terms will
463  // be explored. Both TermIndex arguments can take values in [-1, constraint
464  // size).
465  TermIndex NextRepairingTerm(ConstraintIndex ct_index,
466  TermIndex init_term_index,
467  TermIndex start_term_index) const;
468 
469  // Returns true if the constraint is infeasible and if flipping the variable
470  // at the given index will repair it.
471  bool RepairIsValid(ConstraintIndex ct_index, TermIndex term_index) const;
472 
473  // Returns the literal formed by the variable at the given constraint term and
474  // assigned to the opposite value of this variable in the current assignment.
475  sat::Literal GetFlip(ConstraintIndex ct_index, TermIndex term_index) const;
476 
477  // Local structure to represent the sparse matrix by constraint used for fast
478  // lookups.
479  struct ConstraintTerm {
480  ConstraintTerm(VariableIndex v, int64 w) : var(v), weight(w) {}
481  VariableIndex var;
483  };
484 
485  private:
486  // Sorts the terms of each constraints in the by_constraint_matrix_ to iterate
487  // on most promising variables first.
488  void SortTermsOfEachConstraints(int num_variables);
489 
490  absl::StrongVector<ConstraintIndex,
492  by_constraint_matrix_;
494  const sat::VariablesAssignment& sat_assignment_;
495 
496  DISALLOW_COPY_AND_ASSIGN(OneFlipConstraintRepairer);
497 };
498 
499 // This class is used to iterate on all assignments that can be obtained by
500 // deliberately flipping 'n' variables from the reference solution, 'n' being
501 // smaller than or equal to max_num_decisions.
502 // Note that one deliberate variable flip may lead to many other flips due to
503 // constraint propagation, those additional flips are not counted in 'n'.
505  public:
506  LocalSearchAssignmentIterator(const ProblemState& problem_state,
507  int max_num_decisions,
508  int max_num_broken_constraints,
509  SatWrapper* sat_wrapper);
511 
512  // Parameters of the LS algorithm.
513  void UseTranspositionTable(bool v) { use_transposition_table_ = v; }
515  use_potential_one_flip_repairs_ = v;
516  }
517 
518  // Synchronizes the iterator with the problem state, e.g. set fixed variables,
519  // set the reference solution. Call this only when a new solution has been
520  // found. This will restart the LS.
521  void Synchronize(const ProblemState& problem_state);
522 
523  // Synchronize the SatWrapper with our current search state. This needs to be
524  // called before calls to NextAssignment() if the underlying SatWrapper was
525  // used by someone else than this class.
526  void SynchronizeSatWrapper();
527 
528  // Move to the next assignment. Returns false when the search is finished.
529  bool NextAssignment();
530 
531  // Returns the last feasible assignment.
533  return maintainer_.reference();
534  }
535 
536  // Returns true if the current assignment has a better solution than the one
537  // passed to the last Synchronize() call.
539  return better_solution_has_been_found_;
540  }
541 
542  // Returns a deterministic number that should be correlated with the time
543  // spent in the iterator. The order of magnitude should be close to the time
544  // in seconds.
545  double deterministic_time() const;
546 
547  std::string DebugString() const;
548 
549  private:
550  // This is called when a better solution has been found to restore the search
551  // to the new "root" node.
552  void UseCurrentStateAsReference();
553 
554  // See transposition_table_ below.
555  static constexpr size_t kStoredMaxDecisions = 4;
556 
557  // Internal structure used to represent a node of the search tree during local
558  // search.
559  struct SearchNode {
560  SearchNode()
561  : constraint(OneFlipConstraintRepairer::kInvalidConstraint),
562  term_index(OneFlipConstraintRepairer::kInvalidTerm) {}
563  SearchNode(ConstraintIndex c, TermIndex t) : constraint(c), term_index(t) {}
564  ConstraintIndex constraint;
565  TermIndex term_index;
566  };
567 
568  // Applies the decision. Automatically backtracks when SAT detects conflicts.
569  void ApplyDecision(sat::Literal literal);
570 
571  // Adds one more decision to repair infeasible constraints.
572  // Returns true in case of success.
573  bool GoDeeper();
574 
575  // Backtracks and moves to the next decision in the search tree.
576  void Backtrack();
577 
578  // Looks if the current decisions (in search_nodes_) plus the new one (given
579  // by l) lead to a position already present in transposition_table_.
580  bool NewStateIsInTranspositionTable(sat::Literal l);
581 
582  // Inserts the current set of decisions in transposition_table_.
583  void InsertInTranspositionTable();
584 
585  // Initializes the given array with the current decisions in search_nodes_ and
586  // by filling the other positions with 0.
587  void InitializeTranspositionTableKey(
588  std::array<int32, kStoredMaxDecisions>* a);
589 
590  // Looks for the next repairing term in the given constraints while skipping
591  // the position already present in transposition_table_. A given TermIndex of
592  // -1 means that this is the first time we explore this constraint.
593  bool EnqueueNextRepairingTermIfAny(ConstraintIndex ct_to_repair,
594  TermIndex index);
595 
596  const int max_num_decisions_;
597  const int max_num_broken_constraints_;
598  bool better_solution_has_been_found_;
599  AssignmentAndConstraintFeasibilityMaintainer maintainer_;
600  SatWrapper* const sat_wrapper_;
601  OneFlipConstraintRepairer repairer_;
602  std::vector<SearchNode> search_nodes_;
604 
605  // Temporary vector used by ApplyDecision().
606  std::vector<sat::Literal> tmp_propagated_literals_;
607 
608  // For each set of explored decisions, we store it in this table so that we
609  // don't explore decisions (a, b) and later (b, a) for instance. The decisions
610  // are converted to int32, sorted and padded with 0 before beeing inserted
611  // here.
612  //
613  // TODO(user): We may still miss some equivalent states because it is possible
614  // that completely differents decisions lead to exactly the same state.
615  // However this is more time consuming to detect because we must apply the
616  // last decision first before trying to compare the states.
617  //
618  // TODO(user): Currently, we only store kStoredMaxDecisions or less decisions.
619  // Ideally, this should be related to the maximum number of decision in the
620  // LS, but that requires templating the whole LS optimizer.
621  bool use_transposition_table_;
622  absl::flat_hash_set<std::array<int32, kStoredMaxDecisions>>
623  transposition_table_;
624 
625  bool use_potential_one_flip_repairs_;
626 
627  // The number of explored nodes.
628  int64 num_nodes_;
629 
630  // The number of skipped nodes thanks to the transposition table.
631  int64 num_skipped_nodes_;
632 
633  // The overall number of better solution found. And the ones found by the
634  // use_potential_one_flip_repairs_ heuristic.
635  int64 num_improvements_;
636  int64 num_improvements_by_one_flip_repairs_;
637  int64 num_inspected_one_flip_repairs_;
638 
639  DISALLOW_COPY_AND_ASSIGN(LocalSearchAssignmentIterator);
640 };
641 
642 } // namespace bop
643 } // namespace operations_research
644 #endif // OR_TOOLS_BOP_BOP_LS_H_
void resize(size_type new_size)
size_type size() const
bool empty() const
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
AssignmentAndConstraintFeasibilityMaintainer(const sat::LinearBooleanProblem &problem)
Definition: bop_ls.cc:180
int64 ConstraintLowerBound(ConstraintIndex constraint) const
Definition: bop_ls.h:340
const std::vector< ConstraintIndex > & PossiblyInfeasibleConstraints() const
Definition: bop_ls.h:322
void SetReferenceSolution(const BopSolution &reference_solution)
Definition: bop_ls.cc:243
int64 ConstraintUpperBound(ConstraintIndex constraint) const
Definition: bop_ls.h:345
bool ConstraintIsFeasible(ConstraintIndex constraint) const
Definition: bop_ls.h:357
int64 ConstraintValue(ConstraintIndex constraint) const
Definition: bop_ls.h:352
void Assign(const std::vector< sat::Literal > &literals)
Definition: bop_ls.cc:296
const std::vector< sat::Literal > & PotentialOneFlipRepairs()
Definition: bop_ls.cc:345
const std::vector< IntType > & Superset() const
Definition: bop_ls.h:176
void ChangeState(IntType i, bool should_be_inside)
Definition: bop_ls.cc:130
const std::string & name() const
Definition: bop_base.h:47
bool Value(VariableIndex var) const
Definition: bop_solution.h:44
LocalSearchAssignmentIterator(const ProblemState &problem_state, int max_num_decisions, int max_num_broken_constraints, SatWrapper *sat_wrapper)
Definition: bop_ls.cc:674
void Synchronize(const ProblemState &problem_state)
Definition: bop_ls.cc:704
const BopSolution & LastReferenceAssignment() const
Definition: bop_ls.h:532
LocalSearchOptimizer(const std::string &name, int max_num_decisions, sat::SatSolver *sat_propagator)
Definition: bop_ls.cc:33
uint64 Hash(const std::vector< IntType > &set) const
Definition: bop_ls.h:220
sat::Literal GetFlip(ConstraintIndex ct_index, TermIndex term_index) const
Definition: bop_ls.cc:589
bool RepairIsValid(ConstraintIndex ct_index, TermIndex term_index) const
Definition: bop_ls.cc:572
TermIndex NextRepairingTerm(ConstraintIndex ct_index, TermIndex init_term_index, TermIndex start_term_index) const
Definition: bop_ls.cc:542
OneFlipConstraintRepairer(const sat::LinearBooleanProblem &problem, const AssignmentAndConstraintFeasibilityMaintainer &maintainer, const sat::VariablesAssignment &sat_assignment)
Definition: bop_ls.cc:437
static const ConstraintIndex kInvalidConstraint
Definition: bop_ls.h:444
const sat::VariablesAssignment & SatAssignment() const
Definition: bop_ls.h:66
SatWrapper(sat::SatSolver *sat_solver)
Definition: bop_ls.cc:616
std::vector< sat::Literal > FullSatTrail() const
Definition: bop_ls.cc:620
int ApplyDecision(sat::Literal decision_literal, std::vector< sat::Literal > *propagated_literals)
Definition: bop_ls.cc:629
void ExtractLearnedInfo(LearnedInfo *info)
Definition: bop_ls.cc:662
const VariablesAssignment & Assignment() const
Definition: sat_solver.h:362
SatParameters parameters
SharedTimeLimit * time_limit
int64 value
IntVar * var
Definition: expr_array.cc:1858
int int32
int64_t int64
uint64_t uint64
int64 hash
Definition: matrix_utils.cc:60
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Literal literal
Definition: optimization.cc:84
int index
Definition: pack.cc:508
int64 weight
Definition: pack.cc:509
int64 delta
Definition: resource.cc:1684