Vita
symbol_set.h
Go to the documentation of this file.
1
13#if !defined(VITA_SYMBOL_SET_H)
14#define VITA_SYMBOL_SET_H
15
16#include <string>
17
18#include "kernel/gp/function.h"
19#include "kernel/gp/terminal.h"
20#include "kernel/range.h"
21
22namespace vita
23{
37{
38public:
39 using weight_t = unsigned;
40
41 symbol_set();
42
43 void clear();
44
45 symbol *insert(std::unique_ptr<symbol>, double = 1.0);
46 template<class S, class ...Args> symbol *insert(Args &&...);
47
48 const symbol &roulette(category_t) const;
49 const symbol &roulette_free(category_t) const;
52
53 symbol *decode(opcode_t) const;
54 symbol *decode(const std::string &) const;
55
56 category_t categories() const;
57 std::size_t terminals(category_t) const;
58
59 weight_t weight(const symbol &) const;
60
61 bool enough_terminals() const;
62 bool is_valid() const;
63
64 friend std::ostream &operator<<(std::ostream &, const symbol_set &);
65
66private:
67 // This is the real, raw repository of symbols (it owns/stores the symbols).
68 std::vector<std::unique_ptr<symbol>> symbols_;
69
70 struct w_symbol
71 {
72 w_symbol(symbol *s, weight_t w) : sym(s), weight(w) { Expects(s); }
73
74 bool operator==(w_symbol rhs) const
75 {return sym == rhs.sym && weight == rhs.weight; }
76
77 symbol *sym;
78
81 weight_t weight;
82
84 static constexpr weight_t base_weight = 100;
85 };
86
87 // A collection is a structured-view on `symbols_` or on a subset of
88 // `symbols_` (e.g. only on symbols of a specific category).
89 class collection
90 {
91 public:
92 explicit collection(std::string = "");
93
94 bool is_valid() const;
95
97 {
98 public:
99 using sum_container_t = std::vector<w_symbol>;
100 using iterator = sum_container_t::iterator;
101 using const_iterator = sum_container_t::const_iterator;
102
103 explicit sum_container(std::string n)
104 : elems_(), sum_(0), name_(std::move(n))
105 {
106 Expects(!name_.empty());
107 }
108
109 void insert(const w_symbol &);
110
111 std::size_t size() const { return elems_.size(); }
112 const w_symbol &operator[](std::size_t i) const { return elems_[i]; }
113
114 iterator begin() { return elems_.begin(); }
115 const_iterator begin() const { return elems_.begin(); }
116 iterator end() { return elems_.end(); }
117 const_iterator end() const { return elems_.end(); }
118
119 weight_t sum() const { return sum_; }
120
121 template<class F> void scale_weights(double, F);
122 const symbol &roulette() const;
123
124 bool is_valid() const;
125
126 private:
127 sum_container_t elems_;
128
129 // Sum of the weights of the symbols in the container.
130 weight_t sum_;
131
132 std::string name_;
133 };
134
135 sum_container all;
136 sum_container functions;
138
139 private:
140 std::string name_;
141 };
142
143 // The last element of the vector contains the category-agnostic view of
144 // symbols:
145 // - `views_.back().all.size()` is equal to the total number of symbols
146 // - `views_[0].all.size()` is the number of symbols in category `0`
147 std::vector<collection> views_;
148};
149
167template<class S, class ...Args> symbol *symbol_set::insert(Args &&... args)
168{
169 return insert(std::make_unique<S>(std::forward<Args>(args)...));
170}
171
172std::ostream &operator<<(std::ostream &, const symbol_set &);
173
174} // namespace vita
175
176#endif // include guard
A symbol with arity() > 0.
Definition: function.h:35
void insert(const w_symbol &)
Inserts a weighted symbol in the container.
Definition: symbol_set.cc:406
const symbol & roulette() const
Extracts a random symbol from the collection.
Definition: symbol_set.cc:430
A container for the symbols used by the GP engine.
Definition: symbol_set.h:37
symbol * decode(opcode_t) const
Definition: symbol_set.cc:179
symbol_set()
Sets up the object.
Definition: symbol_set.cc:27
void clear()
Clears the current symbol set.
Definition: symbol_set.cc:35
const function & roulette_function(category_t) const
Definition: symbol_set.cc:101
bool is_valid() const
Definition: symbol_set.cc:307
const symbol & roulette(category_t) const
Extracts a random symbol from the symbol set without bias between terminals and functions .
Definition: symbol_set.cc:143
bool enough_terminals() const
We want at least one terminal for every used category.
Definition: symbol_set.cc:235
weight_t weight(const symbol &) const
Definition: symbol_set.cc:260
symbol * insert(std::unique_ptr< symbol >, double=1.0)
Adds a new symbol to the set.
Definition: symbol_set.cc:55
friend std::ostream & operator<<(std::ostream &, const symbol_set &)
Prints the symbol set to an output stream.
Definition: symbol_set.cc:278
const symbol & roulette_free(category_t) const
Extracts a random symbol from the symbol set.
Definition: symbol_set.cc:168
const terminal & roulette_terminal(category_t) const
Definition: symbol_set.cc:113
category_t categories() const
Definition: symbol_set.cc:214
std::size_t terminals(category_t) const
Definition: symbol_set.cc:223
Together functions and terminals are referred to as symbols.
Definition: symbol.h:36
A symbol with zero-arity.
Definition: terminal.h:27
The main namespace for the project.
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
unsigned opcode_t
This is the type used as key for symbol identification.
Definition: symbol.h:26