ECF 1.5
MutFunctionSilent.cpp
1#include <set>
2#include "MutFunctionSilent.h"
3#include "Cartesian_genotype.h"
4#include "FunctionSet.h"
5namespace cartesian{
6
8 {
9 myGenotype_->registerParameter(state, "mut.funcsilent", (voidP) new double(0), ECF::DOUBLE);
10 }
11
13 {
14 voidP sptr = myGenotype_->getParameterValue(state, "mut.funcsilent");
15 probability_ = *((double*)sptr.get());
16 return true;
17 }
18
19 bool MutateFunctionSilent::mutate(GenotypeP gene)
20 {
21 Cartesian* cartesian = (Cartesian*) gene.get();
22 const std::vector<FunctionP_basic>& vRef = cartesian->functionSet_->vActiveFunctions_;
23 std::set<uint> allActiveIndexes;
24 std::vector<std::vector<uint> > allTrails = cartesian->getActiveTrails();
25 for(uint i = 0; i < allTrails.size(); i++) {
26 for(uint j = 0; j < allTrails[i].size(); j++) {
27 allActiveIndexes.insert(allTrails[i][j]);
28 }
29 }
30 uint toBeMutated = cartesian->get_random_int(0, cartesian->size() - 1 -cartesian->nOutputs);
31 if(allActiveIndexes.size() == cartesian->size() - cartesian->nOutputs) {
32 return true;
33 }
34 while(allActiveIndexes.count(toBeMutated)) {
35 toBeMutated = cartesian->get_random_int(0, cartesian->size() - 1 -cartesian->nOutputs);
36 }
37 uint oldFunctionId = cartesian->operator[](toBeMutated).value;
38 uint oldNumOfArgs = vRef[oldFunctionId]->getNumOfArgs();
39 uint newFunctionId = cartesian->get_random_int(0, vRef.size() - 1);
40 while(newFunctionId == oldFunctionId) {
41 newFunctionId = cartesian->get_random_int(0, vRef.size() - 1);
42 }
43 uint newNumOfArgs = vRef[newFunctionId]->getNumOfArgs();
44 //Assign a new function to the node.
45 cartesian->operator[](toBeMutated).value = newFunctionId;
46
47 //old and new function may not have the same amount of operators.
48 if(oldNumOfArgs < newNumOfArgs){
49 uint rowNumber = cartesian->getRowNumber(toBeMutated);
50 while(oldNumOfArgs < newNumOfArgs) {
51 uint connection = cartesian->randomConnectionGenerator(rowNumber);
52 cartesian->operator[](toBeMutated).inputConnections.push_back(connection);
53 oldNumOfArgs++;
54 }
55 }
56 else if(oldNumOfArgs > newNumOfArgs) {
57 while(oldNumOfArgs > newNumOfArgs) {
58 uint connection = cartesian->get_random_int(0, oldNumOfArgs - 1);
59 cartesian->operator[](toBeMutated).inputConnections.erase(cartesian->operator[](toBeMutated).inputConnections.begin() + connection);
60 oldNumOfArgs--;
61 }
62 }
63 return true;
64 }
65}
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
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.
void registerParameters(StateP state)
Register parameters with the system. Called before MutationOp::initialize.