Ultra 0.9.4
Loading...
Searching...
No Matches
linear_population.tcc
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_LINEAR_POPULATION_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(ULTRA_LINEAR_POPULATION_TCC)
18#define ULTRA_LINEAR_POPULATION_TCC
19
28template<Individual I>
29linear_population<I>::linear_population(const ultra::problem &p)
30 : allowed_(std::max(p.params.population.individuals,
31 p.params.population.min_individuals)),
32 min_allowed_(p.params.population.min_individuals)
33{
34 reset(p);
35}
36
49template<Individual I>
50void linear_population<I>::reset(const ultra::problem &p)
51{
52 Expects(allowed() >= min_allowed_);
53
54 members_.clear();
55 std::generate_n(std::back_inserter(members_), allowed(),
56 [&p] { return I(p); });
57}
58
65template<Individual I>
66I &linear_population<I>::operator[](std::size_t i)
67{
68 Expects(i < size());
69 return members_[i];
70}
71
78template<Individual I>
79const I &linear_population<I>::operator[](std::size_t i) const
80{
81 Expects(i < size());
82 return members_[i];
83}
84
88template<Individual I>
89std::size_t linear_population<I>::size() const noexcept
90{
91 return members_.size();
92}
93
100template<Individual I>
101std::size_t linear_population<I>::safe_size() const
102{
103 std::lock_guard lock(mutex_);
104 return members_.size();
105}
106
110template<Individual I>
111unsigned linear_population<I>::max_age() const noexcept
112{
113 return max_age_;
114}
115
121template<Individual I>
122void linear_population<I>::max_age(unsigned m) noexcept
123{
124 max_age_ = m;
125}
126
133template<Individual I>
134std::shared_mutex &linear_population<I>::mutex() const
135{
136 return mutex_;
137}
138
145template<Individual I>
146std::size_t linear_population<I>::allowed() const noexcept
147{
148 return allowed_;
149}
150
162template<Individual I>
163void linear_population<I>::allowed(std::size_t n)
164{
165 // Don't drop under a predefined number of individuals.
166 n = std::max(n, min_allowed_);
167
168 if (size() > n)
169 {
170 const auto delta(size() - n);
171 members_.erase(members_.end() - delta, members_.end());
172
173 assert(size() == n);
174 }
175
176 allowed_ = n;
177
178 Ensures(is_valid());
179}
180
184template<Individual I>
185bool linear_population<I>::empty() const noexcept
186{
187 return members_.empty();
188}
189
195template<Individual I>
196void linear_population<I>::clear() noexcept
197{
198 members_.clear();
199}
200
209template<Individual I>
210void linear_population<I>::push_back(const I &i)
211{
212 if (size() < allowed())
213 members_.push_back(i);
214}
215
221template<Individual I>
222void linear_population<I>::pop_back()
223{
224 Expects(!empty());
225 members_.pop_back();
226}
227
231template<Individual I>
232typename linear_population<I>::const_iterator
233linear_population<I>::begin() const noexcept
234{
235 return members_.begin();
236}
237
241template<Individual I>
242typename linear_population<I>::iterator linear_population<I>::begin() noexcept
243{
244 return members_.begin();
245}
246
250template<Individual I>
251typename linear_population<I>::const_iterator
252linear_population<I>::end() const noexcept
253{
254 return members_.end();
255}
256
260template<Individual I>
261typename linear_population<I>::iterator linear_population<I>::end() noexcept
262{
263 return members_.end();
264}
265
269template<Individual I>
270void linear_population<I>::inc_age()
271{
272 std::ranges::for_each(*this, [](auto &i) { i.inc_age(); });
273}
274
284template<Individual I>
285bool linear_population<I>::load(std::istream &in, const symbol_set &ss)
286{
287 unsigned tmp_max_age;
288 if (!(in >> tmp_max_age))
289 return false;
290
291 std::size_t tmp_min_allowed;
292 if (!(in >> tmp_min_allowed))
293 return false;
294
295 std::size_t tmp_allowed;
296 if (!(in >> tmp_allowed))
297 return false;
298
299 if (tmp_allowed < tmp_min_allowed)
300 return false;
301
302 std::size_t n_elem;
303 if (!(in >> n_elem))
304 return false;
305
306 if (tmp_allowed < n_elem)
307 return false;
308
309 std::vector<I> tmp_members;
310 tmp_members.reserve(n_elem);
311 for (std::size_t i(0); i < n_elem; ++i)
312 if (I ind; !ind.load(in, ss))
313 return false;
314 else
315 tmp_members.push_back(ind);
316
317 max_age(tmp_max_age);
318 members_ = std::move(tmp_members);
319 min_allowed_ = tmp_min_allowed;
320 allowed(tmp_allowed);
321
322 Ensures(is_valid());
323 return true;
324}
325
332template<Individual I>
333bool linear_population<I>::save(std::ostream &out) const
334{
335 out << max_age() << ' ' << min_allowed_ << ' ' << allowed() << ' ' << size()
336 << '\n';
337
338 for (const auto &prg : *this)
339 if (!prg.save(out))
340 return false;
341
342 return out.good();
343}
344
351template<Individual I>
352population_uid linear_population<I>::uid() const noexcept
353{
354 return uid_;
355}
356
362template<Individual I>
363bool linear_population<I>::is_valid() const
364{
365 if (!std::ranges::all_of(*this, [](const auto &i) { return i.is_valid(); }))
366 return false;
367
368 if (allowed() < min_allowed_)
369 return false;
370
371 return size() <= allowed();
372}
373
374#endif // include guard
STL namespace.
unsigned population_uid
Definition population.h:29
Definition problem.h:26