00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _freESRandomMultiParentSelector_txx
00023 #define _freESRandomMultiParentSelector_txx
00024
00025 #include "freESRandomMultiParentSelector.h"
00026
00027 #include <time.h>
00028
00029 namespace FREE
00030 {
00031 namespace ES
00032 {
00033
00034 template <class TIndividual>
00035 RandomMultiParentSelector<TIndividual>
00036 ::RandomMultiParentSelector()
00037 {
00038 m_RandomGenerator = VariateGeneratorType::New();
00039 m_RandomGenerator->Initialize((int)clock());
00040 m_AllowRedraw = true;
00041 m_NumberOfSelections = 1;
00042 }
00043
00044 template <class TIndividual>
00045 typename RandomMultiParentSelector<TIndividual>::ParentSelectionType
00046 RandomMultiParentSelector<TIndividual>
00047 ::Select(const PopulationType& population) const
00048 {
00049 if (!m_AllowRedraw && (population.size()<m_NumberOfSelections)) throwExceptionMacro("Error. No redrawing allowed, but size of population is smaller the number of draws. Population size: "<<population.size()<<"; number of draws: "<<m_NumberOfSelections);
00050
00051
00052 typedef typename PopulationType::ElementIdentifier PopulationElementIdentifier;
00053 typedef std::vector<PopulationElementIdentifier> IdentifierListType;
00054 IdentifierListType indexPool;
00055 IdentifierListType selection;
00056
00057 for (PopulationElementIdentifier index = 0; index<population.size(); indexPool.push_back(index++));
00058
00059
00060 for (unsigned int count = 0; count<m_NumberOfSelections; count++)
00061 {
00062 VariateGeneratorType::IntegerType selectedIndex = m_RandomGenerator->GetIntegerVariate(indexPool.size()-1);
00063 selection.push_back(indexPool[selectedIndex]);
00064 if (!m_AllowRedraw)
00065 {
00066 indexPool.erase(indexPool.begin()+selectedIndex);
00067 }
00068 }
00069
00070 ParentSelectionType selectedIndividuals;
00071
00072 for (typename IdentifierListType::const_iterator pos = selection.begin(); pos!=selection.end(); pos++)
00073 {
00074 selectedIndividuals.push_back(population.ElementAt(*pos));
00075 }
00076
00077 return selectedIndividuals;
00078 }
00079
00080 }
00081 }
00082
00083 #endif