OR-Tools  9.3
lb_tree_search.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_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 <vector>
23
24#include "absl/strings/string_view.h"
25#include "absl/time/time.h"
27#include "ortools/sat/integer.h"
30#include "ortools/sat/model.h"
33#include "ortools/sat/sat_parameters.pb.h"
36#include "ortools/sat/util.h"
39
40namespace operations_research {
41namespace sat {
42
43// Implement a "classic" MIP tree search by having an exhaustive list of open
44// nodes.
45//
46// The goal of this subsolver is to improve the objective lower bound. It is
47// meant to be used in a multi-thread portfolio, and as such it really do not
48// care about finding solution. It is all about improving the lower bound.
49//
50// TODO(user): What this is doing is really similar to asking a SAT solver if
51// the current objective lower bound is reachable by solving a SAT problem.
52// However, this code handle on the side all the "conflict" of the form
53// objective > current_lb. As a result, when it is UNSAT, we can bump the lower
54// bound by a bigger amount than one. We also do not completely loose everything
55// learned so far for the next iteration.
57 public:
58 explicit LbTreeSearch(Model* model);
59
60 // Explores the search space.
62 const std::function<void()>& feasible_solution_observer);
63
64 private:
65 // Code a binary tree.
66 DEFINE_STRONG_INDEX_TYPE(NodeIndex);
67 struct Node {
68 Node(Literal l, IntegerValue lb)
69 : literal(l), true_objective(lb), false_objective(lb) {}
70
71 // The objective lower bound at this node.
72 IntegerValue MinObjective() const {
73 return std::min(true_objective, false_objective);
74 }
75
76 // Invariant: the objective bounds only increase.
77 void UpdateObjective(IntegerValue v) {
78 true_objective = std::max(true_objective, v);
79 false_objective = std::max(false_objective, v);
80 }
81 void UpdateTrueObjective(IntegerValue v) {
82 true_objective = std::max(true_objective, v);
83 }
84 void UpdateFalseObjective(IntegerValue v) {
85 false_objective = std::max(false_objective, v);
86 }
87
88 // The decision for the true and false branch under this node.
89 /*const*/ Literal literal;
90
91 // The objective lower bound in both branches.
92 IntegerValue true_objective;
93 IntegerValue false_objective;
94
95 // Points to adjacent nodes in the tree. Large if no connection.
98 };
99
100 // Display the current tree, this is mainly here to investigate ideas to
101 // improve the code.
102 void DebugDisplayTree(NodeIndex root) const;
103
104 // Updates the objective of the node in the current branch at level n from
105 // the one at level n - 1.
106 void UpdateObjectiveFromParent(int level);
107
108 // Updates the objective of the node in the current branch at level n - 1 from
109 // the one at level n.
110 void UpdateParentObjective(int level);
111
112 // Model singleton class used here.
113 TimeLimit* time_limit_;
114 ModelRandomGenerator* random_;
115 SatSolver* sat_solver_;
116 IntegerEncoder* integer_encoder_;
117 IntegerTrail* integer_trail_;
118 SharedResponseManager* shared_response_;
119 SatDecisionPolicy* sat_decision_;
120 IntegerSearchHelper* search_helper_;
121 IntegerVariable objective_var_;
122 const SatParameters& parameters_;
123
124 // This can stay null. Otherwise it will be the lp constraint with
125 // objective_var_ as objective.
126 LinearProgrammingConstraint* lp_constraint_ = nullptr;
127
128 // We temporarily cache the shared_response_ objective lb here.
129 IntegerValue current_objective_lb_;
130
131 // Memory for all the nodes.
133
134 // The list of nodes in the current branch, in order from the root.
135 std::vector<NodeIndex> current_branch_;
136
137 // Our heuristic used to explore the tree. See code for detail.
138 std::function<BooleanOrIntegerLiteral()> search_heuristic_;
139
140 int64_t num_rc_detected_ = 0;
141
142 // Counts the number of decisions we are taking while exploring the search
143 // tree.
144 int64_t num_decisions_taken_ = 0;
145
146 // Used to trigger the initial restarts and imports.
147 int64_t num_decisions_taken_at_last_restart_ = 0;
148 int64_t num_decisions_taken_at_last_import_ = 0;
149
150 // Count the number of hard restarts (where all nodes are cleared) and soft
151 // restarts (where the search backtracks to level 0 to import other solver
152 // changes).
153 int64_t num_imports_ = 0;
154
155 // Used to display periodic info to the log.
156 absl::Time last_logging_time_;
157};
158
159} // namespace sat
160} // namespace operations_research
161
162#endif // OR_TOOLS_SAT_LB_TREE_SEARCH_H_
int64_t max
Definition: alldiff_cst.cc:140
int64_t min
Definition: alldiff_cst.cc:139
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:106
SatSolver::Status Search(const std::function< void()> &feasible_solution_observer)
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:42
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:89