00001 #ifndef OPTFRAME_ILSLPerturbation_HPP_
00002 #define OPTFRAME_ILSLPerturbation_HPP_
00003
00004 #include <math.h>
00005 #include <vector>
00006
00007 #include "../NS.hpp"
00008 #include "../RandGen.hpp"
00009
00010 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
00011 class ILSLPerturbation
00012 {
00013 public:
00014 virtual void perturb(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f, int level) = 0;
00015 };
00016
00017 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
00018 class ILSLPerturbationLPlus2: public ILSLPerturbation<R, M>
00019 {
00020 private:
00021 vector<NS<R, M>*> ns;
00022 Evaluator<R, M>& evaluator;
00023 int pMax;
00024 RandGen& rg;
00025
00026 public:
00027 ILSLPerturbationLPlus2(Evaluator<R, M>& e, int _pMax, NS<R, M>& _ns, RandGen& _rg) :
00028 evaluator(e), pMax(_pMax), rg(_rg)
00029 {
00030 ns.push_back(&_ns);
00031 }
00032
00033 void add_ns(NS<R, M>& _ns)
00034 {
00035 ns.push_back(&_ns);
00036 }
00037
00038 void perturb(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f, int level)
00039 {
00040 int f = 0;
00041 int a = 0;
00042
00043 level += 2;
00044
00045 while ((a < level) && (f < pMax))
00046 {
00047 int x = rg.rand(ns.size());
00048
00049 Move<R, M>& m = ns[x]->move(s);
00050
00051 if (m.canBeApplied(e, s))
00052 {
00053 a++;
00054 delete &m.apply(e, s);
00055 }
00056 else
00057 f++;
00058
00059 delete &m;
00060 }
00061
00062 if (f == pMax)
00063 cout << "ILS Warning: perturbation had no effect in " << pMax << " tries!" << endl;
00064
00065 evaluator.evaluate(e, s);
00066 }
00067 };
00068
00069 #endif