Rev | Line | |
---|
[918] | 1 | #ifndef SIMANN_H |
---|
| 2 | #define SIMANN_H |
---|
| 3 | |
---|
| 4 | #ifndef INFTY |
---|
| 5 | #define INFTY 1e24 |
---|
| 6 | #endif |
---|
| 7 | |
---|
| 8 | #include <iostream> |
---|
| 9 | |
---|
| 10 | class SimAnnBase { |
---|
| 11 | public: |
---|
| 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 | }; |
---|
| 21 | protected: |
---|
| 22 | double CurrCost; |
---|
| 23 | double PrevCost; |
---|
| 24 | double BestCost; |
---|
| 25 | |
---|
| 26 | long Iter; |
---|
| 27 | long LastImpr; |
---|
| 28 | |
---|
| 29 | //friend class Controller; |
---|
| 30 | Controller *Ctrl; |
---|
| 31 | public: |
---|
| 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 | |
---|
| 57 | template <typename E> |
---|
| 58 | class SimAnn : public SimAnnBase { |
---|
| 59 | private: |
---|
| 60 | E *CurrEnt; |
---|
| 61 | E *PrevEnt; |
---|
| 62 | E *BestEnt; |
---|
| 63 | public: |
---|
| 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 | |
---|
| 96 | class EntitySkeleton { |
---|
| 97 | public: |
---|
| 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.