COIN-OR::LEMON - Graph Library

Changeset 966:5e865c5c8a87 in lemon-0.x


Ignore:
Timestamp:
11/08/04 09:40:37 (19 years ago)
Author:
Akos Ladanyi
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1351
Message:

Added an init method to the controller, and started writing a second controller.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/work/akos/simann.h

    r957 r966  
    11#ifndef LEMON_SIMANN_H
    22#define LEMON_SIMANN_H
     3
     4#include <cstdlib>
     5#include <cmath>
     6#include <sys/time.h>
    37
    48namespace lemon {
     
    3135    double getBestCost() { return best_cost; }
    3236    void run() {
     37      controller->init();
    3338      while (controller->next()) {
    3439        mutate();
     
    5055    public:
    5156      SimAnnBase *base;
     57      virtual void init() {}
    5258      virtual void acceptEvent() {}
    5359      virtual void improveEvent() {}
     
    8591  class EntitySkeleton {
    8692  public:
    87     // returns the new cost
     93    /*! \brief Makes a minor change to the entity.
     94     *  \return the new cost
     95     */
    8896    double mutate() { return 0.0; }
    89     // restores the entity to its previous state i.e. reverts the effects of
    90     // the last mutate()
     97    /*! \brief Restores the entity to its previous state i.e. reverts the
     98     *  effects of the last mutate.
     99     */
    91100    void revert() {}
    92101  };
    93102
     103  /*! \brief A simple controller for the simulated annealing class.
     104   *  \todo Find a way to set the various parameters.
     105   */
    94106  class SimpleController : public SimAnnBase::Controller {
    95107  public:
     
    118130    }
    119131    bool accept() {
    120       return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp));
     132      return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() /
     133        temp));
     134    }
     135  };
     136
     137  /*! \brief A controller with preset running time for the simulated annealing
     138   *  class.
     139   *  \todo Find a better name.
     140   */
     141  class AdvancedController : public SimAnnBase::Controller {
     142  private:
     143    double threshold() { return 0.0; }
     144  public:
     145    double alpha;
     146    double temp;
     147    double avg_cost;
     148    double start_time, end_time;
     149    void init() {
     150      timeval tv;
     151      gettimeofday(&tv, 0);
     152      start_time = tv.tv_sec + double(tv.tv_usec) / 1e6;
     153    }
     154    void acceptEvent() {
     155      avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost;
     156    }
     157    void improveEvent() {
     158    }
     159    void rejectEvent() {
     160    }
     161    bool next() {
     162      // abs(avg_cost - base->getBestCost())
     163      // ha nagy: cooling factor novelese
     164      // ha kicsi: homerseklet novelese
     165      // de mennyivel?
     166      timeval tv;
     167      gettimeofday(&tv, 0);
     168      double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time;
     169      return elapsed_time < end_time;
     170    }
     171    bool accept() {
     172      return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() /
     173        temp));
    121174    }
    122175  };
Note: See TracChangeset for help on using the changeset viewer.