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;
396 BopSolution assignment_;
397 BopSolution reference_;
400 BacktrackableIntegerSet<ConstraintIndex> infeasible_constraint_set_;
405 std::vector<int> flipped_var_trail_backtrack_levels_;
406 std::vector<VariableIndex> flipped_var_trail_;
409 std::vector<sat::Literal> tmp_potential_repairs_;
410 NonOrderedSetHasher<ConstraintIndexWithDirection> constraint_set_hasher_;
411 absl::flat_hash_map<uint64, std::vector<sat::Literal>>
412 hash_to_potential_repairs_;
438 const sat::LinearBooleanProblem& problem,
464 TermIndex init_term_index,
465 TermIndex start_term_index)
const;
469 bool RepairIsValid(ConstraintIndex ct_index, TermIndex term_index)
const;
486 void SortTermsOfEachConstraints(
int num_variables);
489 by_constraint_matrix_;
504 int max_num_decisions,
505 int max_num_broken_constraints,
512 use_potential_one_flip_repairs_ = v;
536 return better_solution_has_been_found_;
549 void UseCurrentStateAsReference();
552 static constexpr
size_t kStoredMaxDecisions = 4;
560 SearchNode(ConstraintIndex c, TermIndex t) : constraint(c), term_index(t) {}
561 ConstraintIndex constraint;
562 TermIndex term_index;
566 void ApplyDecision(sat::Literal
literal);
577 bool NewStateIsInTranspositionTable(sat::Literal l);
580 void InsertInTranspositionTable();
584 void InitializeTranspositionTableKey(
585 std::array<int32, kStoredMaxDecisions>*
a);
590 bool EnqueueNextRepairingTermIfAny(ConstraintIndex ct_to_repair,
593 const int max_num_decisions_;
594 const int max_num_broken_constraints_;
595 bool better_solution_has_been_found_;
596 AssignmentAndConstraintFeasibilityMaintainer maintainer_;
597 SatWrapper*
const sat_wrapper_;
598 OneFlipConstraintRepairer repairer_;
599 std::vector<SearchNode> search_nodes_;
603 std::vector<sat::Literal> tmp_propagated_literals_;
618 bool use_transposition_table_;
619 absl::flat_hash_set<std::array<int32, kStoredMaxDecisions>>
620 transposition_table_;
622 bool use_potential_one_flip_repairs_;
628 int64 num_skipped_nodes_;
632 int64 num_improvements_;
633 int64 num_improvements_by_one_flip_repairs_;
634 int64 num_inspected_one_flip_repairs_;
641 #endif // OR_TOOLS_BOP_BOP_LS_H_