ECF 1.5
TreeCrxSizeFair.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeCrxSizeFair.h"
4
5
6namespace Tree
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.sizefair", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.sizefair");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23int TreeCrxSizeFair::calculateSize (int avg)
24{
25 int sigma = avg - 1;
26 return avg + state_->getRandomizer()->getRandomInteger(-sigma , sigma);
27}
28
29
30bool TreeCrxSizeFair::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP ch)
31{
32 Tree* male = (Tree*) (gen1.get());
33 Tree* female = (Tree*) (gen2.get());
34 Tree* child = (Tree*) (ch.get());
35
36 uint mIndex, fIndex;
37 uint mRange, fRange;
38 uint mNodeDepth, fNodeDepth, fNodeDepthSize;
39
40 mRange = (uint) male->size();
41 fRange = (uint) female->size();
42
43 // LD: femaleSizeIndexes[i] je vektor indeksa cvorova cija je velicina podstabla = i
44 std::vector < std::vector<uint> > femaleSizeIndexes;
45
46 femaleSizeIndexes.resize( fRange + 1 );
47
48 for(uint i = 0; i < fRange; i++)
49 femaleSizeIndexes[ female->at( i )->size_ ].push_back( i );
50
51 uint nTries = 0;
52 while(1) {
53 // choose random crx point in male parent
54 mIndex = state_->getRandomizer()->getRandomInteger(0 , (int) mRange -1 );
55
56 // chose female crx point with expected size less or equal to the male's subtree
57 uint subtreeSize = calculateSize ((int) male->at( mIndex )->size_);
58 while(1) {
59 //LD: ako ne postoji podstablo trazene velicine, trazimo za jedan manje
60 if( subtreeSize <= fRange && femaleSizeIndexes[ subtreeSize ].size()) break;
61 subtreeSize --;
62 }
63 uint tmpRnd = state_->getRandomizer()->getRandomInteger(0 , (int) femaleSizeIndexes[ subtreeSize ].size() - 1 );
64 fIndex = femaleSizeIndexes[ subtreeSize ][ tmpRnd ];
65
66 mNodeDepth = male->at( mIndex )->depth_;
67 fNodeDepth = female->at( fIndex )->depth_;
68
69 // find max depth
70 uint maxDepth = fNodeDepth, depth;
71 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
72 depth = female->at( fIndex + i )->depth_;
73 maxDepth = depth > maxDepth ? depth : maxDepth;
74 }
75
76 fNodeDepthSize = maxDepth - fNodeDepth;
77 nTries++;
78
79 if(nTries > 4 || mNodeDepth + fNodeDepthSize <= male->maxDepth_ ) break;
80 }
81
82 if(nTries > 4 && mNodeDepth + fNodeDepthSize > male->maxDepth_) {
83 ECF_LOG(state_, 5, "TreeCrxSizeFair not successful.");
84 return false;
85 }
86
87 child->clear();
88 child->maxDepth_ = male->maxDepth_;
89 child->minDepth_ = male->minDepth_;
90 child->startDepth_ = male->startDepth_;
91
92 // copy from male parent
93 for(uint i = 0; i < mIndex; i++) {
94 NodeP node = static_cast<NodeP> (new Node( male->at(i)->primitive_));
95 child->push_back( node );
96 child->at( i )->depth_ = male->at( i )->depth_;
97
98 }
99 // copy from female parent
100 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
101 NodeP node = static_cast<NodeP> (new Node( female->at( fIndex + i)->primitive_));
102 child->push_back( node );
103 }
104 // copy rest from male parent
105 for(uint i = mIndex + male->at( mIndex )->size_; i < mRange; i++) {
106 NodeP node = static_cast<NodeP> (new Node( male->at( i )->primitive_));
107 child->push_back( node );
108 }
109
110 // update node depths and subtree sizes
111 child->update();
112
113 return true;
114}
115
116}
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 initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
Definition: nodes.h:92