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