00001 #ifndef VARIABLENEIGHBORHOODDESCENT_HPP_
00002 #define VARIABLENEIGHBORHOODDESCENT_HPP_
00003
00004 #include "../Heuristic.hpp"
00005 #include "../NSEnum.hpp"
00006 #include "../Evaluator.hpp"
00007
00008 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
00009 class VariableNeighborhoodDescent: public Heuristic<R, M>
00010 {
00011 public:
00012 using Heuristic<R, M>::exec;
00013
00014 VariableNeighborhoodDescent(Evaluator<R, M>& _ev, vector<Heuristic<R, M>*> _neighbors) :
00015 ev(_ev), neighbors(_neighbors)
00016 {
00017 }
00018 ;
00019
00020 virtual void exec(Solution<R>& s, double timelimit, double target_f)
00021 {
00022 Evaluation<M>& e = ev.evaluate(s.getR());
00023
00024 exec(s, e, timelimit, target_f);
00025
00026 delete &e;
00027 }
00028 ;
00029
00030 virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
00031 {
00032
00033 long tini = time(NULL);
00034
00035 int r = neighbors.size();
00036
00037 int k = 1;
00038
00039 long tnow = time(NULL);
00040 while (ev.betterThan(target_f, e.evaluation()) && (k <= r) && ((tnow - tini) < timelimit))
00041 {
00042 Solution<R>* s0 = &s.clone();
00043 Evaluation<M>* e0 = &e.clone();
00044
00045 neighbors[k - 1]->exec(*s0, *e0, timelimit, target_f);
00046 if (ev.betterThan(*s0, s))
00047 {
00048 s = *s0;
00049 e = *e0;
00050 delete s0;
00051 delete e0;
00052 k = 1;
00053 }
00054 else
00055 {
00056 delete s0;
00057 delete e0;
00058 k = k + 1;
00059 }
00060 ev.evaluate(e, s);
00061
00062 tnow = time(NULL);
00063 }
00064
00065 }
00066 ;
00067
00068 private:
00069 vector<Heuristic<R, M>*> neighbors;
00070 Evaluator<R, M>& ev;
00071 };
00072
00073 #endif