[1956] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1633] | 19 | #ifndef LEMON_SIMANN_H |
---|
| 20 | #define LEMON_SIMANN_H |
---|
| 21 | |
---|
| 22 | /// \ingroup experimental |
---|
| 23 | /// \file |
---|
| 24 | /// \brief Simulated annealing framework. |
---|
[1847] | 25 | /// |
---|
| 26 | /// \todo A test and some demo should be added |
---|
| 27 | /// \todo Doc should be improved |
---|
[1633] | 28 | /// \author Akos Ladanyi |
---|
| 29 | |
---|
| 30 | #include <cstdlib> |
---|
| 31 | #include <cmath> |
---|
[1918] | 32 | #include <limits> |
---|
[1633] | 33 | #include <lemon/time_measure.h> |
---|
[2229] | 34 | #include <lemon/random.h> |
---|
[2035] | 35 | |
---|
[1633] | 36 | namespace lemon { |
---|
| 37 | |
---|
| 38 | /// \addtogroup experimental |
---|
| 39 | /// @{ |
---|
| 40 | |
---|
[1932] | 41 | class SimAnnBase; |
---|
| 42 | |
---|
[1918] | 43 | /// \brief A base class for controllers. |
---|
[1633] | 44 | class ControllerBase { |
---|
[1918] | 45 | public: |
---|
[1633] | 46 | friend class SimAnnBase; |
---|
[1918] | 47 | /// \brief Pointer to the simulated annealing base class. |
---|
[1633] | 48 | SimAnnBase *simann; |
---|
[1918] | 49 | /// \brief Initializes the controller. |
---|
[1633] | 50 | virtual void init() {} |
---|
[1918] | 51 | /// \brief This is called by the simulated annealing class when a |
---|
| 52 | /// neighbouring state gets accepted. |
---|
[1633] | 53 | virtual void acceptEvent() {} |
---|
[1918] | 54 | /// \brief This is called by the simulated annealing class when the |
---|
| 55 | /// accepted neighbouring state's cost is less than the best found one's. |
---|
[1633] | 56 | virtual void improveEvent() {} |
---|
[1918] | 57 | /// \brief This is called by the simulated annealing class when a |
---|
| 58 | /// neighbouring state gets rejected. |
---|
[1633] | 59 | virtual void rejectEvent() {} |
---|
[1918] | 60 | /// \brief Decides whether to continue the annealing process or not. |
---|
[1633] | 61 | virtual bool next() = 0; |
---|
[1918] | 62 | /// \brief Decides whether to accept the current solution or not. |
---|
[1633] | 63 | virtual bool accept() = 0; |
---|
[1918] | 64 | /// \brief Destructor. |
---|
| 65 | virtual ~ControllerBase() {} |
---|
[1633] | 66 | }; |
---|
| 67 | |
---|
[1918] | 68 | /// \brief Skeleton of an entity class. |
---|
[1633] | 69 | class EntityBase { |
---|
| 70 | public: |
---|
[1918] | 71 | /// \brief Makes a minor change to the entity. |
---|
| 72 | /// \return the new cost |
---|
[1633] | 73 | virtual double mutate() = 0; |
---|
[1918] | 74 | /// \brief Restores the entity to its previous state i.e. reverts the |
---|
| 75 | /// effects of the last mutate(). |
---|
[1633] | 76 | virtual void revert() = 0; |
---|
[1918] | 77 | /// \brief Makes a copy of the entity. |
---|
[1633] | 78 | virtual EntityBase* clone() = 0; |
---|
[1918] | 79 | /// \brief Makes a major change to the entity. |
---|
[1633] | 80 | virtual void randomize() = 0; |
---|
[1918] | 81 | /// \brief Destructor. |
---|
| 82 | virtual ~EntityBase() {} |
---|
[1633] | 83 | }; |
---|
| 84 | |
---|
[1918] | 85 | /// \brief Simulated annealing abstract base class. |
---|
[2304] | 86 | /// |
---|
[1918] | 87 | /// Can be used to derive a custom simulated annealing class if \ref SimAnn |
---|
| 88 | /// doesn't fit your needs. |
---|
[1633] | 89 | class SimAnnBase { |
---|
| 90 | private: |
---|
[1918] | 91 | /// \brief Pointer to the controller. |
---|
[1633] | 92 | ControllerBase *controller; |
---|
[1918] | 93 | /// \brief Cost of the current solution. |
---|
[1633] | 94 | double curr_cost; |
---|
[1918] | 95 | /// \brief Cost of the best solution. |
---|
[1633] | 96 | double best_cost; |
---|
[1918] | 97 | /// \brief Cost of the previous solution. |
---|
[1633] | 98 | double prev_cost; |
---|
[1918] | 99 | /// \brief Cost of the solution preceding the previous one. |
---|
[1633] | 100 | double prev_prev_cost; |
---|
[1918] | 101 | /// \brief Number of iterations. |
---|
[1633] | 102 | long iter; |
---|
[1918] | 103 | /// \brief Number of iterations which did not improve the solution since |
---|
| 104 | /// the last improvement. |
---|
[1633] | 105 | long last_impr; |
---|
| 106 | protected: |
---|
[1918] | 107 | /// \brief Step to a neighbouring state. |
---|
[1633] | 108 | virtual double mutate() = 0; |
---|
[1918] | 109 | /// \brief Reverts the last mutate(). |
---|
[1633] | 110 | virtual void revert() = 0; |
---|
[1918] | 111 | /// \brief Saves the current solution as the best one. |
---|
[1633] | 112 | virtual void saveAsBest() = 0; |
---|
[1918] | 113 | /// \brief Does initializations before each run. |
---|
[1633] | 114 | virtual void init() { |
---|
| 115 | controller->init(); |
---|
| 116 | curr_cost = prev_cost = prev_prev_cost = best_cost = |
---|
| 117 | std::numeric_limits<double>::infinity(); |
---|
| 118 | iter = last_impr = 0; |
---|
| 119 | } |
---|
| 120 | public: |
---|
[1918] | 121 | /// \brief Sets the controller class to use. |
---|
[1633] | 122 | void setController(ControllerBase &_controller) { |
---|
| 123 | controller = &_controller; |
---|
| 124 | controller->simann = this; |
---|
| 125 | } |
---|
[1918] | 126 | /// \brief Returns the cost of the current solution. |
---|
[1633] | 127 | double getCurrCost() const { return curr_cost; } |
---|
[1918] | 128 | /// \brief Returns the cost of the previous solution. |
---|
[1633] | 129 | double getPrevCost() const { return prev_cost; } |
---|
[1918] | 130 | /// \brief Returns the cost of the best solution. |
---|
[1633] | 131 | double getBestCost() const { return best_cost; } |
---|
[1918] | 132 | /// \brief Returns the number of iterations done. |
---|
[1633] | 133 | long getIter() const { return iter; } |
---|
[1918] | 134 | /// \brief Returns the ordinal number of the last iteration when the |
---|
| 135 | /// solution was improved. |
---|
[1633] | 136 | long getLastImpr() const { return last_impr; } |
---|
[1918] | 137 | /// \brief Performs one iteration. |
---|
[1633] | 138 | bool step() { |
---|
| 139 | iter++; |
---|
| 140 | prev_prev_cost = prev_cost; |
---|
| 141 | prev_cost = curr_cost; |
---|
| 142 | curr_cost = mutate(); |
---|
| 143 | if (controller->accept()) { |
---|
| 144 | controller->acceptEvent(); |
---|
| 145 | last_impr = iter; |
---|
| 146 | if (curr_cost < best_cost) { |
---|
| 147 | best_cost = curr_cost; |
---|
| 148 | saveAsBest(); |
---|
| 149 | controller->improveEvent(); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | else { |
---|
| 153 | revert(); |
---|
| 154 | curr_cost = prev_cost; |
---|
| 155 | prev_cost = prev_prev_cost; |
---|
| 156 | controller->rejectEvent(); |
---|
| 157 | } |
---|
| 158 | return controller->next(); |
---|
| 159 | } |
---|
[1918] | 160 | /// \brief Performs a given number of iterations. |
---|
| 161 | /// \param n the number of iterations |
---|
[1633] | 162 | bool step(int n) { |
---|
| 163 | for(; n > 0 && step(); --n) ; |
---|
| 164 | return !n; |
---|
| 165 | } |
---|
[1918] | 166 | /// \brief Starts the annealing process. |
---|
[1633] | 167 | void run() { |
---|
| 168 | init(); |
---|
| 169 | do { } while (step()); |
---|
| 170 | } |
---|
[1918] | 171 | /// \brief Destructor. |
---|
| 172 | virtual ~SimAnnBase() {} |
---|
[1633] | 173 | }; |
---|
| 174 | |
---|
[1918] | 175 | /// \brief Simulated annealing class. |
---|
[1633] | 176 | class SimAnn : public SimAnnBase { |
---|
| 177 | private: |
---|
[1918] | 178 | /// \brief Pointer to the current entity. |
---|
[1633] | 179 | EntityBase *curr_ent; |
---|
[1918] | 180 | /// \brief Pointer to the best entity. |
---|
[1633] | 181 | EntityBase *best_ent; |
---|
[1918] | 182 | /// \brief Does initializations before each run. |
---|
[1633] | 183 | void init() { |
---|
| 184 | SimAnnBase::init(); |
---|
| 185 | if (best_ent) delete best_ent; |
---|
| 186 | best_ent = NULL; |
---|
| 187 | curr_ent->randomize(); |
---|
| 188 | } |
---|
| 189 | public: |
---|
[1918] | 190 | /// \brief Constructor. |
---|
[1633] | 191 | SimAnn() : curr_ent(NULL), best_ent(NULL) {} |
---|
[1918] | 192 | /// \brief Destructor. |
---|
[1633] | 193 | virtual ~SimAnn() { |
---|
| 194 | if (best_ent) delete best_ent; |
---|
| 195 | } |
---|
[1918] | 196 | /// \brief Step to a neighbouring state. |
---|
[1633] | 197 | double mutate() { |
---|
| 198 | return curr_ent->mutate(); |
---|
| 199 | } |
---|
[1918] | 200 | /// \brief Reverts the last mutate(). |
---|
[1633] | 201 | void revert() { |
---|
| 202 | curr_ent->revert(); |
---|
| 203 | } |
---|
[1918] | 204 | /// \brief Saves the current solution as the best one. |
---|
[1633] | 205 | void saveAsBest() { |
---|
| 206 | if (best_ent) delete best_ent; |
---|
| 207 | best_ent = curr_ent->clone(); |
---|
| 208 | } |
---|
[1918] | 209 | /// \brief Sets the current entity. |
---|
[1633] | 210 | void setEntity(EntityBase &_ent) { |
---|
| 211 | curr_ent = &_ent; |
---|
| 212 | } |
---|
[1918] | 213 | /// \brief Returns a copy of the best found entity. |
---|
[1633] | 214 | EntityBase* getBestEntity() { return best_ent->clone(); } |
---|
| 215 | }; |
---|
| 216 | |
---|
[1918] | 217 | /// \brief A simple controller for the simulated annealing class. |
---|
[2304] | 218 | /// |
---|
[1918] | 219 | /// This controller starts from a given initial temperature and evenly |
---|
| 220 | /// decreases it. |
---|
[1633] | 221 | class SimpleController : public ControllerBase { |
---|
[1918] | 222 | private: |
---|
| 223 | /// \brief Maximum number of iterations. |
---|
| 224 | long max_iter; |
---|
| 225 | /// \brief Maximum number of iterations which do not improve the |
---|
| 226 | /// solution. |
---|
| 227 | long max_no_impr; |
---|
| 228 | /// \brief Temperature. |
---|
| 229 | double temp; |
---|
| 230 | /// \brief Annealing factor. |
---|
| 231 | double ann_fact; |
---|
| 232 | /// \brief Constructor. |
---|
| 233 | /// \param _max_iter maximum number of iterations |
---|
| 234 | /// \param _max_no_impr maximum number of consecutive iterations which do |
---|
| 235 | /// not yield a better solution |
---|
| 236 | /// \param _temp initial temperature |
---|
| 237 | /// \param _ann_fact annealing factor |
---|
[1633] | 238 | public: |
---|
| 239 | SimpleController(long _max_iter = 500000, long _max_no_impr = 20000, |
---|
| 240 | double _temp = 1000.0, double _ann_fact = 0.9999) : max_iter(_max_iter), |
---|
| 241 | max_no_impr(_max_no_impr), temp(_temp), ann_fact(_ann_fact) |
---|
| 242 | { |
---|
| 243 | } |
---|
[1918] | 244 | /// \brief This is called when a neighbouring state gets accepted. |
---|
[1633] | 245 | void acceptEvent() {} |
---|
[1918] | 246 | /// \brief This is called when the accepted neighbouring state's cost is |
---|
| 247 | /// less than the best found one's. |
---|
[1633] | 248 | void improveEvent() {} |
---|
[1918] | 249 | /// \brief This is called when a neighbouring state gets rejected. |
---|
[1633] | 250 | void rejectEvent() {} |
---|
[1918] | 251 | /// \brief Decides whether to continue the annealing process or not. Also |
---|
| 252 | /// decreases the temperature. |
---|
[1633] | 253 | bool next() { |
---|
| 254 | temp *= ann_fact; |
---|
| 255 | bool quit = (simann->getIter() > max_iter) || |
---|
| 256 | (simann->getIter() - simann->getLastImpr() > max_no_impr); |
---|
| 257 | return !quit; |
---|
| 258 | } |
---|
[1918] | 259 | /// \brief Decides whether to accept the current solution or not. |
---|
[1633] | 260 | bool accept() { |
---|
[1918] | 261 | double cost_diff = simann->getCurrCost() - simann->getPrevCost(); |
---|
[2242] | 262 | return (rnd() <= exp(-(cost_diff / temp))); |
---|
[1633] | 263 | } |
---|
[1918] | 264 | /// \brief Destructor. |
---|
| 265 | virtual ~SimpleController() {} |
---|
[1633] | 266 | }; |
---|
| 267 | |
---|
[1918] | 268 | /// \brief A controller with preset running time for the simulated annealing |
---|
| 269 | /// class. |
---|
[2304] | 270 | /// |
---|
[1918] | 271 | /// With this controller you can set the running time of the annealing |
---|
| 272 | /// process in advance. It works the following way: the controller measures |
---|
| 273 | /// a kind of divergence. The divergence is the difference of the average |
---|
| 274 | /// cost of the recently found solutions the cost of the best found one. In |
---|
| 275 | /// case this divergence is greater than a given threshold, then we decrease |
---|
| 276 | /// the annealing factor, that is we cool the system faster. In case the |
---|
| 277 | /// divergence is lower than the threshold, then we increase the temperature. |
---|
| 278 | /// The threshold is a function of the elapsed time which reaches zero at the |
---|
| 279 | /// desired end time. |
---|
[1633] | 280 | class AdvancedController : public ControllerBase { |
---|
| 281 | private: |
---|
[1918] | 282 | /// \brief Timer class to measure the elapsed time. |
---|
[1633] | 283 | Timer timer; |
---|
[1918] | 284 | /// \brief Calculates the threshold value. |
---|
| 285 | /// \param time the elapsed time in seconds |
---|
[1633] | 286 | virtual double threshold(double time) { |
---|
| 287 | return (-1.0) * start_threshold / end_time * time + start_threshold; |
---|
| 288 | } |
---|
[1918] | 289 | /// \brief Parameter used to calculate the running average. |
---|
| 290 | double alpha; |
---|
| 291 | /// \brief Parameter used to decrease the annealing factor. |
---|
| 292 | double beta; |
---|
| 293 | /// \brief Parameter used to increase the temperature. |
---|
| 294 | double gamma; |
---|
| 295 | /// \brief The time at the end of the algorithm. |
---|
| 296 | double end_time; |
---|
| 297 | /// \brief The time at the start of the algorithm. |
---|
| 298 | double start_time; |
---|
| 299 | /// \brief Starting threshold. |
---|
| 300 | double start_threshold; |
---|
| 301 | /// \brief Average cost of recent solutions. |
---|
| 302 | double avg_cost; |
---|
| 303 | /// \brief Temperature. |
---|
| 304 | double temp; |
---|
| 305 | /// \brief Annealing factor. |
---|
| 306 | double ann_fact; |
---|
| 307 | /// \brief Initial annealing factor. |
---|
| 308 | double init_ann_fact; |
---|
| 309 | /// \brief True when the annealing process has been started. |
---|
| 310 | bool start; |
---|
[1633] | 311 | public: |
---|
[1918] | 312 | /// \brief Constructor. |
---|
| 313 | /// \param _end_time running time in seconds |
---|
| 314 | /// \param _alpha parameter used to calculate the running average |
---|
| 315 | /// \param _beta parameter used to decrease the annealing factor |
---|
| 316 | /// \param _gamma parameter used to increase the temperature |
---|
| 317 | /// \param _ann_fact initial annealing factor |
---|
[1633] | 318 | AdvancedController(double _end_time, double _alpha = 0.2, |
---|
| 319 | double _beta = 0.9, double _gamma = 1.6, double _ann_fact = 0.9999) : |
---|
| 320 | alpha(_alpha), beta(_beta), gamma(_gamma), end_time(_end_time), |
---|
[1918] | 321 | ann_fact(_ann_fact), init_ann_fact(_ann_fact), start(false) |
---|
[1633] | 322 | { |
---|
| 323 | } |
---|
[1918] | 324 | /// \brief Does initializations before each run. |
---|
[1633] | 325 | void init() { |
---|
| 326 | avg_cost = simann->getCurrCost(); |
---|
| 327 | } |
---|
[1918] | 328 | /// \brief This is called when a neighbouring state gets accepted. |
---|
[1633] | 329 | void acceptEvent() { |
---|
| 330 | avg_cost = alpha * simann->getCurrCost() + (1.0 - alpha) * avg_cost; |
---|
[1918] | 331 | if (!start) { |
---|
[1633] | 332 | static int cnt = 0; |
---|
| 333 | cnt++; |
---|
| 334 | if (cnt >= 100) { |
---|
| 335 | // calculate starting threshold and starting temperature |
---|
| 336 | start_threshold = 5.0 * fabs(simann->getBestCost() - avg_cost); |
---|
| 337 | temp = 10000.0; |
---|
[1918] | 338 | start = true; |
---|
[1847] | 339 | timer.restart(); |
---|
[1633] | 340 | } |
---|
| 341 | } |
---|
| 342 | } |
---|
[1918] | 343 | /// \brief Decides whether to continue the annealing process or not. |
---|
[1633] | 344 | bool next() { |
---|
[1918] | 345 | if (!start) { |
---|
[1633] | 346 | return true; |
---|
| 347 | } |
---|
| 348 | else { |
---|
[1918] | 349 | double elapsed_time = timer.realTime(); |
---|
[1633] | 350 | if (fabs(avg_cost - simann->getBestCost()) > threshold(elapsed_time)) { |
---|
| 351 | // decrease the annealing factor |
---|
| 352 | ann_fact *= beta; |
---|
| 353 | } |
---|
| 354 | else { |
---|
| 355 | // increase the temperature |
---|
| 356 | temp *= gamma; |
---|
| 357 | // reset the annealing factor |
---|
| 358 | ann_fact = init_ann_fact; |
---|
| 359 | } |
---|
| 360 | temp *= ann_fact; |
---|
| 361 | return elapsed_time < end_time; |
---|
| 362 | } |
---|
| 363 | } |
---|
[1918] | 364 | /// \brief Decides whether to accept the current solution or not. |
---|
[1633] | 365 | bool accept() { |
---|
[1918] | 366 | if (!start) { |
---|
[1633] | 367 | return true; |
---|
| 368 | } |
---|
| 369 | else { |
---|
[1918] | 370 | double cost_diff = simann->getCurrCost() - simann->getPrevCost(); |
---|
[2242] | 371 | return (rnd() <= exp(-(cost_diff / temp))); |
---|
[1633] | 372 | } |
---|
| 373 | } |
---|
[1918] | 374 | /// \brief Destructor. |
---|
| 375 | virtual ~AdvancedController() {} |
---|
[1633] | 376 | }; |
---|
| 377 | |
---|
| 378 | /// @} |
---|
| 379 | |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | #endif |
---|