Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
pricing.h
Go to the documentation of this file.
1// Copyright 2010-2024 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_GLOP_PRICING_H_
15#define OR_TOOLS_GLOP_PRICING_H_
16
17#include <cmath>
18#include <random>
19#include <string>
20
21#include "absl/log/check.h"
22#include "absl/random/bit_gen_ref.h"
23#include "absl/random/random.h"
25#include "ortools/util/bitset.h"
26#include "ortools/util/stats.h"
27
28namespace operations_research {
29namespace glop {
30
31// Maintains a set of elements in [0, n), each with an associated value and
32// allows to query the element of maximum value efficiently.
33//
34// This is optimized for use in the pricing step of the simplex algorithm.
35// Basically at each simplex iterations, you want to:
36//
37// 1/ Get the candidate with the maximum value. The number of candidates
38// can be close to n, or really small. You also want some randomization if
39// several elements have an equivalent (maximum) value.
40//
41// 2/ Update the set of candidate and their values, where the number of update
42// is usually a lot smaller than n. Note that in some corner cases, there are
43// two "updates" phases, so a position can be updated twice.
44//
45// The idea is to be faster than O(num_candidates) per GetMaximum(), most of the
46// time. All updates should be in O(1) with as little overhead as possible. The
47// algorithm here dynamically maintain the top-k (for k=32) with best effort and
48// use it instead of doing a O(num_candidates) scan when possible.
49//
50// Note that when O(num_updates) << n, this can have a huge effect. A basic O(1)
51// per update, O(num_candidates) per maximum query was taking around 60% of the
52// total time on graph40-80-1rand.pb.gz ! with the top-32 algo coded here, it is
53// around 3%, and the number of "fast" GetMaximum() that hit the top-k heap on
54// the first 120s of that problem was 250757 / 255659. Note that n was 282624 in
55// this case, which is not even the biggest size we can tackle.
56//
57// Note(user): This could be moved to util/ as a general class if someone wants
58// to reuse it, it is however tuned for use in Glop pricing step and might
59// becomes even more specific in the future.
60template <typename Index>
62 public:
63 // To simplify the APIs, we take a random number generator at construction.
64 explicit DynamicMaximum(absl::BitGenRef random) : random_(random) {}
65
66 // Prepares the class to hold up to n candidates with indices in [0, n).
67 // Initially no indices is a candidate.
69
70 // Returns the index with the maximum value or Index(-1) if the set is empty
71 // and there is no possible candidate. If there are more than one candidate
72 // with the same maximum value, this will return a random one (not always
73 // uniformly if there is a large number of ties).
75
76 // Removes the given index from the set of candidates.
77 void Remove(Index position);
78
79 // Adds an element to the set of candidate and sets its value. If the element
80 // is already present, this updates its value. The value must be finite.
82
83 // Optimized version of AddOrUpdate() for the dense case. If one knows that
84 // there will be O(n) updates, it is possible to call StartDenseUpdates() and
85 // then use DenseAddOrUpdate() instead of AddOrUpdate() which is slighlty
86 // faster.
87 //
88 // Note that calling AddOrUpdate() will still works fine, but will cause an
89 // extra test per call.
92
93 // Returns the current size n that was used in the last ClearAndResize().
94 void Clear() { ClearAndResize(Index(0)); }
95 Index Size() const { return values_.size(); }
96
97 // Returns some stats about this class if they are enabled.
98 std::string StatString() const { return stats_.StatString(); }
99
100 private:
101 // Adds an elements to the set of top elements.
102 void UpdateTopK(Index position, Fractional value);
103
104 // Returns a random element from the set {best} U {equivalent_choices_}.
105 // If equivalent_choices_ is empty, this just returns best.
106 Index RandomizeIfManyChoices(Index best);
107
108 // For tie-breaking.
109 absl::BitGenRef random_;
110 std::vector<Index> equivalent_choices_;
111
112 // Set of candidates and their value.
113 // Note that if is_candidate_[index] is false, values_[index] can be anything.
115 Bitset64<Index> is_candidate_;
116
117 // We maintain the top-k current candidates for a fixed k. Note that not all
118 // entries in tops_ are necessary up to date since we don't remove elements.
119 // There can even be duplicate elements inside if Update() add an element
120 // already inside. This is fine, since tops_ will be recomputed as soon as we
121 // can't get the true maximum from there.
122 //
123 // The invariant is that:
124 // - All elements > threshold_ are in tops_.
125 // - All elements not in tops have a value <= threshold_.
126 // - elements == threshold_ can be in or out.
127 //
128 // In particular, the threshold only increase until the heap becomes empty and
129 // is recomputed from scratch by GetMaximum().
130 struct HeapElement {
131 HeapElement() = default;
132 HeapElement(Index i, Fractional v) : index(i), value(v) {}
133
134 Index index;
135 Fractional value;
136
137 // We want a min-heap: tops_.top() actually represents the k-th value, not
138 // the max.
139 double operator<(const HeapElement& other) const {
140 return value > other.value;
141 }
142 };
143 Fractional threshold_;
144 std::vector<HeapElement> tops_;
145
146 // Statistics about the class.
147 struct QueryStats : public StatsGroup {
148 QueryStats()
149 : StatsGroup("PricingStats"),
150 get_maximum("get_maximum", this),
151 heap_size_on_hit("heap_size_on_hit", this),
152 random_choices("random_choices", this) {}
153 TimeDistribution get_maximum;
154 IntegerDistribution heap_size_on_hit;
155 IntegerDistribution random_choices;
156 };
157 QueryStats stats_;
158};
159
160template <typename Index>
162 tops_.clear();
163 threshold_ = -kInfinity;
164 values_.resize(n);
165 is_candidate_.ClearAndResize(n);
166}
167
168template <typename Index>
170 is_candidate_.Clear(position);
171}
172
173template <typename Index>
175 // This disable tops_ until the next GetMaximum().
176 tops_.clear();
177 threshold_ = kInfinity;
178}
179
180template <typename Index>
183 DCHECK(!std::isnan(value));
184 DCHECK(tops_.empty());
185 is_candidate_.Set(position);
186 values_[position] = value;
187}
188
189template <typename Index>
192 DCHECK(!std::isnan(value));
193 is_candidate_.Set(position);
194 values_[position] = value;
195 if (value >= threshold_) UpdateTopK(position, value);
196}
197
198template <typename Index>
200 if (equivalent_choices_.empty()) return best;
201 equivalent_choices_.push_back(best);
202 stats_.random_choices.Add(equivalent_choices_.size());
203
204 return equivalent_choices_[std::uniform_int_distribution<int>(
205 0, equivalent_choices_.size() - 1)(random_)];
206}
207
208template <typename Index>
210 SCOPED_TIME_STAT(&stats_);
211 Fractional best_value = -kInfinity;
212 Index best_position(-1);
213 equivalent_choices_.clear();
214
215 // Optimized version if the maximum is in tops_ already.
216 //
217 // We do two things here:
218 // 1/ Filter tops_ to only contain valid entries. This is because we never
219 // remove element, so the value of one of the element in tops might have
220 // decreased now. Note that we leave threshold_ untouched, so it
221 // can actually be lower than the minimum of the element in tops.
222 // 2/ Get the maximum of the valid elements.
223 if (!tops_.empty()) {
224 int new_size = 0;
225 for (const HeapElement e : tops_) {
226 // The two possible sources of "invalidity".
227 if (!is_candidate_[e.index]) continue;
228 if (values_[e.index] != e.value) continue;
229
230 tops_[new_size++] = e;
231 if (e.value >= best_value) {
232 if (e.value == best_value) {
233 equivalent_choices_.push_back(e.index);
234 continue;
235 }
236 equivalent_choices_.clear();
237 best_value = e.value;
238 best_position = e.index;
239 }
240 }
241 tops_.resize(new_size);
242 if (new_size != 0) {
243 stats_.heap_size_on_hit.Add(new_size);
244 return RandomizeIfManyChoices(best_position);
245 }
246 }
247
248 // We need to iterate over all the candidates.
249 threshold_ = -kInfinity;
250 DCHECK(tops_.empty());
251 const auto values = values_.const_view();
252 for (const Index position : is_candidate_) {
253 const Fractional value = values[position];
254
255 // TODO(user): Add a mode when we do not maintain the TopK for small sizes
256 // (like n < 1000) ? The gain might not be worth the extra code though.
257 if (value < threshold_) continue;
258 UpdateTopK(position, value);
259
260 if (value >= best_value) {
261 if (value == best_value) {
262 equivalent_choices_.push_back(position);
263 continue;
264 }
265 equivalent_choices_.clear();
266 best_value = value;
267 best_position = position;
268 }
269 }
270
271 return RandomizeIfManyChoices(best_position);
272}
273
274template <typename Index>
275inline void DynamicMaximum<Index>::UpdateTopK(Index position,
277 // Note that this should only be called when an update is required.
278 DCHECK_GE(value, threshold_);
279
280 // We use a compile time size of the form 2^n - 1 to have a full binary heap.
281 //
282 // TODO(user): Adapt the size depending on the problem size? Note sure it is
283 // worth it. To experiment more.
284 constexpr int k = 31;
285 static_assert(((k + 1) & k) == 0, "k + 1 should be a power of 2.");
286
287 // Simply grow the vector until we hit a size of k.
288 if (tops_.size() < k) {
289 tops_.emplace_back(position, value);
290 if (tops_.size() == k) {
291 std::make_heap(tops_.begin(), tops_.end());
292 threshold_ = tops_[0].value;
293 }
294 return;
295 }
296
297 // If the value is equal, we randomly replace it. Having some randomness can
298 // also be important to increase the chance of keeping the true maximum in the
299 // top k set.
300 //
301 // TODO(user): use proper probability by counting the number of ties seen and
302 // replacing a random minimum element to get an uniform distribution? Note
303 // that it will never be truly uniform since once the top k structure is
304 // constructed, we will reuse it as much as possible, so it will be biased
305 // towards elements already inside.
306 if (value == tops_[0].value) {
307 if (absl::Bernoulli(random_, 0.5)) {
308 tops_[0].index = position;
309 }
310 return;
311 }
312
313 // The code below is basically a custom implementation of this. It is however
314 // only slighlty faster for such a small heap. So it might not be completely
315 // worth it.
316 if (/*DISABLES CODE*/ (false)) {
317 std::pop_heap(tops_.begin(), tops_.end());
318 tops_.back() = HeapElement(position, value);
319 std::push_heap(tops_.begin(), tops_.end());
320 threshold_ = tops_[0].value;
321 return;
322 }
323
324 // To not have to do std::pop_heap() and then std::push_heap(), we code our
325 // own update. Note that we exploit the fact that k is of the form 2^n - 1 to
326 // save one test per update.
327 int i = 0;
328 DCHECK_EQ(tops_.size(), k);
329 constexpr int limit = k / 2;
330 for (; i < limit;) {
331 const int left_child = 2 * i + 1;
332 const int right_child = left_child + 1;
333 const Fractional l_value = tops_[left_child].value;
334 const Fractional r_value = tops_[right_child].value;
335 if (l_value > r_value) {
336 if (value <= r_value) break;
337 tops_[i] = tops_[right_child];
338 i = right_child;
339 } else {
340 if (value <= l_value) break;
341 tops_[i] = tops_[left_child];
342 i = left_child;
343 }
344 }
345 tops_[i] = HeapElement(position, value);
346 threshold_ = tops_[0].value;
347 DCHECK(std::is_heap(tops_.begin(), tops_.end()));
348}
349
350} // namespace glop
351} // namespace operations_research
352
353#endif // OR_TOOLS_GLOP_PRICING_H_
int right_child
StatsGroup(absl::string_view name)
Definition stats.h:135
std::string StatString() const
Definition stats.cc:77
std::string StatString() const
Returns some stats about this class if they are enabled.
Definition pricing.h:98
void DenseAddOrUpdate(Index position, Fractional value)
Definition pricing.h:181
void Remove(Index position)
Removes the given index from the set of candidates.
Definition pricing.h:169
void Clear()
Returns the current size n that was used in the last ClearAndResize().
Definition pricing.h:94
DynamicMaximum(absl::BitGenRef random)
To simplify the APIs, we take a random number generator at construction.
Definition pricing.h:64
void AddOrUpdate(Index position, Fractional value)
Definition pricing.h:190
int64_t value
constexpr double kInfinity
Infinity for type Fractional.
Definition lp_types.h:89
In SWIG mode, we don't want anything besides these top-level includes.
#define SCOPED_TIME_STAT(stats)
Definition stats.h:418