ECF 1.5
GEPChromosomeCrsGene.cpp
1#include "GepChromosomeCrsGene.h"
2namespace GEPChromosome{
4 {
5 myGenotype_->registerParameter(state, "crx.gene", (voidP) new double(0), ECF::DOUBLE);
6 }
7
8
10 {
11 voidP sptr = myGenotype_->getParameterValue(state, "crx.gene");
12 probability_ = *((double*)sptr.get());
13 return true;
14 }
15
16 bool GEPChromosomeCrsGene::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child){
17 // get the genotype from the parents and child
18 GEPChromosome* p1 = (GEPChromosome*)(gen1.get());
19 GEPChromosome* p2 = (GEPChromosome*)(gen2.get());
20 GEPChromosome* ch = (GEPChromosome*)(child.get());
21 // test whether gene crossover is viable (i.e., there is more than one gene)
22 if (p1->genes < 2){
23 ECF_LOG(state_, 5, "Gene crossover failed: chromosome consists of fewer than 2 genes");
24 return true;
25 }
26 ch->clear();
27 // select the gene number to be swapped
28 uint geneCrs = state_->getRandomizer()->getRandomInteger(0, (int)p1->genes);
29 uint geneLen = p1->geneLength;
30 ECF_LOG(state_, 5, "Performing Gene crossover in gene ("+ uint2str(geneCrs) + ")...");
31 // swap the selected gene between the parents
32 uint jStop;
33 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
34 case 0:
35 for (uint i = 0; i <= ch->genes; i++) {
36 jStop = (i == ch->genes) ? ch->linkHeadLength + ch->linkTailLength : geneLen;
37 for (uint j = 0; j < jStop; j++){
38 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node(i == geneCrs ? p2->at(i*geneLen + j) : p1->at(i*geneLen + j))));
39 }
40 }
41 break;
42 case 1:
43 for (uint i = 0; i <= ch->genes; i++) {
44 jStop = (i == ch->genes) ? ch->linkHeadLength + ch->linkTailLength : geneLen;
45 for (uint j = 0; j < jStop; j++){
46 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node(i == geneCrs ? p1->at(i*geneLen + j) : p2->at(i*geneLen + j))));
47 }
48 }
49 }
50
51 return true;
52 }
53}
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
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.
GEPChromosome class - implements genotype as a Gene Expression Programming chromosome.
Definition: GEPChromosome.h:33
Node base class (Tree genotype)
Definition: Node.h:20