EasyLocalpp  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
localsearch.hh
Go to the documentation of this file.
1 #if !defined(_MOVE_RUNNER_HH_)
2 #define _MOVE_RUNNER_HH_
3 
7 #include <boost/signals2.hpp>
8 
9 namespace EasyLocal {
10 
11  namespace Core {
12 
18  template <class Input, class State, class Move, typename CFtype = int>
19  class LocalSearch : public SearchEngine<Input, State, CFtype>
20  {
21  public:
22 
23  typedef Move MoveType;
24 
25  enum Event { START = 1 << 0, NEW_BEST = 1 << 1, MADE_MOVE = 1 << 2, END = 1 << 3 };
26  const size_t events = 4;
27 
28  public:
29  template <typename Observer>
30  void registerObserver(Observer&& observer)
31  {
32  for (unsigned char i = 0; i < events; i++)
33  if (observer.events() & (1 << i))
34  observers[i].connect(observer);
35  }
36 
37  protected:
38  void notify(Event event) const
39  {
40  for (unsigned char i = 0; i < events; i++)
41  if (event & (1 << i))
42  observers[i](event, this->current_state_cost, this->current_move, this->StatusString());
43  }
44 
45  std::vector<boost::signals2::signal<void(Event event, CostStructure<CFtype> current_state_cost, const EvaluatedMove<Move, CFtype>& em, const std::string& status_string)>> observers;
46 
47  public:
48 
50  virtual size_t Modality() const { return ne.Modality(); }
51 
52  virtual std::string StatusString() const { return std::string("[no status info]"); }
53  protected:
54 
59  std::string name, std::string description);
60 
61 
62  virtual void TerminateRun();
63 
65 
66  virtual bool AcceptableMoveFound();
67 
68 
72  virtual void MakeMove();
73 
74  void UpdateBestState();
75  void UpdateStateCost();
76 
77  NeighborhoodExplorer<Input, State, Move, CFtype>& ne;
81  // data
82  EvaluatedMove<Move, CFtype> current_move;
84  };
85 
86  /*************************************************************************
87  * Implementation
88  *************************************************************************/
89 
90 
91  template <class Input, class State, class Move, typename CFtype>
92  void LocalSearch<Input, State, Move, CFtype>::UpdateBestState()
93  {
94  if (LessThan(this->current_state_cost.violations, this->best_state_cost.violations)
95  || (EqualTo(this->current_state_cost.violations, this->best_state_cost.violations) &&
96  (LessThan(this->current_state_cost.total, this->best_state_cost.total))))
97  {
98  *(this->p_best_state) = *(this->p_current_state);
99  this->best_state_cost = this->current_state_cost;
100 
101  notify(NEW_BEST);
102 
103  // so that idle iterations are printed correctly
104  this->iteration_of_best = this->iteration;
105  }
106  }
107 
108 
109  template <class Input, class State, class Move, typename CFtype>
113  std::string name,
114  std::string description)
115  : SearchEngine<Input, State, CFtype>(in, e_sm, name, description), observers(events), ne(e_ne)
116  {}
117 
118  template <class Input, class State, class Move, typename CFtype>
120  {
121  notify(START);
122  }
123 
124  template <class Input, class State, class Move, typename CFtype>
126  {
127  notify(END);
128  }
129 
130  template <class Input, class State, class Move, typename CFtype>
132  {
133  this->no_acceptable_move_found = !this->current_move.is_valid;
134  return this->current_move.is_valid;
135  }
136 
140  template <class Input, class State, class Move, typename CFtype>
142  {
143  if (current_move.is_valid)
144  {
145  ne.MakeMove(*this->p_current_state, current_move.move);
146  this->current_state_cost += current_move.cost;
147  notify(MADE_MOVE);
148  }
149  }
150  }
151 }
152 
153 #endif /*MOVERUNNER_HH_*/
bool LessThan(CFtype value1, CFtype value2)
EvaluatedMove< Move, CFtype > current_move
Definition: localsearch.hh:82
unsigned long int iteration_of_best
virtual size_t Modality() const
Definition: localsearch.hh:50
bool EqualTo(CFtype value1, CFtype value2)
virtual bool AcceptableMoveFound()
Definition: localsearch.hh:131
std::vector< boost::signals2::signal< void(Event event, CostStructure< CFtype > current_state_cost, const EvaluatedMove< Move, CFtype > &em, const std::string &status_string)> > observers
Definition: localsearch.hh:45
std::shared_ptr< State > p_current_state
CostStructure< CFtype > current_state_cost
NeighborhoodExplorer< Input, State, Move, CFtype > & ne
Definition: localsearch.hh:77
const std::string description
Definition: searchengine.hh:90
LocalSearch(const Input &in, StateManager< Input, State, CFtype > &e_sm, NeighborhoodExplorer< Input, State, Move, CFtype > &e_ne, std::string name, std::string description)
Definition: localsearch.hh:110
void notify(Event event) const
Definition: localsearch.hh:38
void registerObserver(Observer &&observer)
Definition: localsearch.hh:30
std::shared_ptr< State > p_best_state
This component is responsible for all operations on the state which are independent of the neighborho...
virtual std::string StatusString() const
Definition: localsearch.hh:52
CostStructure< CFtype > best_state_cost