ECF 1.5
GEPChromosomeCrsTwoPoint.cpp
1#include "GEPChromosomeCrsTwoPoint.h"
2namespace GEPChromosome{
4 {
5 myGenotype_->registerParameter(state, "crx.twopoint", (voidP) new double(0), ECF::DOUBLE);
6 }
7
8
10 {
11 voidP sptr = myGenotype_->getParameterValue(state, "crx.twopoint");
12 probability_ = *((double*)sptr.get());
13 return true;
14 }
15
16 bool GEPChromosomeCrsTwoPoint::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child){
17 // get the genotypes from the parents and child
18 GEPChromosome* p1 = (GEPChromosome*)(gen1.get());
19 GEPChromosome* p2 = (GEPChromosome*)(gen2.get());
20 GEPChromosome* ch = (GEPChromosome*)(child.get());
21 ch->clear();
22 // select two points between which to perform the crossover
23 uint bitCrs = state_->getRandomizer()->getRandomInteger(0, (int)p1->size() - 1);
24 uint bitCrsSecond, tmp;
25 do
26 {
27 bitCrsSecond = state_->getRandomizer()->getRandomInteger(p1->size());
28 } while (bitCrs == bitCrsSecond);
29
30 if (bitCrs>bitCrsSecond)
31 {
32 tmp = bitCrs;
33 bitCrs = bitCrsSecond;
34 bitCrsSecond = tmp;
35 }
36 ECF_LOG(state_, 5, "Performing Two-Pt crossover at nodes (" + uint2str(bitCrs) + ") and (" + uint2str(bitCrsSecond) + ")...");
37 // swap the region delimited by the chosen points between the parents
38 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
39 case 0:
40 for (uint i = 0; i < bitCrs; i++) {
41 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node((p1->at(i)))));
42 }
43 for (uint i = bitCrs; i < bitCrsSecond; i++) {
44 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node((p2->at(i)))));
45 }
46 for (uint i = bitCrsSecond; i < p2->size(); i++) {
47 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node((p1->at(i)))));
48 }
49 break;
50 case 1:
51 for (uint i = 0; i < bitCrs; i++) {
52 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node((p2->at(i)))));
53 }
54 for (uint i = bitCrs; i < bitCrsSecond; i++) {
55 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node((p1->at(i)))));
56 }
57 for (uint i = bitCrsSecond; i < p1->size(); i++) {
58 ch->push_back(static_cast<Tree::NodeP> (new Tree::Node((p2->at(i)))));
59 }
60 }
61
62 return true;
63 }
64}
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 initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
GEPChromosome class - implements genotype as a Gene Expression Programming chromosome.
Definition: GEPChromosome.h:33
Node base class (Tree genotype)
Definition: Node.h:20