EasyLocalpp  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
abstractlocalsearch.hh
Go to the documentation of this file.
1 #if !defined(_ABSTRACT_LOCAL_SEARCH_HH_)
2 #define _ABSTRACT_LOCAL_SEARCH_HH_
3 
4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 
14 
15 namespace EasyLocal {
16 
17 namespace Core {
18 
23 template<class Input, class Output, class State, typename CFtype = int>
24 class AbstractLocalSearch: public Parametrized, public Solver<Input, Output, CFtype>, public Interruptible<int> {
25 public:
28  virtual SolverResult<Input, Output, CFtype> Resolve(const Output& initial_solution) throw (ParameterNotSet, IncorrectParameterValue) final;
29 protected:
31  std::string name, std::string description);
32 
34  }
35 
37  virtual std::function<int(void)> MakeFunction() {
38  return [this](void) -> int {
39  this->Go();
40  return 1;
41  };
42  }
43 
44  virtual void FindInitialState();
45  // This will be the actual solver strategy implementation
46  virtual void Go() = 0;
51  std::shared_ptr<State> p_current_state, p_best_state;
54  std::shared_ptr<Output> p_out;
55  // parameters
56 
57  void RegisterParameters();
58 
62 
63 private:
64  void InitializeSolve() throw (ParameterNotSet, IncorrectParameterValue);
65  void TerminateSolve();
66 };
67 
68 /*************************************************************************
69  * Implementation
70  *************************************************************************/
71 
80 template<class Input, class Output, class State, typename CFtype>
81 AbstractLocalSearch<Input, Output, State, CFtype>::AbstractLocalSearch(const Input& in, StateManager<Input, State, CFtype>& e_sm,
82  OutputManager<Input, Output, State, CFtype>& e_om, std::string name, std::string description) :
83  Parametrized(name, description), Solver<Input, Output, CFtype>(in, name), sm(e_sm), om(e_om) {
84 }
85 
86 template<class Input, class Output, class State, typename CFtype>
88  init_trials("init_trials", "Number of states to be tried in the initialization phase", this->parameters);
89  random_initial_state("random_state", "Random initial state", this->parameters);
90  timeout("timeout", "Solver timeout (if not specified, no timeout)", this->parameters);
91  init_trials = 1;
92  random_initial_state = true;
93 }
94 
99 template<class Input, class Output, class State, typename CFtype>
103  else {
104  sm.GreedyState(*p_current_state);
105  current_state_cost = sm.CostFunctionComponents(*p_current_state);
106  }
109 }
110 
111 template<class Input, class Output, class State, typename CFtype>
113  p_best_state = std::make_shared < State > (this->in);
114  p_current_state = std::make_shared < State > (this->in);
115 }
116 
117 template<class Input, class Output, class State, typename CFtype>
119  auto start = std::chrono::high_resolution_clock::now();
120  InitializeSolve();
122  if (timeout.IsSet()) {
123  SyncRun(std::chrono::milliseconds(static_cast<long long int>(timeout * 1000.0)));
124  } else
125  Go();
126  p_out = std::make_shared < Output > (this->in);
127  om.OutputState(*p_best_state, *p_out);
128  TerminateSolve();
129 
130  double run_time =
131  std::chrono::duration_cast < std::chrono::duration<double, std::ratio<1>>>(std::chrono::high_resolution_clock::now() - start).count();
132 
133  return SolverResult<Input, Output, CFtype>(*p_out, sm.CostFunctionComponents(*p_best_state), run_time);
134 }
135 
136 template<class Input, class Output, class State, typename CFtype>
139  auto start = std::chrono::high_resolution_clock::now();
140 
141  InitializeSolve();
142  om.InputState(*p_current_state, initial_solution);
144  best_state_cost = current_state_cost = sm.CostFunctionComponents(*p_current_state);
145  if (timeout.IsSet())
146  SyncRun(std::chrono::milliseconds(static_cast<long long int>(timeout * 1000.0)));
147  else
148  Go();
149  p_out = std::make_shared < Output > (this->in);
150  om.OutputState(*p_best_state, *p_out);
151  TerminateSolve();
152 
153  double run_time =
154  std::chrono::duration_cast < std::chrono::duration<double, std::ratio<1>>>(std::chrono::high_resolution_clock::now() - start).count();
155 
156  return SolverResult<Input, Output, CFtype>(*p_out, sm.CostFunctionComponents(*p_best_state), run_time);
157 }
158 
159 template<class Input, class Output, class State, typename CFtype>
161 }
162 }
163 }
164 
165 #endif // _ABSTRACT_LOCAL_SEARCH_HH_
int SyncRun(std::chrono::milliseconds timeout, Args...args)
CostStructure< CFtype > current_state_cost
virtual std::function< int(void)> MakeFunction()
virtual SolverResult< Input, Output, CFtype > Solve() final
AbstractLocalSearch(const Input &in, StateManager< Input, State, CFtype > &e_sm, OutputManager< Input, Output, State, CFtype > &e_om, std::string name, std::string description)
Constructs an abstract local search solver.
StateManager< Input, State, CFtype > & sm
virtual SolverResult< Input, Output, CFtype > Resolve(const Output &initial_solution) final
std::shared_ptr< State > p_current_state
OutputManager< Input, Output, State, CFtype > & om
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