src/work/akos/simann.h
changeset 965 1e16b8dac159
parent 956 0ff924405d21
child 966 5e865c5c8a87
equal deleted inserted replaced
1:c6d04fa5aaa5 2:24626b7a3fe1
    20     virtual void saveAsBest() = 0;
    20     virtual void saveAsBest() = 0;
    21   public:
    21   public:
    22     SimAnnBase() {
    22     SimAnnBase() {
    23       curr_cost = prev_cost = best_cost = INFTY;
    23       curr_cost = prev_cost = best_cost = INFTY;
    24     }
    24     }
    25     void setController(Controller &_controller) { controller = &_controller; }
    25     void setController(Controller &_controller) {
       
    26       controller = &_controller;
       
    27       controller->setBase(this);
       
    28     }
       
    29     double getCurrCost() { return curr_cost; }
       
    30     double getPrevCost() { return prev_cost; }
    26     double getBestCost() { return best_cost; }
    31     double getBestCost() { return best_cost; }
    27     void run() {
    32     void run() {
    28       while (controller->next()) {
    33       while (controller->next()) {
    29         mutate();
    34         mutate();
    30         if (controller->accept(prev_cost - curr_cost)) {
    35         if (controller->accept()) {
    31           controller->acceptEvent();
    36           controller->acceptEvent();
    32           if (curr_cost < best_cost) {
    37           if (curr_cost < best_cost) {
    33             saveAsBest();
    38             saveAsBest();
    34             controller->improveEvent();
    39             controller->improveEvent();
    35           }
    40           }
    41       }
    46       }
    42     }
    47     }
    43 
    48 
    44     class Controller {
    49     class Controller {
    45     public:
    50     public:
       
    51       SimAnnBase *base;
    46       virtual void acceptEvent() {}
    52       virtual void acceptEvent() {}
    47       virtual void improveEvent() {}
    53       virtual void improveEvent() {}
    48       virtual void rejectEvent() {}
    54       virtual void rejectEvent() {}
       
    55       virtual void setBase(SimAnnBase *_base) { base = _base; }
    49       virtual bool next() = 0;
    56       virtual bool next() = 0;
    50       virtual bool accept(double cost_diff) = 0;
    57       virtual bool accept() = 0;
    51     };
    58     };
    52   };
    59   };
    53 
    60 
    54   template <typename E>
    61   template <typename E>
    55   class SimAnn : public SimAnnBase {
    62   class SimAnn : public SimAnnBase {
    56   private:
    63   private:
    57     E *curr_ent;
    64     E *curr_ent;
    58     E *best_ent;
    65     E *best_ent;
    59   public:
    66   public:
    60     SimAnn2() : SimAnnBase() {}
    67     SimAnn() : SimAnnBase() {}
    61     void setEntity(E &Ent) {
    68     void setEntity(E &ent) {
    62       curr_ent = new E(Ent);
    69       curr_ent = new E(ent);
    63       best_ent = new E(Ent);
    70       best_ent = new E(ent);
    64     }
    71     }
    65     E getBestEntity() { return *best_ent; }
    72     E getBestEntity() { return *best_ent; }
    66     void mutate() {
    73     void mutate() {
    67       curr_ent->mutate();
    74       curr_ent->mutate();
    68     }
    75     }
    86 
    93 
    87   class SimpleController : public SimAnnBase::Controller {
    94   class SimpleController : public SimAnnBase::Controller {
    88   public:
    95   public:
    89     long iter, last_impr, max_iter, max_no_impr;
    96     long iter, last_impr, max_iter, max_no_impr;
    90     double temp, annealing_factor;
    97     double temp, annealing_factor;
    91     MyController() {
    98     SimpleController() {
    92       iter = last_impr = 0;
    99       iter = last_impr = 0;
    93       max_iter = 500000;
   100       max_iter = 500000;
    94       max_no_impr = 20000;
   101       max_no_impr = 20000;
    95       annealing_factor = 0.9999;
   102       annealing_factor = 0.9999;
    96       temp = 1000;
   103       temp = 1000;
   107     bool next() {
   114     bool next() {
   108       temp *= annealing_factor;
   115       temp *= annealing_factor;
   109       bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
   116       bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
   110       return !quit;
   117       return !quit;
   111     }
   118     }
   112     bool accept(double cost_diff) {
   119     bool accept() {
   113       return (drand48() <= exp(cost_diff / temp));
   120       return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp));
   114     }
   121     }
   115   };
   122   };
   116 
   123 
   117 }
   124 }
   118 
   125