Ultra 0.9.4
Loading...
Searching...
No Matches
vector_individual.tcc
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_VECTOR_INDIVIDUAL_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(ULTRA_VECTOR_INDIVIDUAL_TCC)
18#define ULTRA_VECTOR_INDIVIDUAL_TCC
19
28template<class T>
29vector_individual<T>::vector_individual(std::size_t n) : genome_(n)
30{
31 Expects(parameters());
32}
33
42template<class T>
43vector_individual<T>::vector_individual(const ultra::problem &p)
44 : vector_individual(p.sset.categories())
45{
46 static_assert(std::same_as<T, D_INT> || std::same_as<T, D_DOUBLE>);
47
48 std::ranges::generate(
49 genome_,
50 [&, n = 0]() mutable
51 {
52 return std::get<T>(p.sset.roulette_terminal(n++));
53 });
54
55 signature_ = hash();
56 Ensures(is_valid());
57}
58
62template<class T>
63typename vector_individual<T>::const_iterator
64vector_individual<T>::begin() const noexcept
65{
66 return genome_.begin();
67}
68
72template<class T>
73typename vector_individual<T>::const_iterator
74vector_individual<T>::end() const noexcept
75{
76 return genome_.end();
77}
78
84template<class T>
85const typename vector_individual<T>::value_type &
86vector_individual<T>::operator[](std::size_t i) const
87{
88 Expects(i < parameters());
89 return genome_[i];
90}
91
95template<class T>
96vector_individual<T>::operator const std::vector<T> &() const noexcept
97{
98 return genome_;
99}
100
109template<class T>
110template<class F>
111requires std::invocable<F &, T &>
112 && std::same_as<std::invoke_result_t<F &, T &>, void>
113void vector_individual<T>::apply_each(std::size_t first, std::size_t last, F &&f)
114{
115 Expects(first <= last);
116 Expects(last <= parameters());
117
118 for (auto i(first); i < last; ++i)
119 std::invoke(std::forward<F>(f), genome_[i]);
120
121 signature_ = hash();
122 Ensures(is_valid());
123}
124
131template<class T>
132template<class F>
133requires std::invocable<F &, T &>
134 && std::same_as<std::invoke_result_t<F &, T &>, void>
136{
137 apply_each(0, size(), std::forward<F>(f));
138}
139
146template<class T>
148vector_individual<T>::operator=(const std::vector<T> &v)
149{
150 Expects(empty() || v.size() == size());
151
152 genome_ = v;
153 signature_ = hash();
154
155 Ensures(is_valid());
156 return *this;
157}
158
162template<class T>
163bool vector_individual<T>::empty() const noexcept
164{
165 return genome_.empty();
166}
167
171template<class T>
172std::size_t vector_individual<T>::parameters() const noexcept
173{
174 return size();
175}
176
180template<class T>
181std::size_t vector_individual<T>::size() const noexcept
182{
183 return genome_.size();
184}
185
191template<class T>
192hash_t vector_individual<T>::hash() const
193{
194 const auto len(genome_.size() * sizeof(genome_[0])); // length in bytes
195 return len ? ultra::hash::hash128(genome_.data(), len) : hash_t();
196}
197
206template<class T>
207bool operator==(const vector_individual<T> &lhs,
208 const vector_individual<T> &rhs) noexcept
209{
210 return std::ranges::equal(lhs, rhs);
211}
212
220template<class T>
221std::size_t active_slots(const vector_individual<T> &ind) noexcept
222{
223 return ind.parameters();
224}
225
229template<class T>
230bool vector_individual<T>::is_valid() const
231{
232 const auto expected(hash());
233 if (signature() == expected)
234 return true;
235
236 ultraERROR("Wrong signature: {} should be {}",
237 internal::streamed(signature()),
238 internal::streamed(expected));
239 return false;
240}
241
249template<class T>
250bool vector_individual<T>::load_impl(std::istream &in, const symbol_set &ss)
251{
252 std::size_t sz;
253 if (!(in >> sz))
254 return false;
255
256 genome_t v(sz);
257 for (auto &g : v)
258 {
259 if constexpr (std::same_as<T, D_DOUBLE>)
260 {
261 if (!load_float_from_stream(in, &g))
262 return false;
263 }
264 else if constexpr (std::same_as<T, value_t>)
265 {
266 if (!ultra::load(in, ss, g))
267 return false;
268 }
269 else if (!(in >> g))
270 return false;
271 }
272
273 genome_ = std::move(v);
274 return true;
275}
276
283template<class T>
284bool vector_individual<T>::save_impl(std::ostream &out) const
285{
286 out << parameters() << '\n';
287 for (const auto &g : genome_)
288 {
289 if constexpr (std::same_as<T, D_DOUBLE>)
290 save_float_to_stream(out, g);
291 else if constexpr (std::same_as<T, value_t>)
292 ultra::save(out, g);
293 else
294 out << g;
295
296 out << '\n';
297 }
298
299 return out.good();
300}
301
302#endif // include guard
static hash_t hash128(const void *const, std::size_t, std::uint32_t=1973) noexcept
Definition hash_t.h:273
value_t roulette_terminal(symbol::category_t=symbol::default_category) const
Definition symbol_set.cc:388
Definition vector_individual.h:38
hash_t hash() const override
Definition vector_individual.h:193
std::size_t parameters() const noexcept
Definition vector_individual.h:173
void apply_each(std::size_t first, std::size_t last, F &&f)
Definition vector_individual.tcc:113
bool is_valid() const
Definition vector_individual.h:231
void apply_each(std::size_t, std::size_t, F &&)
Definition vector_individual.h:114
vector_individual & operator=(const std::vector< T > &)
Definition vector_individual.h:149
std::size_t size() const noexcept
Definition vector_individual.h:182
bool save(std::ostream &out, F f)
Definition fitness.h:151
Definition problem.h:26