ECF 1.5
TreeMutNodeReplace.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeMutNodeReplace.h"
4#include <stdio.h>
5
6
7namespace Tree
8{
9
11{
12 myGenotype_->registerParameter(state, "mut.nodereplace", (voidP) new double(0), ECF::DOUBLE);
13}
14
15
17{
18 voidP sptr = myGenotype_->getParameterValue(state, "mut.nodereplace");
19 probability_ = *((double*)sptr.get());
20 return true;
21}
22
23
24bool TreeMutNodeReplace::mutate(GenotypeP gene)
25{
26 Tree* tree = (Tree*) (gene.get());
27
28 // select random node in tree
29 uint origNodeIndex = state_->getRandomizer()->getRandomInteger((int) tree->size());
30 std::string origNodeName = tree->at(origNodeIndex)->primitive_->getName();
31 int origNodeNumberOfArguments = tree->at(origNodeIndex)->primitive_->getNumberOfArguments();
32
33 // try to select a primitive different from selected node's, with the same number of arguments
34 PrimitiveP primitive;
35 uint tries = 0;
36 do {
37 primitive = tree->primitiveSet_->getRandomPrimitive();
38 tries++;
39 } while((origNodeName == primitive->getName()
40 || origNodeNumberOfArguments != primitive->getNumberOfArguments()) && tries < 4);
41
42 if(origNodeName == primitive->getName() || origNodeNumberOfArguments != primitive->getNumberOfArguments()) {
43 ECF_LOG(state_, 5, "TreeMutNodeReplace not successful.");
44 return false;
45 }
46
47 tree->at(origNodeIndex)->setPrimitive(primitive);
48
49 std::stringstream log;
50 log << "TreeMutNodeReplace successful (oldNode = " << origNodeName << ", ";
51 log << "newNode = " << primitive->getName() << ")";
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 initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!