- Main Page
- Classes
- Files

- File List

# OptFrame/Heuristic.hpp

    00001 #ifndef OPTFRAME_HEURISTIC_HPP_
    00002 #define OPTFRAME_HEURISTIC_HPP_
    00003
    00004 #include <iostream>
    00005 #include <vector>
    00006
    00007 using namespace std;
    00008
    00009 #include "./Solution.hpp"
    00010 #include "Population.hpp"
    00011 #include "./Evaluation.hpp"
    00012 #include "./Util/Runnable/Runnable.hpp"
    00013
    00014 template<class R, class M = OPTFRAME_DEFAULT_MEMORY>
    00015 class Heuristic: public Runnable
    00016 {
    00017    typedef vector<Evaluation<M>*> FitnessValues;
    00018    typedef const vector<const Evaluation<M>*> ConstFitnessValues;
    00019
    00020 protected:
    00021    Solution<R>* log_solution;
    00022    //Population log_solution_set;
    00023    Population<R> log_solution_set;
    00024    long log_timelimit;
    00025    double log_efv;
    00026
    00027 public:
    00028
    00029    static const int SAFE_SEARCH_TOLERANCE = 2; // tolerance in seconds
    00030
    00031    Heuristic() :
    00032       Runnable()
    00033    {
    00034       log_solution = NULL;
    00035    }
    00036
    00037    virtual ~Heuristic()
    00038    {
    00039    }
    00040
    00042
    00051    virtual void run()
    00052    {
    00053       /*        if(log_solution) // if not null
    00054        log_solution = exec(log_efv, log_timelimit,log_solution);
    00055        else
    00056        log_solution_set = exec(log_efv, log_timelimit,log_solution_set);
    00057        */
    00058    }
    00059
    00060    /*
    00061
    00062     virtual Solution<R>& safeSearch(double efv, long timelimit, Solution<R>* base)
    00063     {
    00064     if(timelimit<=0)
    00065     return base->clone();
    00066
    00067     log_solution = base->clone();
    00068     log_solution_set.clear();
    00069     log_timelimit = timelimit;
    00070     log_efv = efv;
    00071
    00072     start();
    00073
    00074     for(int t=0;t<timelimit;t++)
    00075     if(!finished())
    00076     sleep(1);
    00077     else
    00078     return log_solution;
    00079
    00080     for(int t=0;t<SAFE_SEARCH_TOLERANCE;t++)
    00081     if(!finished())
    00082     {
    00083     cout << "Warning: Process not finished yet. Safe Search Tolerance ("<<t<<" secs.)"<<endl;
    00084     sleep(1);
    00085     }
    00086     else
    00087     return log_solution;
    00088
    00089     stop();
    00090
    00091     cout << "Warning: Safe Search saved method from looping! (maybe +1 solution memory lack)" << endl;
    00092     return base->clone();
    00093     }
    00094
    00095     virtual vector<Solution<R>*> safeSearch(double efv, long timelimit, vector<Solution<R>*> base)
    00096     {
    00097     if(timelimit<=0)
    00098     return base;
    00099
    00100     vector<Solution<R>*> v;
    00101     for(int i=0;i<base.size();i++)
    00102     v.push_back(base[i]->clone());
    00103
    00104     log_solution = NULL;
    00105     log_solution_set = v;
    00106     log_timelimit = timelimit;
    00107     log_efv = efv;
    00108
    00109     start();
    00110
    00111     for(int t=0;t<timelimit;t++)
    00112     if(!finished())
    00113     sleep(1);
    00114     else
    00115     return log_solution_set;
    00116
    00117     for(int t=0;t<SAFE_SEARCH_TOLERANCE;t++)
    00118     if(!finished())
    00119     {
    00120     cout << "Warning: Process not finished yet. Safe Search Tolerance ("<<t<<" secs.)"<<endl;
    00121     sleep(1);
    00122     }
    00123     else
    00124     return log_solution_set;
    00125
    00126     stop();
    00127
    00128     cout << "Warning: Safe Search saved method from looping! (maybe +1 solution memory lack)" << endl;
    00129     return base;
    00130     }
    00131     */
    00132
    00133    // no-optimization
    00134    Solution<R>& search(const Solution<R>& s, double timelimit = 100000000, double target_f = 0)
    00135    {
    00136       Solution<R>& s2 = s.clone();
    00137       exec(s2, timelimit, target_f);
    00138       return s2;
    00139    }
    00140    // optimizated version
    00141    pair<Solution<R>&, Evaluation<M>&>& search(const Solution<R>& s, const Evaluation<M>& e, double timelimit = 100000000, double target_f = 0)
    00142    {
    00143       Solution<R>& s2 = s.clone();
    00144       Evaluation<M>& e2 = e.clone();
    00145       exec(s2, e2, timelimit, target_f);
    00146       return *new pair<Solution<R>&, Evaluation<M>&> (s2, e2);
    00147    }
    00148
    00149    // no-optimization
    00150    //Population& search(ConstPopulation& p, double timelimit = 100000000, double target_f = 0)
    00151    virtual Population<R>& search(const Population<R>& p, double timelimit = 100000000, double target_f = 0)
    00152    {
    00153       Population<R>* pop = new Population<R> ();
    00154
    00155       for (unsigned i = 0; i < p.size(); i++)
    00156          pop->push_back(p.at(i).clone());
    00157
    00158       exec(*pop, timelimit, target_f);
    00159
    00160       return *pop;
    00161    }
    00162
    00163    // optimizated version
    00164    //pair<Population&, FitnessValues&>& search(ConstPopulation& p, ConstFitnessValues& ev,
    00165    virtual pair<Population<R>&, FitnessValues&>& search(const Population<R>& p, ConstFitnessValues& ev, double timelimit = 100000000, double target_f = 0)
    00166    {
    00167       //Population* p2 = new Population;
    00168       Population<R>* p2 = new Population<R> ;
    00169
    00170       for (unsigned i = 0; i < p.size(); i++)
    00171          p2->push_back(p.at(i).clone());
    00172
    00173       FitnessValues* ev2 = new FitnessValues;
    00174       for (unsigned i = 0; i < p.size(); i++)
    00175          ev2->push_back(&ev[i]->clone());
    00176
    00177       exec(*p2, *ev2, timelimit, target_f);
    00178
    00179       return *new pair<Population<R>&, FitnessValues&> (*p2, *ev2);
    00180    }
    00181
    00182    // core methods
    00183
    00184    // 1
    00185    virtual void exec(Solution<R>& s, double timelimit, double target_f)
    00186    {
    00187       // call 3
    00188       //Population p;
    00189       Population<R> p;
    00190
    00191       //p.push_back(&s);
    00192       p.push_back(s);
    00193
    00194       exec(p, timelimit, target_f);
    00195       // pop back? vector vai deletar o 's' sozinho?? testar! TODO
    00196    }
    00197
    00198    // 2
    00199    virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
    00200    {
    00201       // call 4
    00202       //Population p;
    00203       Population<R> p;
    00204
    00205       FitnessValues ev;
    00206
    00207       //p.push_back(&s);
    00208       p.push_back(s);
    00209
    00210       ev.push_back(&e);
    00211
    00212       exec(p, ev, timelimit, target_f);
    00213
    00214       // pop back? TODO
    00215       //s = v[0].first;
    00216       //e = v[0].second;
    00217    }
    00218
    00219    // TODO: Nao consegui programar vector<Solution<R>& >& v (???)
    00220    // TODO: E o que isso significa??? Usando ponteiro agora. em: vector<Solution<R>* >& v
    00221
    00222    // 3
    00223    //virtual void exec(Population& p, double timelimit, double target_f)
    00224    virtual void exec(Population<R>& p, double timelimit, double target_f)
    00225    {
    00226       // call 1
    00227       //Solution<R>& s = *(p[0]);
    00228       Solution<R>& s = (p.at(0));
    00229
    00230       exec(s, timelimit, target_f);
    00231    }
    00232
    00233    // 4
    00234    //virtual void exec(Population& p, FitnessValues& ev, double timelimit, double target_f)
    00235    virtual void exec(Population<R>& p, FitnessValues& ev, double timelimit, double target_f)
    00236    {
    00237       // call 2
    00238       //Solution<R>& s = *(p[0]);
    00239       Solution<R>& s = (p.at(0));
    00240
    00241       Evaluation<M>& e = *(ev[0]);
    00242
    00243       exec(s, e, timelimit, target_f);
    00244    }
    00245
    00246 };
    00247
    00248 #endif /* OPTFRAME_HEURISTIC_HPP_ */

All Classes Functions