ECF 1.5
MutFunction.cpp
1#include "MutFunction.h"
2#include "Cartesian_genotype.h"
3#include "FunctionSet.h"
4namespace cartesian{
5
7 {
8 myGenotype_->registerParameter(state, "mut.func", (voidP) new double(0), ECF::DOUBLE);
9 }
10
11 bool MutateFunction::initialize(StateP state)
12 {
13 voidP sptr = myGenotype_->getParameterValue(state, "mut.func");
14 probability_ = *((double*)sptr.get());
15 return true;
16 }
17
18 bool MutateFunction::mutate(GenotypeP gene)
19 {
20 Cartesian* cartesian = (Cartesian*) gene.get();
21 const std::vector<FunctionP_basic>& vRef = cartesian->functionSet_->vActiveFunctions_;
22
23 //First a random output is chosen. A function that affects this output will be mutated.
24 uint whichOutput = cartesian->get_random_int(0, cartesian->nOutputs - 1);
25 std::vector<uint> trail = cartesian->getActiveTrail(whichOutput);
26 uint whoIsMutated = trail[cartesian->get_random_int(0, trail.size() - 1)];
27 uint oldFunctionId = cartesian->operator[](whoIsMutated).value;
28 uint oldNumOfArgs = vRef[oldFunctionId]->getNumOfArgs();
29
30 uint newFunctionId = cartesian->get_random_int(0, vRef.size() - 1);
31 while(newFunctionId == oldFunctionId) {
32 newFunctionId = cartesian->get_random_int(0, vRef.size() - 1);
33 }
34 uint newNumOfArgs = vRef[newFunctionId]->getNumOfArgs();
35
36 //Assign a new function to the node.
37 cartesian->operator[](whoIsMutated).value = newFunctionId;
38
39 //old and new function may not have the same amount of operators.
40 if(oldNumOfArgs < newNumOfArgs){
41 uint rowNumber = cartesian->getRowNumber(whoIsMutated);
42 while(oldNumOfArgs < newNumOfArgs) {
43 uint connection = cartesian->randomConnectionGenerator(rowNumber);
44 cartesian->operator[](whoIsMutated).inputConnections.push_back(connection);
45 oldNumOfArgs++;
46 }
47 }
48 else if(oldNumOfArgs > newNumOfArgs) {
49 while(oldNumOfArgs > newNumOfArgs) {
50 uint connection = cartesian->get_random_int(0, oldNumOfArgs - 1);
51 cartesian->operator[](whoIsMutated).inputConnections.erase(cartesian->operator[](whoIsMutated).inputConnections.begin() + connection);
52 oldNumOfArgs--;
53 }
54 }
55 return true;
56 }
57}
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
std::vector< uint > getActiveTrail(uint output_number)
Definition: Cartesian.cpp:251
uint nOutputs
number of final outputs
bool initialize(StateP state)
Initialize mutation operator. Called before first mutation operation.
Definition: MutFunction.cpp:11
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
Definition: MutFunction.cpp:18
void registerParameters(StateP state)
Register parameters with the system. Called before MutationOp::initialize.
Definition: MutFunction.cpp:6