EasyLocalpp  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simplelocalsearch.hh
Go to the documentation of this file.
1 #if !defined(_SIMPLE_LOCAL_SEARCH_HH_)
2 #define _SIMPLE_LOCAL_SEARCH_HH_
3 
4 #include <future>
5 
10 
11 namespace EasyLocal {
12 
13  namespace Core {
14 
19  template <class Input, class Output, class State, typename CFtype = int>
21  : public AbstractLocalSearch<Input, Output, State, CFtype>
22  {
23  public:
25  SimpleLocalSearch(const Input& in,
28  std::string name);
30  void Print(std::ostream& os = std::cout) const;
31  void ReadParameters(std::istream& is = std::cin, std::ostream& os = std::cout);
32  protected:
33  void Go();
34  void AtTimeoutExpired();
36  };
37 
38  /*************************************************************************
39  * Implementation
40  *************************************************************************/
41 
53  template <class Input, class Output, class State, typename CFtype>
57  std::string name)
58  : AbstractLocalSearch<Input, Output, State, CFtype>(in, e_sm, e_om, name, "Simple Local Search Solver"), p_searchengine(nullptr)
59  {}
60 
61  template <class Input, class Output, class State, typename CFtype>
62  void SimpleLocalSearch<Input, Output, State, CFtype>::ReadParameters(std::istream& is, std::ostream& os)
63  {
64  os << "Simple Local Search Solver: " << this->name << " parameters" << std::endl;
65  os << "SearchEngine: " << std::endl;
66  if (this->p_searchengine)
67  this->p_searchengine->ReadParameters(is, os);
68  }
69 
70  template <class Input, class Output, class State, typename CFtype>
72  {
73  os << "Simple Local Search Solver: " << this->name << std::endl;
74  if (this->p_searchengine)
75  this->p_searchengine->Print(os);
76  else
77  os << "<no searchengine attached>" << std::endl;
78  }
79 
80 
87  template <class Input, class Output, class State, typename CFtype>
89  { this->p_searchengine = &r; }
90 
91 
92  template <class Input, class Output, class State, typename CFtype>
94  {
95  if (!p_searchengine)
96  // FIXME: add a more specific exception behavior
97  throw std::logic_error("SearchEngine not set in object " + this->name);
98  this->current_state_cost = p_searchengine->Go(*this->p_current_state);
99 
100  *this->p_best_state = *this->p_current_state;
101  this->best_state_cost = this->current_state_cost;
102  }
103 
104  template <class Input, class Output, class State, typename CFtype>
106  {
107  p_searchengine->Interrupt();
108  }
109  }
110 }
111 
112 #endif // _SIMPLE_LOCAL_SEARCH_HH_
void SetSearchEngine(SearchEngine< Input, State, CFtype > &r)
void Print(std::ostream &os=std::cout) const
void ReadParameters(std::istream &is=std::cin, std::ostream &os=std::cout)
SimpleLocalSearch(const Input &in, StateManager< Input, State, CFtype > &e_sm, OutputManager< Input, Output, State, CFtype > &e_om, std::string name)
SearchEngine< Input, State, CFtype > SearchEngineType
const std::string name
Definition: solver.hh:33
This component is responsible for all operations on the state which are independent of the neighborho...
const Input & in
Definition: solver.hh:65