All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
facility_location_swap.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_FACILITY_LOCATION_SWAP_HPP
16 #define PAAL_FACILITY_LOCATION_SWAP_HPP
17 
21 
22 #include <boost/iterator/iterator_adaptor.hpp>
23 #include <boost/iterator/transform_iterator.hpp>
24 
25 #include <cassert>
26 #include <vector>
27 #include <numeric>
28 #include <cstdlib>
29 
30 namespace paal {
31 namespace local_search {
32 
38 template <typename T> class Swap {
39  public:
46  Swap(T from, T to) : m_from(from), m_to(to) {}
47 
48  Swap() = default;
49 
55  T get_from() const { return m_from; }
56 
62  T get_to() const { return m_to; }
63 
69  void set_from(T from) { m_from = from; }
70 
76  void set_to(T to) { m_to = to; }
77 
78  private:
79  T m_from;
80  T m_to;
81 };
82 
84 struct make_swap {
86  template <typename T> Swap<T> operator()(T from, T to) const {
87  return Swap<T>(from, to);
88  }
89 };
90 
106  template <class Solution, class VertexType>
107  auto operator()(Solution &sol, const Swap<VertexType> &s) const {
108 
109  auto ret = sol.add_facility_tentative(s.get_to());
110  ret += sol.remove_facility_tentative(s.get_from());
111  auto back = sol.add_facility_tentative(s.get_from());
112  back += sol.remove_facility_tentative(s.get_to());
113  assert(ret == -back);
114  return -ret;
115  }
116 };
117 
129  template <typename Solution, typename VertexType>
130  bool operator()(Solution &sol, const Swap<VertexType> &s) const {
131  sol.add_facility(s.get_to());
132  sol.remove_facility(s.get_from());
133  return true;
134  }
135 };
136 
141 
143  template <typename Solution>
144  auto operator()(const Solution &s) const {
146  make_swap{}, s.getChosenCopy(), s.getUnchosenCopy());
147  decltype(begin) end;
148  return boost::make_iterator_range(begin, end);
149  }
150 };
151 
152 } // local_search
153 } // paal
154 
155 #endif // PAAL_FACILITY_LOCATION_SWAP_HPP
void set_to(T to)
from setter
gain functor for swap in facility location problem.
Swap< T > operator()(T from, T to) const
operator()
auto operator()(const Solution &s) const
operator()
T get_from() const
from getter
void set_from(T from)
form setter
commit functor for facility location problem
bool operator()(Solution &sol, const Swap< VertexType > &s) const
operator()
auto operator()(Solution &sol, const Swap< VertexType > &s) const
operator()
combine_iterator< Joiner, detail::rem_ref< Ranges >...> make_combine_iterator(Joiner joiner, Ranges &&...ranges)
make for combine_iterator
bool local_search(Solution &solution, SearchStrategy searchStrategy, ContinueOnSuccess succ, ContinueOnFail fail, components...comps)
detail
get moves functor for facility location problem
Swap(T from, T to)
constructor