ECF 1.5
PermutationCrsOBX.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.OBX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.OBX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsOBX::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 std::vector<int> selectedNum;
30 std::map<int, int> numSelected;
31
32 int index, selectedIndex, currentIndex;
33
34 int rand = state_->getRandomizer()->getRandomInteger(p1->getSize()+1);
35
36 selectedNum.resize(rand);
37
38 for (int i = 0; i < (int) selectedNum.size(); i++) { // izaberi brojeve za polje
39 index = 0;
40 while (numSelected[p1->variables[index]]) { // naši prvi ispravni index
41 index++;
42 }
43
44 selectedIndex = state_->getRandomizer()->getRandomInteger(p1->getSize()- i);
45 currentIndex = 0;
46 while ((index < (int) p1->getSize()) && (currentIndex != selectedIndex)) { // naši izabrani broj
47 index++;
48 if (!numSelected[p1->variables[index]]) {
49 currentIndex++;
50 }
51 }
52 numSelected[p1->variables[index]] = 1;
53 }
54
55 index = 0;
56 for (int i = 0; i < (int) p1->getSize(); i++) { // kopiraj selektirane brojeve u polje
57 if (numSelected[p1->variables[i]]) {
58 selectedNum[index] = p1->variables[i];
59 index++;
60 }
61 }
62
63 index = 0;
64 for (int i = 0; i < (int) p1->getSize(); i++) { // kopiraj ostatak druge permutacije i selektiranih brojeva u poretku prve permutacije
65 if (numSelected[p2->variables[i]]) {
66 ch->variables[i] = selectedNum[index];
67 index++;
68 } else {
69 ch->variables[i] = p2->variables[i];
70 }
71 }
72
73 return true;
74}
75
76}
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 mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
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