Changeset 966:5e865c5c8a87 in lemon-0.x for src/work/akos
- Timestamp:
- 11/08/04 09:40:37 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1351
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/akos/simann.h
r957 r966 1 1 #ifndef LEMON_SIMANN_H 2 2 #define LEMON_SIMANN_H 3 4 #include <cstdlib> 5 #include <cmath> 6 #include <sys/time.h> 3 7 4 8 namespace lemon { … … 31 35 double getBestCost() { return best_cost; } 32 36 void run() { 37 controller->init(); 33 38 while (controller->next()) { 34 39 mutate(); … … 50 55 public: 51 56 SimAnnBase *base; 57 virtual void init() {} 52 58 virtual void acceptEvent() {} 53 59 virtual void improveEvent() {} … … 85 91 class EntitySkeleton { 86 92 public: 87 // returns the new cost 93 /*! \brief Makes a minor change to the entity. 94 * \return the new cost 95 */ 88 96 double mutate() { return 0.0; } 89 // restores the entity to its previous state i.e. reverts the effects of 90 // the last mutate() 97 /*! \brief Restores the entity to its previous state i.e. reverts the 98 * effects of the last mutate. 99 */ 91 100 void revert() {} 92 101 }; 93 102 103 /*! \brief A simple controller for the simulated annealing class. 104 * \todo Find a way to set the various parameters. 105 */ 94 106 class SimpleController : public SimAnnBase::Controller { 95 107 public: … … 118 130 } 119 131 bool accept() { 120 return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp)); 132 return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / 133 temp)); 134 } 135 }; 136 137 /*! \brief A controller with preset running time for the simulated annealing 138 * class. 139 * \todo Find a better name. 140 */ 141 class AdvancedController : public SimAnnBase::Controller { 142 private: 143 double threshold() { return 0.0; } 144 public: 145 double alpha; 146 double temp; 147 double avg_cost; 148 double start_time, end_time; 149 void init() { 150 timeval tv; 151 gettimeofday(&tv, 0); 152 start_time = tv.tv_sec + double(tv.tv_usec) / 1e6; 153 } 154 void acceptEvent() { 155 avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost; 156 } 157 void improveEvent() { 158 } 159 void rejectEvent() { 160 } 161 bool next() { 162 // abs(avg_cost - base->getBestCost()) 163 // ha nagy: cooling factor novelese 164 // ha kicsi: homerseklet novelese 165 // de mennyivel? 166 timeval tv; 167 gettimeofday(&tv, 0); 168 double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time; 169 return elapsed_time < end_time; 170 } 171 bool accept() { 172 return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / 173 temp)); 121 174 } 122 175 };
Note: See TracChangeset
for help on using the changeset viewer.