ECF 1.7
GEPChromosomeMutGauss.cpp
1#include "GEPChromosomeMutGauss.h"
2
3namespace GEP{
5 {
6 myGenotype_->registerParameter(state, "mut.gauss", (voidP) new double(0), ECF::DOUBLE);
7 }
8
10 {
11 voidP sptr = myGenotype_->getParameterValue(state, "mut.gauss");
12 probability_ = *((double*)sptr.get());
13
14 std::random_device rd;
15 std::mt19937 rng(rd());
16 engine_ = rng;
17 //rd.seed((uint)time(NULL));
18
19 return true;
20 }
21
22 bool GEPChromosomeMutGauss::mutate(GenotypeP gene){
23 GEPChromosome* chr = (GEPChromosome*)(gene.get());
24 if (chr->dcLength > 0){
25 // mutate a single random point in the Dc domain by adding Gaussian noise to it
26 // Select a random gene
27 uint iGene = state_->getRandomizer()->getRandomInteger(0, chr->genes - 1);
28 uint geneOffset = iGene * chr->geneLength;
29 uint constOffset = geneOffset + chr->headLength + chr->tailLength; // this is where the Dc of the selected gene begins
30 // Select a random point in the Dc tail
31 uint iPoint = constOffset + state_->getRandomizer()->getRandomInteger(0, (uint)(chr->dcLength) - 1);
32
33 double oldValue;
34 Tree::PrimitiveP oldPrim = chr->at(iPoint)->primitive_;
35 chr->at(iPoint)->primitive_->getValue(&oldValue);
36 std::string oldName = chr->at(iPoint)->primitive_->getName();
37
38 // generate Gauss noise offset and add it
39 std::normal_distribution<double> N(0, 1);
40 //double offset = N.operator () < boost::lagged_fibonacci607 > (engine_);
41 //double offset = N.operator () < std::mt19937 > (engine_);
42 double offset = N(engine_);
43 double newValue = oldValue + offset;
44
45 // change double ERC value and name
46 std::stringstream ss;
47 ss << newValue;
48 std::string newName;
49 ss >> newName;
50 newName = DBL_PREFIX + newName;
51
52 oldPrim->setName(newName);
53 oldPrim->setValue(&newValue);
54 // print result
55 std::stringstream log;
56 log << "GEPMutGauss successful (oldNode = " << oldName << ", newNode = " << newName << ")";
57 ECF_LOG(state_, 5, log.str());
58 }
59 return true;
60 }
61}
GEPChromosome class - implements genotype as a Gene Expression Programming chromosome.
uint genes
number of genes
uint tailLength
length of the tail. Automatically calculated.
uint geneLength
total length of each gene
uint dcLength
length of the constant values domain
uint headLength
length of the head. User-specified
void registerParameters(StateP state)
Register parameters with the system. Called before MutationOp::initialize.
bool initialize(StateP state)
Initialize mutation operator. Called before first mutation operation.
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
std::string getName()
Return genotype's name (each genotype is uniquely identified with its name).
Definition Genotype.h:86
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