27 #ifndef OR_TOOLS_BOP_BOP_LS_H_
28 #define OR_TOOLS_BOP_BOP_LS_H_
32 #include "absl/container/flat_hash_map.h"
33 #include "absl/container/flat_hash_set.h"
34 #include "absl/random/random.h"
80 std::vector<sat::Literal>* propagated_literals);
102 class LocalSearchAssignmentIterator;
121 bool ShouldBeRun(
const ProblemState& problem_state)
const override;
126 int64 state_update_stamp_;
131 const int max_num_decisions_;
140 std::unique_ptr<LocalSearchAssignmentIterator> assignment_iterator_;
155 template <
typename IntType>
168 void ChangeState(IntType i,
bool should_be_inside);
172 int size()
const {
return size_; }
175 const std::vector<IntType>&
Superset()
const {
return stack_; }
188 std::vector<IntType> stack_;
189 std::vector<bool> in_stack_;
193 std::vector<int> saved_sizes_;
194 std::vector<int> saved_stack_sizes_;
199 template <
typename IntType>
207 for (IntType i(0); i < size; ++i) {
208 hashes_[i] = random_.
Rand64();
221 for (
const IntType i : set)
hash ^= hashes_[i];
267 const sat::LinearBooleanProblem& problem);
287 void Assign(
const std::vector<sat::Literal>& literals);
317 return infeasible_constraint_set_.
size();
322 return infeasible_constraint_set_.
Superset();
340 return constraint_lower_bounds_[constraint];
345 return constraint_upper_bounds_[constraint];
352 return constraint_values_[constraint];
366 void InitializeConstraintSetHasher();
373 DEFINE_INT_TYPE(ConstraintIndexWithDirection,
int32);
374 ConstraintIndexWithDirection FromConstraintIndex(ConstraintIndex
index,
376 return ConstraintIndexWithDirection(2 *
index.value() + (up ? 1 : 0));
381 void MakeObjectiveConstraintInfeasible(
int delta);
385 struct ConstraintEntry {
386 ConstraintEntry(ConstraintIndex c,
int64 w) : constraint(c),
weight(w) {}
387 ConstraintIndex constraint;
397 BopSolution assignment_;
398 BopSolution reference_;
401 BacktrackableIntegerSet<ConstraintIndex> infeasible_constraint_set_;
406 std::vector<int> flipped_var_trail_backtrack_levels_;
407 std::vector<VariableIndex> flipped_var_trail_;
410 std::vector<sat::Literal> tmp_potential_repairs_;
411 NonOrderedSetHasher<ConstraintIndexWithDirection> constraint_set_hasher_;
412 absl::flat_hash_map<uint64, std::vector<sat::Literal>>
413 hash_to_potential_repairs_;
439 const sat::LinearBooleanProblem& problem,
465 TermIndex init_term_index,
466 TermIndex start_term_index)
const;
470 bool RepairIsValid(ConstraintIndex ct_index, TermIndex term_index)
const;
487 void SortTermsOfEachConstraints(
int num_variables);
491 by_constraint_matrix_;
506 int max_num_decisions,
507 int max_num_broken_constraints,
514 use_potential_one_flip_repairs_ = v;
538 return better_solution_has_been_found_;
551 void UseCurrentStateAsReference();
554 static constexpr
size_t kStoredMaxDecisions = 4;
562 SearchNode(ConstraintIndex c, TermIndex t) : constraint(c), term_index(t) {}
563 ConstraintIndex constraint;
564 TermIndex term_index;
568 void ApplyDecision(sat::Literal
literal);
579 bool NewStateIsInTranspositionTable(sat::Literal l);
582 void InsertInTranspositionTable();
586 void InitializeTranspositionTableKey(
587 std::array<int32, kStoredMaxDecisions>*
a);
592 bool EnqueueNextRepairingTermIfAny(ConstraintIndex ct_to_repair,
595 const int max_num_decisions_;
596 const int max_num_broken_constraints_;
597 bool better_solution_has_been_found_;
598 AssignmentAndConstraintFeasibilityMaintainer maintainer_;
599 SatWrapper*
const sat_wrapper_;
600 OneFlipConstraintRepairer repairer_;
601 std::vector<SearchNode> search_nodes_;
605 std::vector<sat::Literal> tmp_propagated_literals_;
620 bool use_transposition_table_;
621 absl::flat_hash_set<std::array<int32, kStoredMaxDecisions>>
622 transposition_table_;
624 bool use_potential_one_flip_repairs_;
630 int64 num_skipped_nodes_;
634 int64 num_improvements_;
635 int64 num_improvements_by_one_flip_repairs_;
636 int64 num_inspected_one_flip_repairs_;
643 #endif // OR_TOOLS_BOP_BOP_LS_H_