ECF 1.5
CartesianMutOnePoint.cpp
1#include "CartesianMutOnePoint.h"
2#include <cmath>
3
4namespace cart{
5
7{
8 //TODO: ako ce se jednog dana koristiti vise operatora
9 //myGenotype_->registerParameter(state, "mut.onepoint", (voidP) new double(0), DOUBLE);
10 myGenotype_->registerParameter(state, "mut.onepoint.prob", (voidP) new double(0.001), ECF::DOUBLE);
11}
12
13
15{
16 //TODO: ako ce se jednog dana koristiti vise operatora
17 //voidP sptr = myGenotype_->getParameterValue(state, "mut.onepoint");
18 //probability_ = *((double*)sptr.get());
19
20 voidP sptr = myGenotype_->getParameterValue(state, "mut.onepoint.prob");
21 mutProb_ = *((double*)sptr.get());
22
23 useMutProb_ = false;
24 if(myGenotype_->isParameterDefined(state, "mut.onepoint.prob"))
25 useMutProb_ = true;
26
27 return true;
28}
29
30
31bool CartesianMutOnePoint::mutate(GenotypeP gene)
32{
33 Cartesian* mut = (Cartesian*) (gene.get());
34
35 //if mutation probability is used, then choose values in genotpye to be mutated depending on the
36 //predefined mutation probability
37 if (useMutProb_)
38 {
39 for (int i = 0; i < mut->size(); i++)
40 {
41 if (state_->getRandomizer()->getRandomInteger(0, 1) < mutProb_)
42 {
43 mutOneValue(mut, i);
44 }
45 }
46 }
47 //if mutation probability isn't used, then randomly choose one value in genotype to be mutated
48 else
49 {
50 uint mutPoint = (uint)(state_->getRandomizer()->getRandomInteger(0, mut->size() - 1));
51
52 mutOneValue(mut, mutPoint);
53 }
54
55 return true;
56}
57
58void CartesianMutOnePoint::mutOneValue(Cartesian *mut, int mutPoint)
59{
60 //value chosen to be mutated in either input connection or function token
61 if (mutPoint < (mut->getNumOfRows() * mut->getNumOfCols() * (mut->getNumOfInputConn() + 1)))
62 {
63 //chosen value represents function token
64 if (((mutPoint + 1) % (mut->getNumOfInputConn() + 1) == 0))
65 {
66 mut->at(mutPoint) = mut->randFunction();
67 }
68 //chosen value represents input connection
69 else
70 {
71 double num = (double)mutPoint / (double)(mut->getNumOfRows() * (mut->getNumOfInputConn() + 1));
72 //column in which input connection of current node is placed
73 uint currCol = (uint)floor(num) + 1;
74 mut->at(mutPoint) = mut->randInputConn(currCol - 1);
75 }
76 }
77 //chosen value represents output
78 else
79 {
80 mut->at(mutPoint) = mut->randOutput();
81 }
82}
83
84}
85
GenotypeP myGenotype_
pointer to the Genotype that defines this MutationOp
Definition: Mutation.h:41
uint getNumOfRows()
Definition: Cartesian.cpp:400
uint randInputConn(uint currCol)
Definition: Cartesian.cpp:333
uint randFunction()
Definition: Cartesian.cpp:353
uint getNumOfInputConn()
Definition: Cartesian.cpp:390
uint getNumOfCols()
Definition: Cartesian.cpp:405
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.