src/work/akos/simann_test.cc
author ladanyi
Thu, 04 Nov 2004 18:48:58 +0000
changeset 957 4dd4eaee28e7
parent 918 bb77eaa8fa0e
child 958 75f749682240
permissions -rw-r--r--
Now the controller asks SimAnnBase for the various costs.
ladanyi@942
     1
#include "simann.h"
ladanyi@918
     2
#include <cstdlib>
ladanyi@918
     3
#include <cmath>
ladanyi@918
     4
#include <iostream>
ladanyi@918
     5
ladanyi@942
     6
using namespace lemon;
ladanyi@942
     7
ladanyi@918
     8
class MyController : public SimAnnBase::Controller {
ladanyi@918
     9
public:
ladanyi@942
    10
  long iter, last_impr, max_iter, max_no_impr;
ladanyi@942
    11
  double temp, annealing_factor;
ladanyi@918
    12
  MyController() {
ladanyi@942
    13
    iter = last_impr = 0;
ladanyi@942
    14
    max_iter = 500000;
ladanyi@942
    15
    max_no_impr = 20000;
ladanyi@942
    16
    annealing_factor = 0.9999;
ladanyi@942
    17
    temp = 1000;
ladanyi@942
    18
  }
ladanyi@942
    19
  void acceptEvent() {
ladanyi@942
    20
    iter++;
ladanyi@942
    21
  }
ladanyi@942
    22
  void improveEvent() {
ladanyi@942
    23
    last_impr = iter;
ladanyi@942
    24
  }
ladanyi@942
    25
  void rejectEvent() {
ladanyi@942
    26
    iter++;
ladanyi@918
    27
  }
ladanyi@918
    28
  bool next() {
ladanyi@942
    29
    temp *= annealing_factor;
ladanyi@942
    30
    bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
ladanyi@942
    31
    return !quit;
ladanyi@918
    32
  }
ladanyi@942
    33
  bool accept(double cost_diff) {
ladanyi@942
    34
    return (drand48() <= exp(cost_diff / temp));
ladanyi@918
    35
  }
ladanyi@918
    36
};
ladanyi@918
    37
ladanyi@918
    38
class MyEntity {
ladanyi@918
    39
public:
ladanyi@918
    40
  double mutate() { return 10.0; }
ladanyi@918
    41
};
ladanyi@918
    42
ladanyi@918
    43
int main() {
ladanyi@942
    44
  SimAnn<MyEntity> simann;
ladanyi@942
    45
  MyController ctrl;
ladanyi@942
    46
  simann.setController(ctrl);
ladanyi@918
    47
  MyEntity ent;
ladanyi@942
    48
  simann.setEntity(ent);
ladanyi@942
    49
  simann.run();
ladanyi@918
    50
}