Ultra 0.9.4
Loading...
Searching...
No Matches
alps.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_ALPS_H)
14#define ULTRA_ALPS_H
15
16#include "kernel/population.h"
17
18#include <array>
19#include <cstddef>
20#include <functional>
21#include <span>
22
23namespace ultra
24{
25
46template<class Layer>
48{
49public:
55 alps_layer_pair(Layer &l) noexcept
56 : layers_{std::ref(l), std::ref(l)}, has_secondary_(false) {}
57
64 alps_layer_pair(Layer &p, Layer &s) noexcept
65 : layers_{std::ref(p), std::ref(s)}, has_secondary_(true) {}
66
68 [[nodiscard]] constexpr std::size_t size() const noexcept
69 {
70 return 1 + static_cast<std::size_t>(has_secondary_);
71 }
72
74 [[nodiscard]] constexpr bool has_secondary() const noexcept
75 {
76 return has_secondary_;
77 }
78
80 [[nodiscard]] constexpr auto begin() const noexcept
81 {
82 return layers_.begin();
83 }
84
86 [[nodiscard]] constexpr auto end() const noexcept
87 {
88 return layers_.begin() + static_cast<std::ptrdiff_t>(size());
89 }
90
92 [[nodiscard]] constexpr Layer &primary() const noexcept
93 {
94 return layers_[0].get();
95 }
96
100 [[nodiscard]] constexpr Layer &secondary() const noexcept
101 {
102 Expects(has_secondary());
103 return layers_[1].get();
104 }
105
117 [[nodiscard]] Layer &random(double p_pri) const
118 {
119 Expects(in_0_1(p_pri));
120 return !has_secondary_ || random::boolean(p_pri) ? primary() : secondary();
121 }
122
123private:
124 std::array<std::reference_wrapper<Layer>, 2> layers_;
125 bool has_secondary_ {false};
126};
127
128template<class Layer>
130
131template<class Layer>
132alps_layer_pair(Layer &, Layer &) -> alps_layer_pair<Layer>;
133
134template<class Layer>
136
137template<class Layer>
138alps_layer_pair(const Layer &, const Layer &) -> alps_layer_pair<const Layer>;
139
140namespace alps
141{
142
151{
152 [[nodiscard]] unsigned max_age(std::size_t) const;
153 [[nodiscard]] unsigned max_age(std::size_t, std::size_t) const;
154
169 unsigned age_gap {0};
170
172 std::size_t max_layers {0};
173
178 double p_main_layer {-1.0};
179};
180
181template<LayeredPopulation P>
182void set_age(P &pop)
183{
184 const auto layers(pop.layers());
185 if (!layers)
186 return;
187
188 const auto &params(pop.problem().params);
189
190 for (std::size_t l(0); l < layers; ++l)
191 pop.layer(l).max_age(params.alps.max_age(l, layers));
192}
193
218template<LayeredPopulation P>
219alps_layer_pair<typename P::layer_t>
220replacement_layers(P &pop, typename P::layer_iter l)
221{
222 Expects(pop.layers());
223 Expects(iterator_of(l, pop.range_of_layers()));
224
225 if (l == std::prev(pop.range_of_layers().end()))
226 return *l;
227
228 return {*l, pop.back()};
229}
230
255template<LayeredPopulation P>
257selection_layers(const P &pop, typename P::layer_const_iter l)
258{
259 Expects(pop.layers());
260 Expects(iterator_of(l, pop.range_of_layers()));
261
262 if (l == pop.range_of_layers().begin())
263 return *l;
264
265 return {*l, *std::prev(l)};
266}
267
268} // namespace alps
269} // namespace ultra
270
271#endif // include guard
alps_layer_pair< typename P::layer_t > replacement_layers(P &pop, typename P::layer_iter l)
Definition alps.h:220
alps_layer_pair< const typename P::layer_t > selection_layers(const P &pop, typename P::layer_const_iter l)
Definition alps.h:257
Definition alps.h:48
constexpr auto begin() const noexcept
Definition alps.h:80
alps_layer_pair(Layer &p, Layer &s) noexcept
Definition alps.h:64
constexpr Layer & secondary() const noexcept
Definition alps.h:100
constexpr std::size_t size() const noexcept
Definition alps.h:68
constexpr Layer & primary() const noexcept
Definition alps.h:92
alps_layer_pair(Layer &l) noexcept
Definition alps.h:55
constexpr bool has_secondary() const noexcept
Definition alps.h:74
Layer & random(double p_pri) const
Definition alps.h:117
constexpr auto end() const noexcept
Definition alps.h:86
bool boolean(double p)
Definition random.h:154
Definition alps.h:151
unsigned max_age(std::size_t) const
Definition alps.cc:30
double p_main_layer
Definition alps.h:178
std::size_t max_layers
Maximum number of layers an ALPS layered population can grow to.
Definition alps.h:172
unsigned age_gap
Definition alps.h:169