# HG changeset patch # User ladanyi # Date 1100681240 0 # Node ID 7f4d07047ed8971420bf73089ee9359b40852610 # Parent 5c846ec3f787d813423877e0565d059732ef5700 Some comments and minor additions to the AdvancedController. diff -r 5c846ec3f787 -r 7f4d07047ed8 src/work/akos/simann.h --- a/src/work/akos/simann.h Wed Nov 17 08:46:07 2004 +0000 +++ b/src/work/akos/simann.h Wed Nov 17 08:47:20 2004 +0000 @@ -51,15 +51,23 @@ } } + /*! \brief A base class for controllers. */ class Controller { public: SimAnnBase *base; virtual void init() {} + /*! \brief This is called when a neighbouring state gets accepted. */ virtual void acceptEvent() {} + /*! \brief This is called when the accepted neighbouring state's cost is + * less than the best found one's. + */ virtual void improveEvent() {} + /*! \brief This is called when a neighbouring state gets rejected. */ virtual void rejectEvent() {} virtual void setBase(SimAnnBase *_base) { base = _base; } + /*! */ virtual bool next() = 0; + /*! */ virtual bool accept() = 0; }; }; @@ -106,14 +114,17 @@ class SimpleController : public SimAnnBase::Controller { public: long iter, last_impr, max_iter, max_no_impr; - double temp, annealing_factor; - SimpleController() { - iter = last_impr = 0; - max_iter = 500000; - max_no_impr = 20000; - annealing_factor = 0.9999; - temp = 1000; - } + double temp, ann_fact; + /*! \param _max_iter maximum number of iterations + * \param _max_no_impr maximum number of consecutive iterations which do + * not yield a better solution + * \param _temp initial temperature + * \param _ann_fact annealing factor + */ + SimpleController(long _max_iter = 500000, long _max_no_impr = 20000, + double _temp = 1000, double _ann_fact = 0.9999) : iter(0), last_impr(0), + max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp), + ann_fact(_ann_fact) {} void acceptEvent() { iter++; } @@ -124,7 +135,7 @@ iter++; } bool next() { - temp *= annealing_factor; + temp *= ann_fact; bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr); return !quit; } @@ -140,12 +151,21 @@ */ class AdvancedController : public SimAnnBase::Controller { private: - double threshold() { return 0.0; } + /*! \param time the elapsed time in seconds */ + double threshold(double time) { return 0.0; } public: - double alpha; - double temp; + double alpha, beta, gamma; + double end_time, start_time; double avg_cost; - double start_time, end_time; + double temp, ann_fact; + /*! \param _end_time running_time + * \param _alpha parameter used to calculate the running average + * \param _beta parameter used to decrease the annealing factor + * \param _gamma parameter used to increase the temperature + */ + AdvancedController(double _end_time, double _alpha = 0.2, + double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta), + gamma(_gamma), end_time(_end_time) {} void init() { timeval tv; gettimeofday(&tv, 0); @@ -159,13 +179,18 @@ void rejectEvent() { } bool next() { - // abs(avg_cost - base->getBestCost()) - // ha nagy: cooling factor novelese - // ha kicsi: homerseklet novelese - // de mennyivel? timeval tv; gettimeofday(&tv, 0); double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time; + if (fabs(avg_cost - base->getBestCost()) > threshold(elapsed_time)) { + // decrease the annealing factor + ann_fact *= beta; + } + else { + // increase the temperature + temp *= gamma; + } + temp *= ann_fact; return elapsed_time < end_time; } bool accept() {