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