12 "lower bound for each variable (mandatory)");
14 "upper bound for each variable (mandatory)");
16 "number of real valued variables (mandatory)");
19 "set of functional tree elements (mandatory)");
21 "set of terminal tree elements (mandatory)");
25bool APGenotype::addFunction(PrimitiveP func)
27 userFunctions_.push_back(func);
32bool APGenotype::addTerminal(PrimitiveP term)
34 userTerminals_.push_back(term);
48 if (!persistent->primitiveSet)
49 initializeFirst(persistent);
52 this->primitiveSet = persistent->primitiveSet;
68void APGenotype::initializeFirst(
APGenotype* persistent)
72 persistent->primitiveSet->initialize(state_);
73 this->primitiveSet = persistent->primitiveSet;
78 ECF_LOG_ERROR(state_,
"Error: required parameters not defined (lbound, ubound, dimension)!");
83 persistent->
minValue_ = *((
double*)genp.get());
86 persistent->
maxValue_ = *((
double*)genp.get());
89 ECF_LOG_ERROR(state_,
"Error: 'lbound' must be smaller than 'ubound'!");
97 ECF_LOG_ERROR(state_,
"Error: 'dimension' must be > 0 !");
102 for (
int i = 0; i < (int)userFunctions_.size(); i++) {
103 primitiveSet->mAllPrimitives_[userFunctions_[i]->getName()] = userFunctions_[i];
104 primitiveSet->mAllPrimitives_[userFunctions_[i]->getName()]->initialize(state_);
108 std::stringstream names;
110 names << *((std::string*) sptr.get());
111 while (names >> name) {
112 if (!primitiveSet->addFunction(name)) {
113 ECF_LOG_ERROR(state_,
"Error: unknown function in function set (\'" + name +
"\')!");
119 if (primitiveSet->getFunctionSetSize() == 0) {
120 ECF_LOG_ERROR(state_,
"AP genotype: empty function set!");
125 Primitives::terminal_type currentType = Primitives::Double;
129 std::stringstream tNames;
131 tNames << *((std::string*) sptr.get());
133 while (tNames >> name)
136 typeIter = primitiveSet->mTypeNames_.find(name);
137 if (typeIter != primitiveSet->mTypeNames_.end()) {
138 currentType = typeIter->second;
144 for (; iTerminal < userTerminals_.size(); iTerminal++)
145 if (userTerminals_[iTerminal]->
getName() == name)
147 if (iTerminal < userTerminals_.size()) {
148 userTerminals_[iTerminal]->initialize(state_);
149 primitiveSet->addTerminal(userTerminals_[iTerminal]);
156 if (name[0] ==
'[' || name[0] ==
'{') {
158 std::string ercValues =
"";
162 switch (currentType) {
163 case Primitives::Double:
164 erc = (PrimitiveP)(
new Primitives::ERCD);
165 ercValues = DBL_PREFIX;
167 case Primitives::Int:
168 erc = (PrimitiveP)(
new Primitives::ERC<int>);
169 ercValues = INT_PREFIX;
171 case Primitives::Bool:
172 erc = (PrimitiveP)(
new Primitives::ERC<bool>);
173 ercValues = BOOL_PREFIX;
175 case Primitives::Char:
176 erc = (PrimitiveP)(
new Primitives::ERC<char>);
177 ercValues = CHR_PREFIX;
179 case Primitives::String:
180 erc = (PrimitiveP)(
new Primitives::ERC<std::string>);
181 ercValues = STR_PREFIX;
185 while (name[name.size() - 1] !=
']' && name[name.size() - 1] !=
'}') {
186 ercValues +=
" " + name;
189 ercValues +=
" " + name;
192 erc->setName(ercValues);
193 erc->initialize(state_);
194 primitiveSet->addTerminal(erc);
204 case Primitives::Double:
205 terminal = (PrimitiveP)(
new Primitives::Terminal);
break;
206 case Primitives::Int:
207 terminal = (PrimitiveP)(
new Primitives::TerminalT<int>);
break;
208 case Primitives::Bool:
209 terminal = (PrimitiveP)(
new Primitives::TerminalT<bool>);
break;
210 case Primitives::Char:
211 terminal = (PrimitiveP)(
new Primitives::TerminalT<char>);
break;
212 case Primitives::String:
213 terminal = (PrimitiveP)(
new Primitives::TerminalT<std::string>);
break;
218 std::istringstream ss(name);
221 case Primitives::Double:
224 if (ss.fail() ==
false)
225 terminal->setValue(&dblValue);
227 case Primitives::Int:
230 if (ss.fail() ==
false)
231 terminal->setValue(&intValue);
233 case Primitives::Bool:
238 else if (name ==
"false")
240 if (ss.fail() ==
false || name ==
"true" || name ==
"false") {
245 terminal->setValue(&boolValue);
248 case Primitives::Char:
251 if (ss.fail() ==
false)
252 terminal->setValue(&charValue);
254 case Primitives::String:
255 std::string stringValue;
257 if (ss.fail() ==
false)
258 terminal->setValue(&stringValue);
261 terminal->setName(name);
262 primitiveSet->addTerminal(terminal);
266 if (primitiveSet->getTerminalSetSize() == 0) {
267 ECF_LOG_ERROR(state_,
"APGenotype: Empty terminal set!");
274std::vector<uint> APGenotype::getDiscreteIndices()
276 std::vector<uint> discrete;
279 double dindex, diff, delta;
280 int imax = primitiveSet->getPrimitivesSize() - 1, dis_index;
282 delta = 1.0 / (double)primitiveSet->getPrimitivesSize();
289 while (dindex > 0 && dis_index != imax) {
294 discrete[i] = dis_index;
301void APGenotype::buildTree(std::vector<uint> indices, uint current, uint depth)
303 Node*
node =
new Node();
304 node->depth_ = depth;
306 uint index = current - 1;
308 uint numArgs = primitiveSet->getPrimitiveByIndex(index)->getNumberOfArguments();
310 if (numArgs < toEnd) {
311 node->setPrimitive(primitiveSet->getPrimitiveByIndex(index));
312 nodes.push_back(
static_cast<NodeP
> (
node));
313 }
else if (toEnd >= 2 && toEnd <= numArgs && primitiveSet->getGFSTwoSetSize() != 0) {
314 node->setPrimitive(primitiveSet->getGFSTwoByIndex(index));
315 nodes.push_back(
static_cast<NodeP
> (
node));
316 }
else if (toEnd == 1 && primitiveSet->getGFSOneSetSize() != 0) {
317 node->setPrimitive(primitiveSet->getGFSOneByIndex(index));
318 nodes.push_back(
static_cast<NodeP
> (
node));
320 node->setPrimitive(primitiveSet->getTerminalByIndex(index));
321 nodes.push_back(
static_cast<NodeP
> (
node));
324 numArgs =
node->primitive_->getNumberOfArguments();
329 std::vector<uint> passableIndices(indices.begin(), indices.begin() + numArgs);
330 indices.erase(indices.begin(), indices.begin() + numArgs - 1);
332 for (uint i = 0; i < numArgs; i++) {
333 buildTree(indices, passableIndices[i], depth + 1);
339Tree* APGenotype::convertToPhenotype()
341 std::vector<uint> indices = getDiscreteIndices();
344 uint first = indices[0];
347 indices.erase(indices.begin());
349 buildTree(indices, first, 0);
351 Tree* tree =
new Tree();
352 for (uint i = 0; i < nodes.size(); i++) {
353 tree->addNode(nodes[i]);
356 std::stringstream sValue;
357 for (uint i = 0; i < tree->size(); i++) {
358 sValue << (*tree)[i]->primitive_->getName() <<
" ";
367void APGenotype::setTerminalValue(Tree* tree, std::string name,
void* value)
369 PrimitiveP term = primitiveSet->getTerminalByName(name);
370 if (term == PrimitiveP()) {
371 ECF_LOG_ERROR(state_,
"AP genotype: invalid terminal name referenced in setTerminalValue()!");
375 term->setValue(value);
381 xFloatingPoint = XMLNode::createXMLTopNode(
"APGenotype");
382 std::stringstream sValue;
384 xFloatingPoint.addAttribute(
"size", sValue.str().c_str());
389 xFloatingPoint.addText(sValue.str().c_str());
395 XMLCSTR values = xFloatingPoint.getText();
396 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 genotypeId_
this genotype's unique index in individual structure
uint nDimension_
dimensionality
double minValue_
lower bound
double maxValue_
upper bound
std::vector< double > realValue
vector of floating point values
Analytical Programing genotype class - implements genotype as a vector of floating point values that ...
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)
Primitive set class for APGenotype: collects all Tree Primitives.