Vita
primitive.h
Go to the documentation of this file.
1
13#if !defined(VITA_GA_PRIMITIVE_H)
14#define VITA_GA_PRIMITIVE_H
15
16#include <string>
17
18#include "kernel/random.h"
19#include "kernel/gp/terminal.h"
20
21namespace vita::ga
22{
33using base_t = terminal_param_t;
34
35namespace detail
36{
37template<class T>
38class number : public terminal
39{
40public:
41 using value_type = T;
42
60 explicit number(const std::string &name, range_t<T> r, category_t i)
61 : terminal(name, i), range_(r)
62 {
63 Expects(r.first < r.second);
64 }
65
66 bool parametric() const final { return true; }
67
68 terminal_param_t init() const override { return random::in(range_); }
69
70 std::string display(terminal_param_t v, format) const override
71 { return std::to_string(static_cast<T>(v)); }
72
73private:
75 value_t eval(symbol_params &) const override { return {}; }
76
77 const range_t<T> range_;
78};
79
80} // namespace detail
81
93class real final : public detail::number<double>
94{
95public:
96 // \param[in] r a half open range
97 // \param[in] i an optional category
98 explicit real(range_t<double> r = {-1000.0, 1000.0},
99 category_t i = undefined_category)
100 : detail::number<double>("REAL", r, i)
101 {
102 }
103};
104
105class integer final : public detail::number<int>
106{
107public:
108 // \param[in] r a half open range
109 // \param[in] i an optional category
110 explicit integer(range_t<int> r = {-1000, 1000},
111 category_t i = undefined_category)
112 : detail::number<int>("INTEGER", r, i)
113 {
114 }
115};
116
117} // namespace vita::ga
118
119#endif // include guard
Mainly used for differential evolution.
Definition: primitive.h:94
An interface for parameter passing to functions / terminals.
A symbol with zero-arity.
Definition: terminal.h:27
std::pair< T, T > range_t
Right-open interval.
Definition: range.h:25
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
D_DOUBLE number
This is the return type of the src_interpreter::run method.
std::variant< D_VOID, D_INT, D_DOUBLE, D_STRING > value_t
A variant containing the data types used by the interpreter for internal calculations / output value ...
Definition: value.h:45
terminal_param_t base_t
We assume that errors during floating-point operations aren't terminal error.
Definition: primitive.h:33