ECF 1.7
SymbRegEvalOp.cpp
1#include "SymbRegEvalOp.h"
2
3
4// called only once, before the evolution – generates training data
6{
7 x.clear();
8 y.clear();
9 f.clear();
10 /*// check if the parameters are stated (used) in the conf. file
11 // if not, we return false so the initialization fails
12 if (!state->getRegistry()->isModified("x.data") || !state->getRegistry()->isModified("y.data") || !state->getRegistry()->isModified("f.data"))
13 return false;
14 // read from csv
15 if(!csvRead(state,"x.data", &x)) return false;
16 if(!csvRead(state,"y.data", &y)) return false;
17 if(!csvRead(state,"f.data", &f)) return false;
18 return true;
19 */
20 nSamples = 10;
21 double s = .1;
22 double X = -10*s;
23 double Y = -10*s;
24
25 for (uint i = 0; i < nSamples; i++) {
26 x.push_back(X);
27 y.push_back(Y);
28 Y += 2*s;
29 X += 2*s;
30 }
31
32 for (uint i = 0; i < nSamples; i++) {
33 X = x.at(i);
34 for (uint j = 0; j < nSamples; j++) {
35 Y = y.at(j);
36 // select target function
37 //f.push_back(sin(X) + sin(Y*Y)); // F2
38 //f.push_back(2*sin(X)*cos(Y)); // F3
39 f.push_back(X*Y + sin((X + 1)*(Y - 1))); // F4
40 //f.push_back(8 / (2+X*X+Y*Y)); // F5
41 //f.push_back((X*X*X)/5+(Y*Y*Y)/2-X-Y); // F6
42 }
43 }
44 return true;
45}
46
47bool GEPSymbRegEvalOp::csvRead(StateP state, std::string entry, std::vector<double>* vec){
48 std::ifstream stream;
49 std::string line;
50 voidP sptr = state->getRegistry()->getEntry(entry);
51 std::string fname = *((std::string*) sptr.get());
52 stream.open(fname);
53 if (!stream.is_open()) return false;
54 while (getline(stream, line)){
55 vec->push_back(atof(line.c_str()));
56 }
57 return true;
58}
59
61{
62 // optional - read data from input files
63 /*
64 state->getRegistry()->registerEntry("x.data", (voidP)(new std::string), ECF::STRING);
65 state->getRegistry()->registerEntry("y.data", (voidP)(new std::string), ECF::STRING);
66 state->getRegistry()->registerEntry("f.data", (voidP)(new std::string), ECF::STRING);
67 */
68}
69
70FitnessP GEPSymbRegEvalOp::evaluate(IndividualP individual)
71{
72 // we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
73 FitnessP fitness(new FitnessMin);
74
75 // get genotype
76 GEPChromosomeP gep = std::static_pointer_cast<GEP::GEPChromosome> (individual->getGenotype());
77 // 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
78 // The user specifies the linking function programatically or in the parameters
79
80 // a) just assemble the whole genotype in a single expression to evaluate
81 gep->assemble();
82
84 //std::vector<Tree::Tree*> tree;
85 //tree.push_back(gep->makeCellTree());
86 //for (uint g = 0; g < gep->genes; g++){
87 // tree.push_back(gep->toTree(g));
89 //}
90
91
92 double value = 0;
93 double result;
94 //nSamples = f.size();
95 for(uint i = 0; i < nSamples; i++) {
96 for (uint j = 0; j < nSamples; j++){
97 result = 0;
98
99 // a) execute the whole expression
100 gep->setTerminalValue("X", &x.at(i));
101 gep->setTerminalValue("Y", &y.at(j));
102 gep->execute(&result);
103
104 // b) or, execute every subtree and link manually
105 /*
106 for (uint g = 0; g < gep->genes; g++){
107 // for each test data instance, the x value (domain) must be set
108 tree.at(g)->setTerminalValue("X", &x.at(i));
109 tree.at(g)->setTerminalValue("Y", &y.at(j));
110
111 // get the f value of the current tree
112 tree.at(g)->execute(&tmpResult);
113 // link using addition (for example)
114 result += tmpResult;
115 }*/
116
117 // add the difference
118 value += pow(f.at(i*nSamples+j) - result, 2);
119 }
120 }
121 value /= pow(nSamples,2);
122 fitness->setValue(value);
123 return fitness;
124}
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.