00001 #ifndef OPTFRAME_GRASP_HPP_
00002 #define OPTFRAME_GRASP_HPP_
00003
00004 #include "../Heuristic.hpp"
00005
00006 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
00007 class GRASP: public Heuristic<R, M>
00008 {
00009 private:
00010 Evaluator<R, M>& evaluator;
00011 Heuristic<R, M>& h;
00012 InitialSolution<R>& initsol;
00013 unsigned int iterMax;
00014
00015 public:
00016
00017 using Heuristic<R, M>::exec;
00018
00019 GRASP(Evaluator<R, M>& _eval, InitialSolution<R>& _initsol, Heuristic<R, M>& _h, int _iterMax) :
00020 evaluator(_eval), initsol(_initsol), h(_h), iterMax(_iterMax)
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 exec(s, e, timelimit, target_f);
00028 delete &e;
00029 }
00030
00031 virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
00032 {
00033 long tini = time(NULL);
00034
00035 unsigned int iter = 0;
00036
00037 long tnow = time(NULL);
00038
00039 while (iter < iterMax && ((tnow - tini) < timelimit))
00040 {
00041 Solution<R>& s1 = initsol.generateSolution();
00042 Evaluation<M>& e1 = evaluator.evaluate(s1);
00043
00044 h.exec(s1,e1,timelimit, target_f);
00045
00046 if(evaluator.betterThan(e1,e))
00047 {
00048 s = s1;
00049 e = e1;
00050 cout << "GRASP iter "<<iter<<": ";
00051 e.print();
00052 }
00053
00054 delete &s1;
00055 delete &e1;
00056
00057 tnow = time(NULL);
00058 iter++;
00059 }
00060 }
00061 };
00062
00063 #endif