alpaqa 1.0.0a14
Nonconvex constrained optimization
Loading...
Searching...
No Matches
max-history.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <vector>
6
7namespace alpaqa {
8
9/// Keep track of the maximum value over a specified horizon length.
10template <class T>
12 public:
13 MaxHistory(size_t memory) : buffer(std::vector<T>(memory)) {}
14
15 void add(T newt) {
16 if (full) {
17 T oldt = std::move(*it);
18 *it = std::move(newt);
19 if (*it > max_)
20 max_ = *it;
21 else if (oldt == max_)
22 max_ = *std::max_element(buffer.begin(), buffer.end());
23 ++it;
24 if (it == buffer.end())
25 it = buffer.begin();
26 } else {
27 if (it == buffer.begin() || newt > max_)
28 max_ = newt;
29 *it = std::move(newt);
30 ++it;
31 if (it == buffer.end()) {
32 it = buffer.begin();
33 full = true;
34 }
35 }
36 }
37
38 const T &max() const { return max_; }
39
40 private:
41 std::vector<T> buffer;
42 bool full = false;
43 decltype(buffer.begin()) it = buffer.begin();
45};
46
47} // namespace alpaqa
Keep track of the maximum value over a specified horizon length.
MaxHistory(size_t memory)
std::vector< T > buffer
decltype(buffer.begin()) it
const T & max() const
constexpr const auto inf
Definition config.hpp:85