ECF 1.7
Terminal.h
1#ifndef Terminal_h
2#define Terminal_h
3#include "Primitive.h"
4
5namespace Tree {
6namespace Primitives {
7
14template <class T>
15class TerminalT : public Primitive
16{
17public:
18 TerminalT();
19 T value_;
20 void execute(void* result, Tree& tree);
21 void setValue(void* value);
22 void getValue(void* value);
23 ~TerminalT();
24};
25// default type is double:
26typedef TerminalT<double> Terminal;
27
28
29template <class T>
30TerminalT<T>::TerminalT()
31{
32 nArguments_ = 0;
33}
34
35
36template <class T>
37TerminalT<T>::~TerminalT(void)
38{ }
39
40
45template <class T>
46void TerminalT<T>::setValue(void* value)
47{
48 value_ = *(T*)value;
49}
50
51
56template <class T>
57void TerminalT<T>::getValue(void* value)
58{
59 *(T*)value = value_;
60}
61
62
63template <class T>
64void TerminalT<T>::execute(void* result, Tree& tree)
65{
66 T& res = *(T*)result;
67 res = value_;
68}
69
70
71
84template <class T>
85class ERC : public Primitive
86{
87public:
88 std::string baseName_;
89 T value_;
90
91 ERC()
92 {
93 nArguments_ = 0;
94 name_ = "ERC"; // name should be set in Tree::initialize because of type prefix
95 }
96
97
103 bool initialize(StateP state)
104 {
105 state_ = state;
106 values_ = new std::vector<T>;
107
108 name_.erase(name_.end() - 1);
109 std::stringstream ss;
110 ss << name_;
111 ss >> name_;
112 char bracket;
113 ss >> bracket;
114
115 T datum;
116 while(ss >> datum) {
117 values_->push_back(datum);
118 }
119
120 return true;
121 }
122
123
124 void execute(void *result, Tree& tree)
125 {
126 T& res = *(T*)result;
127 res = value_;
128 }
129
130
135 void setValue(void* value)
136 {
137 value_ = *(T*)value;
138 }
139
140
145 void getValue(void* value)
146 {
147 *(T*)value = value_;
148 }
149
150
151 PrimitiveP copyWithNode(PrimitiveP primitive)
152 {
153 return (PrimitiveP) new ERC<T>(*this);
154 }
155
160 PrimitiveP assignToNode(PrimitiveP primitive)
161 {
162 // shallow copy - the values_ vector is not copied
163 ERC<T> *erc = new ERC<T>(*this);
164
165 uint iData = state_->getRandomizer()->getRandomInteger((int)values_->size());
166 erc->value_ = values_->at(iData);
167 std::ostringstream ss;
168 ss << values_->at(iData);
169 erc->setName(name_ + ss.str());
170 return (PrimitiveP) erc;
171 }
172
173protected:
176 std::vector<T> *values_;
177};
178
179
180
187class ERCD : public ERC<double>
188{
189public:
190 bool initialize(StateP state)
191 {
192 useInterval_ = true;
193 state_ = state;
194 values_ = new std::vector<double>;
195
196 if(name_[name_.size() - 1] == '}')
197 useInterval_ = false;
198 name_.erase(name_.end() - 1);
199 std::stringstream ss;
200 ss << name_;
201 ss >> name_;
202 char bracket;
203 ss >> bracket;
204
205 double datum;
206 while(ss >> datum) {
207 values_->push_back(datum);
208 }
209
210 return true;
211 }
212
213 PrimitiveP assignToNode(PrimitiveP primitive)
214 {
215 // shallow copy - the values_ are not copied
216 ERCD *erc = new ERCD(*this);
217
218 // generate a random value in the interval
219 if(useInterval_) {
220 double r = state_->getRandomizer()->getRandomDouble();
221 erc->value_ = (*values_)[0] + r * ((*values_)[1] - (*values_)[0]);
222 }
223 // or choose a random predefined value
224 else {
225 uint iData = state_->getRandomizer()->getRandomInteger((int)values_->size());
226 erc->value_ = values_->at(iData);
227 }
228 std::ostringstream ss;
229 ss << erc->value_;
230 erc->setName(name_ + ss.str());
231 return (PrimitiveP) erc;
232 }
233
234protected:
235 bool useInterval_;
236};
237
238}
239}
240
241#endif
Ephemereal random constant (ERC) node of type double (Tree genotype).
Definition Terminal.h:188
bool initialize(StateP state)
Initialize ERC value(s).
Definition Terminal.h:190
PrimitiveP assignToNode(PrimitiveP primitive)
Create (or choose) new unique ERC value and assign to tree node.
Definition Terminal.h:213
void getValue(void *value)
Get terminal's value.
Definition Terminal.h:145
std::vector< T > * values_
Definition Terminal.h:176
PrimitiveP assignToNode(PrimitiveP primitive)
Create (or choose) new unique ERC value and assign to tree node.
Definition Terminal.h:160
bool initialize(StateP state)
Initialize ERC value(s).
Definition Terminal.h:103
void setValue(void *value)
Set terminal's value.
Definition Terminal.h:135
PrimitiveP copyWithNode(PrimitiveP primitive)
Copy primitive (when copying a node, e.g. in crossover). The default behaviour just returns the same ...
Definition Terminal.h:151
void execute(void *result, Tree &tree)
Execute the primitive.
Definition Terminal.h:124
void setName(std::string name)
Set primitive's name.
Terminal tree node class (Tree genotype).
Definition Terminal.h:16
void setValue(void *value)
Set terminal's value.
Definition Terminal.h:46
void execute(void *result, Tree &tree)
Execute the primitive.
Definition Terminal.h:64
void getValue(void *value)
Get terminal's value.
Definition Terminal.h:57
Tree class - implements genotype as a tree.
Definition Tree_c.h:29