Added a getCost() method to the Entity. Now prevCost() returns what its name suggests.
authorladanyi
Mon, 29 Nov 2004 15:30:11 +0000
changeset 10233268fef5d623
parent 1022 567f392d1d2e
child 1024 28e117c5bddf
Added a getCost() method to the Entity. Now prevCost() returns what its name suggests.
src/work/akos/simann.h
src/work/akos/simann_demo.cc
     1.1 --- a/src/work/akos/simann.h	Sun Nov 28 16:30:10 2004 +0000
     1.2 +++ b/src/work/akos/simann.h	Mon Nov 29 15:30:11 2004 +0000
     1.3 @@ -16,15 +16,16 @@
     1.4      Controller *controller;
     1.5    protected:
     1.6      double curr_cost;
     1.7 +    double best_cost;
     1.8      double prev_cost;
     1.9 -    double best_cost;
    1.10 +    double prev_prev_cost;
    1.11  
    1.12      virtual void mutate() = 0;
    1.13      virtual void revert() = 0;
    1.14      virtual void saveAsBest() = 0;
    1.15    public:
    1.16      SimAnnBase() {
    1.17 -      curr_cost = prev_cost = best_cost = INFTY;
    1.18 +      best_cost = prev_cost = prev_prev_cost = INFTY;
    1.19      }
    1.20      void setController(Controller &_controller) {
    1.21        controller = &_controller;
    1.22 @@ -72,7 +73,6 @@
    1.23      };
    1.24    };
    1.25  
    1.26 -  /*! \todo atgondolni mi is ez a prev_cost */
    1.27    template <typename E>
    1.28    class SimAnn : public SimAnnBase {
    1.29    private:
    1.30 @@ -83,15 +83,19 @@
    1.31      void setEntity(E &ent) {
    1.32        curr_ent = new E(ent);
    1.33        best_ent = new E(ent);
    1.34 +      curr_cost = curr_ent->getCost();
    1.35      }
    1.36      E getBestEntity() { return *best_ent; }
    1.37      void mutate() {
    1.38 +      prev_prev_cost = prev_cost;
    1.39        prev_cost = curr_cost;
    1.40 -      curr_cost = curr_ent->mutate();
    1.41 +      curr_ent->mutate();
    1.42 +      curr_cost = curr_ent->getCost();
    1.43      }
    1.44      void revert() {
    1.45        curr_ent->revert();
    1.46        curr_cost = prev_cost;
    1.47 +      prev_cost = prev_prev_cost;
    1.48      }
    1.49      void saveAsBest() {
    1.50        *best_ent = *curr_ent;
    1.51 @@ -101,10 +105,10 @@
    1.52  
    1.53    class EntitySkeleton {
    1.54    public:
    1.55 -    /*! \brief Makes a minor change to the entity.
    1.56 -     *  \return the new cost
    1.57 -     */
    1.58 -    double mutate() { return 0.0; }
    1.59 +    /*! \return the cost of the entity */
    1.60 +    double getCost() { return 0.0; }
    1.61 +    /*! \brief Makes a minor change to the entity. */
    1.62 +    void mutate() {}
    1.63      /*! \brief Restores the entity to its previous state i.e. reverts the
    1.64       *  effects of the last mutate.
    1.65       */
    1.66 @@ -174,7 +178,6 @@
    1.67      double avg_cost;
    1.68      double temp, ann_fact;
    1.69      bool warmup;
    1.70 -    long iter;
    1.71      /*! \param _end_time running time in seconds
    1.72       *  \param _alpha parameter used to calculate the running average
    1.73       *  \param _beta parameter used to decrease the annealing factor
    1.74 @@ -182,37 +185,37 @@
    1.75       */
    1.76      AdvancedController(double _end_time, double _alpha = 0.2,
    1.77      double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta),
    1.78 -    gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true),
    1.79 -    iter(0) {}
    1.80 +    gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true) {}
    1.81      void init() {
    1.82        avg_cost = base->getCurrCost();
    1.83      }
    1.84      void acceptEvent() {
    1.85        avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost;
    1.86 -      iter++;
    1.87 +      if (warmup) {
    1.88 +        static double max_cost_diff = 0.0;
    1.89 +        static int incr_cnt = 0;
    1.90 +        double cost_diff = base->getCurrCost() - base->getPrevCost();
    1.91 +        if (cost_diff > 0.0) {
    1.92 +          incr_cnt++;
    1.93 +          if (cost_diff > max_cost_diff) {
    1.94 +            max_cost_diff = cost_diff;
    1.95 +          }
    1.96 +        }
    1.97 +        if (incr_cnt >= 100) {
    1.98 +          // calculate starting threshold and starting temperature
    1.99 +          start_threshold = fabs(base->getBestCost() - avg_cost);
   1.100 +          temp = max_cost_diff / log(0.5);
   1.101 +          warmup = false;
   1.102 +          timer.reset();
   1.103 +        }
   1.104 +      }
   1.105      }
   1.106      void improveEvent() {
   1.107      }
   1.108      void rejectEvent() {
   1.109 -      iter++;
   1.110      }
   1.111      bool next() {
   1.112        if (warmup) {
   1.113 -        static double max_cost_diff = 0.0;
   1.114 -        double cost_diff = base->getCurrCost() - base->getPrevCost();
   1.115 -        // jo ez igy egyaltalan? -> prev_cost
   1.116 -        if ((cost_diff > 0.0) && (cost_diff > max_cost_diff)) {
   1.117 -          max_cost_diff = cost_diff;
   1.118 -        }
   1.119 -        // How to set the starting temperature when all the 100 first
   1.120 -        // iterations improve the solution?
   1.121 -        if (iter > 100) {
   1.122 -          // calculate starting threshold and starting temperature
   1.123 -          start_threshold = fabs(base->getBestCost() - avg_cost);
   1.124 -          temp = exp(max_cost_diff) / 0.5;
   1.125 -          warmup = false;
   1.126 -          timer.reset();
   1.127 -        }
   1.128          return true;
   1.129        }
   1.130        else {
     2.1 --- a/src/work/akos/simann_demo.cc	Sun Nov 28 16:30:10 2004 +0000
     2.2 +++ b/src/work/akos/simann_demo.cc	Mon Nov 29 15:30:11 2004 +0000
     2.3 @@ -4,7 +4,8 @@
     2.4  
     2.5  class MyEntity {
     2.6  public:
     2.7 -  double mutate() { return 10.0; }
     2.8 +  double getCost() { return 10.0; }
     2.9 +  void mutate() {}
    2.10    void revert() {}
    2.11  };
    2.12