ECF 1.5
PermutationCrsCyclic2.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.cyclic2", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.cyclic2");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsCyclic2::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
30 std::map<int, int> index;
31 int j, num;
32
33 j = (int) state_->getRandomizer()->getRandomInteger(p1->getSize());
34
35 while (!index[j]) { //da li je ovo OK?
36 ch->variables[j] = p1->variables[j];
37 num = p2->variables[j];
38 index[j] = 1;
39
40 j = 0;
41 while ((j < (int) p1->getSize()) && (p1->variables[j] != num)) {
42 j++;
43 }
44 }
45
46 for (int i = 0; i < (int) p1->getSize(); i++) {
47 if (!index[i]) {
48 ch->variables[i] = p2->variables[i];
49 }
50 }
51
52 return true;
53}
54
55}
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.
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
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