00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _freESPlusSelection_txx
00023 #define _freESPlusSelection_txx
00024
00025 #include "freESPlusSelection.h"
00026
00027 namespace FREE
00028 {
00029 namespace ES
00030 {
00031
00032 template <class TIndividual>
00033 PlusSelection<TIndividual>
00034 ::PlusSelection()
00035 {
00036 }
00037
00038 template <class TIndividual>
00039 typename PlusSelection<TIndividual>::PopulationPointer
00040 PlusSelection<TIndividual>
00041 ::Select(PopulationType* pPopulation, PopulationSizeType newSize) const
00042 {
00043 if (!pPopulation) throwExceptionMacro("Error. Passed population is NULL.");
00044 if (pPopulation->Size()<newSize) throwExceptionMacro("Error. Passed population is already smaller the prefered size, nothing to select. Population size: "<<pPopulation->Size()<<"; new size: "<<newSize);
00045
00046 PopulationPointer removedPopulation = PopulationType::New();
00047
00048 while (pPopulation->Size()>newSize)
00049 {
00050 typename IndividualType::ObjectiveValueType worstValue = ((*pPopulation)[0])->GetObjectiveValue();
00051 unsigned long worstIndex = 0;
00052
00053 for (unsigned long index = 1; index < pPopulation->Size(); index++)
00054 {
00055 if (this->GetMaximumIsBest())
00056 {
00057 if (worstValue>((*pPopulation)[index])->GetObjectiveValue())
00058 {
00059 worstValue = ((*pPopulation)[index])->GetObjectiveValue();
00060 worstIndex = index;
00061 }
00062 }
00063 else
00064 {
00065 if (worstValue<((*pPopulation)[index])->GetObjectiveValue())
00066 {
00067 worstValue = ((*pPopulation)[index])->GetObjectiveValue();
00068 worstIndex = index;
00069 }
00070 }
00071 }
00072
00073 removedPopulation->push_back((*pPopulation)[worstIndex]);
00074 pPopulation->erase(pPopulation->begin()+worstIndex);
00075 }
00076
00077 return removedPopulation;
00078 };
00079
00080 }
00081 }
00082
00083 #endif