All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
custom_components.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_CUSTOM_COMPONENTS_HPP
16 #define PAAL_CUSTOM_COMPONENTS_HPP
17 
18 #include "paal/utils/functors.hpp"
19 
21 
22 #include <chrono>
23 #include <random>
24 
25 namespace paal {
26 namespace local_search {
27 
34 template <typename Gain = utils::return_one_functor,
35  typename Condition = utils::always_true>
37 
44  conditional_gain_adaptor(Gain gain = Gain(), Condition cond = Condition())
45  : m_gain(std::move(gain)), m_condition(std::move(cond)) {}
46 
54  template <typename... Args>
55  auto operator()(Args &&... args)
56  ->decltype(std::declval<Gain>()(std::forward<Args>(args)...)) {
57  if (!m_condition(std::forward<Args>(args)...)) {
58  return 0;
59  }
60  return m_gain(std::forward<Args>(args)...);
61  }
62 
63  private:
64  Gain m_gain;
65  Condition m_condition;
66 };
67 
78 template <typename Gain = utils::return_one_functor,
79  typename Condition = utils::always_true>
80 conditional_gain_adaptor<Gain, Condition>
82  Condition cond = Condition()) {
84  std::move(cond));
85 }
86 
96 template <typename Gain, typename ValueType> class gain_cut_small_improves {
97  public:
105  gain_cut_small_improves(Gain gain, ValueType currOpt, double epsilon)
106  : m_gain(std::move(gain)), m_curr_opt(currOpt), m_epsilon(epsilon) {}
107 
117  template <typename... Args> ValueType operator()(Args &&... args) {
118  ValueType dist = m_gain(std::forward<Args>(args)...);
119  if (dist > m_epsilon * m_curr_opt) {
120  m_curr_opt -= dist;
121  return dist;
122  }
123  return 0;
124  }
125 
131  void set_epsilon(double e) { m_epsilon = e; }
132 
138  void set_current_opt(ValueType opt) { m_curr_opt = opt; }
139 
140  private:
141  Gain m_gain;
142  ValueType m_curr_opt;
143  double m_epsilon;
144 };
145 
150  public:
156  stop_condition_count_limit(unsigned limit) : m_cnt(0), m_limit(limit) {}
157 
165  template <typename... Args> bool operator()(Args &&...) const {
166  return ++m_cnt >= m_limit;
167  }
168 
169  private:
170  mutable unsigned m_cnt;
171  const unsigned m_limit;
172 };
173 
177 template <typename duration = std::chrono::seconds,
178  typename clock = std::chrono::system_clock>
180  public:
187  : m_duration(d), m_start(clock::now()) {}
188 
197  template <typename... Args> bool operator()(Args &&...) {
198  return m_start + m_duration < clock::now();
199  }
200 
204  void restart() { m_start = clock::now(); }
205 
206  private:
207  typename clock::time_point m_start;
208  const duration m_duration;
209 };
210 
219 template <typename Gain, typename ValueType> struct compute_gain_wrapper {
227  : m_gain(gain), m_val(val) {}
228 
237  template <typename... Args> ValueType operator()(Args &&... args) {
238  auto diff = m_gain(std::forward<Args>(args)...);
239  m_val += diff;
240  return diff;
241  }
242 
248  ValueType get_val() const { return m_val; }
249 
250  private:
251  Gain m_gain;
252  ValueType &m_val;
253 };
254 
262 template <typename TabuList, typename Gain = utils::return_one_functor,
263  typename AspirationCriteria = utils::always_true>
265 
273  tabu_gain_adaptor(TabuList tabuList = TabuList(), Gain gain = Gain(),
274  AspirationCriteria aspirationCriteria =
275  AspirationCriteria())
276  : m_tabu_list(std::move(tabuList)),
277  m_aspiration_criteria_gain(std::move(gain),
278  std::move(aspirationCriteria)) {}
279 
287  template <typename... Args>
288  auto operator()(Args &&... args)
289  ->decltype(std::declval<Gain>()(std::forward<Args>(args)...)) {
290  if (!m_tabu_list.is_tabu(std::forward<Args>(args)...)) {
291  auto diff = m_aspiration_criteria_gain(std::forward<Args>(args)...);
292  if (detail::positive_delta(diff)) {
293  m_tabu_list.accept(std::forward<Args>(args)...);
294  }
295  return diff;
296  }
297  return decltype(std::declval<Gain>()(std::forward<Args>(args)...)){};
298  }
299 
300  private:
301  TabuList m_tabu_list;
303  m_aspiration_criteria_gain;
304 };
305 
318 template <typename TabuList, typename Gain = utils::always_true,
319  typename AspirationCriteria = utils::always_true>
320 tabu_gain_adaptor<TabuList, Gain, AspirationCriteria>
321 make_tabu_gain_adaptor(TabuList tabuList, Gain gain = Gain(),
322  AspirationCriteria aspirationCriteria =
323  AspirationCriteria()) {
325  std::move(tabuList), std::move(gain), std::move(aspirationCriteria));
326 }
327 
338 template <typename Commit, typename Solution, typename Comparator = utils::less>
340 
348  record_solution_commit_adapter(Solution &solution, Commit commit = Commit{},
349  Comparator comparator = Comparator{})
350  : m_solution(&solution), m_commit(std::move(commit)),
351  m_comparator(std::move(comparator)) {}
352 
360  template <typename Move> bool operator()(Solution &sol, const Move &move) {
361  auto ret = m_commit(sol, move);
362  if (m_comparator(*m_solution, sol)) {
363  *m_solution = sol;
364  }
365  return ret;
366  }
367 
373  const Solution &get_solution() const { return *m_solution; }
374 
380  Solution &get_solution() { return *m_solution; }
381 
382  private:
383  Solution *m_solution;
384  Commit m_commit;
385  Comparator m_comparator;
386 };
387 
400 template <typename Commit, typename Solution, typename Condition>
401 record_solution_commit_adapter<Commit, Solution, Condition>
402 make_record_solution_commit_adapter(Solution &s, Commit commit, Condition c) {
404  s, std::move(commit), std::move(c));
405 }
406 
407 } // local_search
408 } // paal
409 
410 #endif // PAAL_CUSTOM_COMPONENTS_HPP
ValueType operator()(Args &&...args)
transfers arguments to original gain, if the value is to small it is changed to 0.
This is custom StopCondition, it returns true after given time limit.
auto operator()(Args &&...args) -> decltype(std::declval< Gain >()(std::forward< Args >(args)...))
tabu_gain_adaptor< TabuList, Gain, AspirationCriteria > make_tabu_gain_adaptor(TabuList tabuList, Gain gain=Gain(), AspirationCriteria aspirationCriteria=AspirationCriteria())
make function for tabu_gain_adaptor
bool operator()(Args &&...)
Checks if the time is up.
This is adaptor on Commit which allows to record solution basing on condition It is particularly usef...
record_solution_commit_adapter(Solution &solution, Commit commit=Commit{}, Comparator comparator=Comparator{})
constructor
Solution & get_solution()
Access to the stored solution (non-const version)
record_solution_commit_adapter< Commit, Solution, Condition > make_record_solution_commit_adapter(Solution &s, Commit commit, Condition c)
make function for record_solution_commit_adapter
void set_current_opt(ValueType opt)
sets current Opt
This is custom StopCondition , it returns true after given count limit.
ValueType get_val() const
Returns sum of the improvements.
This file contains set of simple useful functors or functor adapters.
class describing Move
bool operator()(Solution &sol, const Move &move)
operator
This is the gain adapter which accepts gains improving the current solution by more than epsilon...
gain_cut_small_improves(Gain gain, ValueType currOpt, double epsilon)
Constructor,.
ValueType operator()(Args &&...args)
forwards args to original gain. Sum up the improvements.
compute_gain_wrapper(compute_gain_wrapper gain, ValueType &val)
Constructor.
bool local_search(Solution &solution, SearchStrategy searchStrategy, ContinueOnSuccess succ, ContinueOnFail fail, components...comps)
detail
Adapts gain to implement tabu search.
This wrapper counts sum of the improvements. It makes sense to use it only when ChooseFirstBetter str...
conditional_gain_adaptor< Gain, Condition > make_conditional_gain_adaptor(Gain gain=Gain(), Condition cond=Condition())
make for conditional_gain_adaptor
conditional_gain_adaptor(Gain gain=Gain(), Condition cond=Condition())
constructor
bool operator()(Args &&...) const
increment the counter and checks if the given limit is reached.
if the condition is not fulfilled this gain adaptor returns 0
tabu_gain_adaptor(TabuList tabuList=TabuList(), Gain gain=Gain(), AspirationCriteria aspirationCriteria=AspirationCriteria())
constructor
stop_condition_count_limit(unsigned limit)
Constructor.
const Solution & get_solution() const
Access to the stored solution (const version)
functor return true
Definition: functors.hpp:227
auto operator()(Args &&...args) -> decltype(std::declval< Gain >()(std::forward< Args >(args)...))
operator()