Ultra 0.9.4
Loading...
Searching...
No Matches
symbol_set.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_SYMBOL_SET_H)
14#define ULTRA_SYMBOL_SET_H
15
16#include "kernel/terminal.h"
17#include "kernel/gp/function.h"
18
19#include <algorithm>
20#include <concepts>
21#include <initializer_list>
22#include <memory>
23#include <set>
24#include <string>
25
26namespace ultra
27{
28
29namespace internal
30{
31
32struct w_symbol
33{
34 using weight_t = unsigned;
35
37 static constexpr weight_t base_weight = 100;
38
39 explicit w_symbol(const symbol *, weight_t = base_weight);
40
41 [[nodiscard]] bool operator==(const w_symbol &) const noexcept = default;
42
43 const symbol *sym;
44
47 weight_t weight;
48};
49
50
51class sum_container
52{
53public:
54 explicit sum_container(std::string);
55
56 void insert(const w_symbol &);
57
58 [[nodiscard]] std::size_t size() const noexcept;
59
60 [[nodiscard]] auto begin() noexcept { return elems_.begin(); }
61 [[nodiscard]] auto begin() const noexcept { return elems_.begin(); }
62 [[nodiscard]] auto end() const noexcept { return elems_.end(); }
63
64 [[nodiscard]] w_symbol::weight_t sum() const noexcept;
65
66 template<class F> void scale_weights(double, F);
67 [[nodiscard]] const symbol *roulette() const;
68
69 [[nodiscard]] bool is_valid() const;
70
71private:
72 std::vector<w_symbol> elems_ {};
73
74 // Sum of the weights of the symbols in the container.
75 w_symbol::weight_t sum_ {0};
76
77 std::string name_;
78};
79
80
81// A collection is a structured-view on `symbols_` or on a subset of
82// `symbols_` (e.g. only on symbols of a specific category).
83class collection
84{
85public:
86 explicit collection(std::string = "");
87
88 [[nodiscard]] bool is_valid() const;
89
90 void insert(const w_symbol &);
91
92 sum_container all {"all"};
93 sum_container functions {"functions"};
94 sum_container terminals {"terminals"};
95
96private:
97 std::string name_;
98};
99
100} // namespace internal
101
102
114class symbol_set
115{
116public:
117 // ---- Member types ----
118 using weight_t = internal::w_symbol::weight_t;
119 static constexpr weight_t default_weight = internal::w_symbol::base_weight;
120
121 // ---- Constructor ----
122 symbol_set() = default;
123
124 // ---- Modifiers ----
125 void clear();
126
127 symbol *insert(std::unique_ptr<symbol>, weight_t = default_weight);
128
129 template<Symbol S, weight_t = default_weight, class ...Args>
130 requires std::constructible_from<S, Args...>
131 std::add_pointer_t<S> insert(Args &&...);
132
133 template<std::derived_from<function> F, weight_t W = default_weight,
134 std::convertible_to<symbol::category_t> C = symbol::category_t>
135 std::add_pointer_t<F> insert(symbol::category_t,
136 std::initializer_list<C>);
137
138 template<std::derived_from<function> F, weight_t W = default_weight,
139 std::convertible_to<symbol::category_t> C = symbol::category_t>
140 std::add_pointer_t<F> insert(std::initializer_list<C>);
141
142 // ---- Capacity ----
143 [[nodiscard]] bool enough_terminals() const noexcept;
144 [[nodiscard]] symbol::category_t categories() const noexcept;
145 [[nodiscard]] std::size_t functions(
147 [[nodiscard]] std::size_t terminals(
149 [[nodiscard]] std::size_t variables(
151 template<Symbol S> [[nodiscard]] std::size_t count_if(
153
154 // ---- Lookup / symbol access ----
155 [[nodiscard]] value_t random_argument(
156 std::size_t, symbol::category_t, double) const;
157 [[nodiscard]] const symbol *roulette(
159 [[nodiscard]] const function *roulette_function(
161 [[nodiscard]] value_t roulette_terminal(
163 [[nodiscard]] const symbol *roulette_free(
165
166 [[nodiscard]] const symbol *decode(symbol::opcode_t) const;
167 [[nodiscard]] const symbol *decode(const std::string &) const;
168
169 [[nodiscard]] const terminal *front_terminal(
171
172 // ---- Misc ----
173 [[nodiscard]] weight_t weight(const symbol &) const;
174
175 [[nodiscard]] std::set<symbol::category_t>
177
178 [[nodiscard]] bool is_valid() const;
179
180 friend std::ostream &operator<<(std::ostream &, const symbol_set &);
181
182private:
183 // This is the real, raw repository of symbols (it owns/stores the symbols).
184 std::vector<std::unique_ptr<symbol>> symbols_ {};
185
186 // The last element of the vector contains the category-agnostic view of
187 // symbols:
188 // - `views_.back().all.size()` is equal to the total number of symbols
189 // - `views_[0].all.size()` is the number of symbols in category `0`
190 std::vector<internal::collection> views_ {};
191};
192
198template<Symbol S>
199std::size_t symbol_set::count_if(symbol::category_t c) const noexcept
200{
201 if (c >= categories())
202 return 0;
203
204 if constexpr (std::derived_from<S, function>)
205 return std::ranges::count_if(
206 views_[c].functions, [](const auto &ws) { return is<S>(ws.sym); });
207 else if constexpr (std::derived_from<S, terminal>)
208 return std::ranges::count_if(
209 views_[c].terminals, [](const auto &ws) { return is<S>(ws.sym); });
210 else
211 return std::ranges::count_if(
212 views_[c].all, [](const auto &ws) { return is<S>(ws.sym); });
213}
214
231template<Symbol S, symbol_set::weight_t W, class ...Args>
232requires std::constructible_from<S, Args...>
233std::add_pointer_t<S> symbol_set::insert(Args &&... args)
234{
235 return static_cast<std::add_pointer_t<S>>(
236 insert(std::make_unique<S>(std::forward<Args>(args)...), W));
237}
238
240template<std::derived_from<function> F, symbol_set::weight_t W,
241 std::convertible_to<symbol::category_t> C>
242std::add_pointer_t<F> symbol_set::insert(symbol::category_t ret,
243 std::initializer_list<C> pt)
244{
245 return insert<F, W>(ret, function::param_data_types(pt.begin(), pt.end()));
246}
247
249template<std::derived_from<function> F, symbol_set::weight_t W,
250 std::convertible_to<symbol::category_t> C>
251std::add_pointer_t<F> symbol_set::insert(std::initializer_list<C> pt)
252{
254}
255
256std::ostream &operator<<(std::ostream &, const symbol_set &);
257
258} // namespace ultra
259
260#endif // include guard
Definition function.h:44
std::vector< category_t > param_data_types
Definition function.h:49
Definition symbol_set.h:115
const terminal * front_terminal(symbol::category_t=symbol::default_category) const
Definition symbol_set.cc:332
value_t roulette_terminal(symbol::category_t=symbol::default_category) const
Definition symbol_set.cc:388
std::size_t terminals(symbol::category_t=symbol::default_category) const noexcept
Definition symbol_set.cc:279
std::size_t count_if(symbol::category_t=symbol::default_category) const noexcept
Definition symbol_set.h:199
const symbol * roulette_free(symbol::category_t=symbol::default_category) const
Definition symbol_set.cc:436
std::set< symbol::category_t > categories_missing_terminal() const
Definition symbol_set.cc:297
symbol::category_t categories() const noexcept
Definition symbol_set.cc:261
const symbol * roulette(symbol::category_t=symbol::default_category) const
Definition symbol_set.cc:359
bool is_valid() const
Definition symbol_set.cc:496
const function * roulette_function(symbol::category_t=symbol::default_category) const
Definition symbol_set.cc:374
const symbol * decode(symbol::opcode_t) const
Definition symbol_set.cc:463
bool enough_terminals() const noexcept
Definition symbol_set.cc:321
std::size_t functions(symbol::category_t=symbol::default_category) const noexcept
Definition symbol_set.cc:270
value_t random_argument(std::size_t, symbol::category_t, double) const
Definition symbol_set.cc:408
symbol * insert(std::unique_ptr< symbol >, weight_t=default_weight)
Definition symbol_set.cc:232
weight_t weight(const symbol &) const
Definition symbol_set.cc:449
void clear()
Definition symbol_set.cc:214
Definition symbol.h:42
unsigned category_t
Definition symbol.h:55
unsigned opcode_t
Definition symbol.h:69
static constexpr category_t default_category
Default category assigned when typing is not used.
Definition symbol.h:58
Definition terminal.h:41
Definition symbol.h:99
@ all
Initialises everything.
Definition gp/src/problem.h:41
std::variant< D_VOID, D_INT, D_DOUBLE, D_STRING, const D_NULLARY *, D_ADDRESS, const D_VARIABLE *, D_IVECTOR > value_t
Definition value.h:54
bool is(const symbol *s)
Definition symbol.h:153