ECF 1.7
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
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 useAPGenotype = false;
18 GenotypeP activeGenotype = state->getGenotypes()[0];
19 if(activeGenotype->getName() == "APGenotype")
20 useAPGenotype = true;
21
22 return true;
23}
24
25
26FitnessP APSymbRegEvalOp::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::APGenotype* apg = (Tree::APGenotype*) individual->getGenotype().get();
33
34 Tree::Tree* tree;
35 if (useAPGenotype == true)
36 tree = (Tree::Tree*) ((Tree::APGenotype*)individual->getGenotype().get())->convertToPhenotype();
37 else
38 tree = (Tree::Tree*) individual->getGenotype().get();
39
40 double value = 0;
41 for(uint i = 0; i < nSamples; i++) {
42 // for each test data instance, the x value (domain) must be set
43 if(useAPGenotype == true)
44 apg->setTerminalValue(tree, "X", &domain[i]);
45 else
46 tree->setTerminalValue("X", &domain[i]);
47 // get the y value of the current tree
48 double result;
49 tree->execute(&result);
50 // add the difference
51 value += fabs(codomain[i] - result);
52 }
53 fitness->setValue(value);
54
55 return fitness;
56}
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.
Fitness for minimization problems.
Definition FitnessMin.h:12
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