# HG changeset patch # User ladanyi # Date 1099903237 0 # Node ID 5e865c5c8a873c19a0e4d3d4056e62b0e78ea8f3 # Parent 1e16b8dac159408b874ec50e9255b962d39b9bd8 Added an init method to the controller, and started writing a second controller. diff -r 1e16b8dac159 -r 5e865c5c8a87 src/work/akos/simann.h --- a/src/work/akos/simann.h Mon Nov 08 08:37:41 2004 +0000 +++ b/src/work/akos/simann.h Mon Nov 08 08:40:37 2004 +0000 @@ -1,6 +1,10 @@ #ifndef LEMON_SIMANN_H #define LEMON_SIMANN_H +#include +#include +#include + namespace lemon { const double INFTY = 1e24; @@ -30,6 +34,7 @@ double getPrevCost() { return prev_cost; } double getBestCost() { return best_cost; } void run() { + controller->init(); while (controller->next()) { mutate(); if (controller->accept()) { @@ -49,6 +54,7 @@ class Controller { public: SimAnnBase *base; + virtual void init() {} virtual void acceptEvent() {} virtual void improveEvent() {} virtual void rejectEvent() {} @@ -84,13 +90,19 @@ class EntitySkeleton { public: - // returns the new cost + /*! \brief Makes a minor change to the entity. + * \return the new cost + */ double mutate() { return 0.0; } - // restores the entity to its previous state i.e. reverts the effects of - // the last mutate() + /*! \brief Restores the entity to its previous state i.e. reverts the + * effects of the last mutate. + */ void revert() {} }; + /*! \brief A simple controller for the simulated annealing class. + * \todo Find a way to set the various parameters. + */ class SimpleController : public SimAnnBase::Controller { public: long iter, last_impr, max_iter, max_no_impr; @@ -117,7 +129,48 @@ return !quit; } bool accept() { - return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / temp)); + return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / + temp)); + } + }; + + /*! \brief A controller with preset running time for the simulated annealing + * class. + * \todo Find a better name. + */ + class AdvancedController : public SimAnnBase::Controller { + private: + double threshold() { return 0.0; } + public: + double alpha; + double temp; + double avg_cost; + double start_time, end_time; + void init() { + timeval tv; + gettimeofday(&tv, 0); + start_time = tv.tv_sec + double(tv.tv_usec) / 1e6; + } + void acceptEvent() { + avg_cost = alpha * base->getCurrCost() + (1.0 - alpha) * avg_cost; + } + void improveEvent() { + } + void rejectEvent() { + } + bool next() { + // abs(avg_cost - base->getBestCost()) + // ha nagy: cooling factor novelese + // ha kicsi: homerseklet novelese + // de mennyivel? + timeval tv; + gettimeofday(&tv, 0); + double elapsed_time = tv.tv_sec + double(tv.tv_usec) / 1e6 - start_time; + return elapsed_time < end_time; + } + bool accept() { + return (drand48() <= exp(base->getPrevCost() - base->getCurrCost() / + temp)); } };