- Main Page
- Classes
- Files

- File List

# OptFrame/Heuristic/RandomDescentMethod.hpp

    00001 #ifndef OPTFRAME_RDM_HPP_
    00002 #define OPTFRAME_RDM_HPP_
    00003
    00004 #include "../Heuristic.hpp"
    00005
    00006 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
    00007 class RandomDescentMethod: public Heuristic<R, M>
    00008 {
    00009 private:
    00010         NS<R, M>& ns;
    00011         Evaluator<R, M>& evaluator;
    00012         unsigned int iterMax;
    00013
    00014 public:
    00015
    00016         using Heuristic<R, M>::exec; // prevents name hiding
    00017
    00018         RandomDescentMethod(Evaluator<R, M>& _eval, NS<R, M>& _ns, unsigned int _iterMax) :
    00019                 evaluator(_eval), ns(_ns), iterMax(_iterMax)
    00020         {
    00021         }
    00022
    00023         virtual void exec(Solution<R>& s, double timelimit, double target_f)
    00024         {
    00025                 Evaluation<M>& e = evaluator.evaluate(s.getR());
    00026                 exec(s, e, timelimit, target_f);
    00027                 delete &e;
    00028         }
    00029
    00030         virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
    00031         {
    00032                 long tini = time(NULL);
    00033
    00034                 unsigned int iter = 0;
    00035
    00036                 long tnow = time(NULL);
    00037
    00038                 while (iter < iterMax && ((tnow - tini) < timelimit))
    00039                 {
    00040                         Move<R, M>& move = ns.move(s);
    00041
    00042                         double cost = 0;
    00043
    00044                         if (move.canBeApplied(e, s))
    00045                         {
    00046                                 cost = evaluator.moveCost(e, move, s);
    00047                         }
    00048                         else
    00049                         {
    00050                                 iter++;
    00051                                 delete &move;
    00052                                 tnow = time(NULL);
    00053                                 continue;
    00054                         }
    00055
    00056                         iter++;
    00057
    00058                         if (evaluator.betterThan(cost, 0))
    00059                         {
    00060                                 move.apply(e, s);
    00061                                 iter = 0;
    00062                         }
    00063
    00064                         delete &move;
    00065                         tnow = time(NULL);
    00066                 }
    00067         }
    00068 };
    00069
    00070 #endif /*OPTFRAME_RDM_HPP_*/

All Classes Functions