13#if !defined(ULTRA_SYMBOL_SET_H)
14#define ULTRA_SYMBOL_SET_H
21#include <initializer_list>
34 using weight_t = unsigned;
37 static constexpr weight_t base_weight = 100;
39 explicit w_symbol(
const symbol *, weight_t = base_weight);
41 [[nodiscard]]
bool operator==(
const w_symbol &)
const noexcept =
default;
54 explicit sum_container(std::string);
56 void insert(
const w_symbol &);
58 [[nodiscard]] std::size_t size() const noexcept;
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(); }
64 [[nodiscard]] w_symbol::weight_t sum() const noexcept;
66 template<class F>
void scale_weights(
double, F);
67 [[nodiscard]] const symbol *roulette() const;
69 [[nodiscard]]
bool is_valid() const;
72 std::vector<w_symbol> elems_ {};
75 w_symbol::weight_t sum_ {0};
86 explicit collection(std::string =
"");
88 [[nodiscard]]
bool is_valid()
const;
90 void insert(
const w_symbol &);
92 sum_container all {
"all"};
93 sum_container functions {
"functions"};
94 sum_container terminals {
"terminals"};
118 using weight_t = internal::w_symbol::weight_t;
119 static constexpr weight_t default_weight = internal::w_symbol::base_weight;
122 symbol_set() =
default;
127 symbol *
insert(std::unique_ptr<symbol>, weight_t = default_weight);
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 &&...);
133 template<std::derived_from<function> F, weight_t W = default_weight,
134 std::convertible_to<symbol::category_t> C = symbol::category_t>
136 std::initializer_list<C>);
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>);
149 [[nodiscard]] std::size_t variables(
151 template<Symbol S> [[nodiscard]] std::size_t
count_if(
167 [[nodiscard]]
const symbol *
decode(
const std::string &)
const;
175 [[nodiscard]] std::set<symbol::category_t>
178 [[nodiscard]]
bool is_valid()
const;
180 friend std::ostream &operator<<(std::ostream &,
const symbol_set &);
184 std::vector<std::unique_ptr<symbol>> symbols_ {};
190 std::vector<internal::collection> views_ {};
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); });
211 return std::ranges::count_if(
212 views_[c].
all, [](
const auto &ws) {
return is<S>(ws.sym); });
231template<
Symbol S, symbol_set::weight_t W,
class ...Args>
232requires std::constructible_from<S, Args...>
235 return static_cast<std::add_pointer_t<S>
>(
236 insert(std::make_unique<S>(std::forward<Args>(args)...), W));
240template<std::derived_from<function> F, symbol_set::weight_t W,
241 std::convertible_to<symbol::category_t> C>
243 std::initializer_list<C> pt)
249template<std::derived_from<function> F, symbol_set::weight_t W,
250 std::convertible_to<symbol::category_t> C>
256std::ostream &operator<<(std::ostream &,
const symbol_set &);
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
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
@ 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