ECF 1.5
TreeMutHoist.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeMutHoist.h"
4#include <stdio.h>
5
6
7namespace Tree
8{
9
11{
12 myGenotype_->registerParameter(state, "mut.hoist", (voidP) new double(0), ECF::DOUBLE);
13}
14
15
16bool TreeMutHoist::initialize(StateP state)
17{
18 voidP sptr = myGenotype_->getParameterValue(state, "mut.hoist");
19 probability_ = *((double*)sptr.get());
20 return true;
21}
22
23
24bool TreeMutHoist::mutate(GenotypeP gene)
25{
26 Tree* tree = (Tree*) (gene.get());
27
28 // first of all, make a copy of tree and clear the original
29 Tree* copyTree = tree->copy();
30 tree->clear();
31
32 // select random node in copied tree, it will be the root node for new tree
33 uint chosenNode = state_->getRandomizer()->getRandomInteger((int) copyTree->size());
34 uint chosenNodeSubtreeSize = copyTree->at(chosenNode)->size_;
35
36 // copy chosen subtree to original
37 for(uint i = 0; i < chosenNodeSubtreeSize; i++) {
38 NodeP node = static_cast<NodeP> (new Node(copyTree->at(chosenNode + i)->primitive_));
39 tree->addNode(node);
40 }
41
42 tree->update();
43 delete copyTree;
44
45 std::stringstream log;
46 log << "TreeMutHoist successful (hoisted subtree = ";
47 for(uint i = 0; i < tree->size(); i++)
48 log << tree->at(i)->primitive_->getName() << " ";
49 log << ")";
50 ECF_LOG(state_, 5, log.str());
51
52 return true;
53}
54
55}
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
Node base class (Tree genotype)
Definition: Node.h:20
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!
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
Definition: nodes.h:92