src/work/akos/simann_test.cc
changeset 950 d74557d1f100
parent 918 bb77eaa8fa0e
child 958 75f749682240
equal deleted inserted replaced
0:fa0a2af978c3 1:547c40cb5a80
     1 #include "SimAnn.h"
     1 #include "simann.h"
     2 #include <cstdlib>
     2 #include <cstdlib>
     3 #include <cmath>
     3 #include <cmath>
     4 #include <iostream>
     4 #include <iostream>
     5 
     5 
       
     6 using namespace lemon;
       
     7 
     6 class MyController : public SimAnnBase::Controller {
     8 class MyController : public SimAnnBase::Controller {
     7 public:
     9 public:
     8   long MaxIter, MaxNoImpr;
    10   long iter, last_impr, max_iter, max_no_impr;
     9   double af;
    11   double temp, annealing_factor;
    10   MyController() {
    12   MyController() {
    11     MaxIter = 500000;
    13     iter = last_impr = 0;
    12     MaxNoImpr = 20000;
    14     max_iter = 500000;
    13     af = 0.9999;
    15     max_no_impr = 20000;
    14     T = 1000;
    16     annealing_factor = 0.9999;
       
    17     temp = 1000;
       
    18   }
       
    19   void acceptEvent() {
       
    20     iter++;
       
    21   }
       
    22   void improveEvent() {
       
    23     last_impr = iter;
       
    24   }
       
    25   void rejectEvent() {
       
    26     iter++;
    15   }
    27   }
    16   bool next() {
    28   bool next() {
    17     T *= af;
    29     temp *= annealing_factor;
    18     std::cout << T << std::endl;
    30     bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
    19     return !((sab->getIter() > MaxIter) || (sab->getIter() - sab->getLastImpr() > MaxNoImpr));
    31     return !quit;
    20   }
    32   }
    21   bool accept(double cost) {
    33   bool accept(double cost_diff) {
    22     return (drand48() <= exp(cost / T));
    34     return (drand48() <= exp(cost_diff / temp));
    23   }
    35   }
    24 };
    36 };
    25 
    37 
    26 class MyEntity {
    38 class MyEntity {
    27 public:
    39 public:
    28   void init() {}
       
    29   double mutate() { return 10.0; }
    40   double mutate() { return 10.0; }
    30 };
    41 };
    31 
    42 
    32 int main() {
    43 int main() {
    33   SimAnn<MyEntity> sa;
    44   SimAnn<MyEntity> simann;
    34   MyController c;
    45   MyController ctrl;
    35   sa.setController(c);
    46   simann.setController(ctrl);
    36   MyEntity ent;
    47   MyEntity ent;
    37   sa.setEnt(ent);
    48   simann.setEntity(ent);
    38   sa.run();
    49   simann.run();
    39 }
    50 }