ECF 1.5
SymbRegEvalOp.cpp
1#include <cmath>
2#include <ecf/ECF.h>
3#include "SymbRegEvalOp.h"
4
5
6// called only once, before the evolution – generates training data
7bool SymbRegEvalOp::initialize(StateP state)
8{
9 nSamples = 10;
10 double x = -10;
11 for(uint i = 0; i < nSamples; i++) {
12 domain.push_back(x);
13 codomain.push_back(x + sin(x));
14 x += 2;
15 }
16
17 APGenotype = false;
18 GenotypeP activeGenotype = state->getGenotypes()[0];
19 if(activeGenotype->getName() == "APGenotype")
20 APGenotype = true;
21
22 return true;
23}
24
25
26FitnessP SymbRegEvalOp::evaluate(IndividualP individual)
27{
28 // we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
29 FitnessP fitness (new FitnessMin);
30
31 // get the genotype we defined in the configuration file
32 Tree::Tree* tree = (Tree::Tree*) individual->getGenotype().get();
33 // (you can also use boost smart pointers:)
34 //TreeP tree = boost::static_pointer_cast<Tree::Tree> (individual->getGenotype());
35
36 Tree::APGenotype* apg = (Tree::APGenotype*) individual->getGenotype().get();
37
38 if(APGenotype == true)
39 tree = (Tree::Tree*) ((Tree::APGenotype*)individual->getGenotype().get())->convertToPhenotype();
40
41 double value = 0;
42 for(uint i = 0; i < nSamples; i++) {
43 // for each test data instance, the x value (domain) must be set
44 if(APGenotype == true)
45 apg->setTerminalValue(tree, "X", &domain[i]);
46 else
47 tree->setTerminalValue("X", &domain[i]);
48 // get the y value of the current tree
49 double result;
50 tree->execute(&result);
51 // add the difference
52 value += fabs(codomain[i] - result);
53 }
54 fitness->setValue(value);
55
56 return fitness;
57}
Fitness for minimization problems.
Definition: FitnessMin.h:12
FitnessP evaluate(IndividualP individual)
Evaluate a single individual. Method must create and return a Fitness object.
bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
Analytical Programing genotype class - implements genotype as a vector of floating point values that ...
Definition: APGenotype.h:50
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
void setTerminalValue(std::string, void *)
Set a terminal's value.
Definition: Tree.cpp:504
void execute(void *)
Execute current tree.
Definition: Tree.cpp:362