COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/akos/SimAnn.h @ 926:b53cb2242a42

Last change on this file since 926:b53cb2242a42 was 918:bb77eaa8fa0e, checked in by Akos Ladanyi, 20 years ago

Initial revision.

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