ECF 1.5
PermutationMutInv.cpp
1#include "../ECF_base.h"
2#include "Permutation.h"
3
4
5namespace Permutation
6{
7
9{
10 myGenotype_->registerParameter(state, "mut.inv", (voidP) new double(0), ECF::DOUBLE);
11}
12
13
15{
16 voidP sptr = myGenotype_->getParameterValue(state, "mut.inv");
17 probability_ = *((double*)sptr.get());
18 return true;
19}
20
21
22bool PermutationMutInv::mutate(GenotypeP gene)
23{
24 Permutation* perm = (Permutation*) (gene.get());
25
26 //najviše možemo uzeti predzandnji element kao početak podniza
27 int ind1 = state_->getRandomizer()->getRandomInteger(perm->getSize() - 1);
28 int ind2 = state_->getRandomizer()->getRandomInteger(ind1, (int) perm->getSize() - 1);
29 int temp = perm->variables[ind2];
30 int distance = ind2 -ind1 + 1;
31
32 for(int i = 0; i < distance / 2; i++) {
33 temp = perm->variables[ind1 + i];
34 perm->variables[ind1 + i] = perm->variables[ind2 - i];
35 perm->variables[ind2 - i] = temp;
36 }
37
38 return true;
39}
40
41}
double probability_
probability of usage of this mutation operator
Definition: Mutation.h:40
GenotypeP myGenotype_
pointer to the Genotype that defines this MutationOp
Definition: Mutation.h:41
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!