| [942] | 1 | #ifndef LEMON_SIMANN_H | 
|---|
 | 2 | #define LEMON_SIMANN_H | 
|---|
| [918] | 3 |  | 
|---|
| [942] | 4 | namespace lemon { | 
|---|
| [918] | 5 |  | 
|---|
| [942] | 6 |   const double INFTY = 1e24; | 
|---|
| [918] | 7 |  | 
|---|
| [942] | 8 |   class SimAnnBase { | 
|---|
| [918] | 9 |   public: | 
|---|
| [942] | 10 |     class Controller; | 
|---|
 | 11 |   private: | 
|---|
 | 12 |     Controller *controller; | 
|---|
 | 13 |   protected: | 
|---|
 | 14 |     double curr_cost; | 
|---|
 | 15 |     double prev_cost; | 
|---|
 | 16 |     double best_cost; | 
|---|
| [918] | 17 |  | 
|---|
| [942] | 18 |     virtual void mutate() = 0; | 
|---|
 | 19 |     virtual void revert() = 0; | 
|---|
 | 20 |     virtual void saveAsBest() = 0; | 
|---|
 | 21 |   public: | 
|---|
 | 22 |     SimAnnBase() { | 
|---|
 | 23 |       curr_cost = prev_cost = best_cost = INFTY; | 
|---|
 | 24 |     } | 
|---|
 | 25 |     void setController(Controller &_controller) { controller = &_controller; } | 
|---|
 | 26 |     double getBestCost() { return best_cost; } | 
|---|
 | 27 |     void run() { | 
|---|
 | 28 |       while (controller->next()) { | 
|---|
 | 29 |         mutate(); | 
|---|
 | 30 |         if (controller->accept(prev_cost - curr_cost)) { | 
|---|
 | 31 |           controller->acceptEvent(); | 
|---|
 | 32 |           if (curr_cost < best_cost) { | 
|---|
 | 33 |             saveAsBest(); | 
|---|
 | 34 |             controller->improveEvent(); | 
|---|
 | 35 |           } | 
|---|
 | 36 |         } | 
|---|
 | 37 |         else { | 
|---|
 | 38 |           revert(); | 
|---|
 | 39 |           controller->rejectEvent(); | 
|---|
 | 40 |         } | 
|---|
| [918] | 41 |       } | 
|---|
 | 42 |     } | 
|---|
 | 43 |  | 
|---|
| [942] | 44 |     class Controller { | 
|---|
 | 45 |     public: | 
|---|
 | 46 |       virtual void acceptEvent() {} | 
|---|
 | 47 |       virtual void improveEvent() {} | 
|---|
 | 48 |       virtual void rejectEvent() {} | 
|---|
 | 49 |       virtual bool next() = 0; | 
|---|
 | 50 |       virtual bool accept(double cost_diff) = 0; | 
|---|
 | 51 |     }; | 
|---|
 | 52 |   }; | 
|---|
| [918] | 53 |  | 
|---|
| [942] | 54 |   template <typename E> | 
|---|
 | 55 |   class SimAnn : public SimAnnBase { | 
|---|
 | 56 |   private: | 
|---|
 | 57 |     E *curr_ent; | 
|---|
 | 58 |     E *prev_ent; | 
|---|
 | 59 |     E *best_ent; | 
|---|
 | 60 |   public: | 
|---|
 | 61 |     SimAnn() : SimAnnBase() {} | 
|---|
 | 62 |     void setEntity(E &Ent) { | 
|---|
 | 63 |       curr_ent = new E(Ent); | 
|---|
 | 64 |       prev_ent = new E(Ent); | 
|---|
 | 65 |       best_ent = new E(Ent); | 
|---|
 | 66 |     } | 
|---|
 | 67 |     E getBestEntity() { return *best_ent; } | 
|---|
 | 68 |     void mutate() { | 
|---|
 | 69 |       *prev_ent = *curr_ent; | 
|---|
 | 70 |       prev_cost = curr_cost; | 
|---|
 | 71 |       curr_cost = curr_ent->mutate(); | 
|---|
 | 72 |     } | 
|---|
 | 73 |     void revert() { | 
|---|
 | 74 |       E *tmp = curr_ent; | 
|---|
 | 75 |       curr_ent = prev_ent; | 
|---|
 | 76 |       prev_ent = tmp; | 
|---|
 | 77 |       curr_cost = prev_cost; | 
|---|
 | 78 |     } | 
|---|
 | 79 |     void saveAsBest() { | 
|---|
 | 80 |       *best_ent = *curr_ent; | 
|---|
 | 81 |       best_cost = curr_cost; | 
|---|
 | 82 |     } | 
|---|
 | 83 |   }; | 
|---|
 | 84 |  | 
|---|
 | 85 |   class EntitySkeleton { | 
|---|
 | 86 |   public: | 
|---|
 | 87 |     // returns the new cost | 
|---|
 | 88 |     double mutate() { return 0.0; } | 
|---|
 | 89 |   }; | 
|---|
 | 90 |  | 
|---|
 | 91 |   template <typename E> | 
|---|
 | 92 |   class SimAnn2 : public SimAnnBase { | 
|---|
 | 93 |   private: | 
|---|
 | 94 |     E *curr_ent; | 
|---|
 | 95 |     E *best_ent; | 
|---|
 | 96 |   public: | 
|---|
 | 97 |     SimAnn2() : SimAnnBase() {} | 
|---|
 | 98 |     void setEntity(E &Ent) { | 
|---|
 | 99 |       curr_ent = new E(Ent); | 
|---|
 | 100 |       best_ent = new E(Ent); | 
|---|
 | 101 |     } | 
|---|
 | 102 |     E getBestEntity() { return *best_ent; } | 
|---|
 | 103 |     void mutate() { | 
|---|
 | 104 |       curr_ent->mutate(); | 
|---|
 | 105 |     } | 
|---|
 | 106 |     void revert() { | 
|---|
 | 107 |       curr_ent->revert(); | 
|---|
 | 108 |     } | 
|---|
 | 109 |     void saveAsBest() { | 
|---|
 | 110 |       *best_ent = *curr_ent; | 
|---|
 | 111 |       best_cost = curr_cost; | 
|---|
 | 112 |     } | 
|---|
 | 113 |   }; | 
|---|
 | 114 |  | 
|---|
 | 115 |   class EntitySkeleton2 { | 
|---|
 | 116 |   public: | 
|---|
 | 117 |     // returns the new cost | 
|---|
 | 118 |     double mutate() { return 0.0; } | 
|---|
 | 119 |     // restores the entity to its previous state i.e. reverts the effects of | 
|---|
 | 120 |     // the last mutate() | 
|---|
 | 121 |     void revert() {} | 
|---|
 | 122 |   }; | 
|---|
 | 123 |  | 
|---|
 | 124 | } | 
|---|
| [918] | 125 |  | 
|---|
 | 126 | #endif | 
|---|