00001 #ifndef OPTFRAME_ILSL_HPP_
00002 #define OPTFRAME_ILSL_HPP_
00003
00004 #include <math.h>
00005 #include <vector>
00006
00007 #include "IteratedLocalSearch.hpp"
00008 #include "ILSLPerturbation.hpp"
00009
00010 typedef pair<pair<int, int> , pair<int, int> > levelHistory;
00011
00012 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
00013 class IteratedLocalSearchLevels: public IteratedLocalSearch<levelHistory, R, M>
00014 {
00015 protected:
00016 Heuristic<R, M>& h;
00017 ILSLPerturbation<R, M>& p;
00018 int iterMax, levelMax;
00019
00020 public:
00021
00022 IteratedLocalSearchLevels(Evaluator<R, M>& e, Heuristic<R, M>& _h, ILSLPerturbation<R, M>& _p, int _iterMax, int _levelMax) :
00023 IteratedLocalSearch<levelHistory, R, M> (e), h(_h), p(_p), iterMax(_iterMax), levelMax(_levelMax)
00024 {
00025 }
00026
00027 virtual levelHistory& initializeHistory()
00028 {
00029
00030 pair<int, int> vars(0, 0);
00031
00032
00033 pair<int, int> maxs(iterMax, levelMax);
00034
00035 return *new levelHistory(vars, maxs);
00036 }
00037
00038 virtual void localSearch(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
00039 {
00040
00041 h.exec(s, e, timelimit, target_f);
00042 }
00043
00044 virtual void perturbation(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f, levelHistory& history)
00045 {
00046
00047
00048 int iter = history.first.first;
00049 int level = history.first.second;
00050 int iterMax = history.second.first;
00051
00052
00053
00054
00055
00056 p.perturb(s, e, timelimit, target_f, level);
00057
00058
00059 iter++;
00060
00061 if (iter >= iterMax)
00062 {
00063 iter = 0;
00064 level++;
00065 cout << "level " << level << ".." << endl;
00066 }
00067
00068
00069 history.first.first = iter;
00070 history.first.second = level;
00071 }
00072
00073 virtual Solution<R>& acceptanceCriterion(const Solution<R>& s1, const Solution<R>& s2, levelHistory& history)
00074 {
00075
00076
00077 if (IteratedLocalSearch<levelHistory, R, M>::evaluator.betterThan(s2, s1))
00078 {
00079
00080
00081
00082 Evaluation<M>& e = IteratedLocalSearch<levelHistory, R, M>::evaluator.evaluate(s2);
00083 cout << "Best fo: " << e.evaluation();
00084 cout << " on [iter " << history.first.first << " of level " << history.first.second << "]" << endl;
00085 delete &e;
00086
00087
00088
00089
00090
00091 history.first.first = 0;
00092
00093 history.first.second = 0;
00094
00095
00096
00097
00098 return s2.clone();
00099 }
00100 else
00101 return s1.clone();
00102 }
00103
00104 virtual bool terminationCondition(levelHistory& history)
00105 {
00106
00107 int level = history.first.second;
00108 int levelMax = history.second.second;
00109
00110 return (level >= levelMax);
00111 }
00112 };
00113
00114 #endif