Some comments and minor additions to the AdvancedController.
authorladanyi
Wed, 17 Nov 2004 08:47:20 +0000
changeset 10007f4d07047ed8
parent 999 5c846ec3f787
child 1001 afe5cf34aa50
Some comments and minor additions to the AdvancedController.
src/work/akos/simann.h
     1.1 --- a/src/work/akos/simann.h	Wed Nov 17 08:46:07 2004 +0000
     1.2 +++ b/src/work/akos/simann.h	Wed Nov 17 08:47:20 2004 +0000
     1.3 @@ -51,15 +51,23 @@
     1.4        }
     1.5      }
     1.6  
     1.7 +    /*! \brief A base class for controllers. */
     1.8      class Controller {
     1.9      public:
    1.10        SimAnnBase *base;
    1.11        virtual void init() {}
    1.12 +      /*! \brief This is called when a neighbouring state gets accepted. */
    1.13        virtual void acceptEvent() {}
    1.14 +      /*! \brief This is called when the accepted neighbouring state's cost is
    1.15 +       *  less than the best found one's.
    1.16 +       */
    1.17        virtual void improveEvent() {}
    1.18 +      /*! \brief This is called when a neighbouring state gets rejected. */
    1.19        virtual void rejectEvent() {}
    1.20        virtual void setBase(SimAnnBase *_base) { base = _base; }
    1.21 +      /*! */
    1.22        virtual bool next() = 0;
    1.23 +      /*! */
    1.24        virtual bool accept() = 0;
    1.25      };
    1.26    };
    1.27 @@ -106,14 +114,17 @@
    1.28    class SimpleController : public SimAnnBase::Controller {
    1.29    public:
    1.30      long iter, last_impr, max_iter, max_no_impr;
    1.31 -    double temp, annealing_factor;
    1.32 -    SimpleController() {
    1.33 -      iter = last_impr = 0;
    1.34 -      max_iter = 500000;
    1.35 -      max_no_impr = 20000;
    1.36 -      annealing_factor = 0.9999;
    1.37 -      temp = 1000;
    1.38 -    }
    1.39 +    double temp, ann_fact;
    1.40 +    /*! \param _max_iter maximum number of iterations
    1.41 +     *  \param _max_no_impr maximum number of consecutive iterations which do
    1.42 +     *         not yield a better solution
    1.43 +     *  \param _temp initial temperature
    1.44 +     *  \param _ann_fact annealing factor
    1.45 +     */
    1.46 +    SimpleController(long _max_iter = 500000, long _max_no_impr = 20000,
    1.47 +    double _temp = 1000, double _ann_fact = 0.9999) : iter(0), last_impr(0),
    1.48 +    max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp),
    1.49 +    ann_fact(_ann_fact) {}
    1.50      void acceptEvent() {
    1.51        iter++;
    1.52      }
    1.53 @@ -124,7 +135,7 @@
    1.54        iter++;
    1.55      }
    1.56      bool next() {
    1.57 -      temp *= annealing_factor;
    1.58 +      temp *= ann_fact;
    1.59        bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
    1.60        return !quit;
    1.61      }
    1.62 @@ -140,12 +151,21 @@
    1.63     */
    1.64    class AdvancedController : public SimAnnBase::Controller {
    1.65    private:
    1.66 -    double threshold() { return 0.0; }
    1.67 +    /*! \param time the elapsed time in seconds */
    1.68 +    double threshold(double time) { return 0.0; }
    1.69    public:
    1.70 -    double alpha;
    1.71 -    double temp;
    1.72 +    double alpha, beta, gamma;
    1.73 +    double end_time, start_time;
    1.74      double avg_cost;
    1.75 -    double start_time, end_time;
    1.76 +    double temp, ann_fact;
    1.77 +    /*! \param _end_time running_time
    1.78 +     *  \param _alpha parameter used to calculate the running average
    1.79 +     *  \param _beta parameter used to decrease the annealing factor
    1.80 +     *  \param _gamma parameter used to increase the temperature
    1.81 +     */
    1.82 +    AdvancedController(double _end_time, double _alpha = 0.2,
    1.83 +    double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta),
    1.84 +    gamma(_gamma), end_time(_end_time) {}
    1.85      void init() {
    1.86        timeval tv;
    1.87        gettimeofday(&tv, 0);
    1.88 @@ -159,13 +179,18 @@
    1.89      void rejectEvent() {
    1.90      }
    1.91      bool next() {
    1.92 -      // abs(avg_cost - base->getBestCost())
    1.93 -      // ha nagy: cooling factor novelese
    1.94 -      // ha kicsi: homerseklet novelese
    1.95 -      // de mennyivel?
    1.96        timeval tv;
    1.97        gettimeofday(&tv, 0);
    1.98        double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time;
    1.99 +      if (fabs(avg_cost - base->getBestCost()) > threshold(elapsed_time)) {
   1.100 +	// decrease the annealing factor
   1.101 +        ann_fact *= beta;
   1.102 +      }
   1.103 +      else {
   1.104 +        // increase the temperature
   1.105 +        temp *= gamma;
   1.106 +      }
   1.107 +      temp *= ann_fact;
   1.108        return elapsed_time < end_time;
   1.109      }
   1.110      bool accept() {