Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
restart.h
Go to the documentation of this file.
1// Copyright 2010-2024 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#ifndef OR_TOOLS_SAT_RESTART_H_
15#define OR_TOOLS_SAT_RESTART_H_
16
17#include <string>
18#include <vector>
19
20#include "absl/log/check.h"
22#include "ortools/sat/model.h"
24#include "ortools/sat/sat_parameters.pb.h"
25#include "ortools/util/bitset.h"
27
28namespace operations_research {
29namespace sat {
30
31// Contain the logic to decide when to restart a SAT tree search.
33 public:
35 : parameters_(*(model->GetOrCreate<SatParameters>())),
36 decision_policy_(model->GetOrCreate<SatDecisionPolicy>()) {
37 Reset();
38 }
39
40 // Resets the policy using the current model parameters.
41 void Reset();
42
43 // Returns true if the solver should be restarted before the next decision is
44 // taken. Note that this will return true just once and then assumes that
45 // the search was restarted and starts worrying about the next restart.
46 bool ShouldRestart();
47
48 // This will be called by the solver engine after each conflict. The arguments
49 // reflect the state of the solver when the conflict was detected and before
50 // the backjump.
51 void OnConflict(int conflict_trail_index, int conflict_decision_level,
52 int conflict_lbd);
53
54 // Returns the number of restarts since the last Reset().
55 int NumRestarts() const { return num_restarts_; }
56
57 // Returns a string with the current restart statistics.
58 std::string InfoString() const;
59
60 double LbdAverageSinceReset() const {
61 return lbd_running_average_.GlobalAverage();
62 }
63
64 private:
65 const SatParameters& parameters_;
66 SatDecisionPolicy* decision_policy_;
67
68 int num_restarts_;
69 int conflicts_until_next_strategy_change_;
70 int strategy_change_conflicts_;
71
72 int strategy_counter_;
73 std::vector<SatParameters::RestartAlgorithm> strategies_;
74
75 int luby_count_;
76 int conflicts_until_next_restart_;
77
78 RunningAverage dl_running_average_;
79 RunningAverage lbd_running_average_;
80 RunningAverage trail_size_running_average_;
81};
82
83// Returns the ith element of the strategy S^univ proposed by M. Luby et al. in
84// Optimal Speedup of Las Vegas Algorithms, Information Processing Letters 1993.
85// This is used to decide the number of conflicts allowed before the next
86// restart. This method, used by most SAT solvers, is usually referenced as
87// Luby.
88// Returns 2^{k-1} when i == 2^k - 1
89// and SUniv(i - 2^{k-1} + 1) when 2^{k-1} <= i < 2^k - 1.
90// The sequence is defined for i > 0 and starts with:
91// {1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8, ...}
92inline int SUniv(int i) {
93 DCHECK_GT(i, 0);
94 while (i > 2) {
95 const int most_significant_bit_position =
97 if ((1 << most_significant_bit_position) == i + 1) {
98 return 1 << (most_significant_bit_position - 1);
99 }
100 i -= (1 << most_significant_bit_position) - 1;
101 }
102 return 1;
103}
104
105} // namespace sat
106} // namespace operations_research
107
108#endif // OR_TOOLS_SAT_RESTART_H_
Contain the logic to decide when to restart a SAT tree search.
Definition restart.h:32
int NumRestarts() const
Returns the number of restarts since the last Reset().
Definition restart.h:55
void Reset()
Resets the policy using the current model parameters.
Definition restart.cc:30
std::string InfoString() const
Returns a string with the current restart statistics.
Definition restart.cc:180
void OnConflict(int conflict_trail_index, int conflict_decision_level, int conflict_lbd)
Definition restart.cc:151
GRBmodel * model
In SWIG mode, we don't want anything besides these top-level includes.
int MostSignificantBitPosition64(uint64_t n)
Definition bitset.h:235