SimpleSelect.cpp
Go to the documentation of this file.00001 #include "SimpleSelect.h"
00002 #include <cmath>
00003 #include <iostream>
00004
00005 using namespace std;
00006 namespace gplib
00007 {
00008 SimpleSelect::SimpleSelect(GeneralRNG &LocalRandom,
00009 tProbabilityFunction myPF) :
00010 Random(LocalRandom), ProbabilityFunction(myPF)
00011 {
00012
00013 }
00014
00015 SimpleSelect::~SimpleSelect()
00016 {
00017 }
00018
00019
00020
00021 void SimpleSelect::DoInit()
00022 {
00023 const tprobabilityv probabilities = ProbabilityFunction();
00024 const size_t popsize = probabilities.size();
00025 size_t currentindex = 0;
00026
00027 std::vector<double> fraction(popsize, 0);
00028 indices.assign(popsize, 0);
00029 remain = popsize;
00030
00031 for (size_t i = 0; i < popsize; ++i)
00032 {
00033
00034
00035 const double expected = probabilities(i) * popsize;
00036
00037
00038 int assign = int(floor(expected));
00039
00040
00041
00042 fraction.at(i) = expected - assign;
00043
00044
00045 while (assign > 0)
00046 {
00047 --assign;
00048 indices.at(currentindex) = i;
00049 ++currentindex;
00050 }
00051
00052 }
00053
00054
00055 size_t fractionindex = 0;
00056
00057 while (currentindex < popsize)
00058 {
00059
00060
00061 if (Random.GetNumber() <= fraction.at(fractionindex))
00062 {
00063 indices.at(currentindex) = fractionindex;
00064 ++currentindex;
00065 fraction.at(fractionindex) -= 1.0;
00066 }
00067 ++fractionindex;
00068
00069 if (fractionindex >= popsize)
00070 fractionindex -= popsize;
00071 }
00072 }
00073
00074 size_t SimpleSelect::DoGetOne()
00075 {
00076 int pick = Random.GetNumber(remain);
00077 int result = indices.at(pick);
00078
00079 --remain;
00080 indices.at(pick) = indices.at(remain);
00081 return (result);
00082 }
00083 }