OR-Tools  9.0
implied_bounds.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_IMPLIED_BOUNDS_H_
15 #define OR_TOOLS_SAT_IMPLIED_BOUNDS_H_
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <vector>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "ortools/base/int_type.h"
24 #include "ortools/base/logging.h"
26 #include "ortools/sat/integer.h"
27 #include "ortools/sat/model.h"
28 #include "ortools/sat/sat_base.h"
29 #include "ortools/util/bitset.h"
30 
31 namespace operations_research {
32 namespace sat {
33 
34 // For each IntegerVariable, the ImpliedBound class allows to list all such
35 // entries.
36 //
37 // This is meant to be used in the cut generation code when it make sense: if we
38 // have BoolVar => X >= bound, we can always lower bound the variable X by
39 // (bound - X_lb) * BoolVar + X_lb, and that can lead to stronger cuts.
41  // An integer variable in [0, 1]. When at 1, then the IntegerVariable
42  // corresponding to this entry must be greater or equal to the given lower
43  // bound.
44  IntegerVariable literal_view = kNoIntegerVariable;
45  IntegerValue lower_bound = IntegerValue(0);
46 
47  // If false, it is when the literal_view is zero that the lower bound is
48  // valid.
49  bool is_positive = true;
50 
51  // These constructors are needed for OR-Tools.
52  ImpliedBoundEntry(IntegerVariable lit, IntegerValue lb, bool positive)
53  : literal_view(lit), lower_bound(lb), is_positive(positive) {}
54 
57 };
58 
59 // Maintains all the implications of the form Literal => IntegerLiteral. We
60 // collect these implication at model loading, during probing and during search.
61 //
62 // TODO(user): This can quickly use up too much memory. Add some limit in place.
63 // In particular, each time we have literal => integer_literal we should avoid
64 // storing the same integer_literal for all other_literal for which
65 // other_literal => literal. For this we need to interact with the
66 // BinaryImplicationGraph.
67 //
68 // TODO(user): This is a bit of a duplicate with the Literal <=> IntegerLiteral
69 // stored in the IntegerEncoder class. However we only need one side here.
70 //
71 // TODO(user): Do like in the DomainDeductions class and allow to process
72 // clauses (or store them) to perform more level zero deductions. Note that this
73 // is again a slight duplicate with what we do there (except that we work at the
74 // Domain level in that presolve class).
75 //
76 // TODO(user): Add an implied bound cut generator to add these simple
77 // constraints to the LP when needed.
79  public:
81  : parameters_(*model->GetOrCreate<SatParameters>()),
82  sat_solver_(model->GetOrCreate<SatSolver>()),
83  integer_trail_(model->GetOrCreate<IntegerTrail>()),
84  integer_encoder_(model->GetOrCreate<IntegerEncoder>()) {}
86 
87  // Adds literal => integer_literal to the repository.
88  //
89  // Not that it checks right aways if there is another bound on the same
90  // variable involving literal.Negated(), in which case we can improve the
91  // level zero lower bound of the variable.
92  void Add(Literal literal, IntegerLiteral integer_literal);
93 
94  // This must be called after first_decision has been enqueued and propagated.
95  // It will inspect the trail and add all new implied bounds.
96  //
97  // Preconditions: The decision level must be one (CHECKed). And the decision
98  // must be equal to first decision (we currently do not CHECK that).
99  void ProcessIntegerTrail(Literal first_decision);
100 
101  // Returns all the implied bounds stored for the given variable.
102  // Note that only literal with an IntegerView are considered here.
103  const std::vector<ImpliedBoundEntry>& GetImpliedBounds(IntegerVariable var);
104 
105  // Returns all the variables for which GetImpliedBounds(var) is not empty. Or
106  // at least that was not empty at some point, because we lazily remove bounds
107  // that become trivial as the search progress.
108  const std::vector<IntegerVariable>& VariablesWithImpliedBounds() const {
109  return has_implied_bounds_.PositionsSetAtLeastOnce();
110  }
111 
112  // Adds to the integer trail all the new level-zero deduction made here.
113  // This can only be called at decision level zero. Returns false iff the model
114  // is infeasible.
115  bool EnqueueNewDeductions();
116 
117  // When a literal does not have an integer view, we do not add any
118  // ImpliedBoundEntry. This allows to create missing entries for a literal for
119  // which a view was just created.
120  //
121  // TODO(user): Implement and call when we create new views in the linear
122  // relaxation.
124 
125  private:
126  const SatParameters& parameters_;
127  SatSolver* sat_solver_;
128  IntegerTrail* integer_trail_;
129  IntegerEncoder* integer_encoder_;
130 
131  // TODO(user): Remove the need for this.
132  std::vector<IntegerLiteral> tmp_integer_literals_;
133 
134  // For each (Literal, IntegerVariable) the best lower bound implied by this
135  // literal. Note that there is no need to store any entries that do not
136  // improve on the level zero lower bound.
137  //
138  // TODO(user): we could lazily remove old entries to save a bit of space if
139  // many deduction where made at level zero.
140  absl::flat_hash_map<std::pair<LiteralIndex, IntegerVariable>, IntegerValue>
141  bounds_;
142 
143  // Note(user): The plan is to use these during cut generation, so only the
144  // Literal with an IntegerView that can be used in the LP relaxation need to
145  // be kept here.
146  //
147  // TODO(user): Use inlined vectors.
148  std::vector<ImpliedBoundEntry> empty_implied_bounds_;
150  var_to_bounds_;
151 
152  // Track the list of variables with some implied bounds.
153  SparseBitset<IntegerVariable> has_implied_bounds_;
154 
155  // TODO(user): Ideally, this should go away if we manage to push level-zero
156  // fact at a positive level directly.
157  absl::StrongVector<IntegerVariable, IntegerValue> level_zero_lower_bounds_;
158  SparseBitset<IntegerVariable> new_level_zero_bounds_;
159 
160  // Stats.
161  int64_t num_deductions_ = 0;
162  int64_t num_enqueued_in_var_to_bounds_ = 0;
163 };
164 
165 } // namespace sat
166 } // namespace operations_research
167 
168 #endif // OR_TOOLS_SAT_IMPLIED_BOUNDS_H_
const std::vector< IntegerType > & PositionsSetAtLeastOnce() const
Definition: bitset.h:814
const std::vector< ImpliedBoundEntry > & GetImpliedBounds(IntegerVariable var)
void NotifyNewIntegerView(Literal literal)
void Add(Literal literal, IntegerLiteral integer_literal)
const std::vector< IntegerVariable > & VariablesWithImpliedBounds() const
void ProcessIntegerTrail(Literal first_decision)
Class that owns everything related to a particular optimization model.
Definition: sat/model.h:38
IntVar * var
Definition: expr_array.cc:1874
GRBmodel * model
const IntegerVariable kNoIntegerVariable(-1)
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:85
ImpliedBoundEntry()
bool is_positive
ImpliedBoundEntry(IntegerVariable lit, IntegerValue lb, bool positive)
IntegerValue lower_bound
IntegerVariable literal_view