ECF 1.5
TreeCrxUniform.cpp
1#include "../ECF_base.h"
2#include "Tree.h"
3#include "TreeCrxUniform.h"
4
5
6namespace Tree
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.uniform", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.uniform");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool TreeCrxUniform::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 mRange, fRange;
30
31 mRange = (uint) male->size();
32 fRange = (uint) female->size();
33
34 // copy from male parent
35 child->clear();
36 child->maxDepth_ = male->maxDepth_;
37 child->minDepth_ = male->minDepth_;
38 child->startDepth_ = male->startDepth_;
39
40 for( uint iMale = 0, iFemale = 0, iChild=0; iMale < mRange && iFemale < fRange; ) {
41
42 // choose either male or female node from the common region
43 uint pickMale = state_->getRandomizer()->getRandomInteger(0 , 1);
44
45 if( male->at( iMale )->primitive_->getNumberOfArguments() == female->at( iFemale )->primitive_->getNumberOfArguments() ) {
46 // if the number of arguments is the same, choose randomly from either parent
47 if( pickMale ) {
48 NodeP node = static_cast<NodeP> (new Node( male->at( iMale )->primitive_));
49 child->push_back( node );
50 child->at( iChild )->depth_ = male->at( iMale )->depth_;
51 }
52 else {
53 NodeP node = static_cast<NodeP> (new Node( female->at( iFemale )->primitive_));
54 child->push_back( node );
55 child->at( iChild )->depth_ = female->at( iFemale )->depth_;
56 }
57 iMale++; iFemale++; iChild++;
58 }
59 else {
60 // if numbers of arguments differ, copy subtree to child
61 if(pickMale) {
62 for( uint i = 0; i < male->at( iMale )->size_; i++, iChild++ ) {
63 NodeP node = static_cast<NodeP> (new Node( male->at( iMale + i )->primitive_));
64 child->push_back( node );
65 child->at( iChild )->depth_ = male->at( iMale + i )->depth_;
66 }
67 }
68 else {
69 for( uint i = 0; i < female->at( iFemale )->size_; i++, iChild++ ) {
70 NodeP node = static_cast<NodeP> (new Node( female->at( iFemale + i )->primitive_));
71 child->push_back( node );
72 child->at( iChild )->depth_ = female->at( iFemale + i )->depth_;
73 }
74 }
75 // skip nodes not in common region
76 iMale += male->at( iMale )->size_;
77 iFemale += female->at( iFemale )->size_;
78 }
79 }
80
81 // update node depths and subtree sizes
82 child->update();
83
84 return true;
85}
86
87}
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