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

D:/Projekt/ECF_trunk/ECF/floatingpoint/FloatingPointCrsBga.cpp

00001 #include "../ECF_base.h"
00002 #include "FloatingPoint.h"
00003 #include <math.h>
00004 
00005 
00006 namespace FloatingPoint
00007 {
00008 
00009 void FloatingPointCrsBga::registerParameters(StateP state)
00010 {
00011     myGenotype_->registerParameter(state, "crx.bga", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013 
00014 
00015 bool FloatingPointCrsBga::initialize(StateP state)
00016 {
00017     voidP sptr = myGenotype_->getParameterValue(state, "crx.bga");
00018     probability_ = *((double*)sptr.get());
00019 
00020     return true;
00021 }
00022 
00023 
00024 bool FloatingPointCrsBga::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00025 {
00026     FloatingPoint* p1 = (FloatingPoint*) (gen1.get());
00027     FloatingPoint* p2 = (FloatingPoint*) (gen2.get());
00028     FloatingPoint* ch = (FloatingPoint*) (child.get());
00029 
00030     int a;
00031     uint size = (uint) p1->realValue.size();
00032     // upper - lower bound
00033     double range = 0.5 * (p1->getUBound() - p1->getLBound());
00034     double gama = 0, lambda = 0, b;
00035 
00036     for (int i = 0; i <= 15; i++) {
00037         a = state_->getRandomizer()->getRandomInteger(1, 16);
00038         // 1/16 probability for 1 = 1
00039         if (a == 16)
00040             a = 1;
00041         else
00042             a = 0;
00043         gama = gama + a * pow((double) 2., -i);
00044     }
00045 
00046     double norm = 0;
00047     for(uint i = 0; i < size; i++)
00048         norm += pow(p1->realValue[i] - p2->realValue[i], 2);
00049     norm = sqrt(norm);
00050 
00051     // scaling safeguard 
00052     if(norm < 10e-9)
00053         norm = 1;
00054 
00055     // determine better parent
00056     FloatingPoint *better, *worse;
00057     FitnessP parent2 = state_->getContext()->secondParent->fitness;
00058     if(state_->getContext()->firstParent->fitness->isBetterThan(parent2)) {
00059         better = p1;
00060         worse = p2;
00061     } else {
00062         better = p2;
00063         worse = p1;
00064     }
00065 
00066     // build child
00067     for (uint i = 0; i < size; i++) {
00068         // worse gene minus better gene divided with norm
00069         lambda = (worse->realValue[i] - better->realValue[i]) / norm;
00070 
00071         b = state_->getRandomizer()->getRandomDouble();
00072         // minus with probability 0.9
00073         if (b <= 0.9)
00074             ch->realValue[i] = better->realValue[i] - range * gama * lambda;
00075         else
00076             ch->realValue[i] = better->realValue[i] + range * gama * lambda;
00077 
00078         // check for bounds; if outside the interval, bring closer to better parent
00079         if(ch->realValue[i] > ch->getUBound())
00080             ch->realValue[i] = better->realValue[i] + state_->getRandomizer()->getRandomDouble() * (ch->getUBound() - better->realValue[i]);
00081         else if(ch->realValue[i] < ch->getLBound())
00082             ch->realValue[i] = better->realValue[i] - state_->getRandomizer()->getRandomDouble() * (better->realValue[i] - ch->getLBound());
00083     }
00084 
00085     return true;
00086 }
00087 
00088 }

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