Ultra 0.9.4
Loading...
Searching...
No Matches
individual.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_INDIVIDUAL_H)
14#define ULTRA_INDIVIDUAL_H
15
16#include "kernel/hash_t.h"
17#include "kernel/symbol_set.h"
18
19#include <iosfwd>
20#include <string>
21
22namespace ultra
23{
24
25namespace out
26{
27
29enum print_format_t {list_f, // default value
30 dump_f, graphviz_f, in_line_f, tree_f,
31 language_f,
32 c_language_f = language_f + symbol::c_format,
33 cpp_language_f = language_f + symbol::cpp_format,
34 python_language_f = language_f + symbol::python_format};
35
36} // namespace out
37
54{
55public:
56 // ---- Member types ----
57 using age_t = unsigned;
58
59 // ---- Age management ----
60 [[nodiscard]] age_t age() const noexcept;
61 void inc_age(unsigned = 1) noexcept;
62
63 // ---- Serialization ----
64 [[nodiscard]] bool load(std::istream &, const symbol_set & = {});
65 [[nodiscard]] bool save(std::ostream &) const;
66
67 // ---- Misc ----
68 [[nodiscard]] hash_t signature() const noexcept;
69
70 [[nodiscard]] std::string format(out::print_format_t) const;
71
72protected:
73 ~individual() = default;
74
75 void set_if_older_age(age_t) noexcept;
76
77 // Note that syntactically distinct (but logically equivalent) individuals
78 // have the same signature. This is a very interesting property, useful
79 // for individual comparison, information retrieval, entropy calculation...
80 hash_t signature_ {};
81
82private:
83 [[nodiscard]] virtual bool load_impl(std::istream &, const symbol_set &) = 0;
84 [[nodiscard]] virtual bool save_impl(std::ostream &) const = 0;
85 [[nodiscard]] virtual hash_t hash() const = 0;
86
87 // Writes the representation selected by `format` to the output stream.
88 virtual void print_impl(std::ostream &, out::print_format_t) const = 0;
89
90 age_t age_ {0};
91}; // class individual
92
93template<class I> concept Individual =
94 std::default_initializable<I>
95 && requires(const I &ci, I &i)
96 {
97 { ci.empty() } -> std::convertible_to<bool>;
98 { ci.signature() } -> std::same_as<hash_t>;
99 { ci.age() } -> std::convertible_to<individual::age_t>;
100 { i.inc_age() } -> std::same_as<void>;
101 };
102
103} // namespace ultra
104
105#endif // include guard
Definition individual.h:54
void set_if_older_age(age_t) noexcept
Definition individual.cc:121
age_t age() const noexcept
Definition individual.cc:66
bool save(std::ostream &) const
Definition individual.cc:108
bool load(std::istream &, const symbol_set &={})
Definition individual.cc:89
std::string format(out::print_format_t) const
Definition individual.cc:140
hash_t signature() const noexcept
Definition individual.cc:44
void inc_age(unsigned=1) noexcept
Definition individual.cc:76
Definition symbol_set.h:115
Definition individual.h:93
print_format_t
Rendering format used to print an individual.
Definition individual.h:29
Definition hash_t.h:34