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     //The initialiazation routine basically does all the work
00019     //so that when we draw a member in the propagation phase
00020     //we only return the index of the a random selected member
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         //go through the whole population
00031         for (size_t i = 0; i < popsize; ++i)
00032           {
00033             //how many members can we expect in the new population based
00034             //on the probabilities
00035             const double expected = probabilities(i) * popsize;
00036             //we always assign the expected number rounded down
00037             //to the new population
00038             int assign = int(floor(expected));
00039             //and the save the difference between the rounded
00040             //and not rounded to determine additional members
00041             //later
00042             fraction.at(i) = expected - assign;
00043             //generate assign times index entries in the index
00044             //vector that describes the new population
00045             while (assign > 0)
00046               {
00047                 --assign;
00048                 indices.at(currentindex) = i;
00049                 ++currentindex;
00050               }
00051 
00052           }
00053         //now we determine the remainder of the population
00054         //based on the difference between expected and assigned
00055         size_t fractionindex = 0;
00056         //as long as the population is not full
00057         while (currentindex < popsize)
00058           {
00059             //cycle through the population members
00060             //and see if we want to include the current member
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             //when we reached the end of the population, go back to the beginning
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         // Have to reduce remain before because it starts with popsize but array is 0..popsize-1
00079         --remain;
00080         indices.at(pick) = indices.at(remain);
00081         return (result);
00082       }
00083   }

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8