All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
facility_location_solution_adapter.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 //=======================================================================
16 #ifndef PAAL_FACILITY_LOCATION_SOLUTION_ADAPTER_HPP
17 #define PAAL_FACILITY_LOCATION_SOLUTION_ADAPTER_HPP
18 
19 #define BOOST_RESULT_OF_USE_DECLTYPE
20 
22 #include "paal/utils/functors.hpp"
25 
26 #include <boost/range/algorithm/copy.hpp>
27 #include <boost/range/distance.hpp>
28 #include <boost/range/algorithm/find.hpp>
29 
30 #include <unordered_map>
31 #include <unordered_set>
32 
33 namespace paal {
34 namespace local_search {
35 
43 template <typename facility_location_solution>
45  typedef facility_location_solution FLS;
46 
47  //TODO in fractioned MUCA commit, there will be function separated for that
48  template <typename Collection, typename Range
49  = const typename boost::iterator_range<typename Collection::const_iterator>::type>
50  auto get_cycledCopy(const Collection &col, std::size_t index) const
51  -> boost::joined_range<Range, Range> {
52  return boost::join(
53  boost::make_iterator_range(col.begin() + index, col.end()),
54  boost::make_iterator_range(col.begin(), col.begin() + index));
55  }
56 
57  public:
58  typedef typename facility_location_solution::VertexType VertexType;
60  typedef decltype(std::declval<FLS>().get_chosen_facilities()) Chosen;
62  typedef decltype(std::declval<FLS>().get_unchosen_facilities()) Unchosen;
63  typedef typename data_structures::facility_location_solution_traits<
64  FLS>::Dist Dist;
65  typedef std::vector<VertexType> UnchosenCopy;
66  typedef std::vector<VertexType> ChosenCopy;
67 
68  private:
69  facility_location_solution &m_sol;
71  UnchosenCopy m_unchosen_copy;
73  ChosenCopy m_chosen_copy;
75  std::size_t m_last_used_unchosen;
77  std::size_t m_last_used_chosen;
78 
79 public:
85  facility_location_solution_adapter(facility_location_solution &sol)
86  : m_sol(sol), m_unchosen_copy(m_sol.get_unchosen_facilities().begin(),
87  m_sol.get_unchosen_facilities().end()),
88  m_chosen_copy(m_sol.get_chosen_facilities().begin(),
89  m_sol.get_chosen_facilities().end()),
90  m_last_used_unchosen{}, m_last_used_chosen{} {}
91 
99  Dist add_facility_tentative(VertexType v) { return m_sol.add_facility(v); }
100 
108  Dist add_facility(VertexType v) {
109  auto ret = add_facility_tentative(v);
110  auto elemIter = boost::range::find(m_unchosen_copy, v);
111  assert(elemIter != m_unchosen_copy.end());
112  elemIter = m_unchosen_copy.erase(elemIter);
113  m_last_used_unchosen = elemIter - m_unchosen_copy.begin();
114  m_chosen_copy.push_back(v);
115  return ret;
116  }
117 
125  Dist remove_facility_tentative(VertexType v) {
126  return m_sol.rem_facility(v);
127  }
128 
136  Dist remove_facility(VertexType v) {
137  auto ret = remove_facility_tentative(v);
138  m_unchosen_copy.push_back(v);
139  auto elemIter = boost::range::find(m_chosen_copy, v);
140  assert(elemIter != m_chosen_copy.end());
141  elemIter = m_chosen_copy.erase(elemIter);
142  m_last_used_chosen = elemIter - m_chosen_copy.begin();
143  return ret;
144  }
145 
151  facility_location_solution &getfacility_location_solution() {
152  return m_sol;
153  }
154 
160  const facility_location_solution &getfacility_location_solution() const {
161  return m_sol;
162  }
163 
169  auto getUnchosenCopy() const -> decltype(this->get_cycledCopy(
170  m_unchosen_copy, m_last_used_unchosen)) {
171  return get_cycledCopy(m_unchosen_copy, m_last_used_unchosen);
172  }
173 
181  auto getChosenCopy() const -> decltype(this->get_cycledCopy(
182  m_chosen_copy, m_last_used_chosen)){
183  return get_cycledCopy(m_chosen_copy, m_last_used_chosen);
184  }
185 };
186 
187 } // local_search
188 } // paal
189 
190 #endif // PAAL_FACILITY_LOCATION_SOLUTION_ADAPTER_HPP
decltype(std::declval< FLS >().get_unchosen_facilities()) typedef Unchosen
type of Unchosen collection
facility_location_solution & getfacility_location_solution()
get solution
decltype(std::declval< FLS >().get_chosen_facilities()) typedef Chosen
type of Chosen collection
This file contains set of simple useful functors or functor adapters.
Dist add_facility_tentative(VertexType v)
adds facility tentatively (used in gain computation).
bool local_search(Solution &solution, SearchStrategy searchStrategy, ContinueOnSuccess succ, ContinueOnFail fail, components...comps)
detail
auto getChosenCopy() const -> decltype(this->get_cycledCopy(m_chosen_copy, m_last_used_chosen))
returns copy of chosen facilities
const facility_location_solution & getfacility_location_solution() const
gets solution
Dist remove_facility_tentative(VertexType v)
ads facility tentatively (used in gain computation)
auto getUnchosenCopy() const -> decltype(this->get_cycledCopy(m_unchosen_copy, m_last_used_unchosen))
returns copy of unchosen facilities
facility_location_solution adapter chosen range and unchosen range must be joined into one homogenus ...