00001 #include "../ECF_base.h"
00002 #include "Binary.h"
00003
00004 namespace Binary
00005 {
00006
00007 void BinaryCrsShuffle::registerParameters(StateP state)
00008 {
00009 myGenotype_->registerParameter(state, "crx.shuffle", (voidP) new double(0), ECF::DOUBLE);
00010 }
00011
00012
00013 bool BinaryCrsShuffle::initialize(StateP state)
00014 {
00015 voidP sptr = myGenotype_->getParameterValue(state, "crx.shuffle");
00016 probability_ = *((double*)sptr.get());
00017 return true;
00018 }
00019
00020
00021 bool BinaryCrsShuffle::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 uint rand1, rand2;
00028 bool temp;
00029 typedef std::vector<uint> v_uint;
00030
00031 v_uint pFrom;
00032 v_uint pTo;
00033
00034 uint bitCrs = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
00035
00036 for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
00037
00038 for (uint i = 0; i < p1->getNumBits(); i++) {
00039 rand1 = state_->getRandomizer()->getRandomInteger(0, 1);
00040 rand2 = state_->getRandomizer()->getRandomInteger(0, 1);
00041 pFrom.push_back(rand1);
00042 pTo.push_back(rand2);
00043
00044 temp = p1->variables[dimension][rand1];
00045 p1->variables[dimension][rand1] = p1->variables[dimension][rand2];
00046 p1->variables[dimension][rand2] = temp;
00047
00048 temp = p2->variables[dimension][rand1];
00049 p2->variables[dimension][rand1] = p2->variables[dimension][rand2];
00050 p2->variables[dimension][rand2] = temp;
00051 }
00052
00053 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
00054 case 0: for (uint i = 0; i < bitCrs; i++) {
00055 ch->variables[dimension][i] = p1->variables[dimension][i];
00056 }
00057 for (uint i = bitCrs; i < p2->getNumBits(); i++) {
00058 ch->variables[dimension][i] = p2->variables[dimension][i];
00059 }
00060 break;
00061 case 1: for (uint i = 0; i < bitCrs; i++) {
00062 ch->variables[dimension][i] = p2->variables[dimension][i];
00063 }
00064 for (uint i = bitCrs; i < p1->getNumBits(); i++) {
00065 ch->variables[dimension][i] = p1->variables[dimension][i];
00066 }
00067 }
00068
00069 for (uint i = 0; i < p1->getNumBits(); i++) {
00070 rand1 = pFrom[p1->getNumBits() - 1 - i];
00071 rand2 = pTo[p1->getNumBits() - 1 - i];
00072 temp = ch->variables[dimension][rand1];
00073 ch->variables[dimension][rand1] = ch->variables[dimension][rand2];
00074 ch->variables[dimension][rand2] = temp;
00075 }
00076 }
00077
00078
00079 ch->update();
00080
00081 return true;
00082 }
00083
00084 }