00001 // ZeroTAnnealMinimizer.cpp: implementation of the ZeroTAnnealMinimizer class. 00002 // 00004 00005 #include "ZeroTAnnealMinimizer.h" 00006 00008 // Construction/Destruction 00010 00011 ZeroTAnnealMinimizer::ZeroTAnnealMinimizer() 00012 { 00013 00014 } 00015 00016 ZeroTAnnealMinimizer::ZeroTAnnealMinimizer(int seed, int nSteps) 00017 { 00018 this->seed = seed; 00019 this->nSteps = nSteps; 00020 } 00021 00022 ZeroTAnnealMinimizer::~ZeroTAnnealMinimizer() 00023 { 00024 00025 } 00026 00027 double ZeroTAnnealMinimizer::Minimize(double *parameters, Minimizable *minimizable) 00028 { 00029 Rand *RNG = new Rand(seed); 00030 double currentCost; 00031 double bestCost; 00032 nParameters = minimizable->GetNParameters(); 00033 double *trial = new double[nParameters]; 00034 00035 // compute the cost for the initial parameter values 00036 bestCost = minimizable->ObjectiveFunction(parameters); 00037 00038 cout << "Starting cost of " << bestCost << endl; 00039 00040 for(int iterationStep = 1; iterationStep <= nSteps; iterationStep++) 00041 { 00042 // now construct a set of trial values 00043 // this method adds between -(1/2)value and (1/2)value to each parameter 00044 // to form the trial set of parameters 00045 for(int i = 0; i < nParameters; i++) 00046 { 00047 double temp = parameters[i]; 00048 temp = temp + (-0.5 + 1*(RNG->uniform()))*temp; 00049 trial[i] = temp; 00050 } 00051 currentCost = minimizable->ObjectiveFunction(trial); 00052 00053 if(currentCost < bestCost) 00054 { 00055 bestCost = currentCost; 00056 *parameters = *trial; 00057 } 00058 00059 cout << "Current best cost is " << bestCost << " at iteration number " << iterationStep << endl; 00060 } 00061 00062 // cleanup 00063 delete [] trial; 00064 delete RNG; 00065 00066 // return the best cost 00067 return bestCost; 00068 } 00069