Added the SimpleController class, and removed the first version of SimAnn in favour of the second.
authorladanyi
Thu, 04 Nov 2004 13:32:44 +0000
changeset 9560ff924405d21
parent 955 0a066f80e05f
child 957 4dd4eaee28e7
Added the SimpleController class, and removed the first version of SimAnn in favour of the second.
src/work/akos/simann.h
     1.1 --- a/src/work/akos/simann.h	Tue Nov 02 06:28:38 2004 +0000
     1.2 +++ b/src/work/akos/simann.h	Thu Nov 04 13:32:44 2004 +0000
     1.3 @@ -55,43 +55,6 @@
     1.4    class SimAnn : public SimAnnBase {
     1.5    private:
     1.6      E *curr_ent;
     1.7 -    E *prev_ent;
     1.8 -    E *best_ent;
     1.9 -  public:
    1.10 -    SimAnn() : SimAnnBase() {}
    1.11 -    void setEntity(E &Ent) {
    1.12 -      curr_ent = new E(Ent);
    1.13 -      prev_ent = new E(Ent);
    1.14 -      best_ent = new E(Ent);
    1.15 -    }
    1.16 -    E getBestEntity() { return *best_ent; }
    1.17 -    void mutate() {
    1.18 -      *prev_ent = *curr_ent;
    1.19 -      prev_cost = curr_cost;
    1.20 -      curr_cost = curr_ent->mutate();
    1.21 -    }
    1.22 -    void revert() {
    1.23 -      E *tmp = curr_ent;
    1.24 -      curr_ent = prev_ent;
    1.25 -      prev_ent = tmp;
    1.26 -      curr_cost = prev_cost;
    1.27 -    }
    1.28 -    void saveAsBest() {
    1.29 -      *best_ent = *curr_ent;
    1.30 -      best_cost = curr_cost;
    1.31 -    }
    1.32 -  };
    1.33 -
    1.34 -  class EntitySkeleton {
    1.35 -  public:
    1.36 -    // returns the new cost
    1.37 -    double mutate() { return 0.0; }
    1.38 -  };
    1.39 -
    1.40 -  template <typename E>
    1.41 -  class SimAnn2 : public SimAnnBase {
    1.42 -  private:
    1.43 -    E *curr_ent;
    1.44      E *best_ent;
    1.45    public:
    1.46      SimAnn2() : SimAnnBase() {}
    1.47 @@ -112,7 +75,7 @@
    1.48      }
    1.49    };
    1.50  
    1.51 -  class EntitySkeleton2 {
    1.52 +  class EntitySkeleton {
    1.53    public:
    1.54      // returns the new cost
    1.55      double mutate() { return 0.0; }
    1.56 @@ -121,6 +84,36 @@
    1.57      void revert() {}
    1.58    };
    1.59  
    1.60 +  class SimpleController : public SimAnnBase::Controller {
    1.61 +  public:
    1.62 +    long iter, last_impr, max_iter, max_no_impr;
    1.63 +    double temp, annealing_factor;
    1.64 +    MyController() {
    1.65 +      iter = last_impr = 0;
    1.66 +      max_iter = 500000;
    1.67 +      max_no_impr = 20000;
    1.68 +      annealing_factor = 0.9999;
    1.69 +      temp = 1000;
    1.70 +    }
    1.71 +    void acceptEvent() {
    1.72 +      iter++;
    1.73 +    }
    1.74 +    void improveEvent() {
    1.75 +      last_impr = iter;
    1.76 +    }
    1.77 +    void rejectEvent() {
    1.78 +      iter++;
    1.79 +    }
    1.80 +    bool next() {
    1.81 +      temp *= annealing_factor;
    1.82 +      bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
    1.83 +      return !quit;
    1.84 +    }
    1.85 +    bool accept(double cost_diff) {
    1.86 +      return (drand48() <= exp(cost_diff / temp));
    1.87 +    }
    1.88 +  };
    1.89 +
    1.90  }
    1.91  
    1.92  #endif