ECF 1.5
PermutationCrsPMX.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.PMX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.PMX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsPMX::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 uint start = state_->getRandomizer()->getRandomInteger(p1->getSize());
30 uint end = state_->getRandomizer()->getRandomInteger((int) start, (int) p1->getSize() - 1);
31
32 std::map<int, int> mappings;
33
34 for(uint i = start; i < end; i++) {
35 ch->variables[i] = p1->variables[i];
36 mappings[p1->variables[i]] = p2->variables[i];
37 }
38
39 std::map<int, int>::iterator iter = mappings.begin();
40
41 for(uint i = 0; i < p1->getSize(); i++) {
42 //ako smo unutar podniza nemamo sto raditi sa tim elementima
43 if(i >= start && i < end)
44 continue;
45 //za pocetak probajmo s elementom iz drugog roditelja
46 int map = p2->variables[i];
47 //dok god se trenutni element koji želimo staviti na i-to mjesto
48 //djeteta vec u njemu nalazi trazimo dalje
49 do {
50 iter = mappings.find(map);
51 //ako se nalazi vec u podnizu probajmo s elementom koji je prije stajao na tom mjestu
52 if(iter != mappings.end()) {
53 map = iter->second;
54 }
55 else
56 break;
57 } while(true);
58 ch->variables[i] = map;
59 }
60
61 return true;
62}
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.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37