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

SimulatedAnnealingStrategy.cpp

Go to the documentation of this file.
00001 // SimulatedAnnealingStrategy.cpp: implementation of the CSimulatedAnnealingStrategy class.
00002 //
00004 
00005 #include "SimulatedAnnealingStrategy.h"
00006 
00008 // Construction/Destruction
00010 
00011 CSimulatedAnnealingStrategy::CSimulatedAnnealingStrategy(int RNGSeed, CParameterFilter *pFilter)
00012 {
00013         m_iRNGSeed = RNGSeed;
00014         m_pFilter = pFilter;
00015 
00016         m_pRNG = new Rand(m_iRNGSeed);
00017         m_pMO = new CMatrixOperations();
00018 
00019         m_dEBar = 0.0;
00020         m_dE2Bar = 0.0;
00021         m_dAcceptanceRatio = 0.0;
00022         m_iTrialCount = 0;
00023 }
00024 
00025 CSimulatedAnnealingStrategy::~CSimulatedAnnealingStrategy()
00026 {
00027         delete m_pRNG;
00028         delete m_pMO;
00029 }
00030 
00031 double CSimulatedAnnealingStrategy::Minimize(double *parameters, Minimizable *minimizable)
00032 {
00033         nParameters = minimizable->GetNParameters();
00034         m_pdCurrentParameters = new double[nParameters];
00035         m_pdTrialParameters = new double[nParameters];
00036 
00037         // copy the current parameters into starting parameters
00038         m_pMO->ElementCopy(parameters,m_pdCurrentParameters,nParameters);
00039 
00040         m_dEBest = minimizable->ObjectiveFunction(parameters);
00041         double Eold = m_dEBest;
00042 
00043         cout << "Initial cost of " << m_dEBest << endl;
00044 
00045         this->InitializeTemperature(minimizable);
00046 
00047         // avoids termination before any moves
00048         m_dEBar = -m_dEBest; 
00049 
00050         while( !(this->Terminate()) )
00051         {
00052                 m_dEBar = 0.0;
00053                 m_dE2Bar = 0.0;
00054                 m_iTrialCount = 0;
00055                 m_dAcceptanceRatio = 0.0;
00056 
00057                 while( !(this->Equilibrated()) )
00058                 {
00059                         this->GenerateMove();
00060                         double Enew = minimizable->ObjectiveFunction(m_pdTrialParameters);
00061 
00062                         if(this->AcceptMove(Eold,Enew))
00063                         {                       
00064                                 
00065                                 Eold = Enew;
00066                                 m_dAcceptanceRatio += 1.0;
00067                                 m_pMO->ElementCopy(m_pdTrialParameters,m_pdCurrentParameters,nParameters);
00068 
00069                                 if(Enew < m_dEBest)
00070                                 {
00071                                         cout << "Best cost is now " << Enew << endl;
00072                                         m_dEBest = Enew;
00073                                         m_pMO->ElementCopy(m_pdTrialParameters,parameters,nParameters);
00074                                 }
00075 
00076                         } // end AcceptMove if
00077 
00078                         m_dEBar += Eold;
00079                         m_dE2Bar += Eold*Eold;
00080                         m_iTrialCount++;
00081                 
00082                 } // end equlibration stage
00083 
00084                 // normalize statistical properties
00085                 m_dEBar = m_dEBar/m_iTrialCount;
00086                 m_dE2Bar = m_dE2Bar/m_iTrialCount;
00087                 m_dAcceptanceRatio = m_dAcceptanceRatio/m_iTrialCount;
00088                 
00089                 // notify observers
00090                 this->Notify();
00091                 // update temperature
00092                 this->Cool();
00093         
00094         } // end anneal
00095 
00096         delete [] m_pdCurrentParameters;
00097         delete [] m_pdTrialParameters;
00098         
00099         return m_dEBest;
00100 }

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