- Main Page
- Classes
- Files

- File List

# OptFrame/Heuristic/BestImprovement.hpp

    00001 #ifndef OPTFRAME_BI_HPP_
    00002 #define OPTFRAME_BI_HPP_
    00003
    00004 #include "../Heuristic.hpp"
    00005 #include "../NSSeq.hpp"
    00006 #include "../Evaluator.hpp"
    00007
    00008 template<class R, class M>
    00009 class BestImprovement: 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         BestImprovement(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
    00028                 exec(s, e, timelimit, target_f);
    00029
    00030                 delete &e;
    00031         }
    00032
    00033         virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
    00034         {
    00035                 NSIterator<R, M>& it = nsSeq.getIterator(e.getM(), s.getR());
    00036
    00037                 it.first();
    00038
    00039                 if (it.isDone())
    00040                 {
    00041                         delete &it;
    00042                         return;
    00043                 }
    00044
    00045                 Move<R, M>* bestMove = &it.current();
    00046                 while (!bestMove->canBeApplied(e, s))
    00047                 {
    00048                         delete bestMove;
    00049                         it.next();
    00050                         if (!it.isDone())
    00051                         {
    00052                                 bestMove = &it.current();
    00053                         }
    00054                         else
    00055                         {
    00056                                 delete &it;
    00057                                 return;
    00058                         }
    00059                 }
    00060
    00061                 double bestCost = eval.moveCost(e, *bestMove, s);
    00062                 it.next();
    00063                 while (!it.isDone())
    00064                 {
    00065                         Move<R, M>* move = &it.current();
    00066                         if (move->canBeApplied(e, s))
    00067                         {
    00068                                 double cost = eval.moveCost(e, *move, s);
    00069
    00070                                 if (eval.betterThan(cost, bestCost))
    00071                                 {
    00072                                         delete bestMove;
    00073                                         bestMove = move;
    00074                                         bestCost = cost;
    00075                                 }
    00076                                 else
    00077                                         delete move;
    00078                         }
    00079                         else
    00080                                 delete move;
    00081
    00082                         it.next();
    00083                 }
    00084                 bestMove->apply(e, s);
    00085                 delete bestMove;
    00086                 delete &it;
    00087
    00088                 eval.evaluate(e, s); // updates 'e'
    00089         }
    00090
    00091 };
    00092
    00093 #endif /*OPTFRAME_BI_HPP_*/

All Classes Functions