ECF 1.5
TreeMutNodeComplement.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeMutNodeComplement.h"
4#include <stdio.h>
5
6
7namespace Tree
8{
9
11{
12 myGenotype_->registerParameter(state, "mut.nodecomplement", (voidP) new double(0), ECF::DOUBLE);
13}
14
15
17{
18 voidP sptr = myGenotype_->getParameterValue(state, "mut.nodecomplement");
19 probability_ = *((double*)sptr.get());
20 return true;
21}
22
23
25{
26 Tree* tree = (Tree*) (gene.get());
27
28 // try to select a random node whose complement is in primitive set
29 uint tries = 0;
30 uint index;
31 std::string name, complementName;
32 PrimitiveP complement;
33 do {
34 index = state_->getRandomizer()->getRandomInteger((int) tree->size());
35 name = tree->at(index)->primitive_->getName();
36 complementName = tree->at(index)->primitive_->getComplementName();
37
38 complement = tree->primitiveSet_->getPrimitiveByName(complementName);
39 tries++;
40 } while(complement == PrimitiveP() && tries < 4);
41
42 if(complement == PrimitiveP()) {
43 ECF_LOG(state_, 5, "TreeMutNodeComplement not successful.");
44 return false;
45 }
46
47 tree->at(index)->setPrimitive(complement);
48
49 std::stringstream log;
50 log << "TreeMutNodeComplement successful (oldNode = " << name << ", ";
51 log << "newNode = " << complementName << ")";
52 ECF_LOG(state_, 5, log.str());
53
54 return true;
55}
56
57}
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
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
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.