ECF 1.5
IATree.cpp
1#include "GPSymbRegEvalOp.h"
2
3#include "IATree.h"
4#include <ecf/tree/Tree.h>
5#include "IAConstTerminal.h"
6#include "IAERCD.h"
7
8//using namespace Tree;
9namespace Tree {
10
11IATree::IATree() {
12 name_ = "IATree";
13 boundsCalculated = false;
14 startDepth_ = 0;
15}
16
18 IATree *newObject = new IATree(*this);
19 return newObject;
20}
21
23
24 // create and link PrimitiveSet to the hometree
25 hometree->primitiveSet_ = (PrimitiveSetP) (new PrimitiveSet);
26 hometree->primitiveSet_->initialize(state_);
27 this->primitiveSet_ = hometree->primitiveSet_;
28
29 // add user defined functions to primitiveSet
30 for(int i = 0; i < (int) userFunctions_.size(); i++) {
31 primitiveSet_->mAllPrimitives_[userFunctions_[i]->getName()] = userFunctions_[i];
32 primitiveSet_->mAllPrimitives_[userFunctions_[i]->getName()]->initialize(state_);
33 }
34
35 // read function set from the configuration
36 voidP sptr = getParameterValue(state_, "functionset");
37 std::stringstream names;
38 std::string name;
39 names << *((std::string*) sptr.get());
40 while(names >> name) {
41 if(!primitiveSet_->addFunction(name)) {
42 ECF_LOG_ERROR(state_, "Error: unknown function in function set (\'" + name + "\')!");
43 throw("");
44 }
45 }
46 if(primitiveSet_->getFunctionSetSize() == 0) {
47 ECF_LOG_ERROR(state_, "Tree genotype: empty function set!");
48 throw("");
49 }
50
51 // set default terminal type
52 Primitives::terminal_type currentType = Primitives::Double;
53 type_iter typeIter;
54
55 // read terminal set from the configuration
56 std::stringstream tNames;
57 sptr = getParameterValue(state_, "terminalset");
58 tNames << *((std::string*) sptr.get());
59
60 while(tNames >> name)
61 {
62 // read current terminal type (if set)
63 typeIter = primitiveSet_->mTypeNames_.find(name);
64 if(typeIter != primitiveSet_->mTypeNames_.end()) {
65 currentType = typeIter->second;
66 continue;
67 }
68
69 // see if it's a user-defined terminal
70 uint iTerminal = 0;
71 for(; iTerminal < userTerminals_.size(); iTerminal++)
72 if(userTerminals_[iTerminal]->getName() == name)
73 break;
74 if(iTerminal < userTerminals_.size()) {
75 userTerminals_[iTerminal]->initialize(state_);
76 primitiveSet_->addTerminal(userTerminals_[iTerminal]);
77 continue;
78 }
79
80
81 // read ERC definition (if set)
82 // ERC's are defined as interval [x y] or set {a b c}
83 if(name[0] == '[' || name[0] == '{') {
84
85 std::string ercValues = "";
86
87 // name this ERC (ERC's name is its value!)
88 PrimitiveP erc = (PrimitiveP) (new IAERCD);
89 ercValues = DBL_PREFIX;
90
91 while(name[name.size() - 1] != ']' && name[name.size() - 1] != '}') {
92 ercValues += " " + name;
93 tNames >> name;
94 }
95 ercValues += " " + name;
96
97 // ERC ocekuje "D_ [<value> <value> ...]" kao ime prije initialize()
98 erc->setName(ercValues);
99 erc->initialize(state_);
100 primitiveSet_->addTerminal(erc);
101
102 continue;
103 }
104
105
106 // otherwise, read terminal of current type
107 PrimitiveP terminal = (PrimitiveP) (new IAConstTerminal);
108
109 // if the 'name' can be identified as a value of the 'currentType', then it's a _constant terminal_ (of that value)
110 // otherwise, it's a regular terminal with 'name'
111 std::istringstream ss(name);
112 switch(currentType)
113 {
114 case Primitives::Double:
115 double dblValue;
116 ss >> dblValue;
117 if(ss.fail() == false)
118 terminal->setValue(&dblValue);
119 break;
120 case Primitives::Int:
121 int intValue;
122 ss >> intValue;
123 if(ss.fail() == false)
124 terminal->setValue(&intValue);
125 break;
126 case Primitives::Bool:
127 bool boolValue;
128 ss >> boolValue;
129 if(name == "true")
130 boolValue = true;
131 else if(name == "false")
132 boolValue = false;
133 if(ss.fail() == false || name == "true" || name == "false") {
134 if(boolValue)
135 name = "true";
136 else
137 name = "false";
138 terminal->setValue(&boolValue);
139 }
140 break;
141 case Primitives::Char:
142 char charValue;
143 ss >> charValue;
144 if(ss.fail() == false)
145 terminal->setValue(&charValue);
146 break;
147 case Primitives::String:
148 std::string stringValue;
149 ss >> stringValue;
150 if(ss.fail() == false)
151 terminal->setValue(&stringValue);
152 break;
153 }
154 terminal->setName(name);
155 primitiveSet_->addTerminal(terminal);
156
157 }
158
159 if(primitiveSet_->getTerminalSetSize() == 0) {
160 ECF_LOG_ERROR(state_, "Tree: Empty terminal set!");
161 throw("");
162 }
163
164 // read tree depth constraints, store in hometree
165 sptr = getParameterValue(state_, "maxdepth");
166 hometree->maxDepth_ = *((uint*) sptr.get());
167 sptr = getParameterValue(state_, "mindepth");
168 hometree->minDepth_ = *((uint*) sptr.get());
169 if(hometree->maxDepth_ < hometree->minDepth_ || hometree->maxDepth_ < 1) {
170 ECF_LOG_ERROR(state_, "Tree genotype: invalid values for max and min tree depth!");
171 throw("");
172 }
173
174 hometree->initMaxDepth_ = hometree->maxDepth_;
175 if(isParameterDefined(state_, "initmaxdepth")) {
176 sptr = getParameterValue(state_, "initmaxdepth");
177 hometree->initMaxDepth_ = *((uint*) sptr.get());
178 }
179 hometree->initMinDepth_ = hometree->minDepth_;
180 if(isParameterDefined(state_, "initmindepth")) {
181 sptr = getParameterValue(state_, "initmindepth");
182 hometree->initMinDepth_ = *((uint*) sptr.get());
183 }
184 if(hometree->initMaxDepth_ < hometree->initMinDepth_ || hometree->initMaxDepth_ < 1) {
185 ECF_LOG_ERROR(state_, "Tree genotype: invalid values for initial max and min tree depth!");
186 throw("");
187 }
188 if(hometree->initMaxDepth_ > hometree->maxDepth_) {
189 ECF_LOG_ERROR(state_, "Tree genotype: initial max depth cannot be greater than evolution max depth!");
190 throw("");
191 }
192}
193
194}
voidP getParameterValue(StateP state, std::string name)
Read single parameter value from Registry.
Definition: Genotype.cpp:10
std::string getName()
Return genotype's name (each genotype is uniquely identified with its name).
Definition: Genotype.h:86
std::string name_
genotype's name
Definition: Genotype.h:109
bool isParameterDefined(StateP state, std::string name)
Check if parameter is defined in the configuration.
Definition: Genotype.cpp:22
Definition: IAERCD.h:9
IATree * copy()
Create an identical copy of the genotype object.
Definition: IATree.cpp:17
void initializeFirst(Tree *hometree)
Performs the first Tree initialization (performed only once for each active Tree Genotype)....
Definition: IATree.cpp:22
Primitive set class: collects all Tree Primitives.
Definition: PrimitiveSet.h:18
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
uint startDepth_
start depth of the first node (0 by default)
Definition: Tree_c.h:76