ECF 1.7
CartesianMutNonSilent.cpp
1#include "CartesianMutNonSilent.h"
2#include "Cartesian_c.h"
3#include <cmath>
4#include <vector>
5//#include <algorithm>
6
7namespace Cartesian
8{
10 {
11 myGenotype_->registerParameter(state, "mut.nonsilent", (voidP) new double(0), ECF::DOUBLE);
12 }
13
14
16 {
17 voidP sptr = myGenotype_->getParameterValue(state, "mut.nonsilent");
18 probability_ = *((double*)sptr.get());
19 return true;
20 }
21
22
23 bool CartesianMutNonSilent::mutate(GenotypeP gene)
24 {
25 Cartesian* cart = (Cartesian*)(gene.get());
26 RandomizerP randP = cart->state_->getRandomizer();
27 const uint nOutputs = cart->nOutputs_;
28 const uint nRows = cart->nRows_;
29 const uint nCols = cart->nCols_;
30 const uint nInputs = cart->nInputs_;
31 const uint nLevelsBack = cart->nLevelsBack_;
32
33 std::vector<uint> activeNodes;
34 cart->getActiveFunctionNodes(activeNodes);
35
36 // if all function nodes are active
37 if (activeNodes.size() == (nRows * nCols))
38 return false;
39
40 std::vector<uint> silentNodes;
41 uint iActive = 0;
42 for(uint i = nInputs; i < cart->nodes_.size(); i++)
43 if (iActive < activeNodes.size() && activeNodes[iActive] == i)
44 iActive++;
45 else
46 silentNodes.push_back(i);
47
48 // choose a silent node that should be mutated into active
49 uint silentNodeIndex = randP->getRandomInteger((uint) silentNodes.size());
50 uint silentNode = silentNodes[silentNodeIndex];
51 uint silentNodeColumn = (silentNode - nInputs) / nRows;
52
53 // index of first node in the next column
54 uint nextColumnNode = (silentNodeColumn + 1) * nRows + nInputs;
55
56 // find all active nodes that could have silentNode as an input -> validNodes
57 // validNodes also contain output indexes
58 std::vector<uint> validNodes;
59 for (uint node : activeNodes) {
60 uint nodeColumn = (node - nInputs) / nRows;
61 if (node >= nextColumnNode && (nodeColumn - silentNodeColumn) <= nLevelsBack)
62 validNodes.push_back(node);
63 }
64
65 // add indexes that represent outputs
66 for (uint i = 0; i < nOutputs; i++)
67 validNodes.push_back(i + (uint) cart->nodes_.size());
68
69 uint mutateNodeIndex = randP->getRandomInteger((uint) validNodes.size());
70 uint mutateNode = validNodes[mutateNodeIndex];
71
72 // if mutateNode is output
73 if (mutateNode >= cart->nodes_.size()) {
74 cart->outputs_[mutateNode - cart->nodes_.size()] = silentNode;
75 } else {
76 // choose random input of the node that will be mutated
77 uint input = randP->getRandomInteger(cart->nodes_[mutateNode].primitive_->getNumberOfArguments());
78 cart->nodes_[mutateNode].arguments_[input] = silentNode;
79 }
80
81 return true;
82 }
83
84}
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
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