Vita
i_ga.h
Go to the documentation of this file.
1
13#if !defined(VITA_GA_I_GA_H)
14#define VITA_GA_I_GA_H
15
16#include "kernel/individual.h"
17
18namespace vita
19{
20
24class i_ga : public individual<i_ga>
25{
26public:
27 i_ga() = default;
28 explicit i_ga(const problem &);
29
30 // Iterators.
31 using genome_t = std::vector<int>;
32 using const_iterator = genome_t::const_iterator;
33 using iterator = genome_t::iterator;
34 using value_type = genome_t::value_type;
35
36 const_iterator begin() const;
37 const_iterator end() const;
38
39 iterator begin();
40 iterator end();
41
42 value_type operator[](std::size_t i) const
43 {
44 Expects(i < parameters());
45 return genome_[i];
46 }
47
48 value_type &operator[](std::size_t i)
49 {
50 Expects(i < parameters());
51 signature_.clear();
52 return genome_[i];
53 }
54
55 operator std::vector<value_type>() const;
56 i_ga &operator=(const std::vector<value_type> &);
57
58 // Recombination operators.
59 unsigned mutation(double, const problem &);
60
64 bool empty() const { return size() == 0; }
65
71 std::size_t size() const { return genome_.size(); }
72
78 std::size_t parameters() const { return size(); }
79
80 hash_t signature() const;
81
82 bool operator==(const i_ga &) const;
83 unsigned distance(const i_ga &) const;
84
85 // Visualization/output methods.
86 void graphviz(std::ostream &) const;
87
88 bool is_valid() const;
89
90 friend class individual<i_ga>;
91 friend i_ga crossover(const i_ga &, const i_ga &);
92
93private:
94 // *** Private support methods ***
95 hash_t hash() const;
96
97 // Serialization.
98 bool load_impl(std::istream &, const symbol_set &);
99 bool save_impl(std::ostream &) const;
100
101 // *** Private data members ***
102
103 // This is the genome: the entire collection of genes (the entirety of an
104 // organism's hereditary information).
105 genome_t genome_;
106}; // class i_ga
107
108// Recombination operators.
109i_ga crossover(const i_ga &, const i_ga &);
110
111// Visualization/output methods.
112std::ostream &in_line(const i_ga &, std::ostream & = std::cout);
113std::ostream &operator<<(std::ostream &, const i_ga &);
114
118inline i_ga::const_iterator i_ga::begin() const
119{
120 return genome_.begin();
121}
122
126inline i_ga::const_iterator i_ga::end() const
127{
128 return genome_.end();
129}
130
134inline i_ga::iterator i_ga::begin()
135{
136 return genome_.begin();
137}
138
142inline i_ga::iterator i_ga::end()
143{
144 return genome_.end();
145}
146
147} // namespace vita
148
149#endif // include guard
An GA-individual optimized for combinatorial optimization.
Definition: i_ga.h:25
std::size_t parameters() const
Definition: i_ga.h:78
hash_t signature() const
The signature (hash value) of this individual.
Definition: i_ga.cc:154
void graphviz(std::ostream &) const
Produces a dot-language representation of this individual.
Definition: i_ga.cc:51
const_iterator begin() const
Definition: i_ga.h:118
const_iterator end() const
Definition: i_ga.h:126
std::size_t size() const
Definition: i_ga.h:71
bool is_valid() const
Definition: i_ga.cc:214
bool operator==(const i_ga &) const
Definition: i_ga.cc:184
unsigned distance(const i_ga &) const
Definition: i_ga.cc:199
bool empty() const
Definition: i_ga.h:64
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::ostream & operator<<(std::ostream &o, hash_t h)
Mainly useful for debugging / testing.
Definition: cache_hash.cc:56
A 128bit unsigned integer used as individual's signature / hash table look-up key.
Definition: cache_hash.h:27
void clear()
Resets the content of the object.
Definition: cache_hash.h:31