ECF 1.5
TreeCrxSimple.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeCrxSimple.h"
4
5
6namespace Tree
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.simple", (voidP) new double(0), ECF::DOUBLE);
12 myGenotype_->registerParameter(state, "crx.simple.functionprob", (voidP) new double(0.9), ECF::DOUBLE);
13}
14
15
16bool TreeCrxSimple::initialize(StateP state)
17{
18 voidP sptr = myGenotype_->getParameterValue(state, "crx.simple");
19 probability_ = *((double*)sptr.get());
20 sptr = myGenotype_->getParameterValue(state, "crx.simple.functionprob");
21 funcChoiceProb_ = *((double*)sptr.get());
22
23 return true;
24}
25
26
27bool TreeCrxSimple::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP ch)
28{
29 Tree* male = (Tree*) (gen1.get());
30 Tree* female = (Tree*) (gen2.get());
31 Tree* child = (Tree*) (ch.get());
32
33 uint mIndex, fIndex;
34 uint mRange, fRange;
35 uint mNodeDepth, fNodeDepth, fNodeDepthSize;
36
37 mRange = (uint) male->size();
38 fRange = (uint) female->size();
39
40 // indentify leaf (terminal) and nonLeaf (function) nodes
41 std::vector <uint> maleLeafIndexes, maleNonLeafIndexes;
42 for(uint i = 0; i < mRange; i++) {
43 if( male->at( i )->size_ == 1 ) maleLeafIndexes.push_back( i );
44 else maleNonLeafIndexes.push_back( i );
45 }
46 // analogno za female
47 std::vector <uint> femaleLeafIndexes, femaleNonLeafIndexes;
48 for(uint i = 0; i < fRange; i++) {
49 if( female->at( i )->size_ == 1 ) femaleLeafIndexes.push_back( i );
50 else femaleNonLeafIndexes.push_back( i );
51 }
52
53 uint nTries = 0;
54 while(1) {
55 // choose male crx point
56 if(state_->getRandomizer()->getRandomDouble() > funcChoiceProb_ || maleNonLeafIndexes.empty()) {
57 uint randomLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (int) maleLeafIndexes.size() - 1);
58 mIndex = maleLeafIndexes [ randomLeafIndex ];
59 }
60 else {
61 uint randomNonLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (int) maleNonLeafIndexes.size() - 1);
62 mIndex = maleNonLeafIndexes [ randomNonLeafIndex ];
63 }
64
65 // choose female crx point
66 if(state_->getRandomizer()->getRandomDouble() > funcChoiceProb_ || femaleNonLeafIndexes.empty()) {
67 uint randomLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (int) femaleLeafIndexes.size() - 1);
68 fIndex = femaleLeafIndexes [ randomLeafIndex ];
69 }
70 else {
71 uint randomNonLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (int) femaleNonLeafIndexes.size() - 1);
72 fIndex = femaleNonLeafIndexes [ randomNonLeafIndex ];
73 }
74
75 mNodeDepth = male->at( mIndex )->depth_;
76 fNodeDepth = female->at( fIndex )->depth_;
77
78 // find max depth
79 uint maxDepth = fNodeDepth, depth;
80 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
81 depth = female->at( fIndex + i )->depth_;
82 maxDepth = depth > maxDepth ? depth : maxDepth;
83 }
84
85 fNodeDepthSize = maxDepth - fNodeDepth;
86 nTries++;
87
88 if(nTries > 4 || mNodeDepth + fNodeDepthSize <= male->maxDepth_ ) break;
89 }
90
91 if(nTries > 4 && mNodeDepth + fNodeDepthSize > male->maxDepth_) {
92 ECF_LOG(state_, 5, "TreeCrxSimple not successful.");
93 return false;
94 }
95
96 child->clear();
97
98 // copy from male parent
99 child->maxDepth_ = male->maxDepth_;
100 child->minDepth_ = male->minDepth_;
101 child->startDepth_ = male->startDepth_;
102
103 // copy from male parent
104 for(uint i = 0; i < mIndex; i++) {
105 NodeP node = static_cast<NodeP> (new Node( male->at(i)->primitive_));
106 child->push_back( node );
107 child->at( i )->depth_ = male->at( i )->depth_;
108 }
109 // copy from female parent
110 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
111 NodeP node = static_cast<NodeP> (new Node( female->at( fIndex + i)->primitive_));
112 child->push_back( node );
113 }
114 // copy rest from male parent
115 for(uint i = mIndex + male->at( mIndex )->size_; i < mRange; i++) {
116 NodeP node = static_cast<NodeP> (new Node( male->at( i )->primitive_));
117 child->push_back( node );
118 }
119
120 // update node depths and subtree sizes
121 child->update();
122
123 return true;
124}
125
126}
double probability_
probability of usage of this crossover operator
Definition: Crossover.h:42
GenotypeP myGenotype_
pointer to the Genotype that defines this CrossoverOp
Definition: Crossover.h:43
Node base class (Tree genotype)
Definition: Node.h:20
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
Definition: nodes.h:92