ECF 1.5
TreeMutSubtree.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeMutSubtree.h"
4#include <stdio.h>
5
6
7namespace Tree
8{
9
11{
12 myGenotype_->registerParameter(state, "mut.subtree", (voidP) new double(0), ECF::DOUBLE);
13}
14
15
17{
18 voidP sptr = myGenotype_->getParameterValue(state, "mut.subtree");
19 probability_ = *((double*)sptr.get());
20 return true;
21}
22
23
24bool TreeMutSubtree::mutate(GenotypeP gene)
25{
26 Tree* tree = (Tree*) (gene.get());
27
28 // try to select random node in tree which is not a terminal and which is not on maxDepth
29 // this is a size-fair operator (depth condition)
30 uint chosenNode;
31 uint chosenNodeSubtreeSize;
32 uint chosenNodeDepth;
33 uint tries = 0;
34 do {
35 chosenNode = state_->getRandomizer()->getRandomInteger((int) tree->size());
36 chosenNodeSubtreeSize = tree->at(chosenNode)->size_;
37 chosenNodeDepth = tree->at(chosenNode)->depth_;
38 tries++;
39 } while((/*chosenNodeSubtreeSize == 1 || */chosenNodeDepth == tree->maxDepth_) && tries < 4);
40
41 if(tries == 4) {
42 ECF_LOG(state_, 5, "TreeMutSubtree not successful.");
43 return false;
44 }
45
46 // first of all, make a copy of tree and clear the original
47 Tree* copyTree = tree->copy();
48 tree->clear();
49
50 std::stringstream log;
51 log << "TreeMutSubtree successful (";
52
53 uint i = 0;
54
55 // copy all nodes before chosen subtree to original
56 for( ; i < chosenNode; i++) {
57 NodeP node = static_cast<NodeP> (new Node(copyTree->at(i)->primitive_));
58 tree->addNode(node);
59 }
60
61 log << "mutatedSubtree(at depth " << chosenNodeDepth << ") = ";
62 for( ; i < chosenNode + chosenNodeSubtreeSize; i++) {
63 // this node is an element of the chosen subtree
64 log << copyTree->at(i)->primitive_->getName() << " ";
65 }
66
67 // generate new subtree with same primitives as original tree
68 // maxDepth for that new tree is original tree->maxDepth_ - chosenNodeDepth
69 Tree* newTree = tree->copy();
70 newTree->clear();
71 newTree->initMinDepth_ = 0;
72 newTree->initMaxDepth_ = copyTree->maxDepth_ - chosenNodeDepth;
73
74 if(state_->getRandomizer()->getRandomInteger(0, 1) % 2 == 0)
75 newTree->fullBuild(copyTree->primitiveSet_);
76 else
77 newTree->growBuild(copyTree->primitiveSet_);
78
79 log << ", newSubtree = ";
80 // copy all nodes from newTree to tree
81 for(uint j = 0; j < newTree->size(); j++) {
82 NodeP node = static_cast<NodeP> (new Node(newTree->at(j)->primitive_));
83 tree->addNode(node);
84 log << node->primitive_->getName() << " ";
85 }
86 log << ")";
87
88 // copy all nodes after chosen subtree to original
89 for( ; i < copyTree->size(); i++) {
90 NodeP node = static_cast<NodeP> (new Node(copyTree->at(i)->primitive_));
91 tree->addNode(node);
92 }
93
94 tree->update();
95 delete copyTree;
96 delete newTree;
97
98 ECF_LOG(state_, 5, log.str());
99
100 return true;
101}
102
103}
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!
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
Definition: nodes.h:92