00001 #include "CartesianMutOnePoint.h"
00002 #include <cmath>
00003
00004 namespace cart{
00005
00006 void CartesianMutOnePoint::registerParameters(StateP state)
00007 {
00008
00009
00010 myGenotype_->registerParameter(state, "mut.onepoint.prob", (voidP) new double(0.001), ECF::DOUBLE);
00011 }
00012
00013
00014 bool CartesianMutOnePoint::initialize(StateP state)
00015 {
00016
00017
00018
00019
00020 voidP sptr = myGenotype_->getParameterValue(state, "mut.onepoint.prob");
00021 mutProb_ = *((double*)sptr.get());
00022
00023 useMutProb_ = false;
00024 if(myGenotype_->isParameterDefined(state, "mut.onepoint.prob"))
00025 useMutProb_ = true;
00026
00027 return true;
00028 }
00029
00030
00031 bool CartesianMutOnePoint::mutate(GenotypeP gene)
00032 {
00033 Cartesian* mut = (Cartesian*) (gene.get());
00034
00035
00036
00037 if (useMutProb_)
00038 {
00039 for (int i = 0; i < mut->size(); i++)
00040 {
00041 if (state_->getRandomizer()->getRandomInteger(0, 1) < mutProb_)
00042 {
00043 mutOneValue(mut, i);
00044 }
00045 }
00046 }
00047
00048 else
00049 {
00050 uint mutPoint = (uint)(state_->getRandomizer()->getRandomInteger(0, mut->size() - 1));
00051
00052 mutOneValue(mut, mutPoint);
00053 }
00054
00055 return true;
00056 }
00057
00058 void CartesianMutOnePoint::mutOneValue(Cartesian *mut, int mutPoint)
00059 {
00060
00061 if (mutPoint < (mut->getNumOfRows() * mut->getNumOfCols() * (mut->getNumOfInputConn() + 1)))
00062 {
00063
00064 if (((mutPoint + 1) % (mut->getNumOfInputConn() + 1) == 0))
00065 {
00066 mut->at(mutPoint) = mut->randFunction();
00067 }
00068
00069 else
00070 {
00071 double num = (double)mutPoint / (double)(mut->getNumOfRows() * (mut->getNumOfInputConn() + 1));
00072
00073 uint currCol = (uint)floor(num) + 1;
00074 mut->at(mutPoint) = mut->randInputConn(currCol - 1);
00075 }
00076 }
00077
00078 else
00079 {
00080 mut->at(mutPoint) = mut->randOutput();
00081 }
00082 }
00083
00084 }
00085