pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pgr_ksp.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_ksp.cpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 Mail: vicky_vergara@hotmail.com
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 #if defined(__MINGW32__) || defined(_MSC_VER)
25 #include <winsock2.h>
26 #include <windows.h>
27 #ifdef unlink
28 #undef unlink
29 #endif
30 #endif
31 
32 
33 #include <deque>
34 #include <set>
35 #include "./../../common/src/basePath_SSEC.hpp"
36 
37 template < class G >
39  m_Heap.clear();
40 }
41 
42 template < class G >
44  Path path;
45 
46  Pgr_dijkstra< G > fn_dijkstra;
47  fn_dijkstra.dijkstra(graph, path, m_start, m_end);
48 
49  if (path.empty()) return;
50  curr_result_path = path;
51  m_ResultSet.insert(curr_result_path);
52 }
53 
54 template < class G>
55 std::deque<Path>
57  int64_t start_vertex, int64_t end_vertex, int K, bool heap_paths) {
58  m_ResultSet.clear();
59  m_Heap.clear();
60 
61  if ((start_vertex != end_vertex) && (K > 0)) {
62  if (!graph.has_vertex(start_vertex)
63  || !graph.has_vertex(end_vertex)) {
64  std::deque<Path> l_ResultList;
65  return l_ResultList;
66  }
67  v_source = graph.get_V(start_vertex);
68  v_target = graph.get_V(end_vertex);
69  m_start = start_vertex;
70  m_end = end_vertex;
71  executeYen(graph, K);
72  }
73 
74  while (!m_ResultSet.empty()) {
75  m_Heap.insert(*m_ResultSet.begin());
76  m_ResultSet.erase(m_ResultSet.begin());
77  }
78  std::deque<Path> l_ResultList(m_Heap.begin(), m_Heap.end());
79  if (!heap_paths && l_ResultList.size() > (size_t) K)
80  l_ResultList.resize(K);
81  return l_ResultList;
82 }
83 
84 
85 template < class G >
86 void Pgr_ksp< G >::removeVertices(G &graph, const Path &subpath) {
87  for (const auto &e : subpath)
88  graph.disconnect_vertex(e.node);
89 }
90 
91 template < class G >
92 void Pgr_ksp< G >::doNextCycle(G &graph) {
93  // REG_SIGINT
94 
95 
96  int64_t spurNodeId;
97  Path rootPath;
98  Path spurPath;
99 
100  for (unsigned int i = 0; i < curr_result_path.size() ; ++i) {
101  spurNodeId = curr_result_path[i].node;
102 
103  rootPath = curr_result_path.getSubpath(i);
104 
105  for (const auto &path : m_ResultSet) {
106  if (path.isEqual(rootPath)) {
107  if (path.size() > i + 1) {
108  graph.disconnect_edge(path[i].node, // from
109  path[i + 1].node); // to
110  }
111  }
112  }
113  removeVertices(graph, rootPath);
114 
115 
116  // THROW_ON_SIGINT
117  Pgr_dijkstra< G > fn_dijkstra;
118  fn_dijkstra.dijkstra(graph, spurPath, spurNodeId, m_end);
119  // this->dijkstra(spurPath, spurNodeId , m_end);
120  // THROW_ON_SIGINT
121 
122  if (spurPath.size() > 0) {
123  rootPath.appendPath(spurPath);
124  m_Heap.insert(rootPath);
125  }
126 
127  graph.restore_graph();
128  rootPath.clear();
129  spurPath.clear();
130  }
131 }
132 
133 template < class G >
134 void Pgr_ksp< G >::executeYen(G &graph, int K) {
135  clear();
136  getFirstSolution(graph);
137 
138  if (m_ResultSet.size() == 0) return; // no path found
139 
140  while ( m_ResultSet.size() < (unsigned int) K ) {
141  doNextCycle(graph);
142  if ( m_Heap.empty() ) break;
143  curr_result_path = *m_Heap.begin();
144  m_ResultSet.insert(curr_result_path);
145  m_Heap.erase(m_Heap.begin());
146  }
147 }
void clear()
Definition: pgr_ksp.cpp:38
CGAL::Filtered_kernel< SC > K
void getFirstSolution(G &graph)
Performs the first Dijkstra of the algorithm.
Definition: pgr_ksp.cpp:43
bool empty() const
path_element_t * path
Definition: BDATester.cpp:49
Path getSubpath(unsigned int j) const
void doNextCycle(G &graph)
Performs the next cycle of the algorithm.
Definition: pgr_ksp.cpp:92
std::deque< Path > Yen(G &graph, int64_t source, int64_t target, int K, bool heap_paths)
Definition: pgr_ksp.cpp:56
void dijkstra(G &graph, Path &path, int64_t start_vertex, int64_t end_vertex, bool only_cost=false)
one to one
size_t size() const
void clear()
void appendPath(const Path &o_path)
void removeVertices(G &graph, const Path &path)
stores in subPath the first i elements of path
Definition: pgr_ksp.cpp:86
void executeYen(G &graph, int top_k)
the actual algorithm
Definition: pgr_ksp.cpp:134