local-search.hh
Go to the documentation of this file.
00001 // METS lib source file - local-search.hh                   -*- C++ -*-
00002 //
00003 /*
00004  * Software License Agreement (BSD License)
00005  *
00006  *  Copyright (c) 2006-2012, Mirko Maischberger <mirko.maischberger@gmail.com>
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *
00020  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  *  POSSIBILITY OF SUCH DAMAGE.
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           // evaluate the cost after the move
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         } // end for each move
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


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:25:14