• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/tree/TreeMutGauss.cpp

00001 #include "../ECF_base.h"
00002 #include "Tree.h"
00003 #include "TreeMutGauss.h"
00004 #include <stdio.h>
00005 #include <time.h>
00006 
00007 #include "boost/random/normal_distribution.hpp"
00008 
00009 
00010 namespace Tree
00011 {
00012 
00013 void TreeMutGauss::registerParameters(StateP state)
00014 {
00015     myGenotype_->registerParameter(state, "mut.gauss", (voidP) new double(0), ECF::DOUBLE);
00016 }
00017 
00018 
00019 bool TreeMutGauss::initialize(StateP state)
00020 {
00021     voidP sptr = myGenotype_->getParameterValue(state, "mut.gauss");
00022     probability_ = *((double*)sptr.get());
00023 
00024     engine_.seed((uint) time(NULL));
00025     return true;
00026 }
00027 
00028 
00029 bool TreeMutGauss::mutate(GenotypeP gene)
00030 {
00031     Tree* tree = (Tree*) (gene.get());
00032     
00033     // try to select ERC node of type double
00034     uint iNode;
00035     uint tries = 0;
00036     std::string name;
00037     do {
00038         iNode = state_->getRandomizer()->getRandomInteger((int) tree->size());
00039         tries++;
00040     } while((name = tree->at(iNode)->primitive_->getName()).substr(0, 2) != DBL_PREFIX && tries < 4);
00041 
00042     if(name.substr(0, 2) != DBL_PREFIX) {
00043         ECF_LOG(state_, 5, "TreeMutGauss not successful.");
00044         return false;
00045     }
00046 
00047     double oldValue;
00048     PrimitiveP oldPrim = tree->at(iNode)->primitive_;
00049     tree->at(iNode)->primitive_->getValue(&oldValue);
00050     std::string oldName = tree->at(iNode)->primitive_->getName();
00051 
00052     // generate Gauss noise offset and add it
00053     // TODO: parametrize distribution!
00054     boost::normal_distribution<double> N(0, 1);
00055 
00056     // e.g. http://www.codepedia.com/1/CppBoostRandom
00057     // TODO: preserve state
00058     //boost::lagged_fibonacci607 engine(state_->getRandomizer()->getRandomInteger(100000) + 1);
00059 
00060     double offset = N.operator () <boost::lagged_fibonacci607>(engine_);
00061     double newValue = oldValue + offset;
00062 
00063     // change double ERC value and name
00064     std::stringstream ss;
00065     ss << newValue;
00066     std::string newName;
00067     ss >> newName;
00068     newName = DBL_PREFIX + newName;
00069 
00070     oldPrim->setName(newName);
00071     oldPrim->setValue(&newValue);
00072 
00073     // new ERCs aren't stored in the PrimitiveSet
00074 
00075     std::stringstream log;
00076     log << "TreeMutGauss successful (oldNode = " << oldName << ", newNode = " << newName << ")";
00077     ECF_LOG(state_, 5, log.str());
00078 
00079     return true;
00080 }
00081 
00082 }

Generated on Tue Nov 4 2014 13:04:31 for ECF by  doxygen 1.7.1