Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef GAPopulationH
00033 #define GAPopulationH
00034
00035 #include <zeusmath/Intel/Interfaces/IGAPopulation.hpp>
00036 #include <zeusbase/System/ZObject.h>
00037 #include <zeusbase/System/Time.h>
00038 #include <zeusbase/System/ArrayList.hpp>
00039
00040 BEGIN_NAMESPACE_Zeus
00041
00042
00043
00046
00047 zeusmath_class TGAPopulation : public TZObject, public IGAPopulation
00048 {
00049 public:
00050
00054
00055 enum EGACopyMethod
00056 {
00057 etReplaceCrossed = 0,
00058 etReplaceAll = 1,
00059 etKeepBestParents = 2
00060 };
00061
00062
00065
00066 enum EGACrossMethod
00067 {
00068 etCrossBestPrior = 0,
00069 etCrossOnlyBest = 1
00070 };
00071
00072
00075
00076 enum EGAFitnessMethod
00077 {
00078 etBiggerBetter = 0,
00079 etSmallerBetter = 1
00080 };
00081
00082
00088
00089 typedef Retval (*FCreateIndividual)(IGAIndividual*& rpInd, bool bCreateChromosomes);
00090
00091 TGAPopulation(FCreateIndividual factory,
00092 EGACopyMethod eMethod = etKeepBestParents,
00093 EGACrossMethod eCross = etCrossBestPrior,
00094 EGAFitnessMethod eFitnessMethod= etBiggerBetter,
00095 Float fCrossAmount = 10.0);
00096
00097 void setMutationParams(Float fProbability);
00098 void createPopulation(Int iCount);
00099
00100
00101 virtual Int MQUALIFIER getCount() const;
00102 virtual Retval MQUALIFIER getIndividual(Int iIndex, IGAIndividual*& rpIndividual) const;
00103 virtual Retval MQUALIFIER getBestIndividual(IGAIndividual*& rpIndividual) const;
00104 virtual Retval MQUALIFIER makeNextGeneration();
00105 virtual Int MQUALIFIER makeGenerations(Int iAbort, Float fTolerance = 0.001);
00106
00107
00108 MEMORY_MANAGER_DECL
00109
00110 protected:
00111 virtual ~TGAPopulation();
00112
00113 private:
00115 TArrayList<IGAIndividual*> m_lstIndividuals;
00117 Float m_fCrossAmount;
00119 EGACopyMethod m_eCopyMethod;
00121 EGACrossMethod m_eCrossMethod;
00123 EGAFitnessMethod m_eFitnessMethod;
00125 FCreateIndividual m_Factory;
00127 bool m_bEnableMutation;
00129 Float m_fMutationProbability;
00130
00131
00132
00133
00134
00135
00136 struct RSelection
00137 {
00138 Float fFitness;
00139 Float fFitnessSum;
00140 IGAIndividual* pIndividual;
00141 bool bCrossed;
00142
00143 RSelection()
00144 {
00145 fFitness = 0.0;
00146 fFitnessSum = 0.0;
00147 bCrossed = false;
00148 pIndividual = NULL;
00149 }
00150
00151 bool operator==(const RSelection& inpar) const
00152 {
00153 return (fFitnessSum == inpar.fFitnessSum &&
00154 fFitness == inpar.fFitness &&
00155 pIndividual == inpar.pIndividual &&
00156 bCrossed == inpar.bCrossed);
00157 }
00158 };
00159
00160
00161 void clearPopulation();
00162 bool canMutate();
00163 void crossIndividualsPrior(TArrayList<IGAIndividual*>& rNewGeneration);
00164 void crossOnlyBestIndividuals(TArrayList<IGAIndividual*>& rNewGeneration);
00165 void cross(IGAIndividual& rParent1, IGAIndividual& rParent2, TArrayList<IGAIndividual*>& rNewGeneration);
00166
00167 Int findFromFitnessRange(TArrayList<RSelection>& rList, Float fFitnessRange);
00168 Float fillFitnessList(TArrayList<RSelection>& rList);
00169 void sortListFromFitness(TArrayList<RSelection>& rList);
00170 void copyNewGeneration(TArrayList<RSelection>& rOldGeneration, TArrayList<IGAIndividual*>& rNewGeneration);
00171 };
00172
00173
00174
00178
00179 inline void TGAPopulation::setMutationParams(Float fProbability)
00180 {
00181 m_fMutationProbability = fProbability;
00182 m_bEnableMutation = true;
00183 }
00184
00185
00188
00189 inline bool TGAPopulation::canMutate()
00190 {
00191 Float fValue = TTime::randomFloat(0, 100);
00192 return (fValue <= m_fMutationProbability);
00193 }
00194
00195
00196
00199
00200 inline Int MQUALIFIER TGAPopulation::getCount() const
00201 {
00202 return m_lstIndividuals.getCount();
00203 }
00204
00205 END_NAMESPACE_Zeus
00206
00207 #endif