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
00035 #ifndef LOCAL_SEARCH_HH_
00036 #define LOCAL_SEARCH_HH_
00037
00038 namespace mets {
00041
00048 template<typename move_manager_type>
00049 class local_search : public mets::abstract_search<move_manager_type>
00050 {
00051 public:
00065 local_search(evaluable_solution& starting_point,
00066 solution_recorder& recorder,
00067 move_manager_type& moveman,
00068 gol_type epsilon = 1e-7,
00069 bool short_circuit = false);
00070
00072 local_search(const local_search&);
00073 local_search& operator=(const local_search&);
00074
00081 virtual void
00082 search()
00083 throw(no_moves_error);
00084
00085 protected:
00086 bool short_circuit_m;
00087 gol_type epsilon_m;
00088 };
00089
00091
00092 }
00093
00094 template<typename move_manager_t>
00095 mets::local_search<move_manager_t>::local_search(evaluable_solution& working,
00096 solution_recorder& recorder,
00097 move_manager_t& moveman,
00098 gol_type epsilon,
00099 bool short_circuit)
00100 : abstract_search<move_manager_t>(working, recorder, moveman),
00101 short_circuit_m(short_circuit), epsilon_m(epsilon)
00102 {
00103 typedef abstract_search<move_manager_t> base_t;
00104 base_t::step_m = 0;
00105 }
00106
00107 template<typename move_manager_t>
00108 void
00109 mets::local_search<move_manager_t>::search()
00110 throw(no_moves_error)
00111 {
00112 typedef abstract_search<move_manager_t> base_t;
00113 typename move_manager_t::iterator best_movit;
00114
00115 base_t::solution_recorder_m.accept(base_t::working_solution_m);
00116
00117 gol_type best_cost =
00118 static_cast<mets::evaluable_solution&>(base_t::working_solution_m)
00119 .cost_function();
00120
00121 do
00122 {
00123 base_t::moves_m.refresh(base_t::working_solution_m);
00124 best_movit = base_t::moves_m.end();
00125 for(typename move_manager_t::iterator movit = base_t::moves_m.begin();
00126 movit != base_t::moves_m.end(); ++movit)
00127 {
00128
00129 gol_type cost = (*movit)->evaluate(base_t::working_solution_m);
00130 if(cost < best_cost - epsilon_m)
00131 {
00132 best_cost = cost;
00133 best_movit = movit;
00134 if(short_circuit_m) break;
00135 }
00136 }
00137
00138 if(best_movit != base_t::moves_m.end())
00139 {
00140 (*best_movit)->apply(base_t::working_solution_m);
00141 base_t::solution_recorder_m.accept(base_t::working_solution_m);
00142 base_t::current_move_m = best_movit;
00143 this->notify();
00144 }
00145
00146 } while(best_movit != base_t::moves_m.end());
00147 }
00148 #endif