OR-Tools  9.2
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 <limits>
18 #include <vector>
19 
20 #include "ortools/sat/integer.h"
23 #include "ortools/sat/sat_base.h"
24 #include "ortools/sat/sat_solver.h"
26 
27 namespace operations_research {
28 namespace sat {
29 
30 // Implement a "classic" MIP tree search by having an exhaustive list of open
31 // nodes.
32 //
33 // The goal of this subsolver is to improve the objective lower bound. It is
34 // meant to be used in a multi-thread portfolio, and as such it really do not
35 // care about finding solution. It is all about improving the lower bound.
36 //
37 // TODO(user): What this is doing is really similar to asking a SAT solver if
38 // the current objective lower bound is reachable by solving a SAT problem.
39 // However, this code handle on the side all the "conflict" of the form
40 // objective > current_lb. As a result, when it is UNSAT, we can bump the lower
41 // bound by a bigger amount than one. We also do not completely loose everything
42 // learned so far for the next iteration.
43 class LbTreeSearch {
44  public:
45  explicit LbTreeSearch(Model* model);
46 
47  // Explores the search space.
49  const std::function<void()>& feasible_solution_observer);
50 
51  private:
52  // Code a binary tree.
53  DEFINE_INT_TYPE(NodeIndex, int);
54  struct Node {
55  Node(Literal l, IntegerValue lb)
56  : literal(l), true_objective(lb), false_objective(lb) {}
57 
58  // The objective lower bound at this node.
59  IntegerValue MinObjective() const {
60  return std::min(true_objective, false_objective);
61  }
62 
63  // Invariant: the objective bounds only increase.
64  void UpdateObjective(IntegerValue v) {
65  true_objective = std::max(true_objective, v);
66  false_objective = std::max(false_objective, v);
67  }
68  void UpdateTrueObjective(IntegerValue v) {
69  true_objective = std::max(true_objective, v);
70  }
71  void UpdateFalseObjective(IntegerValue v) {
72  false_objective = std::max(false_objective, v);
73  }
74 
75  // The decision for the true and false branch under this node.
76  /*const*/ Literal literal;
77 
78  // The objective lower bound in both branches.
79  IntegerValue true_objective;
80  IntegerValue false_objective;
81 
82  // Points to adjacent nodes in the tree. Large if no connection.
85  };
86 
87  // Display the current tree, this is mainly here to investigate ideas to
88  // improve the code.
89  void DebugDisplayTree(NodeIndex root) const;
90 
91  // Updates the objective of the node in the current branch at level n from
92  // the one at level n - 1.
93  void UpdateObjectiveFromParent(int level);
94 
95  // Updates the objective of the node in the current branch at level n - 1 from
96  // the one at level n.
97  void UpdateParentObjective(int level);
98 
99  // Model singleton class used here.
100  TimeLimit* time_limit_;
101  ModelRandomGenerator* random_;
102  SatSolver* sat_solver_;
103  IntegerEncoder* integer_encoder_;
104  IntegerTrail* integer_trail_;
105  SharedResponseManager* shared_response_;
106  SatDecisionPolicy* sat_decision_;
107  IntegerSearchHelper* search_helper_;
108  IntegerVariable objective_var_;
109 
110  // This can stay null. Otherwise it will be the lp constraint with
111  // objective_var_ as objective.
112  LinearProgrammingConstraint* lp_constraint_ = nullptr;
113 
114  // We temporarily cache the shared_response_ objective lb here.
115  IntegerValue current_objective_lb_;
116 
117  // Memory for all the nodes.
119 
120  // The list of nodes in the current branch, in order from the root.
121  std::vector<NodeIndex> current_branch_;
122 
123  // Our heuristic used to explore the tree. See code for detail.
124  std::function<BooleanOrIntegerLiteral()> search_heuristic_;
125 
126  int64_t num_rc_detected_ = 0;
127 };
128 
129 } // namespace sat
130 } // namespace operations_research
131 
132 #endif // OR_TOOLS_SAT_LB_TREE_SEARCH_H_
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:106
int64_t min
Definition: alldiff_cst.cc:139
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
GRBmodel * model
int64_t max
Definition: alldiff_cst.cc:140
SatSolver::Status Search(const std::function< void()> &feasible_solution_observer)
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:85