- Main Page
- Classes
- Files

- File List

# OptFrame/Heuristic/IteratedLocalSearchLevels.hpp

    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                 //cout << "initializeHistory()" << endl;
    00030                 pair<int, int> vars(0, 0);
    00031
    00032                 // IterMax e LevelMax
    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                 //cout << "localSearch(.)" << endl;
    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                 //cout << "perturbation(.)" << endl;
    00047
    00048                 int iter = history.first.first;
    00049                 int level = history.first.second;
    00050                 int iterMax = history.second.first;
    00051                 //int levelMax = history.second.second;
    00052
    00053                 //cout << "level = " << level << " e iter = " << iter << endl;
    00054
    00055                 // nivel atual: 'level'
    00056                 p.perturb(s, e, timelimit, target_f, level);
    00057
    00058                 // Incrementa a iteracao
    00059                 iter++;
    00060
    00061                 if (iter >= iterMax)
    00062                 {
    00063                         iter = 0;
    00064                         level++;
    00065                         cout << "level " << level << ".." << endl;
    00066                 }
    00067
    00068                 // Atualiza o historico
    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                 //cout << "acceptanceCriterion(.)" << endl;
    00076
    00077                 if (IteratedLocalSearch<levelHistory, R, M>::evaluator.betterThan(s2, s1))
    00078                 {
    00079                         // =======================
    00080                         //   Melhor solucao: 's2'
    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                         //  Atualiza o historico
    00089                         // =======================
    00090                         // iter = 0
    00091                         history.first.first = 0;
    00092                         // level = 0
    00093                         history.first.second = 0;
    00094
    00095                         // =======================
    00096                         //    Retorna s2
    00097                         // =======================
    00098                         return s2.clone();
    00099                 }
    00100                 else
    00101                         return s1.clone();
    00102         }
    00103
    00104         virtual bool terminationCondition(levelHistory& history)
    00105         {
    00106                 //cout << "terminationCondition(.)" << endl;
    00107                 int level = history.first.second;
    00108                 int levelMax = history.second.second;
    00109
    00110                 return (level >= levelMax);
    00111         }
    00112 };
    00113
    00114 #endif /*OPTFRAME_ILSL_HPP_*/

All Classes Functions