alpaqa
1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
src
alpaqa
include
alpaqa
util
max-history.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <algorithm>
4
#include <cstddef>
5
#include <vector>
6
7
namespace
alpaqa
{
8
9
/// Keep track of the maximum value over a specified horizon length.
10
template
<
class
T>
11
class
MaxHistory
{
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();
44
T
max_
{};
45
};
46
47
}
// namespace alpaqa
alpaqa::MaxHistory
Keep track of the maximum value over a specified horizon length.
Definition
max-history.hpp:11
alpaqa::MaxHistory::MaxHistory
MaxHistory(size_t memory)
Definition
max-history.hpp:13
alpaqa::MaxHistory::full
bool full
Definition
max-history.hpp:42
alpaqa::MaxHistory::add
void add(T newt)
Definition
max-history.hpp:15
alpaqa::MaxHistory::buffer
std::vector< T > buffer
Definition
max-history.hpp:41
alpaqa::MaxHistory::it
decltype(buffer.begin()) it
Definition
max-history.hpp:43
alpaqa::MaxHistory::max_
T max_
Definition
max-history.hpp:44
alpaqa::MaxHistory::max
const T & max() const
Definition
max-history.hpp:38
alpaqa
Definition
anderson.hpp:10
alpaqa::inf
constexpr const auto inf
Definition
config.hpp:98
Generated on Tue Dec 12 2023 for alpaqa by
1.9.8