PGROUTING  3.2
pgr_ksp.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_ksp.hpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 #ifndef INCLUDE_YEN_PGR_KSP_HPP_
26 #define INCLUDE_YEN_PGR_KSP_HPP_
27 #pragma once
28 
30 
31 #include <sstream>
32 #include <deque>
33 #include <vector>
34 #include <algorithm>
35 #include <set>
36 #include <limits>
37 
38 #include "cpp_common/pgr_assert.h"
39 #include "cpp_common/compPaths.h"
42 
43 namespace pgrouting {
44 namespace yen {
45 
46 template < class G >
47 class Pgr_ksp : public Pgr_messages {
48  typedef typename G::V V;
49  typedef std::set<Path, compPathsLess> pSet;
50 
51  public:
52  Pgr_ksp() :
53  m_K(0),
54  m_heap_paths(false) {
55  m_vis = new Visitor;
56  }
58  delete m_vis;
59  }
60 
61  std::deque<Path> Yen(
62  G &graph,
63  int64_t start_vertex,
64  int64_t end_vertex,
65  size_t K,
66  bool heap_paths) {
67  /*
68  * No path: already in destination
69  */
70  if ((start_vertex == end_vertex) || (K == 0)) {
71  return std::deque<Path>();
72  }
73 
74  /*
75  * no path: disconnected vertices
76  */
77  if (!graph.has_vertex(start_vertex)
78  || !graph.has_vertex(end_vertex)) {
79  return std::deque<Path>();
80  }
81 
82  /*
83  * clean up containers
84  */
85  clear();
86 
87 
88  v_source = graph.get_V(start_vertex);
89  v_target = graph.get_V(end_vertex);
90  m_start = start_vertex;
91  m_end = end_vertex;
92  m_K = K;
93  m_heap_paths = heap_paths;
94 
95 
96  executeYen(graph);
97 
98  auto paths = get_results();
99  if (!m_heap_paths && paths.size() > m_K) paths.resize(m_K);
100 
101  return paths;
102  }
103 
104  void clear() {
105  m_Heap.clear();
106  m_ResultSet.clear();
107  }
108 
109 
110 
111  class Visitor {
112  public:
113  virtual ~Visitor() {}
114 
115  virtual void on_insert_first_solution(const Path) const {
116  /* noop */
117  }
118 
119  virtual void on_insert_to_heap(const Path) const {
120  /* noop */
121  }
122  };
123 
124  protected:
126  void executeYen(G &graph) {
127  clear();
130 
131  if (m_ResultSet.size() == 0) return; // no path found
132 
133  while (m_ResultSet.size() < m_K) {
134  doNextCycle(graph);
135  if (m_Heap.empty()) break;
136  curr_result_path = *m_Heap.begin();
139  m_Heap.erase(m_Heap.begin());
140  }
141  }
142 
144 
147  protected:
149  Path path;
150 
151  Pgr_dijkstra< G > fn_dijkstra;
152  path = fn_dijkstra.dijkstra(graph, m_start, m_end);
153  path.recalculate_agg_cost();
154 
155  if (path.empty()) return path;
156  m_ResultSet.insert(path);
157  return path;
158  }
159 
160 
161  protected:
163  void doNextCycle(G &graph) {
164  for (unsigned int i = 0; i < curr_result_path.size(); ++i) {
165  int64_t spurNodeId = curr_result_path[i].node;
166 
167  auto rootPath = curr_result_path.getSubpath(i);
168 
169  for (const auto &path : m_ResultSet) {
170  if (path.isEqual(rootPath)) {
171  if (path.size() > i + 1) {
172  graph.disconnect_edge(path[i].node, // from
173  path[i + 1].node); // to
174  }
175  }
176  }
177 
178  removeVertices(graph, rootPath);
179 
180  Pgr_dijkstra< G > fn_dijkstra;
181  auto spurPath = fn_dijkstra.dijkstra(graph, spurNodeId, m_end);
182 
183  if (spurPath.size() > 0) {
184  rootPath.appendPath(spurPath);
185  m_Heap.insert(rootPath);
186  m_vis->on_insert_to_heap(rootPath);
187  }
188 
189  graph.restore_graph();
190  }
191  }
192 
193  protected:
195  void removeVertices(G &graph, const Path &subpath) {
196  for (const auto &e : subpath)
197  graph.disconnect_vertex(e.node);
198  }
199 
200  std::deque<Path> get_results() {
201  if (this->m_ResultSet.empty()) {
202  return std::deque<Path>();
203  }
204 
205  std::deque<Path> paths(m_ResultSet.begin(), m_ResultSet.end());
206 
207  if (m_heap_paths && !m_Heap.empty()) {
208  paths.insert(paths.end(), m_Heap.begin(), m_Heap.end());
209  }
210  pgassert(!paths.empty());
211 
212  std::sort(paths.begin(), paths.end(), compPathsLess());
213 
214  return paths;
215  }
216 
218 
219  protected:
221  V v_source;
224  int64_t m_start;
225  int64_t m_end;
226  size_t m_K;
228 
230 
233 
235 };
236 
237 
238 } // namespace yen
239 } // namespace pgrouting
240 
241 #endif // INCLUDE_YEN_PGR_KSP_HPP_
pgrouting::Pgr_messages
Definition: pgr_messages.h:39
pgrouting::yen::Pgr_ksp::m_vis
Visitor * m_vis
Definition: pgr_ksp.hpp:234
Path
Definition: basePath_SSEC.hpp:47
pgr_messages.h
pgrouting::yen::Pgr_ksp::Visitor::on_insert_to_heap
virtual void on_insert_to_heap(const Path) const
Definition: pgr_ksp.hpp:119
pgrouting::yen::Pgr_ksp::~Pgr_ksp
~Pgr_ksp()
Definition: pgr_ksp.hpp:57
pgrouting::yen::Pgr_ksp::clear
void clear()
Definition: pgr_ksp.hpp:104
pgrouting::yen::Pgr_ksp::Yen
std::deque< Path > Yen(G &graph, int64_t start_vertex, int64_t end_vertex, size_t K, bool heap_paths)
Definition: pgr_ksp.hpp:61
pgrouting::yen::Pgr_ksp::m_start
int64_t m_start
source id
Definition: pgr_ksp.hpp:224
pgrouting::yen::Pgr_ksp::get_results
std::deque< Path > get_results()
Definition: pgr_ksp.hpp:200
Path::empty
bool empty() const
Definition: basePath_SSEC.hpp:72
Path::getSubpath
Path getSubpath(unsigned int j) const
Definition: basePath_SSEC.cpp:141
pgrouting::yen::Pgr_ksp::m_ResultSet
pSet m_ResultSet
ordered set of shortest paths
Definition: pgr_ksp.hpp:231
pgrouting::yen::Pgr_ksp::V
G::V V
Definition: pgr_ksp.hpp:48
Path::appendPath
void appendPath(const Path &o_path)
Definition: basePath_SSEC.cpp:164
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::yen::Pgr_ksp::getFirstSolution
Path getFirstSolution(G &graph)
Performs the first Dijkstra of the algorithm.
Definition: pgr_ksp.hpp:148
pgrouting::yen::Pgr_ksp::Pgr_ksp
Pgr_ksp()
Definition: pgr_ksp.hpp:52
compPaths.h
pgrouting::yen::Pgr_ksp::pSet
std::set< Path, compPathsLess > pSet
Definition: pgr_ksp.hpp:49
pgrouting::yen::Pgr_ksp::m_heap_paths
bool m_heap_paths
Definition: pgr_ksp.hpp:227
pgrouting::yen::Pgr_ksp::v_target
V v_target
target descriptor
Definition: pgr_ksp.hpp:223
pgrouting::yen::Pgr_ksp
Definition: pgr_ksp.hpp:47
pgrouting::yen::Pgr_ksp::executeYen
void executeYen(G &graph)
the actual algorithm
Definition: pgr_ksp.hpp:126
pgrouting::yen::Pgr_ksp::Visitor::~Visitor
virtual ~Visitor()
Definition: pgr_ksp.hpp:113
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::yen::Pgr_ksp::m_K
size_t m_K
Definition: pgr_ksp.hpp:226
pgr_dijkstra.hpp
pgrouting::yen::Pgr_ksp::curr_result_path
Path curr_result_path
storage for the current result
Definition: pgr_ksp.hpp:229
pgrouting::yen::Pgr_ksp::removeVertices
void removeVertices(G &graph, const Path &subpath)
stores in subPath the first i elements of path
Definition: pgr_ksp.hpp:195
pgrouting::yen::Pgr_ksp::m_end
int64_t m_end
target id
Definition: pgr_ksp.hpp:225
pgrouting::yen::Pgr_ksp::Visitor
Definition: pgr_ksp.hpp:111
Path::size
size_t size() const
Definition: basePath_SSEC.hpp:71
pgrouting::yen::Pgr_ksp::Visitor::on_insert_first_solution
virtual void on_insert_first_solution(const Path) const
Definition: pgr_ksp.hpp:115
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::yen::Pgr_ksp::v_source
V v_source
source descriptor
Definition: pgr_ksp.hpp:222
pgrouting::compPathsLess
Definition: compPaths.h:47
pgrouting::alphashape::V
boost::graph_traits< BG >::vertex_descriptor V
Definition: pgr_alphaShape.h:58
pgrouting::Pgr_dijkstra
Definition: pgr_dijkstra.hpp:62
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::yen::Pgr_ksp::doNextCycle
void doNextCycle(G &graph)
Performs the next cycle of the algorithm.
Definition: pgr_ksp.hpp:163
pgrouting::yen::Pgr_ksp::m_Heap
pSet m_Heap
the heap
Definition: pgr_ksp.hpp:232
pgrouting::Pgr_dijkstra::dijkstra
Path dijkstra(G &graph, int64_t start_vertex, int64_t end_vertex, bool only_cost=false)
Dijkstra 1 to 1.
Definition: pgr_dijkstra.hpp:172
Path::recalculate_agg_cost
void recalculate_agg_cost()
Definition: basePath_SSEC.cpp:77
basePath_SSEC.hpp