1#include "GEPSymbRegEvalOp.h"
8 state->getRegistry()->registerEntry(
"input_file", (voidP)(
new std::string), ECF::STRING);
23 if (state->getRegistry()->isModified(
"input_file")) {
24 std::string dataFile = *((std::string*) state->getRegistry()->getEntry(
"input_file").get());
27 if(!readDataFromFile(eval.data, dataFile))
30 nSamples = eval.data.size();
31 nVariables = eval.data[0].size() - 1;
42 for (uint i = 0; i < nSamples; i++) {
49 for (uint i = 0; i < nSamples; i++) {
51 for (uint j = 0; j < nSamples; j++) {
54 f.push_back(sin(X) + sin(Y*Y));
66bool GEPSymbRegEvalOp::csvRead(StateP state, std::string entry, std::vector<double>* vec){
69 voidP sptr = state->getRegistry()->getEntry(entry);
70 std::string fname = *((std::string*) sptr.get());
72 if (!stream.is_open())
return false;
73 while (getline(stream, line)){
74 vec->push_back(atof(line.c_str()));
85 GEPChromosomeP gep = boost::static_pointer_cast<GEPChromosome::GEPChromosome> (individual->getGenotype());
88 readIndividual(individual);
92 for(uint i = 0; i < nSamples; i++) {
93 result = eval.executeParsedExpression(i);
96 value += pow(eval.data[i][nVariables] - result, 2);
99value /= pow(1.*nSamples, 2);
100fitness->setValue(value);
104void GEPSymbRegEvalOp::readIndividual(IndividualP individual){
105 GEPChromosomeP chr = boost::dynamic_pointer_cast<GEPChromosome::GEPChromosome> (individual->getGenotype());
106 static std::string strPrimitive;
107 uint nTrees = (uint)(chr->genes);
110 eval.parsedExpression.resize(nTrees+1);
113 for (uint iTree = 0; iTree <= nTrees; iTree++) {
114 uint idx = iTree == nTrees ? 0 : iTree+1;
116 if (idx != 0) pTree = chr->subtrees.at(iTree);
117 else pTree = chr->cellTree;
118 uint nTreeSize = (uint)pTree->size();
119 eval.parsedExpression[idx].resize(nTreeSize);
121 for (uint i = 0; i < nTreeSize; i++) {
122 strPrimitive = (*pTree)[i]->primitive_->getName();
124 for (uint iPrim = 0; iPrim < eval.funcNames.size(); iPrim++)
128 if (strPrimitive == eval.funcNames[iPrim]) {
129 eval.parsedExpression[idx][i] = iPrim;
135 for (uint iPrim = 0; iPrim < eval.termNames.size(); iPrim++)
138 if (strPrimitive == eval.termNames[iPrim]) {
139 eval.parsedExpression[idx][i] = iPrim + Evaluator::TERMINALS;
145 for (uint iPrim = 0; iPrim < nTrees; iPrim++)
148 std::string name = GEP_GENE_PREFIX + uint2str(iPrim);
149 if (strPrimitive == name) {
150 eval.parsedExpression[idx][i] = iPrim + Evaluator::SUBTREES;
Fitness for minimization problems.
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
Tree class - implements genotype as a tree.