ECF 1.5
MutFunctionSwap.cpp
1#include "MutFunctionSwap.h"
2#include "Cartesian_genotype.h"
3#include "FunctionSet.h"
4namespace cartesian{
5
7 {
8 myGenotype_->registerParameter(state, "mut.funcswap", (voidP) new double(0), ECF::DOUBLE);
9 }
10
12 {
13 voidP sptr = myGenotype_->getParameterValue(state, "mut.funcswap");
14 probability_ = *((double*)sptr.get());
15 return true;
16 }
17
18 bool MutateFunctionSwap::mutate(GenotypeP gene)
19 {
20 Cartesian* cartesian = (Cartesian*) gene.get();
21 const std::vector<FunctionP_basic>& vRef = cartesian->functionSet_->vActiveFunctions_;
22 //Generate two indexes of functions which will be swapped. Functions must have same number of operands.
23 uint firstIndex = cartesian->get_random_int(0, cartesian->size() - 1 - cartesian->nOutputs);
24 uint secondIndex = cartesian->get_random_int(0, cartesian->size() - 1 - cartesian->nOutputs);
25 uint firstFunction = cartesian->operator[](firstIndex).value;
26 uint secondFunction = cartesian->operator[](secondIndex).value;
27 uint firstNoOfArgs = vRef[firstFunction]->getNumOfArgs();
28 uint secondNoOfArgs = vRef[secondFunction]->getNumOfArgs();
29 while(firstNoOfArgs != secondNoOfArgs) {
30 secondIndex = cartesian->get_random_int(0, cartesian->size() - 1 - cartesian->nOutputs);
31 secondFunction = cartesian->operator[](secondIndex).value;
32 secondNoOfArgs = vRef[secondFunction]->getNumOfArgs();
33 }
34 cartesian->operator[](firstIndex).value = secondFunction;
35 cartesian->operator[](secondIndex).value = firstFunction;
36 return true;
37 }
38}
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 nOutputs
number of final outputs
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.