Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
adaptative_parameter_value.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_UTIL_ADAPTATIVE_PARAMETER_VALUE_H_
15#define OR_TOOLS_UTIL_ADAPTATIVE_PARAMETER_VALUE_H_
16
17#include <algorithm>
18#include <cmath>
19#include <cstdint>
20
21namespace operations_research {
22
23// Basic adaptive [0.0, 1.0] parameter that can be increased or decreased with a
24// step that get smaller and smaller with the number of updates.
25//
26// After a while, if the probability of getting a Decrease() vs Increase() when
27// running at a given value is f(value), then this should converge towards a
28// value such that f(value) = 0.5 provided f is a non-decreasing function over
29// [0.0, 1.0].
30//
31// TODO(user): The current logic work well in practice, but has no strong
32// theoretical foundation. We should be able to come up with a better understood
33// formula that converge way faster. It will also be nice to generalize the 0.5
34// above to a target probability p.
36 public:
37 // Initial value is in [0.0, 1.0], both 0.0 and 1.0 are valid.
38 explicit AdaptiveParameterValue(double initial_value)
39 : value_(initial_value) {}
40
41 void Reset() { num_changes_ = 0; }
42
43 void Increase() {
44 const double factor = IncreaseNumChangesAndGetFactor();
45 value_ = std::min(1.0 - (1.0 - value_) / factor, value_ * factor);
46 }
47
48 void Decrease() {
49 const double factor = IncreaseNumChangesAndGetFactor();
50 value_ = std::max(value_ / factor, 1.0 - (1.0 - value_) * factor);
51 }
52
53 // If we get more than 1 datapoints from the same value(), we use a formula
54 // that is more sound than calling n times Increase()/Decrease() which depends
55 // on the order of calls.
56 void Update(int num_decreases, int num_increases) {
57 if (num_decreases == num_increases) {
58 num_changes_ += num_decreases + num_increases;
59 } else if (num_decreases < num_increases) {
60 for (int i = num_decreases; i < num_increases; ++i) Increase();
61 num_changes_ += 2 * num_decreases;
62 } else {
63 for (int i = num_increases; i < num_decreases; ++i) Decrease();
64 num_changes_ += 2 * num_increases;
65 }
66 }
67
68 double value() const { return value_; }
69
70 private:
71 // We want to change the parameters more and more slowly.
72 double IncreaseNumChangesAndGetFactor() {
73 ++num_changes_;
74 return 1.0 + 1.0 / std::sqrt(num_changes_ + 1);
75 }
76
77 double value_;
78 int64_t num_changes_ = 0;
79};
80
81} // namespace operations_research
82
83#endif // OR_TOOLS_UTIL_ADAPTATIVE_PARAMETER_VALUE_H_
void Update(int num_decreases, int num_increases)
AdaptiveParameterValue(double initial_value)
Initial value is in [0.0, 1.0], both 0.0 and 1.0 are valid.
In SWIG mode, we don't want anything besides these top-level includes.