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

ClonalCommaES.cpp

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

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