src/work/akos/SimAnn.h
author deba
Mon, 04 Oct 2004 17:13:21 +0000
changeset 937 d4e911acef3d
permissions -rw-r--r--
Revert backport changes -r1230.
ladanyi@918
     1
#ifndef SIMANN_H
ladanyi@918
     2
#define SIMANN_H
ladanyi@918
     3
ladanyi@918
     4
#ifndef INFTY
ladanyi@918
     5
#define INFTY 1e24
ladanyi@918
     6
#endif
ladanyi@918
     7
ladanyi@918
     8
#include <iostream>
ladanyi@918
     9
ladanyi@918
    10
class SimAnnBase {
ladanyi@918
    11
public:
ladanyi@918
    12
  friend class Controller;
ladanyi@918
    13
  class Controller {
ladanyi@918
    14
  public:
ladanyi@918
    15
    SimAnnBase *sab;
ladanyi@918
    16
    double T;
ladanyi@918
    17
    void init(SimAnnBase *_sab) { sab = _sab; }
ladanyi@918
    18
    bool next() {}
ladanyi@918
    19
    bool accept(double cost) {}
ladanyi@918
    20
  };
ladanyi@918
    21
protected:
ladanyi@918
    22
  double CurrCost;
ladanyi@918
    23
  double PrevCost;
ladanyi@918
    24
  double BestCost;
ladanyi@918
    25
ladanyi@918
    26
  long Iter;
ladanyi@918
    27
  long LastImpr;
ladanyi@918
    28
ladanyi@918
    29
  //friend class Controller;
ladanyi@918
    30
  Controller *Ctrl;
ladanyi@918
    31
public:
ladanyi@918
    32
  long getIter() { return Iter; }
ladanyi@918
    33
  long getLastImpr() { return LastImpr; }
ladanyi@918
    34
  double getBestCost() { return BestCost; }
ladanyi@918
    35
  virtual void mutate() = 0;
ladanyi@918
    36
  virtual void revert() = 0;
ladanyi@918
    37
  virtual void saveAsBest() = 0;
ladanyi@918
    38
  virtual void init() = 0;
ladanyi@918
    39
ladanyi@918
    40
  void run() {
ladanyi@918
    41
    init();
ladanyi@918
    42
    while (Ctrl->next()) {
ladanyi@918
    43
      Iter++;
ladanyi@918
    44
      std::cout << Iter << std::endl;
ladanyi@918
    45
      mutate();
ladanyi@918
    46
      if (Ctrl->accept(PrevCost - CurrCost) && (CurrCost < BestCost)) {
ladanyi@918
    47
	LastImpr = Iter;
ladanyi@918
    48
        saveAsBest();
ladanyi@918
    49
      }
ladanyi@918
    50
      else {
ladanyi@918
    51
        revert();
ladanyi@918
    52
      }
ladanyi@918
    53
    }
ladanyi@918
    54
  }
ladanyi@918
    55
};
ladanyi@918
    56
ladanyi@918
    57
template <typename E>
ladanyi@918
    58
class SimAnn : public SimAnnBase {
ladanyi@918
    59
private:
ladanyi@918
    60
  E *CurrEnt;
ladanyi@918
    61
  E *PrevEnt;
ladanyi@918
    62
  E *BestEnt;
ladanyi@918
    63
public:
ladanyi@918
    64
  SimAnn() {
ladanyi@918
    65
    CurrCost = PrevCost = BestCost = INFTY;
ladanyi@918
    66
    Iter = LastImpr = 0;
ladanyi@918
    67
  }
ladanyi@918
    68
  void setController(Controller &_Ctrl) { Ctrl = &_Ctrl; }
ladanyi@918
    69
  void setEnt(E &Ent) {
ladanyi@918
    70
    CurrEnt = new E(Ent);
ladanyi@918
    71
    PrevEnt = new E(Ent);
ladanyi@918
    72
    BestEnt = new E(Ent);
ladanyi@918
    73
  }
ladanyi@918
    74
  E getBestEnt() { return *BestEnt; }
ladanyi@918
    75
  void mutate() {
ladanyi@918
    76
    *PrevEnt = *CurrEnt;
ladanyi@918
    77
    PrevCost = CurrCost;
ladanyi@918
    78
    CurrCost = CurrEnt->mutate();
ladanyi@918
    79
  }
ladanyi@918
    80
  void revert() {
ladanyi@918
    81
    E *tmp = CurrEnt;
ladanyi@918
    82
    CurrEnt = PrevEnt;
ladanyi@918
    83
    PrevEnt = tmp;
ladanyi@918
    84
    CurrCost = PrevCost;
ladanyi@918
    85
  }
ladanyi@918
    86
  void saveAsBest() {
ladanyi@918
    87
    *BestEnt = *CurrEnt;
ladanyi@918
    88
    BestCost = CurrCost;
ladanyi@918
    89
  }
ladanyi@918
    90
  void init() {
ladanyi@918
    91
    Ctrl->init(this);
ladanyi@918
    92
    CurrEnt->init();
ladanyi@918
    93
  }
ladanyi@918
    94
};
ladanyi@918
    95
ladanyi@918
    96
class EntitySkeleton {
ladanyi@918
    97
public:
ladanyi@918
    98
  void init() {}
ladanyi@918
    99
  // returns the new cost
ladanyi@918
   100
  double mutate() { return 0.0; }
ladanyi@918
   101
};
ladanyi@918
   102
ladanyi@918
   103
#endif