PGROUTING  3.2
bdAstar_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdAstar_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2016 Celia Virginia Vergara Castillo
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
31 
32 #include <sstream>
33 #include <deque>
34 #include <vector>
35 #include <algorithm>
36 
37 #include "bdAstar/pgr_bdAstar.hpp"
38 
39 #include "cpp_common/pgr_alloc.hpp"
40 #include "cpp_common/pgr_assert.h"
41 
42 
43 
44 
45 
46 /************************************************************
47  edges_sql TEXT,
48  start_vid BIGINT,
49  end_vid BIGINT,
50  directed BOOLEAN DEFAULT true,
51  only_cost BOOLEAN DEFAULT false,
52  ***********************************************************/
53 
54 template < class G >
55 static
56 std::deque<Path>
58  G &graph,
59  std::vector < pgr_combination_t > &combinations,
60  std::vector < int64_t > sources,
61  std::vector < int64_t > targets,
62 
63  int heuristic,
64  double factor,
65  double epsilon,
66  std::ostream &log,
67  bool only_cost) {
68  log << "entering static function\n";
69 
71  std::deque<Path> paths;
72 
73  if (combinations.empty()) {
74  std::sort(sources.begin(), sources.end());
75  sources.erase(
76  std::unique(sources.begin(), sources.end()),
77  sources.end());
78 
79  std::sort(targets.begin(), targets.end());
80  targets.erase(
81  std::unique(targets.begin(), targets.end()),
82  targets.end());
83 
84  for (const auto source : sources) {
85  for (const auto target : targets) {
86  fn_bdAstar.clear();
87 
88  if (!graph.has_vertex(source)
89  || !graph.has_vertex(target)) {
90  paths.push_back(Path(source, target));
91  continue;
92  }
93 
94  paths.push_back(fn_bdAstar.pgr_bdAstar(
95  graph.get_V(source), graph.get_V(target),
96  heuristic, factor, epsilon, only_cost));
97  }
98  }
99 
100  } else {
101  std::sort(combinations.begin(), combinations.end(),
102  [](const pgr_combination_t &lhs, const pgr_combination_t &rhs)->bool {
103  return lhs.target < rhs.target;
104  });
105  std::stable_sort(combinations.begin(), combinations.end(),
106  [](const pgr_combination_t &lhs, const pgr_combination_t &rhs)->bool {
107  return lhs.source < rhs.source;
108  });
109 
110  pgr_combination_t previousCombination{0, 0};
111 
112  for (const pgr_combination_t &comb : combinations) {
113  fn_bdAstar.clear();
114 
115  if (comb.source == previousCombination.source &&
116  comb.target == previousCombination.target) {
117  continue;
118  }
119 
120  if (!graph.has_vertex(comb.source)
121  || !graph.has_vertex(comb.target)) {
122  paths.push_back(Path(comb.source, comb.target));
123  continue;
124  }
125 
126  paths.push_back(fn_bdAstar.pgr_bdAstar(
127  graph.get_V(comb.source), graph.get_V(comb.target),
128  heuristic, factor, epsilon, only_cost));
129 
130  previousCombination = comb;
131  }
132  }
133 
134  log << fn_bdAstar.log();
135 
136  return paths;
137 }
138 
139 
140 void
142  Pgr_edge_xy_t *edges,
143  size_t total_edges,
144  pgr_combination_t *combinations,
145  size_t total_combinations,
146  int64_t *start_vidsArr,
147  size_t size_start_vidsArr,
148  int64_t *end_vidsArr,
149  size_t size_end_vidsArr,
150 
151 
152  bool directed,
153  int heuristic,
154  double factor,
155  double epsilon,
156  bool only_cost,
157 
158  General_path_element_t **return_tuples,
159  size_t *return_count,
160 
161  char ** log_msg,
162  char ** notice_msg,
163  char ** err_msg) {
164  std::ostringstream log;
165  std::ostringstream err;
166  std::ostringstream notice;
167  try {
168  pgassert(!(*log_msg));
169  pgassert(!(*notice_msg));
170  pgassert(!(*err_msg));
171  pgassert(!(*return_tuples));
172  pgassert(*return_count == 0);
173  pgassert(total_edges != 0);
174 
175 
176  log << "Inserting vertices into a c++ vector structure";
177  std::vector<int64_t>
178  start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
179  std::vector< int64_t >
180  end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
181  std::vector< pgr_combination_t >
182  combinations_vector(combinations, combinations + total_combinations);
183 
184  graphType gType = directed? DIRECTED: UNDIRECTED;
185 
186  std::deque<Path> paths;
187  log << "starting process\n";
188  if (directed) {
189  log << "Working with directed Graph\n";
191  pgrouting::extract_vertices(edges, total_edges),
192  gType);
193  digraph.insert_edges(edges, total_edges);
194 
195  paths = pgr_bdAstar(digraph,
196  combinations_vector,
197  start_vertices,
198  end_vertices,
199  heuristic,
200  factor,
201  epsilon,
202  log,
203  only_cost);
204  } else {
205  log << "Working with Undirected Graph\n";
207  pgrouting::extract_vertices(edges, total_edges),
208  gType);
209  undigraph.insert_edges(edges, total_edges);
210 
211  paths = pgr_bdAstar(
212  undigraph,
213  combinations_vector,
214  start_vertices,
215  end_vertices,
216  heuristic,
217  factor,
218  epsilon,
219  log,
220  only_cost);
221  }
222 
223  size_t count(0);
224  count = count_tuples(paths);
225 
226  if (count == 0) {
227  (*return_tuples) = NULL;
228  (*return_count) = 0;
229  notice <<
230  "No paths found";
231  *log_msg = pgr_msg(notice.str().c_str());
232  return;
233  }
234 
235  (*return_tuples) = pgr_alloc(count, (*return_tuples));
236  log << "\nConverting a set of paths into the tuples";
237  (*return_count) = (collapse_paths(return_tuples, paths));
238 
239 
240 #if 0
241  auto count = path.size();
242 
243  if (count == 0) {
244  (*return_tuples) = NULL;
245  (*return_count) = 0;
246  notice <<
247  "No paths found between start_vid and end_vid vertices";
248  } else {
249  (*return_tuples) = pgr_alloc(count, (*return_tuples));
250  size_t sequence = 0;
251  path.generate_postgres_data(return_tuples, sequence);
252  (*return_count) = sequence;
253  }
254 #endif
255 
256  pgassert(*err_msg == NULL);
257  *log_msg = log.str().empty()?
258  nullptr :
259  pgr_msg(log.str().c_str());
260  *notice_msg = notice.str().empty()?
261  nullptr :
262  pgr_msg(notice.str().c_str());
263  } catch (AssertFailedException &except) {
264  if (*return_tuples) free(*return_tuples);
265  (*return_count) = 0;
266  err << except.what();
267  *err_msg = pgr_msg(err.str().c_str());
268  *log_msg = pgr_msg(log.str().c_str());
269  } catch (std::exception& except) {
270  if (*return_tuples) free(*return_tuples);
271  (*return_count) = 0;
272  err << except.what();
273  *err_msg = pgr_msg(err.str().c_str());
274  *log_msg = pgr_msg(log.str().c_str());
275  } catch(...) {
276  if (*return_tuples) free(*return_tuples);
277  (*return_count) = 0;
278  err << "Caught unknown exception!";
279  *err_msg = pgr_msg(err.str().c_str());
280  *log_msg = pgr_msg(log.str().c_str());
281  }
282 }
pgr_bdAstar
static std::deque< Path > pgr_bdAstar(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets, int heuristic, double factor, double epsilon, std::ostream &log, bool only_cost)
Definition: bdAstar_driver.cpp:57
Path
Definition: basePath_SSEC.hpp:47
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
do_pgr_bdAstar
void do_pgr_bdAstar(Pgr_edge_xy_t *edges, size_t total_edges, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: bdAstar_driver.cpp:141
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
bdAstar_driver.h
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
pgrouting::bidirectional::Pgr_bdAstar::pgr_bdAstar
Path pgr_bdAstar(V start_vertex, V end_vertex, int heuristic, double factor, double epsilon, bool only_cost)
Definition: pgr_bdAstar.hpp:86
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_alloc.hpp
pgr_combination_t
Definition: pgr_combination_t.h:43
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
pgr_bdAstar.hpp
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgrouting::bidirectional::Pgr_bidirectional::clear
void clear()
Definition: pgr_bidirectional.hpp:86
pgrouting::extract_vertices
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
Definition: basic_vertex.cpp:59
pgrouting::bidirectional::Pgr_bdAstar
Definition: pgr_bdAstar.hpp:50
Pgr_edge_xy_t
Definition: pgr_edge_xy_t.h:38
pgrouting::bidirectional::Pgr_bidirectional::log
std::string log() const
Definition: pgr_bidirectional.hpp:84
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139