ECF 1.5
PermutationCrsSPX.cpp
1#include "../ECF_base.h"
2#include "Permutation.h"
3#include <map>
4
5
6namespace Permutation
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.SPX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.SPX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsSPX::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
24{
25 Permutation* p1 = (Permutation*) (gen1.get());
26 Permutation* p2 = (Permutation*) (gen2.get());
27
28 // uzmimo radije smart pointer, da ne moramo rucno brisati visak
29 PermutationP ch = boost::static_pointer_cast<Permutation> (child);
30
31 // stvori kopiju genotipa
32 PermutationP ch2(ch->copy());
33 // dohvati jedinku dijete
34 IndividualP myInd = state_->getContext()->child;
35 // dohvati redni broj genotipa
36 uint gId = child->getGenotypeId();
37
38 // definiraj novi ch i ch2
39
40 //Make ch clone of parent1, ch2 clone of parent2
41 int capacity = (int) ch->getSize();
42 for(int i = 0; i < capacity; i++) {
43 ch->variables[i] = p1->variables[i];
44 ch2->variables[i] = p2->variables[i];
45 }
46
47 // Pick two different positions, ensure pos1 < pos2:
48 int pos1 = state_->getRandomizer()->getRandomInteger(capacity);
49 int pos2 = state_->getRandomizer()->getRandomInteger(capacity);
50 if(pos1==pos2) {
51 if(pos1==0) {
52 pos2++;
53 } else {
54 pos1--;
55 }
56 } else if(pos2<pos1) {
57 int t = pos1;
58 pos1 = pos2;
59 pos2 = t;
60 }
61
62 // Swap selected element in first child
63 int elem1 = p1->variables[pos1];
64 int elem2 = p1->variables[pos2];
65 ch->variables[pos1] = elem2;
66 ch->variables[pos2] = elem1;
67
68 // Find locations of selected elements in second child:
69 for(int i = 0; i < capacity; i++) {
70 if(p2->variables[i]==elem1) {
71 pos1 = i;
72 break;
73 }
74 }
75 for(int i = 0; i < capacity; i++) {
76 if(p2->variables[i]==elem2) {
77 pos2 = i;
78 break;
79 }
80 }
81
82 // Swap selected element in second child
83 ch2->variables[pos1] = elem2;
84 ch2->variables[pos2] = elem1;
85
86 FitnessP chFitness, ch2Fitness;
87 // evaluiraj dijete uz ch
88 state_->getAlgorithm()->evaluate(myInd);
89 chFitness = myInd->fitness;
90
91 // umetni ch2 u jedinku dijete
92 myInd->at(gId) = (GenotypeP) ch2;
93 // 'rucno' smo promijenili jedinku, pa to oznaci (za svaki slucaj...)
94 myInd->fitness->setInvalid();
95 // evaluiraj dijete uz ch2
96 state_->getAlgorithm()->evaluate(myInd);
97 ch2Fitness = myInd->fitness;
98
99 // ako je ch bio bolji, trebamo ga vratiti
100 if(chFitness->isBetterThan(ch2Fitness)) {
101 myInd->at(gId) = (GenotypeP) ch;
102 // ne zaboravimo fitness! (da se ne evaluira ponovo)
103 myInd->fitness = chFitness;
104 }
105
106 return true;
107}
108
109}
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 initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37