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 return true;
18}
19
20
21FitnessP SymbRegEvalOp::evaluate(IndividualP individual)
22{
23 // we try to minimize the function value, so we use FitnessMin fitness (for minimization problems)
24 FitnessP fitness (new FitnessMin);
25
26 // get the genotype we defined in the configuration file
27 Tree::Tree* tree = (Tree::Tree*) individual->getGenotype().get();
28 // (you can also use boost smart pointers:)
29 //TreeP tree = boost::static_pointer_cast<Tree::Tree> (individual->getGenotype());
30
31 double value = 0;
32 for(uint i = 0; i < nSamples; i++) {
33 // for each test data instance, the x value (domain) must be set
34 tree->setTerminalValue("X", &domain[i]);
35 // get the y value of the current tree
36 double result;
37 tree->execute(&result);
38 // add the difference
39 value += fabs(codomain[i] - result);
40 }
41 fitness->setValue(value);
42
43 return fitness;
44}
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.
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