ECF 1.5
TreeCrxOnePoint.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeCrxOnePoint.h"
4
5
6namespace Tree
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.onepoint", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.onepoint");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool TreeCrxOnePoint::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP ch)
24{
25 Tree* male = (Tree*) (gen1.get());
26 Tree* female = (Tree*) (gen2.get());
27 Tree* child = (Tree*) (ch.get());
28
29 uint mIndex, fIndex;
30 uint mRange, fRange;
31 uint mNodeDepth, fNodeDepth, fNodeDepthSize;
32
33 mRange = (uint) male->size();
34 fRange = (uint) female->size();
35
36 // for common region nodes
37 std::vector <uint> maleCommonRegionIndexes;
38 std::vector <uint> femaleCommonRegionIndexes;
39
40 for( uint iMale = 0, iFemale = 0; iMale < mRange && iFemale < fRange; iMale++, iFemale++ ) {
41 // add common region nodes
42 maleCommonRegionIndexes.push_back( iMale );
43 femaleCommonRegionIndexes.push_back( iFemale );
44
45 // skip nodes with different no. of arguments
46 if( male->at( iMale )->primitive_->getNumberOfArguments() != female->at( iFemale )->primitive_->getNumberOfArguments() ) {
47 iMale += male->at( iMale )->size_;
48 iFemale += female->at( iFemale )->size_;
49 }
50 }
51
52 uint nTries = 0;
53 while(1) {
54 // choose random node from common region
55 uint randomNode = state_->getRandomizer()->getRandomInteger(0 , (int) maleCommonRegionIndexes.size()-1 );
56 mIndex = maleCommonRegionIndexes[ randomNode ];
57 fIndex = femaleCommonRegionIndexes[ randomNode ];
58
59 // LD: provjera dubine je redundantna u slucaju da sve jedinke imaju jednaki maxDepth_
60 // ili kada otac ima veci ili jednak maxDepth_
61
62 mNodeDepth = male->at( mIndex )->depth_;
63 fNodeDepth = female->at( fIndex )->depth_;
64
65 // find max depth
66 uint maxDepth = fNodeDepth, depth;
67 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
68 depth = female->at( fIndex + i )->depth_;
69 maxDepth = depth > maxDepth ? depth : maxDepth;
70 }
71
72 fNodeDepthSize = maxDepth - fNodeDepth;
73 nTries++;
74
75 if(nTries > 4 || mNodeDepth + fNodeDepthSize <= male->maxDepth_ ) break;
76 }
77
78 if(nTries > 4 && mNodeDepth + fNodeDepthSize > male->maxDepth_) {
79 ECF_LOG(state_, 5, "TreeCrxOnePoint not successful.");
80 return false;
81 }
82
83 child->clear();
84 child->maxDepth_ = male->maxDepth_;
85 child->minDepth_ = male->minDepth_;
86 child->startDepth_ = male->startDepth_;
87
88 // copy from male parent
89 for(uint i = 0; i < mIndex; i++) {
90 NodeP node = static_cast<NodeP> (new Node( male->at(i)->primitive_));
91 child->push_back( node );
92 child->at( i )->depth_ = male->at( i )->depth_;
93
94 }
95 // copy from female parent
96 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
97 NodeP node = static_cast<NodeP> (new Node( female->at( fIndex + i)->primitive_));
98 child->push_back( node );
99 }
100 // copy rest from male parent
101 for(uint i = mIndex + male->at( mIndex )->size_; i < mRange; i++) {
102 NodeP node = static_cast<NodeP> (new Node( male->at( i )->primitive_));
103 child->push_back( node );
104 }
105
106 // update node depths and subtree sizes
107 child->update();
108
109 return true;
110}
111
112}
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
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
Definition: nodes.h:92