ECF 1.5
MutConnectionSilent.cpp
1#include <set>
2#include "MutConnectionSilent.h"
3#include "Cartesian_genotype.h"
4#include "FunctionSet.h"
5namespace cartesian{
7 {
8 myGenotype_->registerParameter(state, "mut.connectionsilent", (voidP) new double(0), ECF::DOUBLE);
9 }
10
12 {
13 voidP sptr = myGenotype_->getParameterValue(state, "mut.connectionsilent");
14 probability_ = *((double*)sptr.get());
15 return true;
16 }
17
18 bool MutateConnectionSilent::mutate(GenotypeP gene)
19 {
20 Cartesian* cartesian = (Cartesian*) gene.get();
21 const std::vector<FunctionP_basic>& vRef = cartesian->functionSet_->vActiveFunctions_;
22 std::set<uint> allActiveIndexes;
23 std::vector<std::vector<uint> > allTrails = cartesian->getActiveTrails();
24 for(uint i = 0; i < allTrails.size(); i++) {
25 for(uint j = 0; j < allTrails[i].size(); j++) {
26 allActiveIndexes.insert(allTrails[i][j]);
27 }
28 }
29 uint toBeMutated = cartesian->get_random_int(0, cartesian->size() - 1 - cartesian->nOutputs);
30 if(allActiveIndexes.size() == cartesian->size() - cartesian->nOutputs) {
31 return true;
32 }
33 while(allActiveIndexes.count(toBeMutated)) {
34 toBeMutated = cartesian->get_random_int(0, cartesian->size() - 1 - cartesian->nOutputs);
35 }
36 uint oldFunctionId = cartesian->operator[](toBeMutated).value;
37 uint rowNumber = cartesian->getRowNumber(toBeMutated);
38 uint oldConnection = cartesian->get_random_int(0, vRef[oldFunctionId]->getNumOfArgs() - 1);
39 uint newConnection = cartesian->randomConnectionGenerator(rowNumber);
40 cartesian->operator[](toBeMutated).inputConnections[oldConnection] = newConnection;
41 return true;
42 }
43}
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
uint randomConnectionGenerator(uint rowNumber)
Definition: Cartesian.cpp:213
uint nOutputs
number of final outputs
std::vector< std::vector< uint > > getActiveTrails()
Definition: Cartesian.cpp:275
void registerParameters(StateP state)
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!
bool initialize(StateP state)
Initialize mutation operator. Called before first mutation operation.