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
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;
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
00054
00055
00056
00057
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
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
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
00150
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
00164
00165 virtual pair<Population<R>&, FitnessValues&>& search(const Population<R>& p, ConstFitnessValues& ev, double timelimit = 100000000, double target_f = 0)
00166 {
00167
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
00183
00184
00185 virtual void exec(Solution<R>& s, double timelimit, double target_f)
00186 {
00187
00188
00189 Population<R> p;
00190
00191
00192 p.push_back(s);
00193
00194 exec(p, timelimit, target_f);
00195
00196 }
00197
00198
00199 virtual void exec(Solution<R>& s, Evaluation<M>& e, double timelimit, double target_f)
00200 {
00201
00202
00203 Population<R> p;
00204
00205 FitnessValues ev;
00206
00207
00208 p.push_back(s);
00209
00210 ev.push_back(&e);
00211
00212 exec(p, ev, timelimit, target_f);
00213
00214
00215
00216
00217 }
00218
00219
00220
00221
00222
00223
00224 virtual void exec(Population<R>& p, double timelimit, double target_f)
00225 {
00226
00227
00228 Solution<R>& s = (p.at(0));
00229
00230 exec(s, timelimit, target_f);
00231 }
00232
00233
00234
00235 virtual void exec(Population<R>& p, FitnessValues& ev, double timelimit, double target_f)
00236 {
00237
00238
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