COIN-OR::LEMON - Graph Library

Changeset 1000:7f4d07047ed8 in lemon-0.x


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

Some comments and minor additions to the AdvancedController?.

File:
1 edited

Legend:

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

    r966 r1000  
    5252    }
    5353
     54    /*! \brief A base class for controllers. */
    5455    class Controller {
    5556    public:
    5657      SimAnnBase *base;
    5758      virtual void init() {}
     59      /*! \brief This is called when a neighbouring state gets accepted. */
    5860      virtual void acceptEvent() {}
     61      /*! \brief This is called when the accepted neighbouring state's cost is
     62       *  less than the best found one's.
     63       */
    5964      virtual void improveEvent() {}
     65      /*! \brief This is called when a neighbouring state gets rejected. */
    6066      virtual void rejectEvent() {}
    6167      virtual void setBase(SimAnnBase *_base) { base = _base; }
     68      /*! */
    6269      virtual bool next() = 0;
     70      /*! */
    6371      virtual bool accept() = 0;
    6472    };
     
    107115  public:
    108116    long iter, last_impr, max_iter, max_no_impr;
    109     double temp, annealing_factor;
    110     SimpleController() {
    111       iter = last_impr = 0;
    112       max_iter = 500000;
    113       max_no_impr = 20000;
    114       annealing_factor = 0.9999;
    115       temp = 1000;
    116     }
     117    double temp, ann_fact;
     118    /*! \param _max_iter maximum number of iterations
     119     *  \param _max_no_impr maximum number of consecutive iterations which do
     120     *         not yield a better solution
     121     *  \param _temp initial temperature
     122     *  \param _ann_fact annealing factor
     123     */
     124    SimpleController(long _max_iter = 500000, long _max_no_impr = 20000,
     125    double _temp = 1000, double _ann_fact = 0.9999) : iter(0), last_impr(0),
     126    max_iter(_max_iter), max_no_impr(_max_no_impr), temp(_temp),
     127    ann_fact(_ann_fact) {}
    117128    void acceptEvent() {
    118129      iter++;
     
    125136    }
    126137    bool next() {
    127       temp *= annealing_factor;
     138      temp *= ann_fact;
    128139      bool quit = (iter > max_iter) || (iter - last_impr > max_no_impr);
    129140      return !quit;
     
    141152  class AdvancedController : public SimAnnBase::Controller {
    142153  private:
    143     double threshold() { return 0.0; }
    144   public:
    145     double alpha;
    146     double temp;
     154    /*! \param time the elapsed time in seconds */
     155    double threshold(double time) { return 0.0; }
     156  public:
     157    double alpha, beta, gamma;
     158    double end_time, start_time;
    147159    double avg_cost;
    148     double start_time, end_time;
     160    double temp, ann_fact;
     161    /*! \param _end_time running_time
     162     *  \param _alpha parameter used to calculate the running average
     163     *  \param _beta parameter used to decrease the annealing factor
     164     *  \param _gamma parameter used to increase the temperature
     165     */
     166    AdvancedController(double _end_time, double _alpha = 0.2,
     167    double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta),
     168    gamma(_gamma), end_time(_end_time) {}
    149169    void init() {
    150170      timeval tv;
     
    160180    }
    161181    bool next() {
    162       // abs(avg_cost - base->getBestCost())
    163       // ha nagy: cooling factor novelese
    164       // ha kicsi: homerseklet novelese
    165       // de mennyivel?
    166182      timeval tv;
    167183      gettimeofday(&tv, 0);
    168184      double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time;
     185      if (fabs(avg_cost - base->getBestCost()) > threshold(elapsed_time)) {
     186        // decrease the annealing factor
     187        ann_fact *= beta;
     188      }
     189      else {
     190        // increase the temperature
     191        temp *= gamma;
     192      }
     193      temp *= ann_fact;
    169194      return elapsed_time < end_time;
    170195    }
Note: See TracChangeset for help on using the changeset viewer.