[Lemon-commits] [lemon_svn] ladanyi: r1413 - hugo/trunk/src/work/akos

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:45:14 CET 2006


Author: ladanyi
Date: Mon Nov 29 16:30:11 2004
New Revision: 1413

Modified:
   hugo/trunk/src/work/akos/simann.h
   hugo/trunk/src/work/akos/simann_demo.cc

Log:
Added a getCost() method to the Entity. Now prevCost() returns what its name suggests.

Modified: hugo/trunk/src/work/akos/simann.h
==============================================================================
--- hugo/trunk/src/work/akos/simann.h	(original)
+++ hugo/trunk/src/work/akos/simann.h	Mon Nov 29 16:30:11 2004
@@ -16,15 +16,16 @@
     Controller *controller;
   protected:
     double curr_cost;
-    double prev_cost;
     double best_cost;
+    double prev_cost;
+    double prev_prev_cost;
 
     virtual void mutate() = 0;
     virtual void revert() = 0;
     virtual void saveAsBest() = 0;
   public:
     SimAnnBase() {
-      curr_cost = prev_cost = best_cost = INFTY;
+      best_cost = prev_cost = prev_prev_cost = INFTY;
     }
     void setController(Controller &_controller) {
       controller = &_controller;
@@ -72,7 +73,6 @@
     };
   };
 
-  /*! \todo atgondolni mi is ez a prev_cost */
   template <typename E>
   class SimAnn : public SimAnnBase {
   private:
@@ -83,15 +83,19 @@
     void setEntity(E &ent) {
       curr_ent = new E(ent);
       best_ent = new E(ent);
+      curr_cost = curr_ent->getCost();
     }
     E getBestEntity() { return *best_ent; }
     void mutate() {
+      prev_prev_cost = prev_cost;
       prev_cost = curr_cost;
-      curr_cost = curr_ent->mutate();
+      curr_ent->mutate();
+      curr_cost = curr_ent->getCost();
     }
     void revert() {
       curr_ent->revert();
       curr_cost = prev_cost;
+      prev_cost = prev_prev_cost;
     }
     void saveAsBest() {
       *best_ent = *curr_ent;
@@ -101,10 +105,10 @@
 
   class EntitySkeleton {
   public:
-    /*! \brief Makes a minor change to the entity.
-     *  \return the new cost
-     */
-    double mutate() { return 0.0; }
+    /*! \return the cost of the entity */
+    double getCost() { return 0.0; }
+    /*! \brief Makes a minor change to the entity. */
+    void mutate() {}
     /*! \brief Restores the entity to its previous state i.e. reverts the
      *  effects of the last mutate.
      */
@@ -174,7 +178,6 @@
     double avg_cost;
     double temp, ann_fact;
     bool warmup;
-    long iter;
     /*! \param _end_time running time in seconds
      *  \param _alpha parameter used to calculate the running average
      *  \param _beta parameter used to decrease the annealing factor
@@ -182,37 +185,37 @@
      */
     AdvancedController(double _end_time, double _alpha = 0.2,
     double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta),
-    gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true),
-    iter(0) {}
+    gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true) {}
     void init() {
       avg_cost = base->getCurrCost();
     }
     void acceptEvent() {
       avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost;
-      iter++;
-    }
-    void improveEvent() {
-    }
-    void rejectEvent() {
-      iter++;
-    }
-    bool next() {
       if (warmup) {
         static double max_cost_diff = 0.0;
+        static int incr_cnt = 0;
         double cost_diff = base->getCurrCost() - base->getPrevCost();
-        // jo ez igy egyaltalan? -> prev_cost
-        if ((cost_diff > 0.0) && (cost_diff > max_cost_diff)) {
-          max_cost_diff = cost_diff;
-        }
-        // How to set the starting temperature when all the 100 first
-        // iterations improve the solution?
-        if (iter > 100) {
+        if (cost_diff > 0.0) {
+          incr_cnt++;
+          if (cost_diff > max_cost_diff) {
+            max_cost_diff = cost_diff;
+          }
+        }
+        if (incr_cnt >= 100) {
           // calculate starting threshold and starting temperature
           start_threshold = fabs(base->getBestCost() - avg_cost);
-          temp = exp(max_cost_diff) / 0.5;
+          temp = max_cost_diff / log(0.5);
           warmup = false;
           timer.reset();
         }
+      }
+    }
+    void improveEvent() {
+    }
+    void rejectEvent() {
+    }
+    bool next() {
+      if (warmup) {
         return true;
       }
       else {

Modified: hugo/trunk/src/work/akos/simann_demo.cc
==============================================================================
--- hugo/trunk/src/work/akos/simann_demo.cc	(original)
+++ hugo/trunk/src/work/akos/simann_demo.cc	Mon Nov 29 16:30:11 2004
@@ -4,7 +4,8 @@
 
 class MyEntity {
 public:
-  double mutate() { return 10.0; }
+  double getCost() { return 10.0; }
+  void mutate() {}
   void revert() {}
 };
 



More information about the Lemon-commits mailing list