Vita
i_mep.h
Go to the documentation of this file.
1
13#if !defined(VITA_I_MEP_H)
14#define VITA_I_MEP_H
15
16#include <cmath>
17#include <iomanip>
18#include <set>
19
20#include "kernel/gp/function.h"
21#include "kernel/gp/gene.h"
22#include "kernel/individual.h"
23#include "utility/matrix.h"
24
25namespace vita
26{
33class i_mep : public individual<i_mep>
34{
35public:
36 i_mep() : individual(), genome_(), best_(locus::npos()),
37 active_crossover_type_() {}
38
39 explicit i_mep(const problem &);
40 explicit i_mep(const std::vector<gene> &);
41
42 // ---- Recombination operators ----
43 enum crossover_t {one_point, two_points, tree, uniform, NUM_CROSSOVERS};
44
45 friend i_mep crossover(const i_mep &, const i_mep &);
46 unsigned mutation(double, const problem &);
47
48 // ---- Working with blocks / genome ----
49 std::set<locus> blocks() const;
50 i_mep destroy_block(index_t, const symbol_set &) const;
51 i_mep get_block(const locus &) const;
52
53 i_mep replace(const gene &) const;
54 i_mep replace(const locus &, const gene &) const;
55
56 i_mep cse() const;
57
58 bool operator==(const i_mep &) const;
59
60 hash_t signature() const;
61
62 const gene &operator[](locus) const;
63
64 unsigned active_symbols() const;
65 category_t categories() const;
66 bool empty() const;
67 unsigned size() const;
68
69 category_t category() const;
70 locus best() const;
71
72 bool is_valid() const;
73
74 // ---- Iterators ----
75 template<bool> class basic_iterator;
78 using value_type = gene;
79
80 const_iterator begin() const;
81 const_iterator end() const;
82
84 iterator end();
85
86 template<bool> friend class basic_iterator;
87 friend class individual<i_mep>;
88
89private:
90 // ---- Private support methods ----
91 hash_t hash() const;
92 void pack(const locus &, std::vector<std::byte> *) const;
93
94 // Serialization.
95 bool load_impl(std::istream &, const symbol_set &);
96 bool save_impl(std::ostream &) const;
97
98 // ---- Private data members ----
99
100 // This is the genome: the entire collection of genes (the entirety of an
101 // organism's hereditary information).
102 matrix<gene> genome_;
103
104 // Starting point of the active code in this individual (the best sequence
105 // of genes starts here).
106 locus best_;
107
108 // Crossover operator used to create this individual. Initially this is set
109 // to a random type.
110 crossover_t active_crossover_type_;
111}; // class i_mep
112
113[[nodiscard]] unsigned distance(const i_mep &, const i_mep &);
114[[nodiscard]] locus random_locus(const i_mep &);
115
119inline locus i_mep::best() const
120{
121 return best_;
122}
123
128inline const gene &i_mep::operator[](locus l) const
129{
130 return genome_(l);
131}
132
137{
138 return static_cast<category_t>(genome_.cols());
139}
140
149inline unsigned i_mep::size() const
150{
151 return static_cast<unsigned>(genome_.rows());
152}
153
157inline bool i_mep::empty() const
158{
159 return size() == 0;
160}
161
162bool operator!=(const i_mep &, const i_mep &);
163
164std::ostream &operator<<(std::ostream &, const i_mep &);
165
166#include "kernel/gp/mep/i_mep_iterator.tcc"
167
172{
173 return i_mep::const_iterator(*this);
174}
175
180{
181 return i_mep::const_iterator();
182}
183
188{
189 return i_mep::iterator(*this);
190}
191
196{
197 return i_mep::iterator();
198}
199
200template<> struct has_introns<i_mep> : std::true_type {};
201
202} // namespace vita
203
204#endif // include guard
A gene is a unit of heredity in a living organism.
Definition: gene.h:34
A MEP (Multi Expression Programming) single member of a population.
Definition: i_mep.h:34
bool operator==(const i_mep &) const
Definition: i_mep.cc:257
hash_t signature() const
Signature maps syntactically distinct (but logically equivalent) individuals to the same value.
Definition: i_mep.cc:387
i_mep get_block(const locus &) const
Definition: i_mep.cc:115
bool empty() const
Definition: i_mep.h:157
i_mep replace(const gene &) const
Creates a new individual obtained from this replacing the original symbol at locus best() with g.
Definition: i_mep.cc:216
const gene & operator[](locus) const
Definition: i_mep.h:128
std::set< locus > blocks() const
Calculates a set of loci referring to blocks contained in the individual.
Definition: i_mep.cc:179
bool is_valid() const
Definition: i_mep.cc:398
locus best() const
Definition: i_mep.h:119
i_mep destroy_block(index_t, const symbol_set &) const
Definition: i_mep.cc:227
category_t categories() const
Definition: i_mep.h:136
category_t category() const
Definition: i_mep.cc:245
const_iterator begin() const
Definition: i_mep.h:171
unsigned size() const
Definition: i_mep.h:149
unsigned mutation(double, const problem &)
A new individual is created mutating this.
Definition: i_mep.cc:136
const_iterator end() const
Definition: i_mep.h:179
unsigned active_symbols() const
Number of active symbols.
Definition: i_mep.cc:103
i_mep cse() const
A sort of "common subexpression elimination" optimization.
Definition: i_mep.cc:605
A single member of a population.
Definition: individual.h:41
MurmurHash3 (https://github.com/aappleby/smhasher) by Austin Appleby.
Definition: cache_hash.h:101
Aggregates the problem-related data needed by an evolutionary program.
Definition: problem.h:24
A container for the symbols used by the GP engine.
Definition: symbol_set.h:37
The main namespace for the project.
std::size_t index_t
Index in the genome.
Definition: locus.h:25
basic_gene< 4 > gene
A basic_gene with the standard size.
Definition: gene.h:73
std::size_t category_t
A category provide operations which supplement or supersede those of the domain but which are restric...
Definition: common.h:44
std::ostream & operator<<(std::ostream &o, hash_t h)
Mainly useful for debugging / testing.
Definition: cache_hash.cc:56
The SFINAE way of recognizing if an individual has introns.
Definition: individual.h:70
A 128bit unsigned integer used as individual's signature / hash table look-up key.
Definition: cache_hash.h:27