ECF 1.7
TreeMutGauss.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeMutGauss.h"
4#include <stdio.h>
5#include <time.h>
6
7
8namespace Tree
9{
10
12{
13 myGenotype_->registerParameter(state, "mut.gauss", (voidP) new double(0), ECF::DOUBLE);
14}
15
16
17bool TreeMutGauss::initialize(StateP state)
18{
19 voidP sptr = myGenotype_->getParameterValue(state, "mut.gauss");
20 probability_ = *((double*)sptr.get());
21 std::random_device rd;
22 std::mt19937 rng(rd());
23 engine_ = rng;
24 //engine_.seed((uint32_t) time(NULL));
25
26 return true;
27}
28
29
30bool TreeMutGauss::mutate(GenotypeP gene)
31{
32 Tree* tree = (Tree*) (gene.get());
33
34 // try to select ERC node of type double
35 uint iNode;
36 uint tries = 0;
37 std::string name;
38 do {
39 iNode = state_->getRandomizer()->getRandomInteger((int) tree->size());
40 tries++;
41 } while((name = tree->at(iNode)->primitive_->getName()).substr(0, 2) != DBL_PREFIX && tries < 4);
42
43 if(name.substr(0, 2) != DBL_PREFIX) {
44 ECF_LOG(state_, 5, "TreeMutGauss not successful.");
45 return false;
46 }
47
48 double oldValue;
49 PrimitiveP oldPrim = tree->at(iNode)->primitive_;
50 tree->at(iNode)->primitive_->getValue(&oldValue);
51 std::string oldName = tree->at(iNode)->primitive_->getName();
52
53 // generate Gauss noise offset and add it
54 // TODO: parametrize distribution!
55 std::normal_distribution<double> N(0, 1);
56
57 // e.g. http://www.codepedia.com/1/CppBoostRandom
58 // TODO: preserve state
59 //boost::lagged_fibonacci607 engine(state_->getRandomizer()->getRandomInteger(100000) + 1);
60
61 //double offset = N.operator () <boost::lagged_fibonacci607>(engine_);
62 double offset = N(engine_);
63 double newValue = oldValue + offset;
64
65 // change double ERC value and name
66 std::stringstream ss;
67 ss << newValue;
68 std::string newName;
69 ss >> newName;
70 newName = DBL_PREFIX + newName;
71
72 oldPrim->setName(newName);
73 oldPrim->setValue(&newValue);
74
75 // new ERCs aren't stored in the PrimitiveSet
76
77 std::stringstream log;
78 log << "TreeMutGauss successful (oldNode = " << oldName << ", newNode = " << newName << ")";
79 ECF_LOG(state_, 5, log.str());
80
81 return true;
82}
83
84}
double probability_
probability of usage of this mutation operator
Definition Mutation.h:40
GenotypeP myGenotype_
pointer to the Genotype that defines this MutationOp
Definition Mutation.h:41
Tree class - implements genotype as a tree.
Definition Tree_c.h:29
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.