Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lb_tree_search.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_LB_TREE_SEARCH_H_
15#define OR_TOOLS_SAT_LB_TREE_SEARCH_H_
16
17#include <stdint.h>
18
19#include <algorithm>
20#include <functional>
21#include <limits>
22#include <string>
23#include <vector>
24
25#include "absl/log/check.h"
26#include "absl/types/span.h"
29#include "ortools/sat/integer.h"
33#include "ortools/sat/model.h"
37#include "ortools/sat/sat_parameters.pb.h"
40#include "ortools/sat/util.h"
43
44namespace operations_research {
45namespace sat {
46
47// Implement a "classic" MIP tree search by having an exhaustive list of open
48// nodes.
49//
50// The goal of this subsolver is to improve the objective lower bound. It is
51// meant to be used in a multi-thread portfolio, and as such it really do not
52// care about finding solution. It is all about improving the lower bound.
53//
54// TODO(user): What this is doing is really similar to asking a SAT solver if
55// the current objective lower bound is reachable by solving a SAT problem.
56// However, this code handle on the side all the "conflict" of the form
57// objective > current_lb. As a result, when it is UNSAT, we can bump the lower
58// bound by a bigger amount than one. We also do not completely loose everything
59// learned so far for the next iteration.
61 public:
62 explicit LbTreeSearch(Model* model);
63
64 // Explores the search space.
66 const std::function<void()>& feasible_solution_observer);
67
68 private:
69 // Code a binary tree.
71 struct Node {
72 explicit Node(IntegerValue lb) : true_objective(lb), false_objective(lb) {}
73
74 // The objective lower bound at this node.
75 IntegerValue MinObjective() const {
76 return std::min(true_objective, false_objective);
77 }
78
79 // Invariant: the objective bounds only increase.
80 void UpdateObjective(IntegerValue v) {
81 true_objective = std::max(true_objective, v);
82 false_objective = std::max(false_objective, v);
83 }
84 void UpdateTrueObjective(IntegerValue v) {
85 true_objective = std::max(true_objective, v);
86 }
87 void UpdateFalseObjective(IntegerValue v) {
88 false_objective = std::max(false_objective, v);
89 }
90
91 // Should be called only once.
92 void SetDecision(Literal l) {
93 DCHECK(!is_deleted);
94 DCHECK_EQ(literal_index, kNoLiteralIndex);
95 literal_index = l.Index();
96 }
97
98 Literal Decision() const {
99 DCHECK(!is_deleted);
100 DCHECK_NE(literal_index, kNoLiteralIndex);
101 return sat::Literal(literal_index);
102 }
103
104 bool IsLeaf() const { return literal_index == kNoLiteralIndex; }
105
106 // The decision for the true and false branch under this node.
107 // Initially this is kNoLiteralIndex until SetDecision() is called.
108 LiteralIndex literal_index = kNoLiteralIndex;
109
110 // The objective lower bound in both branches.
111 IntegerValue true_objective;
112 IntegerValue false_objective;
113
114 // Points to adjacent nodes in the tree. Large if no connection.
115 NodeIndex true_child = NodeIndex(std::numeric_limits<int32_t>::max());
116 NodeIndex false_child = NodeIndex(std::numeric_limits<int32_t>::max());
117
118 // Indicates if this nodes was removed from the tree.
119 bool is_deleted = false;
120
121 // Experimental. Store the optimal basis at each node.
122 int64_t basis_timestamp;
123 glop::BasisState basis;
124 };
125
126 // Regroup some logic done when we are back at level zero in Search().
127 // Returns false if UNSAT.
128 bool LevelZeroLogic();
129
130 // Returns true if we save/load LP basis.
131 // Note that when this is true we also do not solve the LP as often.
132 bool SaveLpBasisOption() const {
133 return lp_constraint_ != nullptr &&
134 parameters_.save_lp_basis_in_lb_tree_search();
135 }
136
137 // Display the current tree, this is mainly here to investigate ideas to
138 // improve the code.
139 std::string NodeDebugString(NodeIndex node) const;
140 void DebugDisplayTree(NodeIndex root) const;
141
142 // Updates the objective of the node in the current branch at level n from
143 // the one at level n - 1.
144 void UpdateObjectiveFromParent(int level);
145
146 // Updates the objective of the node in the current branch at level n - 1 from
147 // the one at level n.
148 void UpdateParentObjective(int level);
149
150 // Returns false on conflict.
151 bool FullRestart();
152
153 // Loads any known basis that is the closest to the current branch.
154 void EnableLpAndLoadBestBasis();
155 void SaveLpBasisInto(Node& node);
156 bool NodeHasUpToDateBasis(const Node& node) const;
157 bool NodeHasBasis(const Node& node) const;
158
159 // Mark the given node as deleted. Its literal is assumed to be set. We also
160 // delete the subtree that is not longer relevant.
161 void MarkAsDeletedNodeAndUnreachableSubtree(Node& node);
162 void MarkBranchAsInfeasible(Node& node, bool true_branch);
163 void MarkSubtreeAsDeleted(NodeIndex root);
164
165 // Create a new node at the end of the current branch.
166 // This assume the last decision in the branch is assigned.
167 NodeIndex CreateNewEmptyNodeIfNeeded();
168 void AppendNewNodeToCurrentBranch(Literal decision);
169
170 // Update the bounds on the given nodes by using reduced costs if possible.
171 void ExploitReducedCosts(NodeIndex n);
172
173 // Returns a small number of decision needed to reach the same conflict.
174 // We basically reduce the number of decision at each level to 1.
175 std::vector<Literal> ExtractDecisions(int base_level,
176 absl::Span<const Literal> conflict);
177
178 // Used in the solve logs.
179 std::string SmallProgressString() const;
180
181 // Save the current number of iterations on creation and add the difference
182 // to the counter when the returned function is called. This is meant to
183 // be used with:
184 // const auto cleanup = absl::MakeCleanup(UpdateLpIters(&counter));
185 std::function<void()> UpdateLpIters(int64_t* counter);
186
187 // Model singleton class used here.
188 const std::string name_;
189 TimeLimit* time_limit_;
190 ModelRandomGenerator* random_;
191 SatSolver* sat_solver_;
192 IntegerEncoder* integer_encoder_;
193 Trail* trail_;
194 const VariablesAssignment& assignment_;
195 IntegerTrail* integer_trail_;
196 GenericLiteralWatcher* watcher_;
197 SharedResponseManager* shared_response_;
198 PseudoCosts* pseudo_costs_;
199 SatDecisionPolicy* sat_decision_;
200 IntegerSearchHelper* search_helper_;
201 IntegerVariable objective_var_;
202 const SatParameters& parameters_;
203
204 // This can stay null. Otherwise it will be the lp constraint with
205 // objective_var_ as objective.
206 LinearProgrammingConstraint* lp_constraint_ = nullptr;
207
208 // We temporarily cache the shared_response_ objective lb here.
209 IntegerValue current_objective_lb_;
210
211 // Memory for all the nodes.
212 int num_nodes_in_tree_ = 0;
214
215 // The list of nodes in the current branch, in order from the root.
216 std::vector<NodeIndex> current_branch_;
217
218 // Our heuristic used to explore the tree. See code for detail.
219 std::function<BooleanOrIntegerLiteral()> search_heuristic_;
220
221 int64_t num_rc_detected_ = 0;
222
223 // Counts the number of decisions we are taking while exploring the search
224 // tree.
225 int64_t num_decisions_taken_ = 0;
226
227 // Counts number of lp iterations at various places.
228 int64_t num_lp_iters_at_level_zero_ = 0;
229 int64_t num_lp_iters_save_basis_ = 0;
230 int64_t num_lp_iters_first_branch_ = 0;
231 int64_t num_lp_iters_dive_ = 0;
232
233 // Used to trigger the initial restarts and imports.
234 int num_full_restarts_ = 0;
235 int64_t num_decisions_taken_at_last_restart_ = 0;
236 int64_t num_decisions_taken_at_last_level_zero_ = 0;
237
238 // Count the number of time we are back to decision level zero.
239 int64_t num_back_to_root_node_ = 0;
240};
241
242} // namespace sat
243} // namespace operations_research
244
245#endif // OR_TOOLS_SAT_LB_TREE_SEARCH_H_
An helper class to share the code used by the different kind of search.
SatSolver::Status Search(const std::function< void()> &feasible_solution_observer)
Explores the search space.
LiteralIndex Index() const
Definition sat_base.h:91
const LiteralIndex kNoLiteralIndex(-1)
In SWIG mode, we don't want anything besides these top-level includes.
#define DEFINE_STRONG_INDEX_TYPE(index_type_name)