ECF 1.5
GEPSymbRegEvalOp.cpp
1#include "GEPSymbRegEvalOp.h"
2#include "ReadData.h"
3
4
5
7{
8 state->getRegistry()->registerEntry("input_file", (voidP)(new std::string), ECF::STRING);
9}
10
11
12// called only once, before the evolution – generates training data
14{
15 x.clear();
16 y.clear();
17 f.clear();
18
19 // initialize the Evaluator
20 eval.initialize();
21
22 // check if the parameters are stated (used) in the conf. file
23 if (state->getRegistry()->isModified("input_file")) {
24 std::string dataFile = *((std::string*) state->getRegistry()->getEntry("input_file").get());
25
26 // read from file into Evaluator data
27 if(!readDataFromFile(eval.data, dataFile))
28 return false;
29
30 nSamples = eval.data.size();
31 nVariables = eval.data[0].size() - 1;
32
33 return true;
34 }
35
36
37 nSamples = 10;
38 double s = 1;
39 double X = -10*s;
40 double Y = -10*s;
41
42 for (uint i = 0; i < nSamples; i++) {
43 x.push_back(X);
44 y.push_back(Y);
45 Y += 2*s;
46 X += 2*s;
47 }
48
49 for (uint i = 0; i < nSamples; i++) {
50 X = x.at(i);
51 for (uint j = 0; j < nSamples; j++) {
52 Y = y.at(j);
53 // F1
54 f.push_back(sin(X) + sin(Y*Y)); // F2
55 //f.push_back(2*sin(X)*cos(Y)); // F3
56 //f.push_back(X*Y + sin((X + 1)*(Y - 1))); // F4
57 //f.push_back(8 / (2+X*X+Y*Y)); // F5
58 //f.push_back((X*X*X)/5+(Y*Y*Y)/2-X-Y); // F6
59 }
60 }
61 return true;
62
63}
64
65
66bool GEPSymbRegEvalOp::csvRead(StateP state, std::string entry, std::vector<double>* vec){
67 std::ifstream stream;
68 std::string line;
69 voidP sptr = state->getRegistry()->getEntry(entry);
70 std::string fname = *((std::string*) sptr.get());
71 stream.open(fname);
72 if (!stream.is_open()) return false;
73 while (getline(stream, line)){
74 vec->push_back(atof(line.c_str()));
75 }
76 return true;
77}
78
79
80FitnessP GEPSymbRegEvalOp::evaluate(IndividualP individual)
81{
82 // we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
83 FitnessP fitness(new FitnessMin);
84 // get genotype
85 GEPChromosomeP gep = boost::static_pointer_cast<GEPChromosome::GEPChromosome> (individual->getGenotype());
86 // The system is multigenic. We iterate over every gene, transform it to a tree, execute it and link it with the results of its fellow genes
87 // The user programatically specifies the linking function. In this case, addition is used.
88 readIndividual(individual);
89 double value = 0;
90 double result;
91 //nSamples = f.size();
92 for(uint i = 0; i < nSamples; i++) {
93 result = eval.executeParsedExpression(i);
94
95 // add the difference
96 value += pow(eval.data[i][nVariables] - result, 2);
97 // += fabs(f.at(i*nSamples + j) - result);
98 }
99value /= pow(1.*nSamples, 2);
100fitness->setValue(value);
101return fitness;
102}
103
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);
108 // The first dimension encompasses the list of genes in the chromosome
109 // The final gene is the cell gene
110 eval.parsedExpression.resize(nTrees+1);
111 //Translate to tree form
112 chr->assemble();
113 for (uint iTree = 0; iTree <= nTrees; iTree++) {
114 uint idx = iTree == nTrees ? 0 : iTree+1;
115 Tree::Tree* pTree;
116 if (idx != 0) pTree = chr->subtrees.at(iTree); // Assign a subtree
117 else pTree = chr->cellTree; // Asign the cell structure tree
118 uint nTreeSize = (uint)pTree->size();
119 eval.parsedExpression[idx].resize(nTreeSize);
120 // read whole tree expression
121 for (uint i = 0; i < nTreeSize; i++) {
122 strPrimitive = (*pTree)[i]->primitive_->getName();
123 // check function names
124 for (uint iPrim = 0; iPrim < eval.funcNames.size(); iPrim++)
125 { // TODO: skip unused functions
126 //if(!Nodes[iPrim].active)
127 // continue;
128 if (strPrimitive == eval.funcNames[iPrim]) {
129 eval.parsedExpression[idx][i] = iPrim;
130 break;
131 }
132 }
133
134 // check terminals
135 for (uint iPrim = 0; iPrim < eval.termNames.size(); iPrim++)
136 { //if(!Nodes[iPrim].active)
137 // continue;
138 if (strPrimitive == eval.termNames[iPrim]) {
139 eval.parsedExpression[idx][i] = iPrim + Evaluator::TERMINALS;
140 break;
141 }
142 }
143
144 // check gene IDs
145 for (uint iPrim = 0; iPrim < nTrees; iPrim++)
146 { //if(!Nodes[iPrim].active)
147 // continue;
148 std::string name = GEP_GENE_PREFIX + uint2str(iPrim);
149 if (strPrimitive == name) {
150 eval.parsedExpression[idx][i] = iPrim + Evaluator::SUBTREES;
151 break;
152 }
153 }
154 }
155 }
156}
Fitness for minimization problems.
Definition: FitnessMin.h:12
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.
Definition: Tree_c.h:29