ECF 1.5
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#include "boost/random/normal_distribution.hpp"
8
9
10namespace Tree
11{
12
14{
15 myGenotype_->registerParameter(state, "mut.gauss", (voidP) new double(0), ECF::DOUBLE);
16}
17
18
19bool TreeMutGauss::initialize(StateP state)
20{
21 voidP sptr = myGenotype_->getParameterValue(state, "mut.gauss");
22 probability_ = *((double*)sptr.get());
23
24 engine_.seed((uint32_t) time(NULL));
25 return true;
26}
27
28
29bool TreeMutGauss::mutate(GenotypeP gene)
30{
31 Tree* tree = (Tree*) (gene.get());
32
33 // try to select ERC node of type double
34 uint iNode;
35 uint tries = 0;
36 std::string name;
37 do {
38 iNode = state_->getRandomizer()->getRandomInteger((int) tree->size());
39 tries++;
40 } while((name = tree->at(iNode)->primitive_->getName()).substr(0, 2) != DBL_PREFIX && tries < 4);
41
42 if(name.substr(0, 2) != DBL_PREFIX) {
43 ECF_LOG(state_, 5, "TreeMutGauss not successful.");
44 return false;
45 }
46
47 double oldValue;
48 PrimitiveP oldPrim = tree->at(iNode)->primitive_;
49 tree->at(iNode)->primitive_->getValue(&oldValue);
50 std::string oldName = tree->at(iNode)->primitive_->getName();
51
52 // generate Gauss noise offset and add it
53 // TODO: parametrize distribution!
54 boost::normal_distribution<double> N(0, 1);
55
56 // e.g. http://www.codepedia.com/1/CppBoostRandom
57 // TODO: preserve state
58 //boost::lagged_fibonacci607 engine(state_->getRandomizer()->getRandomInteger(100000) + 1);
59
60 double offset = N.operator () <boost::lagged_fibonacci607>(engine_);
61 double newValue = oldValue + offset;
62
63 // change double ERC value and name
64 std::stringstream ss;
65 ss << newValue;
66 std::string newName;
67 ss >> newName;
68 newName = DBL_PREFIX + newName;
69
70 oldPrim->setName(newName);
71 oldPrim->setValue(&newValue);
72
73 // new ERCs aren't stored in the PrimitiveSet
74
75 std::stringstream log;
76 log << "TreeMutGauss successful (oldNode = " << oldName << ", newNode = " << newName << ")";
77 ECF_LOG(state_, 5, log.str());
78
79 return true;
80}
81
82}
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.