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