00001 #ifndef OPTFRAME_HILLCLIMBING_HPP_
00002 #define OPTFRAME_HILLCLIMBING_HPP_
00003
00004 #include "../Heuristic.hpp"
00005 #include "../NSSeq.hpp"
00006 #include "../Evaluator.hpp"
00007
00008 template<class R, class M>
00009 class HillClimbing: public Heuristic<R, M>
00010 {
00011 private:
00012 Evaluator<R, M>& evaluator;
00013 Heuristic<R, M>& heuristic;
00014
00015 public:
00016
00017 using Heuristic<R, M>::exec;
00018
00019 HillClimbing(Evaluator<R, M>& _ev, Heuristic<R, M>& _h) :
00020 evaluator(_ev), heuristic(_h)
00021 {
00022 }
00023
00024 virtual void exec(Solution<R>& s, double timelimit, double target_f)
00025 {
00026 Evaluation<M>& e = evaluator.evaluate(s.getR());
00027
00028 exec(s, e, timelimit, target_f);
00029
00030 delete &e;
00031 }
00032
00033 virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit,
00034 double target_f)
00035 {
00036 long tini = time(NULL);
00037
00038 Solution<R>* s0 = &s.clone();
00039 Evaluation<M>* e0 = &e.clone();
00040
00041 heuristic.exec(s, e, timelimit, target_f);
00042
00043 long tnow = time(NULL);
00044
00045 while ((evaluator.betterThan(s, *s0)) && ((tnow - tini) < timelimit))
00046 {
00047 delete s0;
00048 s0 = &s.clone();
00049 delete e0;
00050 e0 = &e.clone();
00051
00052 heuristic.exec(s, e, timelimit, target_f);
00053
00054 tnow = time(NULL);
00055 }
00056
00057 s = *s0;
00058 e = *e0;
00059
00060 delete s0;
00061 delete e0;
00062 }
00063 };
00064
00065 #endif