EasyLocalpp  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
searchengine.hh
Go to the documentation of this file.
1 #if !defined(RUNNER_HH_)
2 #define RUNNER_HH_
3 
4 #include <stdexcept>
5 #include <climits>
6 #include <chrono>
7 #include <condition_variable>
8 #include <atomic>
9 
14 
15 namespace EasyLocal {
16 
17  namespace Debug {
18 
20  template <class Input, class State, typename CFtype>
22  }
23 
24  namespace Core {
25 
26 
32  template <class Input, class State, typename CFtype = int>
33  class SearchEngine : public Interruptible<CostStructure<CFtype>, State&>, public Parametrized
34  {
35  friend class Debug::AbstractTester<Input, State, CFtype>;
36 
37  public:
38 
39  typedef Input InputType;
40  typedef State StateType;
41  typedef CFtype CostFunctionType;
42 
50 
58  CostStructure<CFtype> Step(State& s, unsigned int n = 1) throw (ParameterNotSet, IncorrectParameterValue);
59 
61  virtual void RegisterParameters();
62 
67  virtual void ReadParameters(std::istream& is = std::cin, std::ostream& os = std::cout);
68 
72  virtual void Print(std::ostream& os = std::cout) const;
73 
75  unsigned int IterationOfBest() const
76  {
77  return iteration_of_best;
78  }
79 
81  unsigned long int Iteration() const
82  {
83  return iteration;
84  }
85 
87  const std::string name;
88 
90  const std::string description;
91 
93  virtual ~SearchEngine() {}
94 
96  virtual size_t Modality() const = 0;
97 
99  static std::vector<SearchEngine<Input, State, CFtype>*> searchengines;
100 
101  protected:
102 
109  SearchEngine(const Input& i, StateManager<Input, State, CFtype>&, std::string, std::string);
110 
116 
118  virtual void TerminateRun() = 0;
119 
121  virtual void PrepareIteration();
122 
124  virtual void CompleteIteration();
125 
127  virtual bool StopCriterion() = 0;
128 
130  virtual bool LowerBoundReached() const;
131 
133  bool MaxEvaluationsExpired() const;
134 
136  virtual void SelectMove() = 0;
137 
139  virtual bool AcceptableMoveFound() = 0;
140 
142  virtual void PrepareMove() {};
143 
145  virtual void MakeMove() = 0;
146 
148  virtual void CompleteMove() {};
149 
151  virtual std::function<CostStructure<CFtype>(State&)> MakeFunction()
152  {
153  return [this](State& s) -> CostStructure<CFtype> { return this->Go(s); };
154  }
155 
158 
160  const Input& in;
161 
164 
166  std::shared_ptr<State> p_current_state,
168  p_best_state;
169 
172 
175 
177  unsigned long int iteration_of_best;
178 
180  unsigned long int iteration;
181 
183  unsigned long int evaluations;
184 
187 
190  std::vector<double> weights;
191 
192  private:
193 
195  virtual void UpdateBestState() = 0;
196 
199 
201  CostStructure<CFtype> TerminateRun(State&);
202  };
203 
204  /*************************************************************************
205  * Implementation
206  *************************************************************************/
207 
208  template <class Input, class State, typename CFtype>
209  std::vector<SearchEngine<Input, State, CFtype>*> SearchEngine<Input, State, CFtype>::searchengines;
210 
211  template <class Input, class State, typename CFtype>
212  SearchEngine<Input, State, CFtype>::SearchEngine(const Input& in, StateManager<Input, State, CFtype>& sm, std::string name, std::string description)
213  : // Parameters
214  Parametrized(name, description), name(name), description(description), no_acceptable_move_found(false), in(in), sm(sm), weights(0)
215  {
216  // Add to the list of all searchengines
217  searchengines.push_back(this);
218  }
219 
220  template <class Input, class State, typename CFtype>
222  {
223  max_evaluations("max_evaluations", "Maximum total number of cost function evaluations allowed", this->parameters);
224  // This parameter has a default value
226  }
227 
228  template <class Input, class State, typename CFtype>
230  {
231  InitializeRun(s);
232  while (!MaxEvaluationsExpired() && !StopCriterion() && !LowerBoundReached() && !this->TimeoutExpired())
233  {
235  try
236  {
237  SelectMove();
238  if (AcceptableMoveFound())
239  {
240  PrepareMove();
241  MakeMove();
242  CompleteMove();
243  UpdateBestState();
244  }
245  }
246  catch (EmptyNeighborhood)
247  {
248  break;
249  }
251  }
252 
253  return TerminateRun(s);
254  }
255 
256 
260  template <class Input, class State, typename CFtype>
262  {
263  no_acceptable_move_found = false;
264  iteration++;
265  }
266 
270  template <class Input, class State, typename CFtype>
272  {}
273 
274  template <class Input, class State, typename CFtype>
276  {
277  iteration = 0;
278  iteration_of_best = 0;
279  evaluations = 0;
280  p_best_state = std::make_shared<State>(s); // creates the best state object by copying the content of s
281  p_current_state = std::make_shared<State>(s); // creates the current state object by copying the content of s
282  best_state_cost = current_state_cost = sm.CostFunctionComponents(s);
283  InitializeRun();
284  }
285 
286  template <class Input, class State, typename CFtype>
287  CostStructure<CFtype> SearchEngine<Input, State, CFtype>::TerminateRun(State& s)
288  {
289  s = *p_best_state;
290  TerminateRun();
291  return best_state_cost;
292  }
293 
294  template <class Input, class State, typename CFtype>
296  {
297  return sm.LowerBoundReached(current_state_cost.total);
298  }
299 
300  template <class Input, class State, typename CFtype>
302  {
303  return evaluations >= max_evaluations;
304  }
305 
306  template <class Input, class State, typename CFtype>
307  void SearchEngine<Input, State, CFtype>::ReadParameters(std::istream& is, std::ostream& os)
308  {
309  os << this->name << " -- INPUT PARAMETERS" << std::endl;
311  }
312 
313  template <class Input, class State, typename CFtype>
314  void SearchEngine<Input, State, CFtype>::Print(std::ostream& os) const
315  {
316  os << " " << this->name << std::endl;
317  Parametrized::Print(os);
318  }
319  }
320 }
321 
322 #endif // _RUNNER_HH_
static std::vector< SearchEngine< Input, State, CFtype > * > searchengines
Definition: searchengine.hh:99
CostStructure< CFtype > Step(State &s, unsigned int n=1)
CFtype max(const std::vector< CFtype > &values)
Definition: types.hh:34
virtual bool StopCriterion()=0
unsigned long int iteration_of_best
virtual void ReadParameters(std::istream &is=std::cin, std::ostream &os=std::cout)
StateManager< Input, State, CFtype > & sm
std::vector< double > weights
unsigned int IterationOfBest() const
Definition: searchengine.hh:75
SearchEngine(const Input &i, StateManager< Input, State, CFtype > &, std::string, std::string)
virtual void ReadParameters(std::istream &is=std::cin, std::ostream &os=std::cout)
Definition: parameter.hh:340
std::shared_ptr< State > p_current_state
virtual std::function< CostStructure< CFtype >State &)> MakeFunction()
unsigned long int Iteration() const
Definition: searchengine.hh:81
CostStructure< CFtype > Go(State &s)
CostStructure< CFtype > current_state_cost
const std::string description
Definition: searchengine.hh:90
virtual void Print(std::ostream &os=std::cout) const
virtual size_t Modality() const =0
virtual void TerminateRun()=0
std::shared_ptr< State > p_best_state
This component is responsible for all operations on the state which are independent of the neighborho...
Parameter< unsigned long int > max_evaluations
virtual bool LowerBoundReached() const
CostStructure< CFtype > best_state_cost
virtual void Print(std::ostream &os=std::cout) const
Definition: parameter.hh:355
unsigned long int evaluations
virtual bool AcceptableMoveFound()=0