ECF 1.7
CartesianMutSilent.cpp
1#include "CartesianMutSilent.h"
2#include "Cartesian_c.h"
3#include <cmath>
4#include <vector>
5#include <algorithm>
6
7namespace Cartesian
8{
9
11 {
12 myGenotype_->registerParameter(state, "mut.silent", (voidP) new double(0), ECF::DOUBLE);
13 }
14
15
17 {
18 voidP sptr = myGenotype_->getParameterValue(state, "mut.silent");
19 probability_ = *((double*)sptr.get());
20 return true;
21 }
22
23
24 bool CartesianMutSilent::mutate(GenotypeP gene)
25 {
26 Cartesian* cart = (Cartesian*)(gene.get());
27 RandomizerP randP = cart->state_->getRandomizer();
28 const uint nConstants = cart->nConstants_;
29 const uint nOutputs = cart->nOutputs_;
30 const uint nVariables = cart->nVariables_;
31 const uint nRows = cart->nRows_;
32 const uint nCols = cart->nCols_;
33 const uint nLevelsBack = cart->nLevelsBack_;
34 const uint maxArity = cart->maxArity_;
35 const uint nInputs = cart->nInputs_;
36
37 std::vector<uint> activeNodes;
38 cart->getActiveFunctionNodes(activeNodes);
39
40 // If all nodes are active
41 if (activeNodes.size() == nRows * nCols)
42 return true;
43
44 std::vector<uint> allNodes;
45 for (uint i = 0; i < nRows * nCols; i++) {
46 allNodes.push_back(i + nInputs);
47 }
48
49 std::vector<uint> silentNodes;
50 std::sort(activeNodes.begin(), activeNodes.end());
51 set_difference(allNodes.begin(), allNodes.end(), activeNodes.begin(), activeNodes.end(), inserter(silentNodes, silentNodes.begin()));
52
53 // Choose silent node that will be mutated
54 uint silentNodeIndex = randP->getRandomInteger(0, silentNodes.size() - 1);
55 uint silentNode = silentNodes.at(silentNodeIndex);
56
57 uint nodeColumn = (silentNode - nInputs) / nRows;
58 int minColumn = nodeColumn - nLevelsBack;
59 uint lowerBound = nInputs + minColumn * nRows;
60 if (minColumn < 0) lowerBound = 0;
61 uint upperBound = nInputs + nodeColumn * nRows;
62 uint newSilentNodeInput = randP->getRandomInteger(lowerBound, upperBound - 1);
63
64 // Choose random input of silent node that will be mutated
65 uint inputIndex = randP->getRandomInteger(1, maxArity);
66 // Find the right index of node in genotype
67 uint genotypeIndex = (silentNode - nInputs) * (maxArity + 1);
68
69 cart->at(genotypeIndex + inputIndex) = newSilentNodeInput;
70
71 return true;
72 }
73
74}
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
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