OR-Tools  9.3
restart.h
Go to the documentation of this file.
1// Copyright 2010-2021 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
21#include "ortools/sat/model.h"
23#include "ortools/sat/sat_parameters.pb.h"
24#include "ortools/util/bitset.h"
26
27namespace operations_research {
28namespace sat {
29
30// Contain the logic to decide when to restart a SAT tree search.
32 public:
34 : parameters_(*(model->GetOrCreate<SatParameters>())),
35 decision_policy_(model->GetOrCreate<SatDecisionPolicy>()) {
36 Reset();
37 }
38
39 // Resets the policy using the current model parameters.
40 void Reset();
41
42 // Returns true if the solver should be restarted before the next decision is
43 // taken. Note that this will return true just once and then assumes that
44 // the search was restarted and starts worrying about the next restart.
45 bool ShouldRestart();
46
47 // This will be called by the solver engine after each conflict. The arguments
48 // reflect the state of the solver when the conflict was detected and before
49 // the backjump.
50 void OnConflict(int conflict_trail_index, int conflict_decision_level,
51 int conflict_lbd);
52
53 // Returns the number of restarts since the last Reset().
54 int NumRestarts() const { return num_restarts_; }
55
56 // Returns a string with the current restart statistics.
57 std::string InfoString() const;
58
59 private:
60 const SatParameters& parameters_;
61 SatDecisionPolicy* decision_policy_;
62
63 int num_restarts_;
64 int conflicts_until_next_strategy_change_;
65 int strategy_change_conflicts_;
66
67 int strategy_counter_;
68 std::vector<SatParameters::RestartAlgorithm> strategies_;
69
70 int luby_count_;
71 int conflicts_until_next_restart_;
72
73 RunningAverage dl_running_average_;
74 RunningAverage lbd_running_average_;
75 RunningAverage trail_size_running_average_;
76};
77
78// Returns the ith element of the strategy S^univ proposed by M. Luby et al. in
79// Optimal Speedup of Las Vegas Algorithms, Information Processing Letters 1993.
80// This is used to decide the number of conflicts allowed before the next
81// restart. This method, used by most SAT solvers, is usually referenced as
82// Luby.
83// Returns 2^{k-1} when i == 2^k - 1
84// and SUniv(i - 2^{k-1} + 1) when 2^{k-1} <= i < 2^k - 1.
85// The sequence is defined for i > 0 and starts with:
86// {1, 1, 2, 1, 1, 2, 4, 1, 1, 2, 1, 1, 2, 4, 8, ...}
87inline int SUniv(int i) {
88 DCHECK_GT(i, 0);
89 while (i > 2) {
90 const int most_significant_bit_position =
92 if ((1 << most_significant_bit_position) == i + 1) {
93 return 1 << (most_significant_bit_position - 1);
94 }
95 i -= (1 << most_significant_bit_position) - 1;
96 }
97 return 1;
98}
99
100} // namespace sat
101} // namespace operations_research
102
103#endif // OR_TOOLS_SAT_RESTART_H_
#define DCHECK_GT(val1, val2)
Definition: base/logging.h:896
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
void OnConflict(int conflict_trail_index, int conflict_decision_level, int conflict_lbd)
Definition: restart.cc:151
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.
int MostSignificantBitPosition64(uint64_t n)
Definition: bitset.h:231