ECF 1.5
TreeCrxSimple_.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeCrxSimple.h"
4
5
7{
8 myGenotype_->registerParameter(state, "crx.simple", (voidP) new double(0), DOUBLE);
9}
10
11
12bool TreeCrxSimple::initialize(StateP state)
13{
14 voidP sptr = myGenotype_->getParameterValue(state, "crx.simple");
15 probability_ = *((double*)sptr.get());
16 return true;
17}
18
19
20// TODO: reimplement for efficiency
21bool TreeCrxSimple::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP ch)
22{
23 Tree* male = (Tree*) (gen1.get());
24 Tree* female = (Tree*) (gen2.get());
25 Tree* child = (Tree*) (ch.get());
26
27 uint mIndex, fIndex;
28 uint mRange, fRange;
29 uint mNodeDepth, fNodeDepth, fNodeDepthSize;
30 mRange = (uint) male->size();
31 fRange = (uint) female->size();
32
33 // check depth limitations
34 uint nTries = 0;
35 while(1) {
36 mIndex = state_->getRandomizer()->getRandomInteger(0 , mRange -1 );
37 fIndex = state_->getRandomizer()->getRandomInteger(0 , fRange -1 );
38 mNodeDepth = male->at( mIndex )->depth_;
39 fNodeDepth = female->at( fIndex )->depth_;
40
41 // find max depth
42 int maxDepth = fNodeDepth, depth;
43 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
44 depth = female->at( fIndex + i )->depth_;
45 maxDepth = depth > maxDepth ? depth : maxDepth;
46 }
47
48 fNodeDepthSize = maxDepth - fNodeDepth;
49 nTries++;
50
51 if( nTries > 4 || mNodeDepth + fNodeDepthSize <= male->maxDepth_ ) break;
52 }
53
54 if(nTries > 4 && mNodeDepth + fNodeDepthSize > male->maxDepth_) {
55 state_->getLogger()->log(5, "TreeCrxSimple not successful.");
56 return false;
57 }
58
59 // empty the child tree
60 child->clear();
61
62 // copy from male parent
63 child->maxDepth_ = male->maxDepth_;
64 child->minDepth_ = male->minDepth_;
65 child->startDepth_ = male->startDepth_;
66
67 // copy from male parent
68 for(uint i = 0; i < mIndex; i++) {
69 NodeP node = static_cast<NodeP> (new Node( male->at(i)->primitive_));
70 child->push_back( node );
71 child->at( i )->depth_ = male->at( i )->depth_;
72 }
73 // copy from female parent
74 for(uint i = 0; i < female->at( fIndex )->size_; i++) {
75 NodeP node = static_cast<NodeP> (new Node( female->at( fIndex + i)->primitive_));
76 child->push_back( node );
77 }
78 // copy rest from male parent
79 for(uint i = mIndex + male->at( mIndex )->size_; i < mRange; i++) {
80 NodeP node = static_cast<NodeP> (new Node( male->at( i )->primitive_));
81 child->push_back( node );
82 }
83
84 // update node depths and subtree sizes
85 child->update();
86
87 return true;
88}
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
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)
Definition: nodes.h:92