Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef METS_TERMINATION_CRITERIA_HH_
00035 #define METS_TERMINATION_CRITERIA_HH_
00036
00037 namespace mets {
00038
00041
00045 class termination_criteria_chain
00046 {
00047 public:
00051 explicit
00052 termination_criteria_chain(termination_criteria_chain* next = 0)
00053 : next_m(next)
00054 { }
00056 termination_criteria_chain(const termination_criteria_chain&);
00057 termination_criteria_chain& operator=(const termination_criteria_chain&);
00058
00060 virtual
00061 ~termination_criteria_chain()
00062 { }
00063
00071 virtual bool
00072 operator()(const feasible_solution& fs);
00073
00078 virtual void reset();
00079
00080 protected:
00081 termination_criteria_chain* next_m;
00082 };
00083
00088 class iteration_termination_criteria
00089 : public termination_criteria_chain
00090 {
00091 public:
00093 iteration_termination_criteria(int max)
00094 : termination_criteria_chain(),
00095 max_m(max),
00096 iterations_m(max) {}
00097
00098 explicit
00099 iteration_termination_criteria
00100 (termination_criteria_chain* next, int max)
00101 : termination_criteria_chain(next),
00102 max_m(max),
00103 iterations_m(max) {}
00104
00105 bool
00106 operator()(const feasible_solution& fs)
00107 {
00108 if (iterations_m <= 0)
00109 return true;
00110
00111 --iterations_m;
00112 return termination_criteria_chain::operator()(fs);
00113 }
00114
00115 void
00116 reset()
00117 { iterations_m = max_m; termination_criteria_chain::reset(); }
00118
00119 protected:
00120 int max_m;
00121 int iterations_m;
00122 };
00123
00130 class noimprove_termination_criteria
00131 : public termination_criteria_chain
00132 {
00133 public:
00134 noimprove_termination_criteria(int max, gol_type epsilon = 1e-7)
00135 : termination_criteria_chain(),
00136 best_cost_m(std::numeric_limits<gol_type>::max()),
00137 max_noimprove_m(max),
00138 iterations_left_m(max),
00139 total_iterations_m(0),
00140 resets_m(0),
00141 second_guess_m(0),
00142 epsilon_m(epsilon)
00143 {}
00144
00145 noimprove_termination_criteria
00146 (termination_criteria_chain* next, int max, gol_type epsilon = 1e-7)
00147 : termination_criteria_chain(next),
00148 best_cost_m(std::numeric_limits<gol_type>::max()),
00149 max_noimprove_m(max),
00150 iterations_left_m(max),
00151 total_iterations_m(0),
00152 resets_m(0),
00153 second_guess_m(0),
00154 epsilon_m(epsilon)
00155 { }
00156
00157 bool
00158 operator()(const feasible_solution& fs);
00159 void reset()
00160 { iterations_left_m = max_noimprove_m;
00161 second_guess_m = total_iterations_m = resets_m = 0;
00162 best_cost_m = std::numeric_limits<gol_type>::max();
00163 termination_criteria_chain::reset();
00164 }
00165
00166 int second_guess() { return second_guess_m; }
00167 int iteration() { return total_iterations_m; }
00168 int resets() { return resets_m; }
00169
00170 protected:
00171 gol_type best_cost_m;
00172 int max_noimprove_m;
00173 int iterations_left_m;
00174 int total_iterations_m;
00175 int resets_m;
00176 int second_guess_m;
00177 gol_type epsilon_m;
00178 };
00179
00184 class threshold_termination_criteria
00185 : public termination_criteria_chain
00186 {
00187 public:
00188 threshold_termination_criteria(gol_type level, gol_type epsilon = 1e-7)
00189 : termination_criteria_chain(),
00190 level_m(level),
00191 epsilon_m(epsilon)
00192 { }
00193
00194 threshold_termination_criteria
00195 (termination_criteria_chain* next, gol_type level, gol_type epsilon = 1e-7)
00196 : termination_criteria_chain(next),
00197 level_m(level),
00198 epsilon_m(epsilon)
00199 { }
00200
00201 bool
00202 operator()(const feasible_solution& fs)
00203 {
00204 mets::gol_type current_cost =
00205 dynamic_cast<const evaluable_solution&>(fs).cost_function();
00206
00207 if(current_cost < level_m + epsilon_m)
00208 return true;
00209
00210 return termination_criteria_chain::operator()(fs);
00211 }
00212
00213 void reset()
00214 { termination_criteria_chain::reset(); }
00215
00216 protected:
00217 gol_type level_m;
00218 gol_type epsilon_m;
00219 };
00220
00231 class forever : public termination_criteria_chain
00232 {
00233 public:
00234 forever() : termination_criteria_chain() {}
00235 bool
00236 operator()(const feasible_solution& )
00237 { return false; }
00238 void reset()
00239 { termination_criteria_chain::reset(); }
00240 };
00241
00243 }
00244
00245
00246 inline bool
00247 mets::termination_criteria_chain::operator()(const feasible_solution& fs)
00248 {
00249 if(next_m)
00250 return next_m->operator()(fs);
00251 else
00252 return false;
00253 }
00254
00255
00256 inline void
00257 mets::termination_criteria_chain::reset()
00258 {
00259 if(next_m) next_m->reset();
00260 }
00261
00262
00263 inline bool
00264 mets::noimprove_termination_criteria::operator()(const feasible_solution& fs)
00265 {
00266 mets::gol_type current_cost =
00267 dynamic_cast<const evaluable_solution&>(fs).cost_function();
00268 if(current_cost < best_cost_m - epsilon_m)
00269 {
00270 best_cost_m = current_cost;
00271 second_guess_m = std::max(second_guess_m,
00272 (max_noimprove_m - iterations_left_m));
00273 iterations_left_m = max_noimprove_m;
00274 resets_m++;
00275 }
00276
00277
00278 if(iterations_left_m <= 0)
00279 return true;
00280
00281 total_iterations_m++;
00282 --iterations_left_m;
00283
00284 return termination_criteria_chain::operator()(fs);
00285 }
00286
00287 #endif