Vita
individual.tcc
1/**
2 * \file
3 * \remark This file is part of VITA.
4 *
5 * \copyright Copyright (C) 2015-2017, 2019 EOS di Manlio Morini.
6 *
7 * \license
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
10 * You can obtain one at http://mozilla.org/MPL/2.0/
11 */
12
13#if !defined(VITA_INDIVIDUAL_H)
14# error "Don't include this file directly, include the specific .h instead"
15#endif
16
17#if !defined(VITA_INDIVIDUAL_TCC)
18#define VITA_INDIVIDUAL_TCC
19
20///
21/// A measurement of the age of an individual (mainly used for ALPS).
22///
23/// \return The individual's age
24///
25/// This is a measure of how long an individual's family of genotypic
26/// material has been in the population. Randomly generated individuals,
27/// such as those that are created when the search algorithm are started,
28/// start with an age of `0`. Each generation that an individual stays in the
29/// population (such as through elitism) its age is increased by `1`.
30/// **Individuals that are created through mutation or recombination take the
31/// age of their oldest parent**.
32///
33/// \note
34/// This differs from conventional measures of age, in which individuals
35/// created through applying some type of variation to an existing
36/// individual (e.g. mutation or recombination) start with an age of `0`.
37template<class Derived>
38inline unsigned individual<Derived>::age() const
39{
40 return age_;
41}
42
43///
44/// Increments the individual's age.
45///
46template<class Derived>
47inline void individual<Derived>::inc_age()
48{
49 ++age_;
50}
51
52///
53/// \param[in] ss active symbol set
54/// \param[in] in input stream
55/// \return `true` if the object has been loaded correctly
56///
57/// \note If the load operation isn't successful the object isn't modified.
58///
59template<class Derived>
60bool individual<Derived>::load(std::istream &in, const symbol_set &ss)
61{
62 decltype(age()) t_age;
63 if (!(in >> t_age))
64 return false;
65
66 if (!static_cast<Derived *>(this)->load_impl(in, ss))
67 return false;
68
69 age_ = t_age;
70
71 // We don't save/load signature: it can be easily calculated on the fly.
72 signature_.clear();
73
74 return true;
75}
76
77///
78/// \param[out] out output stream
79/// \return `true` if the object has been saved correctly
80///
81template<class Derived>
82bool individual<Derived>::save(std::ostream &out) const
83{
84 out << age() << '\n';
85
86 return static_cast<const Derived *>(this)->save_impl(out);
87}
88
89///
90/// Updates the age of this individual if it's smaller than `rhs_age`.
91///
92/// \param[in] rhs_age the age of an individual
93///
94template<class Derived>
95void individual<Derived>::set_older_age(unsigned rhs_age)
96{
97 if (age() < rhs_age)
98 age_ = rhs_age;
99}
100
101#endif // include guard