ECF 1.5
BinaryCrsShuffle.cpp
1#include "../ECF_base.h"
2#include "Binary.h"
3#include <algorithm>
4
5namespace Binary
6{
7
9{
10 myGenotype_->registerParameter(state, "crx.shuffle", (voidP) new double(0), ECF::DOUBLE);
11}
12
13
15{
16 voidP sptr = myGenotype_->getParameterValue(state, "crx.shuffle");
17 probability_ = *((double*)sptr.get());
18 return true;
19}
20
21bool BinaryCrsShuffle::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
22{
23 Binary* p1 = (Binary*) (gen1.get());
24 Binary* p2 = (Binary*) (gen2.get());
25 Binary* ch = (Binary*) (child.get());
26
27 Binary* p1copy = (Binary*) (gen1->copy());
28 Binary* p2copy = (Binary*) (gen2->copy());
29 Binary* chcopy = (Binary*) (child->copy());
30
31 //uint rand1, rand2;
32 //bool temp;
33 typedef std::vector<uint> v_uint;
34
35 //v_uint pFrom;
36 //v_uint pTo;
37
38 v_uint mapiranje;
39
40 for (uint i = 0; i < p1->getNumBits(); i++) { //this is initial arrangement
41 mapiranje.push_back(i);
42 }
43
44 std::random_shuffle(mapiranje.begin(), mapiranje.end()); //jel moze ovo
45
46 uint bitCrs = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
47
48
49 //sada citam redom sto pise u vectoru mapa i u kopije roditelja spremam na novu poziciju vrijednosti iz originalnih roditelja
50
51 for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
52 //shuffle parents
53 for (uint i = 0; i < p1->getNumBits(); i++) {
54
55 p1copy->variables[dimension][i] = p1->variables[dimension][mapiranje[i]];
56 p2copy->variables[dimension][i] = p2->variables[dimension][mapiranje[i]];
57 }
58
59 //shuffle done, now just one-point crossover
60 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
61 case 0: for (uint i = 0; i < bitCrs; i++) {
62 chcopy->variables[dimension][i] = p1copy->variables[dimension][i];
63 }
64 for (uint i = bitCrs; i < p2->getNumBits(); i++) {
65 chcopy->variables[dimension][i] = p2copy->variables[dimension][i];
66 }
67 break;
68 case 1: for (uint i = 0; i < bitCrs; i++) {
69 chcopy->variables[dimension][i] = p2copy->variables[dimension][i];
70 }
71 for (uint i = bitCrs; i < p1->getNumBits(); i++) {
72 chcopy->variables[dimension][i] = p1copy->variables[dimension][i];
73 }
74 }
75 //unshuffle child
76 for (uint i = 0; i < p1->getNumBits(); i++) {
77 ch->variables[dimension][mapiranje[i]] = chcopy->variables[dimension][i];
78 }
79 }
80
81 // update real domain representation
82 ch->update();
83
84 delete p1copy;
85 delete p2copy;
86 delete chcopy;
87
88 return true;
89}
90
91}
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.
Binary class - implements genotype as a vector of binary coded real values with variable interval and...
Definition: Binary.h:38
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