From ba19b29a2256348017886d0a0a1bb456b667b08c Mon Sep 17 00:00:00 2001 From: LPTK Date: Mon, 15 Jul 2013 11:34:55 +0200 Subject: [PATCH] intermediate commit 7 --- mo/src/continuator/moCounterMonitorSaver.h | 6 +- .../coolingSchedule/moHuangCoolingSchedule.h | 77 ++++++++++++++----- .../coolingSchedule/moTrikiCoolingSchedule.h | 20 +++-- 3 files changed, 76 insertions(+), 27 deletions(-) diff --git a/mo/src/continuator/moCounterMonitorSaver.h b/mo/src/continuator/moCounterMonitorSaver.h index f57e05d2d..6ac5a7062 100644 --- a/mo/src/continuator/moCounterMonitorSaver.h +++ b/mo/src/continuator/moCounterMonitorSaver.h @@ -52,7 +52,11 @@ public : moCounterMonitorSaver(unsigned _interval, eoMonitor& _mon) : interval(_interval), counter(0) { monitors.push_back(&_mon); } - + + moCounterMonitorSaver(unsigned _interval) + : interval(_interval), counter(0) + { } + /** * call monitors if interval is reach by a counter */ diff --git a/mo/src/coolingSchedule/moHuangCoolingSchedule.h b/mo/src/coolingSchedule/moHuangCoolingSchedule.h index 6ac29d778..c702b852f 100644 --- a/mo/src/coolingSchedule/moHuangCoolingSchedule.h +++ b/mo/src/coolingSchedule/moHuangCoolingSchedule.h @@ -35,8 +35,11 @@ Lionel Parreaux #include -#include -using namespace std; +//#include +//using namespace std; + +#include + //! /*! @@ -51,23 +54,30 @@ public: //! Constructor /*! */ - - moHuangCoolingSchedule (double _initTemp, double _stdDevEstimation, double _lambda = .7, double _finalTemp = .01) - : initTemp(_initTemp), - stdDevEstimation(_stdDevEstimation), - lambda(_lambda), - finalTemp(_finalTemp) - { } +// moHuangCoolingSchedule (double _initTemp, int _spanSize, double _lambda = .7, double _finalTemp = .01) +// moHuangCoolingSchedule (double _initTemp, int _spanSize, double _lambda = .7, double _finalStdDev = .01) + moHuangCoolingSchedule (double _initTemp, int _spanSize, double _lambda = .7, double _finalTempDecrease = .995) + : initTemp(_initTemp) + , spanSize(_spanSize) + , lambda(_lambda) +// , finalTemp(_finalTemp) + , finalStdDev(_finalTempDecrease) +// , statIsInitialized(false) +// , step(0) +// , currentStdDevEstimation(std::numeric_limits::max()) + { } + /** * Initial temperature * @param _solution initial solution */ double init(EOT & _solution) { - + statIsInitialized = terminated = false; + step = 0; return initTemp; } - + /** * update the temperature by a factor * @param _temp current temperature to update @@ -75,8 +85,30 @@ public: */ void update(double& _temp, bool _acceptedMove, EOT & _solution) { - _temp *= exp ( -lambda*_temp / stdDevEstimation ) ; + if (_acceptedMove) + { + if (statIsInitialized) + momentStat(_solution); + else momentStat.init(_solution), statIsInitialized = true; + + } + if (step >= spanSize) + { + step = 0; + /* + //double avgFitness = momentStat.value().first; + double variance = momentStat.value().second; + //double stdDevEstimation = sqrt(variance); + double currentStdDevEstimation = sqrt(variance); + _temp *= exp( -lambda*_temp / currentStdDevEstimation );*/ + double alpha = exp( -lambda*_temp / sqrt(momentStat.value().second) ); + _temp *= alpha; + //std::cout << alpha << std::endl; + terminated = alpha > finalStdDev; + } + + step++; } //! Function which proceeds to the cooling @@ -84,8 +116,9 @@ public: */ bool operator() (double temperature) { - return temperature > finalTemp; - + //return temperature > finalTemp; + //return currentStdDevEstimation > finalStdDev; + return !terminated; } @@ -93,11 +126,19 @@ private: const double // parameters of the algorithm - initTemp, - stdDevEstimation, - lambda, - finalTemp + initTemp +// , stdDevEstimation + , lambda +// , finalTemp + , finalStdDev ; + const int spanSize; + int step; + + //double currentStdDevEstimation; + + moFitnessMomentsStat momentStat; + bool statIsInitialized, terminated; }; diff --git a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h index eb078120a..0b4466671 100644 --- a/mo/src/coolingSchedule/moTrikiCoolingSchedule.h +++ b/mo/src/coolingSchedule/moTrikiCoolingSchedule.h @@ -144,6 +144,8 @@ public: negative_temp = equilibrium_not_reached = frozen = 0; + chainStat.delta = initTemp/mu2; + //cout << "acc " << max_accepted << " " << max_generated << endl; /* @@ -230,7 +232,9 @@ public: //double stdDev = sqrt(variance); chainStat.stdDev = sqrt(variance); double sigma = chainStat.stdDev; - double delta = sigma/mu2; + /////////////// + //double delta = sigma/mu2; + /////////////// //outf << avgFitness << endl; @@ -262,7 +266,7 @@ public: //if (avgFitness/(chainStat.prevAvgFitness-delta) > xi) cout << "/!\\ eq not reached!" << endl; /// - if (avgFitness/(prevAvgFitness-delta) > xi) + if (avgFitness/(prevAvgFitness-chainStat.delta) > xi) equilibrium_not_reached++, chainStat.equilibriumNotReached = true; else equilibrium_not_reached = 0; } @@ -277,10 +281,10 @@ public: //chainStat.equilibriumNotReached = true; alpha = lambda1; - delta = sigma/mu1; + chainStat.delta = sigma/mu1; equilibrium_not_reached = 0; // ADDED! Otherwise the algo gets trapped here! } - else if (_temp*delta/(sigma*sigma) >= 1) + else if (_temp*chainStat.delta/(sigma*sigma) >= 1) { /// //cout << "/!\\ neg temp!" << endl; @@ -293,7 +297,7 @@ public: if (negative_temp < K2) { alpha = lambda1; - delta = sigma/mu1; + chainStat.delta = sigma/mu1; } else alpha = lambda2; } @@ -320,7 +324,7 @@ public: reinitializing = false; //chainStat.avgFitness = avgFitness; //alpha = 1-_temp*delta/(sigma*sigma); - alpha = 1-_temp*delta/variance; + alpha = 1-_temp*chainStat.delta/variance; //alpha = (sigma==0? 1: 1-_temp*delta/(sigma*sigma)); // ADDED! but removed @@ -338,12 +342,12 @@ public: chainStat.currentFitness = _solution.fitness(); // FIXME here? chainStat.alpha = alpha; - chainStat.delta = delta; + //chainStat.delta = delta; chainStat.avgFitness = avgFitness; // Never seems to be used - if (avgFitness == prevAvgFitness) // use a neighborhood to approximate double equality? + if (avgFitness == prevAvgFitness) // use a neighborhood to approximate double floating equality? frozen++; else frozen = 0;