00001 #include "../ECF_base.h"
00002 #include "Binary.h"
00003
00004 namespace Binary
00005 {
00006
00007 void BinaryCrsSegmented::registerParameters(StateP state)
00008 {
00009 myGenotype_->registerParameter(state, "crx.segmented", (voidP) new double(0), ECF::DOUBLE);
00010 }
00011
00012
00013 bool BinaryCrsSegmented::initialize(StateP state)
00014 {
00015 voidP sptr = myGenotype_->getParameterValue(state, "crx.segmented");
00016 probability_ = *((double*)sptr.get());
00017 return true;
00018 }
00019
00020
00021 bool BinaryCrsSegmented::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00022 {
00023 Binary* p1 = (Binary*) (gen1.get());
00024 Binary* p2 = (Binary*) (gen2.get());
00025 Binary* ch = (Binary*) (child.get());
00026
00027 double s=0.2;
00028
00029 for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
00030 uint parent = state_->getRandomizer()->getRandomInteger(0, 1);
00031 for (uint i = 0; i < p1->getNumBits(); i++) {
00032 if (parent==0) {
00033 double q = state_->getRandomizer()->getRandomDouble();
00034 uint j = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
00035 if (q > s) {
00036 for (uint k = 0; k < j; k++) {
00037 ch->variables[dimension][k] = p1->variables[dimension][k];
00038 }
00039 for (uint k = j; k < p2->getNumBits(); k++) {
00040 ch->variables[dimension][k] = p2->variables[dimension][k];
00041 }
00042 }
00043 else
00044 ch->variables[dimension][i] = p1->variables[dimension][i];
00045 }
00046 else {
00047 double q = state_->getRandomizer()->getRandomDouble();
00048 uint j = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
00049 if (q > s) {
00050 for (uint k = 0; k < j; k++) {
00051 ch->variables[dimension][k] = p2->variables[dimension][k];
00052 }
00053 for (uint k = j; k < p1->getNumBits(); k++) {
00054 ch->variables[dimension][k] = p1->variables[dimension][k];
00055 }
00056 }
00057 else
00058 ch->variables[dimension][i] = p2->variables[dimension][i];
00059 }
00060 }
00061 }
00062
00063 ch->update();
00064
00065 return true;
00066 }
00067
00068 }