Changeset 1023:3268fef5d623 in lemon-0.x for src/work/akos
- Timestamp:
- 11/29/04 16:30:11 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1413
- Location:
- src/work/akos
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/akos/simann.h
r1018 r1023 17 17 protected: 18 18 double curr_cost; 19 double best_cost; 19 20 double prev_cost; 20 double best_cost;21 double prev_prev_cost; 21 22 22 23 virtual void mutate() = 0; … … 25 26 public: 26 27 SimAnnBase() { 27 curr_cost = prev_cost = best_cost = INFTY;28 best_cost = prev_cost = prev_prev_cost = INFTY; 28 29 } 29 30 void setController(Controller &_controller) { … … 73 74 }; 74 75 75 /*! \todo atgondolni mi is ez a prev_cost */76 76 template <typename E> 77 77 class SimAnn : public SimAnnBase { … … 84 84 curr_ent = new E(ent); 85 85 best_ent = new E(ent); 86 curr_cost = curr_ent->getCost(); 86 87 } 87 88 E getBestEntity() { return *best_ent; } 88 89 void mutate() { 90 prev_prev_cost = prev_cost; 89 91 prev_cost = curr_cost; 90 curr_cost = curr_ent->mutate(); 92 curr_ent->mutate(); 93 curr_cost = curr_ent->getCost(); 91 94 } 92 95 void revert() { 93 96 curr_ent->revert(); 94 97 curr_cost = prev_cost; 98 prev_cost = prev_prev_cost; 95 99 } 96 100 void saveAsBest() { … … 102 106 class EntitySkeleton { 103 107 public: 104 /*! \ brief Makes a minor change to the entity.105 * \return the new cost106 */107 double mutate() { return 0.0;}108 /*! \return the cost of the entity */ 109 double getCost() { return 0.0; } 110 /*! \brief Makes a minor change to the entity. */ 111 void mutate() {} 108 112 /*! \brief Restores the entity to its previous state i.e. reverts the 109 113 * effects of the last mutate. … … 175 179 double temp, ann_fact; 176 180 bool warmup; 177 long iter;178 181 /*! \param _end_time running time in seconds 179 182 * \param _alpha parameter used to calculate the running average … … 183 186 AdvancedController(double _end_time, double _alpha = 0.2, 184 187 double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta), 185 gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true), 186 iter(0) {} 188 gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true) {} 187 189 void init() { 188 190 avg_cost = base->getCurrCost(); … … 190 192 void acceptEvent() { 191 193 avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost; 192 iter++; 194 if (warmup) { 195 static double max_cost_diff = 0.0; 196 static int incr_cnt = 0; 197 double cost_diff = base->getCurrCost() - base->getPrevCost(); 198 if (cost_diff > 0.0) { 199 incr_cnt++; 200 if (cost_diff > max_cost_diff) { 201 max_cost_diff = cost_diff; 202 } 203 } 204 if (incr_cnt >= 100) { 205 // calculate starting threshold and starting temperature 206 start_threshold = fabs(base->getBestCost() - avg_cost); 207 temp = max_cost_diff / log(0.5); 208 warmup = false; 209 timer.reset(); 210 } 211 } 193 212 } 194 213 void improveEvent() { 195 214 } 196 215 void rejectEvent() { 197 iter++;198 216 } 199 217 bool next() { 200 218 if (warmup) { 201 static double max_cost_diff = 0.0;202 double cost_diff = base->getCurrCost() - base->getPrevCost();203 // jo ez igy egyaltalan? -> prev_cost204 if ((cost_diff > 0.0) && (cost_diff > max_cost_diff)) {205 max_cost_diff = cost_diff;206 }207 // How to set the starting temperature when all the 100 first208 // iterations improve the solution?209 if (iter > 100) {210 // calculate starting threshold and starting temperature211 start_threshold = fabs(base->getBestCost() - avg_cost);212 temp = exp(max_cost_diff) / 0.5;213 warmup = false;214 timer.reset();215 }216 219 return true; 217 220 } -
src/work/akos/simann_demo.cc
r999 r1023 5 5 class MyEntity { 6 6 public: 7 double mutate() { return 10.0; } 7 double getCost() { return 10.0; } 8 void mutate() {} 8 9 void revert() {} 9 10 };
Note: See TracChangeset
for help on using the changeset viewer.