# HG changeset patch # User ladanyi # Date 1106730040 0 # Node ID 1cfb25ef14d208e1c926c12b5f31d21407132268 # Parent f1eb997f041842bbe558f4ad4f375766d6b4ee37 Various changes. diff -r f1eb997f0418 -r 1cfb25ef14d2 src/work/akos/simann.h --- a/src/work/akos/simann.h Wed Jan 26 00:09:06 2005 +0000 +++ b/src/work/akos/simann.h Wed Jan 26 09:00:40 2005 +0000 @@ -98,7 +98,8 @@ prev_cost = prev_prev_cost; } void saveAsBest() { - *best_ent = *curr_ent; + delete(best_ent); + best_ent = new E(*curr_ent); best_cost = curr_cost; } }; @@ -129,7 +130,7 @@ * \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), + double _temp = 1000.0, 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() { @@ -149,7 +150,8 @@ bool accept() { double cost_diff = base->getPrevCost() - base->getCurrCost(); if (cost_diff < 0.0) { - return (drand48() <= exp(cost_diff / temp)); + bool ret = drand48() <= exp(cost_diff / temp); + return ret; } else { return true; @@ -166,10 +168,13 @@ Timer timer; /*! \param time the elapsed time in seconds */ virtual double threshold(double time) { - // this is the function 1 / log(x) scaled and offset + // 1 / log(x) + /* static double xm = 5.0 / end_time; static double ym = start_threshold / (1 / log(1.2) - 1 / log(5.0 + 1.2)); return ym * (1 / log(xm * time + 1.2) - 1 / log(5.0 + 1.2)); + */ + return (-1.0) * start_threshold / end_time * time + start_threshold; } public: double alpha, beta, gamma; @@ -184,27 +189,21 @@ * \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), ann_fact(0.9999), warmup(true) {} + double _beta = 0.9, double _gamma = 1.6) : alpha(_alpha), beta(_beta), + gamma(_gamma), end_time(_end_time), ann_fact(0.99999999), warmup(true) {} void init() { avg_cost = base->getCurrCost(); } void acceptEvent() { avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost; if (warmup) { - static double max_cost_diff = 0.0; - static int incr_cnt = 0; - double cost_diff = base->getCurrCost() - base->getPrevCost(); - if (cost_diff > 0.0) { - incr_cnt++; - if (cost_diff > max_cost_diff) { - max_cost_diff = cost_diff; - } - } - if (incr_cnt >= 100) { + static int cnt = 0; + cnt++; + if (cnt >= 100) { // calculate starting threshold and starting temperature - start_threshold = fabs(base->getBestCost() - avg_cost); - temp = max_cost_diff / log(0.5); + start_threshold = 5.0 * fabs(base->getBestCost() - avg_cost); + //temp = max_cost_diff / log(0.5); + temp = 10000.0; warmup = false; timer.reset(); } @@ -227,6 +226,7 @@ else { // increase the temperature temp *= gamma; + ann_fact = 0.99999999; } temp *= ann_fact; return elapsed_time < end_time; diff -r f1eb997f0418 -r 1cfb25ef14d2 src/work/akos/simann_demo.cc --- a/src/work/akos/simann_demo.cc Wed Jan 26 00:09:06 2005 +0000 +++ b/src/work/akos/simann_demo.cc Wed Jan 26 09:00:40 2005 +0000 @@ -10,15 +10,17 @@ }; int main() { + /* SimAnn simann; SimpleController ctrl; simann.setController(ctrl); MyEntity ent; simann.setEntity(ent); simann.run(); + */ SimAnn simann2; - AdvancedController ctrl2(20.0); + AdvancedController ctrl2(10.0); simann2.setController(ctrl2); MyEntity ent2; simann2.setEntity(ent2);