ECF 1.7
CartesianMutOnePoint.cpp
1#include "CartesianMutOnePoint.h"
2#include "Cartesian_c.h"
3#include "FunctionSet.h"
4#include <cmath>
5#include <algorithm>
6
7namespace Cartesian
8{
9
11 {
12 myGenotype_->registerParameter(state, "mut.onepoint", (voidP) new double(0), ECF::DOUBLE);
13 }
14
15
17 {
18 voidP sptr = myGenotype_->getParameterValue(state, "mut.onepoint");
19 probability_ = *((double*)sptr.get());
20 return true;
21 }
22
23
24 bool CartesianMutOnePoint::mutate(GenotypeP gene)
25 {
26 Cartesian* cart = (Cartesian*)(gene.get());
27 RandomizerP rand = cart->state_->getRandomizer();
28 const uint size = cart->getGenomeSize();
29 const uint nOutputs = cart->nOutputs_;
30 const uint nRows = cart->nRows_;
31 const uint nCols = cart->nCols_;
32 const uint nFunctions = cart->nFunctions_;
33 const uint nInputs = cart->nInputs_;
34 uint randomGeneIndex;
35
36 // select random function or output node
37 randomGeneIndex = nInputs + rand->getRandomInteger(size);
38
39 if (randomGeneIndex < (size + nInputs - nOutputs)) {
40 // Function node
41 uint iColumn = (randomGeneIndex - nInputs) / nRows;
42 if (rand->getRandomInteger(2) == 0) {
43 // mutate function
44 uint currentNArgs = cart->nodes_[randomGeneIndex].primitive_->getNumberOfArguments();
45 int functionID = rand->getRandomInteger(nFunctions);
46 Function* func = cart->functionSet_->vFunctions[functionID].get();
47 cart->nodes_[randomGeneIndex].setPrimitive(cart->functionSet_->vFunctions[functionID]);
48
49 // generate random arguments if needed
50 uint newNArgs = func->getNumberOfArguments();
51 cart->nodes_[randomGeneIndex].arguments_.resize(func->getNumberOfArguments());
52 if (newNArgs > currentNArgs) {
53 for (uint k = currentNArgs; k < newNArgs; k++) {
54 uint iArgument = cart->randomNodeInputConnection(iColumn);
55 cart->nodes_[randomGeneIndex].arguments_[k] = iArgument;
56 }
57 }
58 }
59 else {
60 // mutate one of the arguments
61 uint iArg = rand->getRandomInteger(cart->nodes_[randomGeneIndex].primitive_->getNumberOfArguments());
62 cart->nodes_[randomGeneIndex].arguments_[iArg] = cart->randomNodeInputConnection(iColumn);
63 }
64 }
65 else {
66 // Output gene
67 cart->outputs_[randomGeneIndex - cart->nodes_.size()] = cart->randomOutputConnection();
68 }
69
70 //std::cout << randomGeneIndex << "\n";
71
72 return true;
73 }
74
75}
76
77
78
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
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