Vita
interpreter.cc
Go to the documentation of this file.
1
14#include "kernel/gp/mep/i_mep.h"
15
16namespace vita
17{
18
26 : core_interpreter(), prg_(ind), cache_(ind->size(), ind->categories()),
27 ip_(ind->best())
28{
29 Expects(ind);
30}
31
37{
38 for (auto &e : cache_)
39 e.valid = false;
40
41 ip_ = ip;
42 return (*prg_)[ip_].sym->eval(*this);
43}
44
50value_t interpreter<i_mep>::run_nvi()
51{
52 return run_locus(prg_->best());
53}
54
58terminal_param_t interpreter<i_mep>::fetch_param() const
59{
60 const gene &g((*prg_)[ip_]);
61
62 assert(g.sym->terminal() && terminal::cast(g.sym)->parametric());
63 return g.par;
64}
65
82{
83 const gene &g((*prg_)[ip_]);
84
85 assert(g.sym->arity());
86 assert(i < g.sym->arity());
87
88 auto &elem(cache_(g.locus_of_argument(i)));
89
90 if (!elem.valid)
91 {
92 elem.value = fetch_opaque_arg(i);
93 elem.valid = true;
94 }
95#if !defined(NDEBUG)
96 else // cache not empty... checking if the cached value is right
97 {
98 assert(fetch_opaque_arg(i) == elem.value);
99 }
100#endif
101
102 Ensures(elem.valid);
103 return elem.value;
104}
105
107{
108 const gene &g((*prg_)[ip_]);
109
110 assert(g.sym->arity());
111 assert(i < g.sym->arity());
112
113 const locus backup(ip_);
114 ip_ = g.locus_of_argument(i);
115 assert(ip_.index > backup.index);
116
117 const auto ret((*prg_)[ip_].sym->eval(*this));
118 ip_ = backup;
119
120 return ret;
121}
122
129{
130 const gene &g((*prg_)[ip_]);
131
132 assert(g.sym->arity());
133 assert(i < g.sym->arity());
134
135 return g.args[i];
136}
137
143{
144 ip_ = ip;
145 return (*prg_)[ip_].sym->penalty(this);
146}
147
153double interpreter<i_mep>::penalty_nvi()
154{
155 return penalty_locus(prg_->best());
156}
157
161bool interpreter<i_mep>::is_valid_nvi() const
162{
163 return ip_.index < prg_->size();
164}
165
166} // namespace vita
A gene is a unit of heredity in a living organism.
Definition: gene.h:34
Minimum interface of an interpreter.
A MEP (Multi Expression Programming) single member of a population.
Definition: i_mep.h:34
bool terminal() const
Definition: symbol.h:153
virtual bool parametric() const
A parametric terminal needs an additional parameter to be evaluated.
Definition: terminal.h:59
static const terminal * cast(const symbol *)
This is a short cut function.
Definition: terminal.h:83
The main namespace for the project.
std::size_t index_t
Index in the genome.
Definition: locus.h:25
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