# HG changeset patch # User ladanyi # Date 1101742211 0 # Node ID 3268fef5d62385f2e301743b8f7a51ad40481adc # Parent 567f392d1d2e0f32c6825272c2ced1843183971e Added a getCost() method to the Entity. Now prevCost() returns what its name suggests. diff -r 567f392d1d2e -r 3268fef5d623 src/work/akos/simann.h --- a/src/work/akos/simann.h Sun Nov 28 16:30:10 2004 +0000 +++ b/src/work/akos/simann.h Mon Nov 29 15:30:11 2004 +0000 @@ -16,15 +16,16 @@ Controller *controller; protected: double curr_cost; + double best_cost; double prev_cost; - double best_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 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++; + if (warmup) { + static double max_cost_diff = 0.0; + static int incr_cnt = 0; + double cost_diff = base->getCurrCost() - base->getPrevCost(); + 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 = max_cost_diff / log(0.5); + warmup = false; + timer.reset(); + } + } } void improveEvent() { } void rejectEvent() { - iter++; } bool next() { if (warmup) { - static double max_cost_diff = 0.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) { - // calculate starting threshold and starting temperature - start_threshold = fabs(base->getBestCost() - avg_cost); - temp = exp(max_cost_diff) / 0.5; - warmup = false; - timer.reset(); - } return true; } else { diff -r 567f392d1d2e -r 3268fef5d623 src/work/akos/simann_demo.cc --- a/src/work/akos/simann_demo.cc Sun Nov 28 16:30:10 2004 +0000 +++ b/src/work/akos/simann_demo.cc Mon Nov 29 15:30:11 2004 +0000 @@ -4,7 +4,8 @@ class MyEntity { public: - double mutate() { return 10.0; } + double getCost() { return 10.0; } + void mutate() {} void revert() {} };