Changeset 1000:7f4d07047ed8 in lemon0.x
 Timestamp:
 11/17/04 09:47:20 (16 years ago)
 Branch:
 default
 Phase:
 public
 Convert:
 svn:c9d7d8f590d60310b91f818b3a526b0e/lemon/trunk@1390
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/work/akos/simann.h
r966 r1000 52 52 } 53 53 54 /*! \brief A base class for controllers. */ 54 55 class Controller { 55 56 public: 56 57 SimAnnBase *base; 57 58 virtual void init() {} 59 /*! \brief This is called when a neighbouring state gets accepted. */ 58 60 virtual void acceptEvent() {} 61 /*! \brief This is called when the accepted neighbouring state's cost is 62 * less than the best found one's. 63 */ 59 64 virtual void improveEvent() {} 65 /*! \brief This is called when a neighbouring state gets rejected. */ 60 66 virtual void rejectEvent() {} 61 67 virtual void setBase(SimAnnBase *_base) { base = _base; } 68 /*! */ 62 69 virtual bool next() = 0; 70 /*! */ 63 71 virtual bool accept() = 0; 64 72 }; … … 107 115 public: 108 116 long iter, last_impr, max_iter, max_no_impr; 109 double temp, annealing_factor; 110 SimpleController() { 111 iter = last_impr = 0; 112 max_iter = 500000; 113 max_no_impr = 20000; 114 annealing_factor = 0.9999; 115 temp = 1000; 116 } 117 double temp, ann_fact; 118 /*! \param _max_iter maximum number of iterations 119 * \param _max_no_impr maximum number of consecutive iterations which do 120 * not yield a better solution 121 * \param _temp initial temperature 122 * \param _ann_fact annealing factor 123 */ 124 SimpleController(long _max_iter = 500000, long _max_no_impr = 20000, 125 double _temp = 1000, double _ann_fact = 0.9999) : iter(0), last_impr(0), 126 max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp), 127 ann_fact(_ann_fact) {} 117 128 void acceptEvent() { 118 129 iter++; … … 125 136 } 126 137 bool next() { 127 temp *= ann ealing_factor;138 temp *= ann_fact; 128 139 bool quit = (iter > max_iter)  (iter  last_impr > max_no_impr); 129 140 return !quit; … … 141 152 class AdvancedController : public SimAnnBase::Controller { 142 153 private: 143 double threshold() { return 0.0; } 144 public: 145 double alpha; 146 double temp; 154 /*! \param time the elapsed time in seconds */ 155 double threshold(double time) { return 0.0; } 156 public: 157 double alpha, beta, gamma; 158 double end_time, start_time; 147 159 double avg_cost; 148 double start_time, end_time; 160 double temp, ann_fact; 161 /*! \param _end_time running_time 162 * \param _alpha parameter used to calculate the running average 163 * \param _beta parameter used to decrease the annealing factor 164 * \param _gamma parameter used to increase the temperature 165 */ 166 AdvancedController(double _end_time, double _alpha = 0.2, 167 double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta), 168 gamma(_gamma), end_time(_end_time) {} 149 169 void init() { 150 170 timeval tv; … … 160 180 } 161 181 bool next() { 162 // abs(avg_cost  base>getBestCost())163 // ha nagy: cooling factor novelese164 // ha kicsi: homerseklet novelese165 // de mennyivel?166 182 timeval tv; 167 183 gettimeofday(&tv, 0); 168 184 double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6  start_time; 185 if (fabs(avg_cost  base>getBestCost()) > threshold(elapsed_time)) { 186 // decrease the annealing factor 187 ann_fact *= beta; 188 } 189 else { 190 // increase the temperature 191 temp *= gamma; 192 } 193 temp *= ann_fact; 169 194 return elapsed_time < end_time; 170 195 }
Note: See TracChangeset
for help on using the changeset viewer.