- Main Page
- Classes
- Files

- File List

# OptFrame/Heuristic/IntensifiedIteratedLocalSearchLevels.hpp

    00001 #ifndef OPTFRAME_IILSL_HPP_
    00002 #define OPTFRAME_IILSL_HPP_
    00003
    00004 #include <math.h>
    00005 #include <vector>
    00006
    00007 #include "IntensifiedIteratedLocalSearch.hpp"
    00008 #include "ILSLPerturbation.hpp"
    00009 #include "Intensification.hpp"
    00010
    00011 typedef pair<pair<int, int> , pair<int, int> > levelHistory;
    00012
    00013 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
    00014 class IntensifiedIteratedLocalSearchLevels: public IntensifiedIteratedLocalSearch<levelHistory, R, M>
    00015 {
    00016 protected:
    00017         Heuristic<R, M>& h;
    00018         Intensification<R, M>& h2;
    00019         ILSLPerturbation<R, M>& p;
    00020         int iterMax, levelMax;
    00021
    00022 public:
    00023
    00024         IntensifiedIteratedLocalSearchLevels(Evaluator<R, M>& e, Heuristic<R, M>& _h, Intensification<R, M>& _h2, ILSLPerturbation<R, M>& _p, int _iterMax,
    00025                         int _levelMax) :
    00026                 h(_h), h2(_h2), p(_p), iterMax(_iterMax), levelMax(_levelMax), IntensifiedIteratedLocalSearch<levelHistory, R, M> (e)
    00027         {
    00028         }
    00029
    00030         virtual levelHistory& initializeHistory()
    00031         {
    00032                 //cout << "initializeHistory()" << endl;
    00033                 pair<int, int> vars(0, 0);
    00034
    00035                 // IterMax e LevelMax
    00036                 pair<int, int> maxs(iterMax, levelMax);
    00037
    00038                 return *new levelHistory(vars, maxs);
    00039         }
    00040
    00041         virtual void intensification(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f, levelHistory& history)
    00042         {
    00043                 h2.addSolution(s);
    00044
    00045                 if (history.first.first == history.second.first-1) //TODO intensification applied only at last iteration of each level
    00046                 {
    00047                         Solution<R>& s1 = h2.search(s);
    00048
    00049                         Evaluator<R,M> & ev = this->getEvaluator();
    00050                         Evaluation<M>& s1_e = ev.evaluate(s1);
    00051
    00052                         if (ev.betterThan(s1_e, e))
    00053                         {
    00054                                 e = s1_e;
    00055                                 s = s1;
    00056                                 h2.addSolution(s);
    00057                         }
    00058
    00059                         delete &s1;
    00060                 }
    00061
    00062         }
    00063
    00064         virtual void localSearch(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
    00065         {
    00066                 //cout << "localSearch(.)" << endl;
    00067                 h.exec(s, e, timelimit, target_f);
    00068         }
    00069
    00070         virtual void perturbation(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f, levelHistory& history)
    00071         {
    00072                 //cout << "perturbation(.)" << endl;
    00073
    00074                 int iter = history.first.first;
    00075                 int level = history.first.second;
    00076                 int iterMax = history.second.first;
    00077                 //int levelMax = history.second.second;
    00078
    00079                 //cout << "level = " << level << " e iter = " << iter << endl;
    00080
    00081                 // nivel atual: 'level'
    00082                 p.perturb(s, e, timelimit, target_f, level);
    00083
    00084                 // Incrementa a iteracao
    00085                 iter++;
    00086
    00087                 if (iter >= iterMax)
    00088                 {
    00089                         iter = 0;
    00090                         level++;
    00091                         cout << "level " << level << ".." << endl;
    00092                 }
    00093
    00094                 // Atualiza o historico
    00095                 history.first.first = iter;
    00096                 history.first.second = level;
    00097         }
    00098
    00099         virtual Solution<R>& acceptanceCriterion(const Solution<R>& s1, const Solution<R>& s2, levelHistory& history)
    00100         {
    00101                 if (IntensifiedIteratedLocalSearch<levelHistory, R, M>::evaluator.betterThan(s2, s1))
    00102                 {
    00103                         // =======================
    00104                         //   Melhor solucao: 's2'
    00105                         // =======================
    00106                         Evaluation<M>& e = IntensifiedIteratedLocalSearch<levelHistory, R, M>::evaluator.evaluate(s2);
    00107                         cout << "Best fo: " << e.evaluation();
    00108                         cout << " on [iter " << history.first.first << " of level " << history.first.second << "]" << endl;
    00109                         delete &e;
    00110
    00111                         // =======================
    00112                         //  Atualiza o historico
    00113                         // =======================
    00114                         // iter = 0
    00115                         history.first.first = 0;
    00116                         // level = 0
    00117                         history.first.second = 0;
    00118
    00119                         // =======================
    00120                         //    Retorna s2
    00121                         // =======================
    00122
    00123                         h2.addSolution(s2);
    00124
    00125                         return s2.clone();
    00126                 }
    00127                 else
    00128                         return s1.clone();
    00129         }
    00130
    00131         virtual bool terminationCondition(levelHistory& history)
    00132         {
    00133                 //cout << "terminationCondition(.)" << endl;
    00134                 int level = history.first.second;
    00135                 int levelMax = history.second.second;
    00136
    00137                 return (level >= levelMax);
    00138         }
    00139 };
    00140
    00141 #endif /*OPTFRAME_IILSL_HPP_*/

All Classes Functions