src/work/akos/simann.h
changeset 1148 1eea022c7a16
parent 1142 450f794dca81
child 1150 c20bcf71efe3
equal deleted inserted replaced
8:af93b547b958 9:c80ea62d6665
   178      */
   178      */
   179     SimpleController(long _max_iter = 500000, long _max_no_impr = 20000,
   179     SimpleController(long _max_iter = 500000, long _max_no_impr = 20000,
   180     double _temp = 1000.0, double _ann_fact = 0.9999) : iter(0), last_impr(0),
   180     double _temp = 1000.0, double _ann_fact = 0.9999) : iter(0), last_impr(0),
   181     max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp),
   181     max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp),
   182     ann_fact(_ann_fact) {}
   182     ann_fact(_ann_fact) {}
       
   183     /*! \brief This is called when a neighbouring state gets accepted. */
   183     void acceptEvent() {
   184     void acceptEvent() {
   184       iter++;
   185       iter++;
   185     }
   186     }
   186     /*! \brief This is called when the accepted neighbouring state's cost is
   187     /*! \brief This is called when the accepted neighbouring state's cost is
   187      *  less than the best found one's.
   188      *  less than the best found one's.
   213     }
   214     }
   214   };
   215   };
   215 
   216 
   216   /*! \brief A controller with preset running time for the simulated annealing
   217   /*! \brief A controller with preset running time for the simulated annealing
   217    *  class.
   218    *  class.
       
   219    *
       
   220    *  With this controller you can set the running time of the annealing
       
   221    *  process in advance. It works the following way: the controller measures
       
   222    *  a kind of divergence. The divergence is the difference of the average
       
   223    *  cost of the recently found solutions the cost of the best found one. In
       
   224    *  case this divergence is greater than a given threshold, then we decrease
       
   225    *  the annealing factor, that is we cool the system faster. In case the
       
   226    *  divergence is lower than the threshold, then we increase the temperature.
       
   227    *  The threshold is a function of the elapsed time which reaches zero at the
       
   228    *  desired end time.
   218    */
   229    */
   219   class AdvancedController : public SimAnnBase::Controller {
   230   class AdvancedController : public SimAnnBase::Controller {
   220   private:
   231   private:
   221     Timer timer;
   232     Timer timer;
   222     /*! \param time the elapsed time in seconds */
   233     /*! \param time the elapsed time in seconds */
   225     }
   236     }
   226   public:
   237   public:
   227     double alpha;
   238     double alpha;
   228     double beta;
   239     double beta;
   229     double gamma;
   240     double gamma;
       
   241     /*! \brief The time at the end of the algorithm. */
   230     double end_time;
   242     double end_time;
       
   243     /*! \brief The time at the start of the algorithm. */
   231     double start_time;
   244     double start_time;
       
   245     /*! \brief Starting threshold. */
   232     double start_threshold;
   246     double start_threshold;
       
   247     /*! \brief Average cost of recent solutions. */
   233     double avg_cost;
   248     double avg_cost;
       
   249     /*! \brief Temperature. */
   234     double temp;
   250     double temp;
       
   251     /*! \brief Annealing factor. */
   235     double ann_fact;
   252     double ann_fact;
       
   253     /*! \brief Initial annealing factor. */
       
   254     double init_ann_fact;
   236     bool warmup;
   255     bool warmup;
   237     /*! \brief Constructor.
   256     /*! \brief Constructor.
   238      *  \param _end_time running time in seconds
   257      *  \param _end_time running time in seconds
   239      *  \param _alpha parameter used to calculate the running average
   258      *  \param _alpha parameter used to calculate the running average
   240      *  \param _beta parameter used to decrease the annealing factor
   259      *  \param _beta parameter used to decrease the annealing factor
   241      *  \param _gamma parameter used to increase the temperature
   260      *  \param _gamma parameter used to increase the temperature
       
   261      *  \param _ann_fact initial annealing factor
   242      */
   262      */
   243     AdvancedController(double _end_time, double _alpha = 0.2,
   263     AdvancedController(double _end_time, double _alpha = 0.2,
   244     double _beta = 0.9, double _gamma = 1.6) : alpha(_alpha), beta(_beta),
   264     double _beta = 0.9, double _gamma = 1.6, double _ann_fact = 0.9999) :
   245     gamma(_gamma), end_time(_end_time), ann_fact(0.99999999), warmup(true) {}
   265     alpha(_alpha), beta(_beta), gamma(_gamma), end_time(_end_time),
       
   266     ann_fact(_ann_fact), init_ann_fact(_ann_fact), warmup(true) {}
   246     void init() {
   267     void init() {
   247       avg_cost = base->getCurrCost();
   268       avg_cost = base->getCurrCost();
   248     }
   269     }
   249     /*! \brief This is called when a neighbouring state gets accepted. */
   270     /*! \brief This is called when a neighbouring state gets accepted. */
   250     void acceptEvent() {
   271     void acceptEvent() {
   273           ann_fact *= beta;
   294           ann_fact *= beta;
   274         }
   295         }
   275         else {
   296         else {
   276           // increase the temperature
   297           // increase the temperature
   277           temp *= gamma;
   298           temp *= gamma;
   278           ann_fact = 0.99999999; // !!!!!!!!!!!
   299           // reset the annealing factor
       
   300           ann_fact = init_ann_fact;
   279         }
   301         }
   280         temp *= ann_fact;
   302         temp *= ann_fact;
   281         return elapsed_time < end_time;
   303         return elapsed_time < end_time;
   282       }
   304       }
   283     }
   305     }