ECF 1.5
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:
27
28
29template <class T>
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";
95 // ime se treba postaviti u Tree::initialize zbog prefiksa po tipu
96 }
97
98
104 bool initialize(StateP state)
105 {
106 state_ = state;
107 values_ = new std::vector<T>;
108
109 name_.erase(name_.end() - 1);
110 std::stringstream ss;
111 ss << name_;
112 ss >> name_;
113 char bracket;
114 ss >> bracket;
115
116 T datum;
117 while(ss >> datum) {
118 values_->push_back(datum);
119 }
120
121 return true;
122 }
123
124
125 void execute(void *result, Tree& tree)
126 {
127 T& res = *(T*)result;
128 res = value_;
129 }
130
131
136 void setValue(void* value)
137 {
138 value_ = *(T*)value;
139 }
140
141
146 void getValue(void* value)
147 {
148 *(T*)value = value_;
149 }
150
151
152 PrimitiveP copyWithNode(PrimitiveP primitive)
153 {
154 return (PrimitiveP) new ERC<T>(*this);
155 }
156
161 PrimitiveP assignToNode(PrimitiveP primitive)
162 {
163 // shallow copy - the values_ vector is not copied
164 ERC<T> *erc = new ERC<T>(*this);
165
166 uint iData = state_->getRandomizer()->getRandomInteger((int)values_->size());
167 erc->value_ = values_->at(iData);
168 std::ostringstream ss;
169 ss << values_->at(iData);
170 erc->setName(name_ + ss.str());
171 return (PrimitiveP) erc;
172 }
173
174protected:
177 std::vector<T> *values_;
178};
179
180
181
188class ERCD : public ERC<double>
189{
190public:
191 bool initialize(StateP state)
192 {
193 useInterval_ = true;
194 state_ = state;
195 values_ = new std::vector<double>;
196
197 if(name_[name_.size() - 1] == '}')
198 useInterval_ = false;
199 name_.erase(name_.end() - 1);
200 std::stringstream ss;
201 ss << name_;
202 ss >> name_;
203 char bracket;
204 ss >> bracket;
205
206 double datum;
207 while(ss >> datum) {
208 values_->push_back(datum);
209 }
210
211 return true;
212 }
213
214 PrimitiveP assignToNode(PrimitiveP primitive)
215 {
216 // shallow copy - the values_ are not copied
217 ERCD *erc = new ERCD(*this);
218
219 // generate a random value in the interval
220 if(useInterval_) {
221 double r = state_->getRandomizer()->getRandomDouble();
222 erc->value_ = (*values_)[0] + r * ((*values_)[1] - (*values_)[0]);
223 }
224 // or choose a random predefined value
225 else {
226 uint iData = state_->getRandomizer()->getRandomInteger((int)values_->size());
227 erc->value_ = values_->at(iData);
228 }
229 std::ostringstream ss;
230 ss << erc->value_;
231 erc->setName(name_ + ss.str());
232 return (PrimitiveP) erc;
233 }
234
235protected:
236 bool useInterval_;
237};
238
239}
240}
241
242#endif
Ephemereal random constant (ERC) node of type double (Tree genotype).
Definition: Terminal.h:189
bool initialize(StateP state)
Initialize the primitive (default just sets the StateP pointer).
Definition: Terminal.h:191
PrimitiveP assignToNode(PrimitiveP primitive)
Assign primitive to node (when building a tree). The default behaviour just returns the same pointer ...
Definition: Terminal.h:214
Ephemereal random constant (ERC) node class (Tree genotype).
Definition: Terminal.h:86
void getValue(void *value)
Get terminal's value.
Definition: Terminal.h:146
std::vector< T > * values_
Definition: Terminal.h:177
PrimitiveP assignToNode(PrimitiveP primitive)
Create (or choose) new unique ERC value and assign to tree node.
Definition: Terminal.h:161
bool initialize(StateP state)
Initialize ERC value(s).
Definition: Terminal.h:104
void setValue(void *value)
Set terminal's value.
Definition: Terminal.h:136
PrimitiveP copyWithNode(PrimitiveP primitive)
Copy primitive (when copying a node, e.g. in crossover). The default behaviour just returns the same ...
Definition: Terminal.h:152
void execute(void *result, Tree &tree)
Execute the primitive.
Definition: Terminal.h:125
Base primitive class (Tree genotype).
Definition: Primitive.h:37
void setName(std::string name)
Set primitive's name.
Definition: Primitive.cpp:100
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