ECF 1.5
PermutationCrsOX.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.OX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.OX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsOX::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
24{
25 Permutation* p1 = (Permutation*) (gen1.get());
26 Permutation* p2 = (Permutation*) (gen2.get());
27 Permutation* ch = (Permutation*) (child.get());
28
29 int start = state_->getRandomizer()->getRandomInteger(p1->getSize());
30 int end = state_->getRandomizer()->getRandomInteger(start, (int) p1->getSize() - 1);
31
32 std::map<int, int> subSet;
33
34 int ind1 = state_->getRandomizer()->getRandomInteger(p1->getSize() - 1);
35 int ind2 = state_->getRandomizer()->getRandomInteger(ind1, (int) p1->getSize() - 1);
36
37 // copy subvector into child
38 for(int i = ind1; i <= ind2; i++) {
39 ch->variables[i] = p1->variables[i];
40 subSet[p1->variables[i]] = 0;
41 }
42 int indexChild = (ind2 + 1) % p1->getSize();
43 std::map<int, int>::iterator iter = subSet.begin();
44
45 // krecemo od indeksa za jedan vise od indeksa zadnjeg elementa podniza
46 // i kopiramo iz drugog roditelja u dijete
47 // ako naidjemo na element koji se vec nalazi u podnizu idemo dalje
48 // postupak se zaustavlja kada popunimo cijelo dijete
49 do {
50 ind2 = (++ind2) % p1->getSize();
51 if(subSet.find(p2->variables[ind2]) == subSet.end()) {
52 ch->variables[indexChild] = p2->variables[ind2];
53 indexChild = (++indexChild) % p1->getSize();
54 }
55 } while(indexChild != ind1);
56
57 return true;
58}
59
60}
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.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37