Various changes.
1.1 --- a/src/work/akos/simann.h Wed Jan 26 00:09:06 2005 +0000
1.2 +++ b/src/work/akos/simann.h Wed Jan 26 09:00:40 2005 +0000
1.3 @@ -98,7 +98,8 @@
1.4 prev_cost = prev_prev_cost;
1.5 }
1.6 void saveAsBest() {
1.7 - *best_ent = *curr_ent;
1.8 + delete(best_ent);
1.9 + best_ent = new E(*curr_ent);
1.10 best_cost = curr_cost;
1.11 }
1.12 };
1.13 @@ -129,7 +130,7 @@
1.14 * \param _ann_fact annealing factor
1.15 */
1.16 SimpleController(long _max_iter = 500000, long _max_no_impr = 20000,
1.17 - double _temp = 1000, double _ann_fact = 0.9999) : iter(0), last_impr(0),
1.18 + double _temp = 1000.0, double _ann_fact = 0.9999) : iter(0), last_impr(0),
1.19 max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp),
1.20 ann_fact(_ann_fact) {}
1.21 void acceptEvent() {
1.22 @@ -149,7 +150,8 @@
1.23 bool accept() {
1.24 double cost_diff = base->getPrevCost() - base->getCurrCost();
1.25 if (cost_diff < 0.0) {
1.26 - return (drand48() <= exp(cost_diff / temp));
1.27 + bool ret = drand48() <= exp(cost_diff / temp);
1.28 + return ret;
1.29 }
1.30 else {
1.31 return true;
1.32 @@ -166,10 +168,13 @@
1.33 Timer timer;
1.34 /*! \param time the elapsed time in seconds */
1.35 virtual double threshold(double time) {
1.36 - // this is the function 1 / log(x) scaled and offset
1.37 + // 1 / log(x)
1.38 + /*
1.39 static double xm = 5.0 / end_time;
1.40 static double ym = start_threshold / (1 / log(1.2) - 1 / log(5.0 + 1.2));
1.41 return ym * (1 / log(xm * time + 1.2) - 1 / log(5.0 + 1.2));
1.42 + */
1.43 + return (-1.0) * start_threshold / end_time * time + start_threshold;
1.44 }
1.45 public:
1.46 double alpha, beta, gamma;
1.47 @@ -184,27 +189,21 @@
1.48 * \param _gamma parameter used to increase the temperature
1.49 */
1.50 AdvancedController(double _end_time, double _alpha = 0.2,
1.51 - double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta),
1.52 - gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true) {}
1.53 + double _beta = 0.9, double _gamma = 1.6) : alpha(_alpha), beta(_beta),
1.54 + gamma(_gamma), end_time(_end_time), ann_fact(0.99999999), warmup(true) {}
1.55 void init() {
1.56 avg_cost = base->getCurrCost();
1.57 }
1.58 void acceptEvent() {
1.59 avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost;
1.60 if (warmup) {
1.61 - static double max_cost_diff = 0.0;
1.62 - static int incr_cnt = 0;
1.63 - double cost_diff = base->getCurrCost() - base->getPrevCost();
1.64 - if (cost_diff > 0.0) {
1.65 - incr_cnt++;
1.66 - if (cost_diff > max_cost_diff) {
1.67 - max_cost_diff = cost_diff;
1.68 - }
1.69 - }
1.70 - if (incr_cnt >= 100) {
1.71 + static int cnt = 0;
1.72 + cnt++;
1.73 + if (cnt >= 100) {
1.74 // calculate starting threshold and starting temperature
1.75 - start_threshold = fabs(base->getBestCost() - avg_cost);
1.76 - temp = max_cost_diff / log(0.5);
1.77 + start_threshold = 5.0 * fabs(base->getBestCost() - avg_cost);
1.78 + //temp = max_cost_diff / log(0.5);
1.79 + temp = 10000.0;
1.80 warmup = false;
1.81 timer.reset();
1.82 }
1.83 @@ -227,6 +226,7 @@
1.84 else {
1.85 // increase the temperature
1.86 temp *= gamma;
1.87 + ann_fact = 0.99999999;
1.88 }
1.89 temp *= ann_fact;
1.90 return elapsed_time < end_time;
2.1 --- a/src/work/akos/simann_demo.cc Wed Jan 26 00:09:06 2005 +0000
2.2 +++ b/src/work/akos/simann_demo.cc Wed Jan 26 09:00:40 2005 +0000
2.3 @@ -10,15 +10,17 @@
2.4 };
2.5
2.6 int main() {
2.7 + /*
2.8 SimAnn<MyEntity> simann;
2.9 SimpleController ctrl;
2.10 simann.setController(ctrl);
2.11 MyEntity ent;
2.12 simann.setEntity(ent);
2.13 simann.run();
2.14 + */
2.15
2.16 SimAnn<MyEntity> simann2;
2.17 - AdvancedController ctrl2(20.0);
2.18 + AdvancedController ctrl2(10.0);
2.19 simann2.setController(ctrl2);
2.20 MyEntity ent2;
2.21 simann2.setEntity(ent2);