00001
00002
00004
00005 #include "BasicQuenchMinimizer.h"
00006
00008
00010
00011 CBasicQuenchMinimizer::CBasicQuenchMinimizer(int nTrialSteps,int nCycles, double initialStepScale, int seed, CParameterFilter *pFilter)
00012 :CSimulatedAnnealingStrategy(seed,pFilter)
00013 {
00014 m_iNCycles = nCycles;
00015 m_iNTrialSteps = nTrialSteps;
00016 m_dInitialStepScale = initialStepScale;
00017 }
00018
00019 CBasicQuenchMinimizer::~CBasicQuenchMinimizer()
00020 {
00021
00022 }
00023
00024 void CBasicQuenchMinimizer::InitializeTemperature(Minimizable *minimizable)
00025 {
00026
00027 m_dTemperature = 1.0;
00028 m_dStepScale = m_dInitialStepScale;
00029 }
00030
00031 void CBasicQuenchMinimizer::GenerateMove()
00032 {
00033
00034 m_pFilter->ForwardTransformation(m_pdCurrentParameters,nParameters);
00035 for(int i = 0; i < nParameters; i++)
00036 {
00037 m_pdTrialParameters[i] = m_pdCurrentParameters[i] + m_dStepScale*(2*m_pRNG->uniform() - 1.0);
00038 }
00039
00040 m_pFilter->BackwardTransformation(m_pdCurrentParameters,nParameters);
00041 m_pFilter->BackwardTransformation(m_pdTrialParameters,nParameters);
00042 }
00043
00044 bool CBasicQuenchMinimizer::AcceptMove(double Eold, double Enew)
00045 {
00046 double deltaE = Enew - Eold;
00047
00048 if(deltaE < 0)
00049 {
00050 return true;
00051 }
00052
00053 return false;
00054 }
00055
00056 void CBasicQuenchMinimizer::Cool()
00057 {
00058 m_dTemperature += 1.0;
00059 cout << "Iteration number " << m_dTemperature << ", ";
00060
00061 if(m_dAcceptanceRatio < 0.5)
00062 {
00063
00064 if(m_dAcceptanceRatio == 0.0)
00065 {
00066 m_dStepScale = __min(m_dStepScale/2.0,m_dInitialStepScale/2.0);
00067 cout << "Scale cut in half." << endl;
00068 }
00069 else
00070 {
00071
00072
00073 m_dStepScale = 0.85*m_dStepScale;
00074 cout << "Scale decreased." << endl;
00075 }
00076 }
00077 else if(m_dAcceptanceRatio > 0.8)
00078 {
00079 m_dStepScale = (1/0.85)*m_dStepScale;
00080 cout << "Scale increased." << endl;
00081 }
00082
00083
00084 fstream *pfbest = new fstream("optimized.par",ios::out);
00085 *pfbest << "##################################################################" << endl;
00086 *pfbest << "# BEST PARAMETERS (PARTIAL)" << endl;
00087 *pfbest << "##################################################################" << endl;
00088 for(int i = 0; i < nParameters; i++)
00089 {
00090 *pfbest << m_pdCurrentParameters[i] << endl;
00091 }
00092 delete pfbest;
00093 }
00094
00095 bool CBasicQuenchMinimizer::Terminate()
00096 {
00097 if(m_dTemperature > m_iNCycles)
00098 {
00099 return true;
00100 }
00101
00102 return false;
00103 }
00104
00105 bool CBasicQuenchMinimizer::Equilibrated()
00106 {
00107 if(m_iTrialCount > m_iNTrialSteps)
00108 {
00109 return true;
00110 }
00111
00112 return false;
00113 }