00001
00002
00004
00005 #include "EvolutionStrategy.h"
00006
00008
00010
00011 CEvolutionStrategy::CEvolutionStrategy(CParameterFilter *pFilter)
00012 {
00013 m_iMu = 15;
00014 m_iRho = 2;
00015 m_iLambda = 100;
00016 m_iNGenerations = 1000;
00017 m_pRNG = new Rand();
00018 m_pFilter = pFilter;
00019 }
00020
00021 CEvolutionStrategy::CEvolutionStrategy(CParameterFilter *pFilter, int mu, int rho, int lambda, int nGenerations, int seed)
00022 {
00023 m_iMu = mu;
00024 m_iRho = rho;
00025 m_iLambda = lambda;
00026 m_iNGenerations = nGenerations;
00027 m_pRNG = new Rand(seed);
00028 m_pFilter = pFilter;
00029 }
00030
00031 CEvolutionStrategy::~CEvolutionStrategy()
00032 {
00033 int i;
00034 if(0 != m_pRNG) delete m_pRNG;
00035 for(i = 0; i < m_iMu; i++)
00036 {
00037 delete m_vpPopulation[i];
00038 }
00039 for(i = 0; i < m_iLambda; i++)
00040 {
00041 delete m_vpOffspring[i];
00042 }
00043 }
00044
00045 double CEvolutionStrategy::Minimize(double *parameters, Minimizable *minimizable)
00046 {
00047
00048 m_dAvgCost = 0.0;
00049 nParameters = minimizable->GetNParameters();
00050
00051 for(int i = 0; i < m_iRho; i++) { m_viParents.push_back(0); }
00052
00053
00054 m_pFilter->ForwardTransformation(parameters,nParameters);
00055
00056 cout << "Generating initial population . . ." << endl;
00057
00058 InitializePopulation(parameters);
00059
00060 for(int genCount = 0; genCount < m_iNGenerations; genCount++)
00061 {
00062 cout << "Generation " << genCount << " of " << m_iNGenerations << endl;
00063
00064 for(int nOff = 0; nOff < m_iLambda; nOff++)
00065 {
00066 SelectParents();
00067 Recombination(nOff);
00068 Mutation(nOff);
00069
00070
00071 double cost = EvaluateOffspring(minimizable,nOff);
00072 m_dAvgCost += cost;
00073 }
00074
00075 Select();
00076
00077 cout << "Best cost in new population " << m_vpPopulation[0]->GetObjFuncValue();
00078 cout << ", worst cost in new population " << m_vpPopulation[m_iMu-1]->GetObjFuncValue();
00079 cout << endl;
00080 }
00081
00082 m_dAvgCost = 0.0;
00083
00084
00085 fstream *pfPars = new fstream("population.par",ios::out);
00086 *pfPars << "##################################################################" << endl;
00087 *pfPars << "# POPULATION UPON TERMINATION - \"#\" IS SEPARATOR" << endl;
00088 *pfPars << "##################################################################" << endl;
00089 for(int popNum = 0; popNum < m_iMu; popNum++)
00090 {
00091 double *pars = m_vpPopulation[popNum]->GetObjectParameters();
00092 double func = m_vpPopulation[popNum]->GetObjFuncValue();
00093 m_dAvgCost += func;
00094 *pfPars << "# COST = " << func << endl;
00095 int term = m_vpPopulation[popNum]->GetNObjectParameters();
00096 for(int j = 0; j < term; j++)
00097 {
00098 *pfPars << m_pFilter->OperatorInverse(m_vpPopulation[popNum]->GetObjectParameter(j)) << endl;
00099 }
00100 }
00101 delete pfPars;
00102
00103 m_dAvgCost = m_dAvgCost/m_iMu;
00104
00105 return m_dAvgCost;
00106 }
00107
00108 double CEvolutionStrategy::EvaluateOffspring(Minimizable *minimizable, int whichOffspring)
00109 {
00110 m_pFilter->BackwardTransformation(m_vpOffspring[whichOffspring]->GetObjectParameters(),nParameters);
00111 m_vpOffspring[whichOffspring]->SetObjFuncValue(minimizable->ObjectiveFunction(m_vpOffspring[whichOffspring]->GetObjectParameters()));
00112 m_pFilter->ForwardTransformation(m_vpOffspring[whichOffspring]->GetObjectParameters(),nParameters);
00113 return m_vpOffspring[whichOffspring]->GetObjFuncValue();
00114 }