00001 #include "../ECF_base.h"
00002 #include "FloatingPoint.h"
00003 #include <math.h>
00004
00005
00006 namespace FloatingPoint
00007 {
00008
00009 void FloatingPointCrsSbx::registerParameters(StateP state)
00010 {
00011 myGenotype_->registerParameter(state, "crx.sbx", (voidP) new double(0), ECF::DOUBLE);
00012 state->getRegistry()->registerEntry("crx.sbx.ni", (voidP) new uint(1), ECF::UINT);
00013 }
00014
00015
00016 bool FloatingPointCrsSbx::initialize(StateP state)
00017 {
00018 voidP sptr = myGenotype_->getParameterValue(state, "crx.sbx");
00019 probability_ = *((double*)sptr.get());
00020
00021 voidP par = state->getRegistry()->getEntry("crx.sbx.ni");
00022 ni = *((uint*) par.get());
00023
00024 return true;
00025 }
00026
00027
00028 bool FloatingPointCrsSbx::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00029 {
00030 FloatingPoint* p1 = (FloatingPoint*) (gen1.get());
00031 FloatingPoint* p2 = (FloatingPoint*) (gen2.get());
00032 FloatingPoint* ch = (FloatingPoint*) (child.get());
00033
00034
00035 for(uint i = 0; i < p1->realValue.size(); i++) {
00036 double p1x = p1->realValue[i];
00037 double p2x = p2->realValue[i];
00038
00039
00040 double low = p1x, high = p2x;
00041 if(p2x < p1x) {
00042 low = p2x;
00043 high = p1x;
00044 }
00045
00046
00047 if(fabs(high - low) < 1.e-12) {
00048 ch->realValue[i] = (high + low) / 2;
00049 continue;
00050 }
00051
00052
00053 double min = low - p1->getLBound();
00054 if((p1->getUBound() - high) < min)
00055 min = p1->getUBound() - high;
00056
00057
00058 double beta = 1 + 2 * min / (high - low);
00059 double alpha = 2 - 1 / pow(beta, 1. + ni);
00060
00061 double u = state_->getRandomizer()->getRandomDouble();
00062
00063 u *= 0.999;
00064
00065 double beta_dash;
00066
00067 if (u <= (1. / alpha)) {
00068 beta_dash = pow(alpha * u, 1.0 / (ni + 1.0));
00069 }
00070
00071 else {
00072 beta_dash = pow(1. / (2. - alpha * u), 1.0 / (ni + 1.0));
00073 }
00074
00075
00076 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
00077 case 0: ch->realValue[i] = ((p1->realValue[i] + p2->realValue[i])/2.0) - beta_dash * 0.5 * fabs(p1->realValue[i] - p2->realValue[i]);
00078 break;
00079 case 1: ch->realValue[i] = ((p1->realValue[i] + p2->realValue[i])/2.0) + beta_dash * 0.5 * fabs(p1->realValue[i] - p2->realValue[i]);
00080 }
00081 }
00082
00083 return true;
00084 }
00085
00086 }