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