Now the controller asks SimAnnBase for the various costs.
authorladanyi
Thu, 04 Nov 2004 18:48:58 +0000
changeset 9574dd4eaee28e7
parent 956 0ff924405d21
child 958 75f749682240
Now the controller asks SimAnnBase for the various costs.
src/work/akos/simann.h
     1.1 --- a/src/work/akos/simann.h	Thu Nov 04 13:32:44 2004 +0000
     1.2 +++ b/src/work/akos/simann.h	Thu Nov 04 18:48:58 2004 +0000
     1.3 @@ -22,12 +22,17 @@
     1.4      SimAnnBase() {
     1.5        curr_cost = prev_cost = best_cost = INFTY;
     1.6      }
     1.7 -    void setController(Controller &_controller) { controller = &_controller; }
     1.8 +    void setController(Controller &_controller) {
     1.9 +      controller = &_controller;
    1.10 +      controller->setBase(this);
    1.11 +    }
    1.12 +    double getCurrCost() { return curr_cost; }
    1.13 +    double getPrevCost() { return prev_cost; }
    1.14      double getBestCost() { return best_cost; }
    1.15      void run() {
    1.16        while (controller->next()) {
    1.17          mutate();
    1.18 -        if (controller->accept(prev_cost - curr_cost)) {
    1.19 +        if (controller->accept()) {
    1.20            controller->acceptEvent();
    1.21            if (curr_cost < best_cost) {
    1.22              saveAsBest();
    1.23 @@ -43,11 +48,13 @@
    1.24  
    1.25      class Controller {
    1.26      public:
    1.27 +      SimAnnBase *base;
    1.28        virtual void acceptEvent() {}
    1.29        virtual void improveEvent() {}
    1.30        virtual void rejectEvent() {}
    1.31 +      virtual void setBase(SimAnnBase *_base) { base = _base; }
    1.32        virtual bool next() = 0;
    1.33 -      virtual bool accept(double cost_diff) = 0;
    1.34 +      virtual bool accept() = 0;
    1.35      };
    1.36    };
    1.37  
    1.38 @@ -57,10 +64,10 @@
    1.39      E *curr_ent;
    1.40      E *best_ent;
    1.41    public:
    1.42 -    SimAnn2() : SimAnnBase() {}
    1.43 -    void setEntity(E &Ent) {
    1.44 -      curr_ent = new E(Ent);
    1.45 -      best_ent = new E(Ent);
    1.46 +    SimAnn() : SimAnnBase() {}
    1.47 +    void setEntity(E &ent) {
    1.48 +      curr_ent = new E(ent);
    1.49 +      best_ent = new E(ent);
    1.50      }
    1.51      E getBestEntity() { return *best_ent; }
    1.52      void mutate() {
    1.53 @@ -88,7 +95,7 @@
    1.54    public:
    1.55      long iter, last_impr, max_iter, max_no_impr;
    1.56      double temp, annealing_factor;
    1.57 -    MyController() {
    1.58 +    SimpleController() {
    1.59        iter = last_impr = 0;
    1.60        max_iter = 500000;
    1.61        max_no_impr = 20000;
    1.62 @@ -109,8 +116,8 @@
    1.63        bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
    1.64        return !quit;
    1.65      }
    1.66 -    bool accept(double cost_diff) {
    1.67 -      return (drand48() <= exp(cost_diff / temp));
    1.68 +    bool accept() {
    1.69 +      return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp));
    1.70      }
    1.71    };
    1.72