PGROUTING  3.2
astar_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail:
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 <algorithm>
35 #include <vector>
36 
37 #include "astar/pgr_astar.hpp"
38 
39 #include "cpp_common/pgr_alloc.hpp"
40 #include "cpp_common/pgr_assert.h"
41 
42 
43 
44 template < class G >
45 std::deque<Path>
47  G &graph,
48  std::vector <pgr_combination_t> &combinations,
49  std::vector<int64_t> sources,
50  std::vector<int64_t> targets,
51  int heuristic,
52  double factor,
53  double epsilon,
54  bool only_cost,
55  bool normal) {
56  std::sort(sources.begin(), sources.end());
57  sources.erase(
58  std::unique(sources.begin(), sources.end()),
59  sources.end());
60 
61  std::sort(targets.begin(), targets.end());
62  targets.erase(
63  std::unique(targets.begin(), targets.end()),
64  targets.end());
65 
67  auto paths = combinations.empty() ?
68  fn_astar.astar(graph, sources, targets, heuristic, factor, epsilon, only_cost)
69  : fn_astar.astar(graph, combinations, heuristic, factor, epsilon, only_cost);
70 
71  if (!normal) {
72  for (auto &path : paths) {
73  path.reverse();
74  }
75  }
76  return paths;
77 }
78 
79 
80 /************************************************************
81  edges_sql TEXT,
82  vertex_table TEXT,
83  start_vid BIGINT,
84  end_vid BIGINT directed BOOLEAN DEFAULT true,
85  ***********************************************************/
87  Pgr_edge_xy_t *edges, size_t total_edges,
88 
89  pgr_combination_t *combinations, size_t total_combinations,
90 
91  int64_t *start_vidsArr, size_t size_start_vidsArr,
92  int64_t *end_vidsArr, size_t size_end_vidsArr,
93  bool directed,
94  int heuristic,
95  double factor,
96  double epsilon,
97  bool only_cost,
98  bool normal,
99  General_path_element_t **return_tuples,
100  size_t *return_count,
101  char** log_msg,
102  char** notice_msg,
103  char** err_msg) {
104  std::ostringstream log;
105  std::ostringstream notice;
106  std::ostringstream err;
107  try {
108  pgassert(!(*log_msg));
109  pgassert(!(*err_msg));
110  pgassert(!(*return_tuples));
111  pgassert(*return_count == 0);
112  pgassert(total_edges != 0);
113 
114 
115  log << "Inserting target vertices into a c++ vector structure\n";
116  std::vector<pgr_combination_t>
117  combinations_vector(combinations, combinations + total_combinations);
118  std::vector< int64_t > end_vids(
119  end_vidsArr,
120  end_vidsArr + size_end_vidsArr);
121  std::vector< int64_t > start_vids(
122  start_vidsArr,
123  start_vidsArr + size_start_vidsArr);
124 
125  graphType gType = directed? DIRECTED: UNDIRECTED;
126 
127  std::deque< Path >paths;
128  if (directed) {
129  log << "Working with directed Graph\n";
131  pgrouting::extract_vertices(edges, total_edges),
132  gType);
133  digraph.insert_edges(edges, total_edges);
134  paths = pgr_astar(digraph, combinations_vector, start_vids, end_vids,
135  heuristic, factor, epsilon, only_cost, normal);
136  } else {
137  log << "Working with Undirected Graph\n";
139  pgrouting::extract_vertices(edges, total_edges),
140  gType);
141  undigraph.insert_edges(edges, total_edges);
142  paths = pgr_astar(undigraph, combinations_vector, start_vids, end_vids,
143  heuristic, factor, epsilon, only_cost, normal);
144  }
145 
146  size_t count(0);
147  count = count_tuples(paths);
148 
149 
150  if (count == 0) {
151  (*return_tuples) = NULL;
152  (*return_count) = 0;
153  notice <<
154  "No paths found\n";
155  *log_msg = pgr_msg(notice.str().c_str());
156  return;
157  }
158 
159  (*return_tuples) = pgr_alloc(count, (*return_tuples));
160  log << "Converting a set of paths into the tuples\n";
161  (*return_count) = (collapse_paths(return_tuples, paths));
162 
163  *log_msg = log.str().empty()?
164  *log_msg :
165  pgr_msg(log.str().c_str());
166  *notice_msg = notice.str().empty()?
167  *notice_msg :
168  pgr_msg(notice.str().c_str());
169  } catch (AssertFailedException &except) {
170  (*return_tuples) = pgr_free(*return_tuples);
171  (*return_count) = 0;
172  err << except.what();
173  *err_msg = pgr_msg(err.str().c_str());
174  *log_msg = pgr_msg(log.str().c_str());
175  } catch (std::exception &except) {
176  (*return_tuples) = pgr_free(*return_tuples);
177  (*return_count) = 0;
178  err << except.what();
179  *err_msg = pgr_msg(err.str().c_str());
180  *log_msg = pgr_msg(log.str().c_str());
181  } catch(...) {
182  (*return_tuples) = pgr_free(*return_tuples);
183  (*return_count) = 0;
184  err << "Caught unknown exception!";
185  *err_msg = pgr_msg(err.str().c_str());
186  *log_msg = pgr_msg(log.str().c_str());
187  }
188 }
189 
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
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
Path::reverse
void reverse()
Definition: basePath_SSEC.cpp:57
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
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
do_pgr_astarManyToMany
void do_pgr_astarManyToMany(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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: astar_driver.cpp:86
pgr_combination_t
Definition: pgr_combination_t.h:43
pgr_astar.hpp
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::algorithms::Pgr_astar
Definition: pgr_astar.hpp:54
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
astar_driver.h
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
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::algorithms::Pgr_astar::astar
Path astar(G &graph, int64_t start_vertex, int64_t end_vertex, int heuristic, double factor, double epsilon, bool only_cost)
one to one astar 1 to 1
Definition: pgr_astar.hpp:69
pgr_astar
std::deque< Path > pgr_astar(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets, int heuristic, double factor, double epsilon, bool only_cost, bool normal)
Definition: astar_driver.cpp:46
Pgr_edge_xy_t
Definition: pgr_edge_xy_t.h:38
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139