13 "lower bound for each variable (mandatory)");
15 "upper bound for each variable (mandatory)");
17 "number of real valued variables (mandatory)");
20 "set of functional tree elements (mandatory)");
22 "set of terminal tree elements (mandatory)");
26bool APGenotype::addFunction(PrimitiveP func)
28 userFunctions_.push_back(func);
33bool APGenotype::addTerminal(PrimitiveP term)
35 userTerminals_.push_back(term);
45 ECF_LOG_ERROR(state,
"Error: required parameters not defined (lbound, ubound, dimension)!");
58 ECF_LOG_ERROR(state,
"Error: 'lbound' must be smaller than 'ubound'!");
66 ECF_LOG_ERROR(state,
"Error: 'dimension' must be > 0 !");
71 this->primitiveSet = (PrimitiveSetAPP)(
new PrimitiveSetAP);
72 this->primitiveSet->initialize(state);
75 for (
int i = 0; i < (int)userFunctions_.size(); i++) {
76 primitiveSet->mAllPrimitives_[userFunctions_[i]->getName()] = userFunctions_[i];
77 primitiveSet->mAllPrimitives_[userFunctions_[i]->getName()]->initialize(state);
81 std::stringstream names;
83 names << *((std::string*) sptr.get());
84 while (names >> name) {
85 if (!primitiveSet->addFunction(name)) {
86 ECF_LOG_ERROR(state,
"Error: unknown function in function set (\'" + name +
"\')!");
92 if (primitiveSet->getFunctionSetSize() == 0) {
93 ECF_LOG_ERROR(state,
"AP genotype: empty function set!");
98 Primitives::terminal_type currentType = Primitives::Double;
102 std::stringstream tNames;
104 tNames << *((std::string*) sptr.get());
106 while (tNames >> name)
109 typeIter = primitiveSet->mTypeNames_.find(name);
110 if (typeIter != primitiveSet->mTypeNames_.end()) {
111 currentType = typeIter->second;
117 for (; iTerminal < userTerminals_.size(); iTerminal++)
118 if (userTerminals_[iTerminal]->
getName() == name)
120 if (iTerminal < userTerminals_.size()) {
121 userTerminals_[iTerminal]->initialize(state);
122 primitiveSet->addTerminal(userTerminals_[iTerminal]);
129 if (name[0] ==
'[' || name[0] ==
'{') {
131 std::string ercValues =
"";
135 switch (currentType) {
136 case Primitives::Double:
137 erc = (PrimitiveP)(
new Primitives::ERCD);
138 ercValues = DBL_PREFIX;
140 case Primitives::Int:
141 erc = (PrimitiveP)(
new Primitives::ERC<int>);
142 ercValues = INT_PREFIX;
144 case Primitives::Bool:
145 erc = (PrimitiveP)(
new Primitives::ERC<bool>);
146 ercValues = BOOL_PREFIX;
148 case Primitives::Char:
149 erc = (PrimitiveP)(
new Primitives::ERC<char>);
150 ercValues = CHR_PREFIX;
152 case Primitives::String:
153 erc = (PrimitiveP)(
new Primitives::ERC<std::string>);
154 ercValues = STR_PREFIX;
158 while (name[name.size() - 1] !=
']' && name[name.size() - 1] !=
'}') {
159 ercValues +=
" " + name;
162 ercValues +=
" " + name;
165 erc->setName(ercValues);
166 erc->initialize(state);
167 primitiveSet->addTerminal(erc);
177 case Primitives::Double:
178 terminal = (PrimitiveP)(
new Primitives::Terminal);
break;
179 case Primitives::Int:
180 terminal = (PrimitiveP)(
new Primitives::TerminalT<int>);
break;
181 case Primitives::Bool:
182 terminal = (PrimitiveP)(
new Primitives::TerminalT<bool>);
break;
183 case Primitives::Char:
184 terminal = (PrimitiveP)(
new Primitives::TerminalT<char>);
break;
185 case Primitives::String:
186 terminal = (PrimitiveP)(
new Primitives::TerminalT<std::string>);
break;
191 std::istringstream ss(name);
194 case Primitives::Double:
197 if (ss.fail() ==
false)
198 terminal->setValue(&dblValue);
200 case Primitives::Int:
203 if (ss.fail() ==
false)
204 terminal->setValue(&intValue);
206 case Primitives::Bool:
211 else if (name ==
"false")
213 if (ss.fail() ==
false || name ==
"true" || name ==
"false") {
218 terminal->setValue(&boolValue);
221 case Primitives::Char:
224 if (ss.fail() ==
false)
225 terminal->setValue(&charValue);
227 case Primitives::String:
228 std::string stringValue;
230 if (ss.fail() ==
false)
231 terminal->setValue(&stringValue);
234 terminal->setName(name);
235 primitiveSet->addTerminal(terminal);
239 if (primitiveSet->getTerminalSetSize() == 0) {
240 ECF_LOG_ERROR(state,
"APGenotype: Empty terminal set!");
255std::vector<uint> APGenotype::getDiscreteIndices()
257 std::vector<uint> discrete;
260 double dindex, diff, delta;
261 int imax = primitiveSet->getPrimitivesSize() - 1, dis_index;
263 delta = 1.0 / (double)primitiveSet->getPrimitivesSize();
270 while (dindex > 0 && dis_index != imax) {
275 discrete[i] = dis_index;
282void APGenotype::buildTree(std::vector<uint> indices, uint current, uint depth)
284 Node*
node =
new Node();
285 node->depth_ = depth;
287 uint index = current - 1;
289 uint numArgs = primitiveSet->getPrimitiveByIndex(index)->getNumberOfArguments();
291 if (numArgs < toEnd) {
292 node->setPrimitive(primitiveSet->getPrimitiveByIndex(index));
293 nodes.push_back(
static_cast<NodeP
> (
node));
294 }
else if (toEnd >= 2 && toEnd <= numArgs && primitiveSet->getGFSTwoSetSize() != 0) {
295 node->setPrimitive(primitiveSet->getGFSTwoByIndex(index));
296 nodes.push_back(
static_cast<NodeP
> (
node));
297 }
else if (toEnd == 1 && primitiveSet->getGFSOneSetSize() != 0) {
298 node->setPrimitive(primitiveSet->getGFSOneByIndex(index));
299 nodes.push_back(
static_cast<NodeP
> (
node));
301 node->setPrimitive(primitiveSet->getTerminalByIndex(index));
302 nodes.push_back(
static_cast<NodeP
> (
node));
305 numArgs =
node->primitive_->getNumberOfArguments();
310 std::vector<uint> passableIndices(indices.begin(), indices.begin() + numArgs);
311 indices.erase(indices.begin(), indices.begin() + numArgs - 1);
313 for (uint i = 0; i < numArgs; i++) {
314 buildTree(indices, passableIndices[i], depth + 1);
320Tree* APGenotype::convertToPhenotype()
322 std::vector<uint> indices = getDiscreteIndices();
325 uint first = indices[0];
328 indices.erase(indices.begin());
330 buildTree(indices, first, 0);
332 Tree* tree =
new Tree();
333 for (uint i = 0; i < nodes.size(); i++) {
334 tree->addNode(nodes[i]);
337 std::stringstream sValue;
338 for (uint i = 0; i < tree->size(); i++) {
339 sValue << (*tree)[i]->primitive_->getName() <<
" ";
348void APGenotype::setTerminalValue(Tree* tree, std::string name,
void* value)
350 PrimitiveP term = primitiveSet->getTerminalByName(name);
351 if (term == PrimitiveP()) {
352 ECF_LOG_ERROR(state_,
"AP genotype: invalid terminal name referenced in setTerminalValue()!");
356 term->setValue(value);
362 xFloatingPoint = XMLNode::createXMLTopNode(
"APGenotype");
363 std::stringstream sValue;
365 xFloatingPoint.addAttribute(
"size", sValue.str().c_str());
370 xFloatingPoint.addText(sValue.str().c_str());
376 XMLCSTR values = xFloatingPoint.getText();
377 std::stringstream sValues;
voidP getParameterValue(StateP state, std::string name)
Read single parameter value from Registry.
std::string getName()
Return genotype's name (each genotype is uniquely identified with its name).
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Register a single parameter.
bool isParameterDefined(StateP state, std::string name)
Check if parameter is defined in the configuration.
uint nDimension_
dimensionality
double minValue_
lower bound
double maxValue_
upper bound
std::vector< double > realValue
vector of floating point values
void write(XMLNode &xFloatingPoint)
Write genotype data to XMLNode.
void read(XMLNode &xFloatingPoint)
Read genotype data from XMLNode.
bool initialize(StateP state)
Initialize a genotype object (read parameters, perform sanity check, build data)
void registerParameters(StateP)
Register genotype's parameters (called before Genotype::initialize)