- Main Page
- Classes
- Files

- File List

# OptFrame/Evaluator.hpp

    00001 #ifndef OPTFRAME_EVALUATOR_HPP_
    00002 #define OPTFRAME_EVALUATOR_HPP_
    00003
    00004 #include "Solution.hpp"
    00005 #include "Evaluation.hpp"
    00006 #include "Move.hpp"
    00007
    00008 #include <iostream>
    00009
    00010 //#define EPSILON 0.0001
    00011
    00012 using namespace std;
    00013
    00014 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
    00015 class Evaluator
    00016 {
    00017 public:
    00018
    00019         virtual ~Evaluator()
    00020         {
    00021         }
    00022
    00023         Evaluation<M>& evaluate(const Solution<R>& s)
    00024         {
    00025                 return evaluate(s.getR());
    00026         }
    00027
    00028         virtual Evaluation<M>& evaluate(const R& r) = 0;
    00029
    00030         void evaluate(Evaluation<M>& e, const Solution<R>& s)
    00031         {
    00032                 evaluate(e, s.getR());
    00033         }
    00034
    00035         virtual void evaluate(Evaluation<M>& e, const R& r)
    00036         {
    00037                 Evaluation<M>& e1 = evaluate(r);
    00038                 e = e1;
    00039                 delete &e1;
    00040         }
    00041
    00042         // Apply movement considering a previous evaluation => Faster.
    00043         // Update evaluation 'e'
    00044         Move<R, M>& applyMove(Evaluation<M>& e, Move<R, M>& m, Solution<R>& s)
    00045         {
    00046                 Move<R, M>& rev = m.apply(e, s);
    00047                 evaluate(e, s);
    00048                 return rev;
    00049         }
    00050
    00051         // Apply movement without considering a previous evaluation => Slower.
    00052         // Return new evaluation 'e'
    00053         pair<Move<R, M>&, Evaluation<M>&>& applyMove(Move<R, M>& m, Solution<R>& s)
    00054         {
    00055                 Move<R, M>& rev = m.apply(s);
    00056                 return *new pair<Move<R, M>&, Evaluation<M>&> (rev, evaluate(s));
    00057         }
    00058
    00059         // Movement cost based on reevaluation of 'e'
    00060         double moveCost(Evaluation<M>& e, Move<R, M>& m, Solution<R>& s)
    00061         {
    00062                 Move<R, M>& rev = applyMove(e, m, s);
    00063                 double e_end = e.evaluation();
    00064
    00065                 Move<R, M>& ini = applyMove(e, rev, s);
    00066                 double e_ini = e.evaluation();
    00067
    00068                 // Difference: new - original
    00069                 double diff = e_end - e_ini;
    00070
    00071                 delete &rev;
    00072                 delete &ini;
    00073
    00074                 return diff;
    00075         }
    00076
    00077         // Movement cost based on complete evaluation
    00078         double moveCost(Move<R, M>& m, Solution<R>& s)
    00079         {
    00080                 pair<Move<R, M>&, Evaluation<M>&>& rev = applyMove(m, s);
    00081
    00082                 pair<Move<R, M>&, Evaluation<M>&>& ini = applyMove(rev.first, s);
    00083
    00084                 // Difference: new - original
    00085                 double diff = rev.second.evaluation() - ini.second.evaluation();
    00086
    00087                 delete &rev.first;
    00088                 delete &rev.second;
    00089                 delete &ini.first;
    00090                 delete &ini.second;
    00091
    00092                 delete &rev;
    00093                 delete &ini;
    00094
    00095                 return diff;
    00096         }
    00097
    00098         // true if 's1' is better than 's2'
    00099         bool betterThan(const Solution<R>& s1, const Solution<R>& s2)
    00100         {
    00101                 Evaluation<M>& e1 = evaluate(s1);
    00102                 Evaluation<M>& e2 = evaluate(s2);
    00103
    00104                 double f1 = e1.evaluation();
    00105                 double f2 = e2.evaluation();
    00106
    00107                 delete &e1;
    00108                 delete &e2;
    00109
    00110                 return betterThan(f1, f2);
    00111         }
    00112
    00113         // true if 'e1' is better than 'e2'
    00114         bool betterThan(const Evaluation<M>& e1, const Evaluation<M>& e2)
    00115         {
    00116                 return betterThan(e1.evaluation(), e2.evaluation());
    00117         }
    00118
    00119         // minimization problem: true when a < b
    00120         // maximization problem: true when a > b
    00121         virtual bool betterThan(double a, double b) = 0;
    00122 };
    00123
    00124 #endif /*OPTFRAME_EVALUATOR_HPP_*/

All Classes Functions