Added an init method to the controller, and started writing a second controller.
authorladanyi
Mon, 08 Nov 2004 08:40:37 +0000
changeset 9665e865c5c8a87
parent 965 1e16b8dac159
child 967 6563019430ba
Added an init method to the controller, and started writing a second controller.
src/work/akos/simann.h
     1.1 --- a/src/work/akos/simann.h	Mon Nov 08 08:37:41 2004 +0000
     1.2 +++ b/src/work/akos/simann.h	Mon Nov 08 08:40:37 2004 +0000
     1.3 @@ -1,6 +1,10 @@
     1.4  #ifndef LEMON_SIMANN_H
     1.5  #define LEMON_SIMANN_H
     1.6  
     1.7 +#include <cstdlib>
     1.8 +#include <cmath>
     1.9 +#include <sys/time.h>
    1.10 +
    1.11  namespace lemon {
    1.12  
    1.13    const double INFTY = 1e24;
    1.14 @@ -30,6 +34,7 @@
    1.15      double getPrevCost() { return prev_cost; }
    1.16      double getBestCost() { return best_cost; }
    1.17      void run() {
    1.18 +      controller->init();
    1.19        while (controller->next()) {
    1.20          mutate();
    1.21          if (controller->accept()) {
    1.22 @@ -49,6 +54,7 @@
    1.23      class Controller {
    1.24      public:
    1.25        SimAnnBase *base;
    1.26 +      virtual void init() {}
    1.27        virtual void acceptEvent() {}
    1.28        virtual void improveEvent() {}
    1.29        virtual void rejectEvent() {}
    1.30 @@ -84,13 +90,19 @@
    1.31  
    1.32    class EntitySkeleton {
    1.33    public:
    1.34 -    // returns the new cost
    1.35 +    /*! \brief Makes a minor change to the entity.
    1.36 +     *  \return the new cost
    1.37 +     */
    1.38      double mutate() { return 0.0; }
    1.39 -    // restores the entity to its previous state i.e. reverts the effects of
    1.40 -    // the last mutate()
    1.41 +    /*! \brief Restores the entity to its previous state i.e. reverts the
    1.42 +     *  effects of the last mutate.
    1.43 +     */
    1.44      void revert() {}
    1.45    };
    1.46  
    1.47 +  /*! \brief A simple controller for the simulated annealing class.
    1.48 +   *  \todo Find a way to set the various parameters.
    1.49 +   */
    1.50    class SimpleController : public SimAnnBase::Controller {
    1.51    public:
    1.52      long iter, last_impr, max_iter, max_no_impr;
    1.53 @@ -117,7 +129,48 @@
    1.54        return !quit;
    1.55      }
    1.56      bool accept() {
    1.57 -      return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp));
    1.58 +      return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() /
    1.59 +        temp));
    1.60 +    }
    1.61 +  };
    1.62 +
    1.63 +  /*! \brief A controller with preset running time for the simulated annealing
    1.64 +   *  class.
    1.65 +   *  \todo Find a better name.
    1.66 +   */
    1.67 +  class AdvancedController : public SimAnnBase::Controller {
    1.68 +  private:
    1.69 +    double threshold() { return 0.0; }
    1.70 +  public:
    1.71 +    double alpha;
    1.72 +    double temp;
    1.73 +    double avg_cost;
    1.74 +    double start_time, end_time;
    1.75 +    void init() {
    1.76 +      timeval tv;
    1.77 +      gettimeofday(&tv, 0);
    1.78 +      start_time = tv.tv_sec + double(tv.tv_usec) / 1e6;
    1.79 +    }
    1.80 +    void acceptEvent() {
    1.81 +      avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost;
    1.82 +    }
    1.83 +    void improveEvent() {
    1.84 +    }
    1.85 +    void rejectEvent() {
    1.86 +    }
    1.87 +    bool next() {
    1.88 +      // abs(avg_cost - base->getBestCost())
    1.89 +      // ha nagy: cooling factor novelese
    1.90 +      // ha kicsi: homerseklet novelese
    1.91 +      // de mennyivel?
    1.92 +      timeval tv;
    1.93 +      gettimeofday(&tv, 0);
    1.94 +      double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time;
    1.95 +      return elapsed_time < end_time;
    1.96 +    }
    1.97 +    bool accept() {
    1.98 +      return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() /
    1.99 +        temp));
   1.100      }
   1.101    };
   1.102