- Main Page
- Classes
- Files

- File List

# OptFrame/Heuristic/FirstImprovement.hpp

    00001 #ifndef OPTFRAME_FI_HPP_
    00002 #define OPTFRAME_FI_HPP_
    00003
    00004 #include "../Heuristic.hpp"
    00005 #include "../NSSeq.hpp"
    00006 #include "../Evaluator.hpp"
    00007
    00008 template<class R, class M>
    00009 class FirstImprovement: public Heuristic<R, M>
    00010 {
    00011 private:
    00012         Evaluator<R, M>& eval;
    00013         NSSeq<R, M>& nsSeq;
    00014
    00015 public:
    00016
    00017         using Heuristic<R, M>::exec; // prevents name hiding
    00018
    00019         FirstImprovement(Evaluator<R, M>& _eval, NSSeq<R, M>& _nsSeq) :
    00020                 eval(_eval), nsSeq(_nsSeq)
    00021         {
    00022         }
    00023
    00024         virtual void exec(Solution<R>& s, double timelimit, double target_f)
    00025         {
    00026                 Evaluation<M>& e = eval.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                 NSIterator<R, M>& it = nsSeq.getIterator(e.getM(), s.getR());
    00034
    00035                 it.first();
    00036
    00037                 if (it.isDone())
    00038                 {
    00039                         delete &it;
    00040                         return;
    00041                 }
    00042
    00043                 do
    00044                 {
    00045                         Move<R, M>* move = &it.current();
    00046
    00047                         if (move->canBeApplied(e, s) && eval.betterThan(eval.moveCost(e, *move, s), 0))
    00048                         {
    00049                                 move->apply(e, s);
    00050                                 delete move;
    00051
    00052                                 delete &it;
    00053
    00054                                 eval.evaluate(e, s); // updates 'e'
    00055                                 return;
    00056                         }
    00057
    00058                         delete move;
    00059
    00060                         it.next();
    00061                 } while (!it.isDone());
    00062
    00063                 delete &it;
    00064         }
    00065 };
    00066
    00067 #endif /*OPTFRAME_FI_HPP_*/

All Classes Functions