Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

CommaStrategyOne.cpp

Go to the documentation of this file.
00001 // CommaStrategyOne.cpp: implementation of the CCommaStrategyOne class.
00002 //
00004 
00005 #include "CommaStrategyOne.h"
00006 
00008 // Construction/Destruction
00010 
00011 CCommaStrategyOne::CCommaStrategyOne(CParameterFilter *pFilter):CEvolutionStrategy(pFilter)
00012 {
00013         m_dInitialSigma = 1.0;
00014 }
00015 
00016 CCommaStrategyOne::CCommaStrategyOne(CParameterFilter *pFilter, double sigma, int mu, int rho, int lambda, int nGenerations, int seed)
00017 :CEvolutionStrategy(pFilter,mu,rho,lambda,nGenerations,seed)
00018 {
00019         // be sure the std. dev. is positive
00020         m_dInitialSigma = fabs(sigma);
00021 }
00022 
00023 CCommaStrategyOne::~CCommaStrategyOne()
00024 {
00025 
00026 }
00027 
00028 void CCommaStrategyOne::InitializePopulation(double *parameters)
00029 {
00030         int i;
00031         // this variant of ES just has nParameters std. deviations as the
00032         // strategy parameters
00033         m_iNStrategyParameters = nParameters;
00035         // tau,tau' are recommended to be some multiple of the following:
00036         //      - tau  ~   1/sqrt(2*sqrt(nParameters))
00037         //  - tau' ~   1/sqrt(2*nParameters)  
00039         m_dTau = 1.0/sqrt(2.0*sqrt((double)nParameters));
00040         m_dTauPrime = 1.0/(sqrt(2.0*nParameters));
00041         // allocate the space for the populations.  Starting objective function values
00042         // are just dummies.  Objective function values are rarely copied, since any
00043         // changes in the parameters require re-evaluation 
00044         for(i = 0; i < m_iMu; i++)
00045         {
00046                 m_vpPopulation.push_back(new CEvolutionStrategyChromosome(nParameters,m_iNStrategyParameters));
00047                 m_vpPopulation[i]->SetObjFuncValue(-1.0);
00048         }
00049         for(i = 0; i < m_iLambda; i++)
00050         {
00051                 m_vpOffspring.push_back(new CEvolutionStrategyChromosome(nParameters,m_iNStrategyParameters));
00052                 m_vpOffspring[i]->SetObjFuncValue(-1.0);
00053         }
00054 
00055         // now set up the initial parent pool; they are random large mutations 
00056         // of the starting parameters (with std. dev. given by m_dInitialSigma).  
00057         double *oVector = new double[nParameters];
00058         double *sVector = new double[nParameters];
00059         
00060         for(i = 0; i < m_iMu; i++)
00061         {
00062                 for(int j = 0; j < nParameters; j++)
00063                 {
00064                         oVector[j] = parameters[j] + m_pRNG->gaussian(m_dInitialSigma);
00065                         sVector[j] = 0.5 + m_pRNG->uniform();
00066                 }
00067                 m_vpPopulation[i]->SetObjectParameters(oVector);
00068                 m_vpPopulation[i]->SetStrategyParameters(sVector);
00069         }
00070 
00071         delete [] oVector;
00072         delete [] sVector;
00073 }
00074 
00075 void CCommaStrategyOne::SelectParents()
00076 {
00077         // choose rho parents at random with replacement from a pool
00078         // of mu potentials
00079         for(int i = 0; i < m_iRho; i++)
00080         {
00081                 m_viParents[i] = m_pRNG->discrete(0,m_iMu-1);
00082         }
00083 }
00084 
00085 void CCommaStrategyOne::Recombination(int whichOffspring)
00086 {
00087         double *oParams = new double[nParameters];
00088         double *sParams = new double[nParameters];
00089         // the object parameters use discrete recombination and the strategy 
00090         // parameters use intermediate recombination.  Remember, in this ES 
00091         // nObjPars = nStrategyPars.
00092         double objPar,stratPar;
00093         for(int i = 0; i < nParameters; i++)
00094         {
00095                 // discrete for the object parameters
00096                 int chooser = m_pRNG->discrete(0,m_iRho-1);
00097                 objPar = m_vpPopulation[m_viParents[chooser]]->GetObjectParameter(i);
00098                 // intermediate for the strategy parameters
00099                 stratPar = 0.0;
00100                 for(int nParent = 0; nParent < m_iRho; nParent++)
00101                 {
00102                         stratPar += (1.0/m_iRho)*(m_vpPopulation[nParent]->GetStrategyParameter(i));
00103                 }
00104                 
00105                 oParams[i] = objPar;
00106                 sParams[i] = stratPar;
00107         }
00108 
00109         m_vpOffspring[whichOffspring]->SetObjectParameters(oParams);
00110         m_vpOffspring[whichOffspring]->SetStrategyParameters(sParams);
00111 
00112         delete [] oParams;
00113         delete [] sParams;
00114 }
00115 
00116 void CCommaStrategyOne::Mutation(int whichOffspring)
00117 {
00118         double *oParams = new double[nParameters];
00119         double *sParams = new double[nParameters];
00120 
00121         // get the current parameters, then start mutating.  Mutate the strategy
00122         // parameters first and THEN use them to mutate the object parameters
00123         double fixedTerm = m_dTauPrime*m_pRNG->gaussian(1.0);
00124         for(int i = 0; i < nParameters; i++)
00125         {
00126                 oParams[i] = m_vpOffspring[whichOffspring]->GetObjectParameter(i);
00127                 sParams[i] = m_vpOffspring[whichOffspring]->GetStrategyParameter(i);
00128                 double floatTerm = m_dTau*m_pRNG->gaussian(1.0);
00129                 oParams[i] = oParams[i]*exp(fixedTerm + floatTerm);
00130                 sParams[i] = sParams[i] + oParams[i]*m_pRNG->gaussian(1.0);
00131         }
00132         // copy back
00133         m_vpOffspring[whichOffspring]->SetObjectParameters(oParams);
00134         m_vpOffspring[whichOffspring]->SetStrategyParameters(sParams);
00135         
00136         delete [] oParams;
00137         delete [] sParams;
00138 }
00139 
00140 void CCommaStrategyOne::Select()
00141 {
00142         // use STL sort with the correct comparator
00143         CEvolutionStrategyChromosome::CEvolutionStrategyChromosomeComparator comparator;
00144         std::sort(m_vpOffspring.begin(),m_vpOffspring.end(),comparator);
00145         // this selection scheme uses elitist selection, taking the mu most fit
00146         // individuals
00147         for(int i = 0; i < m_iMu; i++)
00148         {
00149                 // set object parameters
00150                 m_vpPopulation[i]->SetObjectParameters(m_vpOffspring[i]->GetObjectParameters());
00151                 // set strategy parameters
00152                 m_vpPopulation[i]->SetStrategyParameters(m_vpOffspring[i]->GetStrategyParameters());
00153                 // set function value
00154                 m_vpPopulation[i]->SetObjFuncValue(m_vpOffspring[i]->GetObjFuncValue());
00155         }
00156 }

Generated on Mon Nov 3 09:37:42 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002