[Lemon-commits] [lemon_svn] ladanyi: r1390 - hugo/trunk/src/work/akos
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:45:02 CET 2006
Author: ladanyi
Date: Wed Nov 17 09:47:20 2004
New Revision: 1390
Modified:
hugo/trunk/src/work/akos/simann.h
Log:
Some comments and minor additions to the AdvancedController.
Modified: hugo/trunk/src/work/akos/simann.h
==============================================================================
--- hugo/trunk/src/work/akos/simann.h (original)
+++ hugo/trunk/src/work/akos/simann.h Wed Nov 17 09:47:20 2004
@@ -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() {
More information about the Lemon-commits
mailing list