ECF 1.5
MutIntoSilent.cpp
1#include <set>
2#include "MutIntoSilent.h"
3#include "Cartesian_genotype.h"
4#include "FunctionSet.h"
5namespace cartesian{
7 {
8 myGenotype_->registerParameter(state, "mut.tosilent", (voidP) new double(0), ECF::DOUBLE);
9 }
10
12 {
13 voidP sptr = myGenotype_->getParameterValue(state, "mut.tosilent");
14 probability_ = *((double*)sptr.get());
15 return true;
16 }
17
18 bool MutateIntoSilent::mutate(GenotypeP gene)
19 {
20 Cartesian* cartesian = (Cartesian*) gene.get();
21 //First random output is chosen. Trail which decides the output will be mutated.
22 uint whichOutput = cartesian->get_random_int(0, cartesian->nOutputs - 1);
23 std::vector<uint> activeTrail = cartesian->getActiveTrail(whichOutput);
24 uint whoIsMutated = activeTrail[cartesian->get_random_int(0, activeTrail.size() - 1)];
25 //Now, indexes of silent nodes are needed. Only silent nodes before whoIsMutated are needed.
26 std::set<uint> allActiveIndexes;
27 std::vector<std::vector<uint> > allTrails = cartesian->getActiveTrails();
28 for(uint i = 0; i < allTrails.size(); i++) {
29 for(uint j = 0; j < allTrails[i].size(); j++) {
30 if(allTrails[i][j] < whoIsMutated) {
31 allActiveIndexes.insert(allTrails[i][j]);
32 }
33 }
34 }
35 if(allTrails.empty()){
36 return true;
37 }
38 if(allActiveIndexes.size() == cartesian->size() - cartesian->nOutputs) {
39 return true;
40 }
41 uint rowNumber = cartesian->getRowNumber(whoIsMutated);
42 uint newConnection = cartesian->randomConnectionGenerator(rowNumber);
43 std::set<uint> allPossibleConnections = cartesian->allPossibleConnection(rowNumber);
44 std::set<uint> tempSet;
45 for(std::set<uint>::iterator it = allPossibleConnections.begin(); it != allPossibleConnections.end(); it++) {
46 if(allActiveIndexes.count(*it) > 0) {
47 continue;
48 }
49 else {
50 tempSet.insert(*it);
51 }
52 }
53 allPossibleConnections = tempSet;
54 if(allPossibleConnections.empty()) {
55 return true;
56 }
57 while(allActiveIndexes.count(newConnection)) {
58 newConnection = cartesian->randomConnectionGenerator(rowNumber);
59 }
60 for(uint i = 0; i < activeTrail.size(); i++) {
61 CartesianGene& cg = cartesian->operator[](activeTrail[i]);
62 if(activeTrail[i] + cartesian->nInputs > newConnection) {
63 for(uint j = 0; j < cg.inputConnections.size(); j++) {
64 if(cg.inputConnections[j] == whoIsMutated) {
65 cg.inputConnections[j] = newConnection;
66 }
67 }
68 }
69 }
70 return true;
71 }
72}
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 nInputs
total number of inputs (including constants)
uint randomConnectionGenerator(uint rowNumber)
Definition: Cartesian.cpp:213
std::vector< uint > getActiveTrail(uint output_number)
Definition: Cartesian.cpp:251
uint nOutputs
number of final outputs
std::vector< std::vector< uint > > getActiveTrails()
Definition: Cartesian.cpp:275
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
void registerParameters(StateP state)
Register parameters with the system. Called before MutationOp::initialize.
bool initialize(StateP state)
Initialize mutation operator. Called before first mutation operation.