COIN-OR::LEMON - Graph Library

Changeset 1023:3268fef5d623 in lemon-0.x for src/work/akos


Ignore:
Timestamp:
11/29/04 16:30:11 (20 years ago)
Author:
Akos Ladanyi
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1413
Message:

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

Location:
src/work/akos
Files:
2 edited

Legend:

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

    r1018 r1023  
    1717  protected:
    1818    double curr_cost;
     19    double best_cost;
    1920    double prev_cost;
    20     double best_cost;
     21    double prev_prev_cost;
    2122
    2223    virtual void mutate() = 0;
     
    2526  public:
    2627    SimAnnBase() {
    27       curr_cost = prev_cost = best_cost = INFTY;
     28      best_cost = prev_cost = prev_prev_cost = INFTY;
    2829    }
    2930    void setController(Controller &_controller) {
     
    7374  };
    7475
    75   /*! \todo atgondolni mi is ez a prev_cost */
    7676  template <typename E>
    7777  class SimAnn : public SimAnnBase {
     
    8484      curr_ent = new E(ent);
    8585      best_ent = new E(ent);
     86      curr_cost = curr_ent->getCost();
    8687    }
    8788    E getBestEntity() { return *best_ent; }
    8889    void mutate() {
     90      prev_prev_cost = prev_cost;
    8991      prev_cost = curr_cost;
    90       curr_cost = curr_ent->mutate();
     92      curr_ent->mutate();
     93      curr_cost = curr_ent->getCost();
    9194    }
    9295    void revert() {
    9396      curr_ent->revert();
    9497      curr_cost = prev_cost;
     98      prev_cost = prev_prev_cost;
    9599    }
    96100    void saveAsBest() {
     
    102106  class EntitySkeleton {
    103107  public:
    104     /*! \brief Makes a minor change to the entity.
    105      *  \return the new cost
    106     */
    107     double mutate() { return 0.0; }
     108    /*! \return the cost of the entity */
     109    double getCost() { return 0.0; }
     110    /*! \brief Makes a minor change to the entity. */
     111    void mutate() {}
    108112    /*! \brief Restores the entity to its previous state i.e. reverts the
    109113     *  effects of the last mutate.
     
    175179    double temp, ann_fact;
    176180    bool warmup;
    177     long iter;
    178181    /*! \param _end_time running time in seconds
    179182     *  \param _alpha parameter used to calculate the running average
     
    183186    AdvancedController(double _end_time, double _alpha = 0.2,
    184187    double _beta = 0.9, double _gamma = 1.2) : alpha(_alpha), beta(_beta),
    185     gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true),
    186     iter(0) {}
     188    gamma(_gamma), end_time(_end_time), ann_fact(0.9999), warmup(true) {}
    187189    void init() {
    188190      avg_cost = base->getCurrCost();
     
    190192    void acceptEvent() {
    191193      avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost;
    192       iter++;
     194      if (warmup) {
     195        static double max_cost_diff = 0.0;
     196        static int incr_cnt = 0;
     197        double cost_diff = base->getCurrCost() - base->getPrevCost();
     198        if (cost_diff > 0.0) {
     199          incr_cnt++;
     200          if (cost_diff > max_cost_diff) {
     201            max_cost_diff = cost_diff;
     202          }
     203        }
     204        if (incr_cnt >= 100) {
     205          // calculate starting threshold and starting temperature
     206          start_threshold = fabs(base->getBestCost() - avg_cost);
     207          temp = max_cost_diff / log(0.5);
     208          warmup = false;
     209          timer.reset();
     210        }
     211      }
    193212    }
    194213    void improveEvent() {
    195214    }
    196215    void rejectEvent() {
    197       iter++;
    198216    }
    199217    bool next() {
    200218      if (warmup) {
    201         static double max_cost_diff = 0.0;
    202         double cost_diff = base->getCurrCost() - base->getPrevCost();
    203         // jo ez igy egyaltalan? -> prev_cost
    204         if ((cost_diff > 0.0) && (cost_diff > max_cost_diff)) {
    205           max_cost_diff = cost_diff;
    206         }
    207         // How to set the starting temperature when all the 100 first
    208         // iterations improve the solution?
    209         if (iter > 100) {
    210           // calculate starting threshold and starting temperature
    211           start_threshold = fabs(base->getBestCost() - avg_cost);
    212           temp = exp(max_cost_diff) / 0.5;
    213           warmup = false;
    214           timer.reset();
    215         }
    216219        return true;
    217220      }
  • src/work/akos/simann_demo.cc

    r999 r1023  
    55class MyEntity {
    66public:
    7   double mutate() { return 10.0; }
     7  double getCost() { return 10.0; }
     8  void mutate() {}
    89  void revert() {}
    910};
Note: See TracChangeset for help on using the changeset viewer.