1#include "../ECF_base.h"
3#include "TreeCrxSimple.h"
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);
18 voidP sptr =
myGenotype_->getParameterValue(state,
"crx.simple");
20 sptr =
myGenotype_->getParameterValue(state,
"crx.simple.functionprob");
21 funcChoiceProb_ = *((
double*)sptr.get());
35 uint mNodeDepth, fNodeDepth, fNodeDepthSize;
37 mRange = (uint) male->size();
38 fRange = (uint) female->size();
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 );
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 );
56 if(state_->getRandomizer()->getRandomDouble() > funcChoiceProb_ || maleNonLeafIndexes.empty()) {
57 uint randomLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (
int) maleLeafIndexes.size() - 1);
58 mIndex = maleLeafIndexes [ randomLeafIndex ];
61 uint randomNonLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (
int) maleNonLeafIndexes.size() - 1);
62 mIndex = maleNonLeafIndexes [ randomNonLeafIndex ];
66 if(state_->getRandomizer()->getRandomDouble() > funcChoiceProb_ || femaleNonLeafIndexes.empty()) {
67 uint randomLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (
int) femaleLeafIndexes.size() - 1);
68 fIndex = femaleLeafIndexes [ randomLeafIndex ];
71 uint randomNonLeafIndex = state_->getRandomizer()->getRandomInteger(0 , (
int) femaleNonLeafIndexes.size() - 1);
72 fIndex = femaleNonLeafIndexes [ randomNonLeafIndex ];
75 mNodeDepth = male->at( mIndex )->depth_;
76 fNodeDepth = female->at( fIndex )->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;
85 fNodeDepthSize = maxDepth - fNodeDepth;
88 if(nTries > 4 || mNodeDepth + fNodeDepthSize <= male->maxDepth_ )
break;
91 if(nTries > 4 && mNodeDepth + fNodeDepthSize > male->maxDepth_) {
92 ECF_LOG(state_, 5,
"TreeCrxSimple not successful.");
99 child->maxDepth_ = male->maxDepth_;
100 child->minDepth_ = male->minDepth_;
101 child->startDepth_ = male->startDepth_;
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_;
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 );
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 );
double probability_
probability of usage of this crossover operator
GenotypeP myGenotype_
pointer to the Genotype that defines this CrossoverOp
Node base class (Tree genotype)
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.