PGROUTING  2.5
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
6 Mail: project@pgrouting.org
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"
42 
43 
44 #if 0
45 void
47  int heuristic,
48  double factor,
49  double epsilon) {
50  if (heuristic > 5 || heuristic < 0) {
51  ereport(ERROR,
52  (errmsg("Unknown heuristic"),
53  errhint("Valid values: 0~5")));
54  }
55  if (factor <= 0) {
56  ereport(ERROR,
57  (errmsg("Factor value out of range"),
58  errhint("Valid values: positive non zero")));
59  }
60  if (epsilon < 1) {
61  ereport(ERROR,
62  (errmsg("Epsilon value out of range"),
63  errhint("Valid values: 1 or greater than 1")));
64  }
65 }
66 #endif
67 
68 template < class G >
69 std::deque<Path>
71  G &graph,
72  std::vector<int64_t> sources,
73  std::vector<int64_t> targets,
74  int heuristic,
75  double factor,
76  double epsilon,
77  bool only_cost,
78  bool normal) {
79  std::sort(sources.begin(), sources.end());
80  sources.erase(
81  std::unique(sources.begin(), sources.end()),
82  sources.end());
83 
84  std::sort(targets.begin(), targets.end());
85  targets.erase(
86  std::unique(targets.begin(), targets.end()),
87  targets.end());
88 
90  auto paths = fn_astar.astar(graph, sources, targets,
91  heuristic, factor, epsilon, only_cost);
92 
93  if (!normal) {
94  for (auto &path : paths) {
95  path.reverse();
96  }
97  }
98  return paths;
99 }
100 
101 
102 /************************************************************
103  edges_sql TEXT,
104  vertex_table TEXT,
105  start_vid BIGINT,
106  end_vid BIGINT directed BOOLEAN DEFAULT true,
107  ***********************************************************/
109  Pgr_edge_xy_t *edges, size_t total_edges,
110  int64_t *start_vidsArr, size_t size_start_vidsArr,
111  int64_t *end_vidsArr, size_t size_end_vidsArr,
112  bool directed,
113  int heuristic,
114  double factor,
115  double epsilon,
116  bool only_cost,
117  bool normal,
118  General_path_element_t **return_tuples,
119  size_t *return_count,
120  char** log_msg,
121  char** notice_msg,
122  char** err_msg) {
123  std::ostringstream log;
124  std::ostringstream notice;
125  std::ostringstream err;
126  try {
127  pgassert(!(*log_msg));
128  pgassert(!(*err_msg));
129  pgassert(!(*return_tuples));
130  pgassert(*return_count == 0);
131  pgassert(total_edges != 0);
132 
133 
134  log << "Inserting target vertices into a c++ vector structure\n";
135  std::vector< int64_t > end_vids(
136  end_vidsArr,
137  end_vidsArr + size_end_vidsArr);
138  std::vector< int64_t > start_vids(
139  start_vidsArr,
140  start_vidsArr + size_start_vidsArr);
141 
142  graphType gType = directed? DIRECTED: UNDIRECTED;
143 
144  std::deque< Path >paths;
145  if (directed) {
146  log << "Working with directed Graph\n";
148  pgrouting::extract_vertices(edges, total_edges),
149  gType);
150  digraph.insert_edges(edges, total_edges);
151  paths = pgr_astar(digraph, start_vids, end_vids,
152  heuristic, factor, epsilon, only_cost, normal);
153  } else {
154  log << "Working with Undirected Graph\n";
156  pgrouting::extract_vertices(edges, total_edges),
157  gType);
158  undigraph.insert_edges(edges, total_edges);
159  paths = pgr_astar(undigraph, start_vids, end_vids,
160  heuristic, factor, epsilon, only_cost, normal);
161  }
162 
163  size_t count(0);
164  count = count_tuples(paths);
165 
166 
167  if (count == 0) {
168  (*return_tuples) = NULL;
169  (*return_count) = 0;
170  notice <<
171  "No paths found\n";
172  *log_msg = pgr_msg(notice.str().c_str());
173  return;
174  }
175 
176  (*return_tuples) = pgr_alloc(count, (*return_tuples));
177  log << "Converting a set of paths into the tuples\n";
178  (*return_count) = (collapse_paths(return_tuples, paths));
179 
180  *log_msg = log.str().empty()?
181  *log_msg :
182  pgr_msg(log.str().c_str());
183  *notice_msg = notice.str().empty()?
184  *notice_msg :
185  pgr_msg(notice.str().c_str());
186  } catch (AssertFailedException &except) {
187  (*return_tuples) = pgr_free(*return_tuples);
188  (*return_count) = 0;
189  err << except.what();
190  *err_msg = pgr_msg(err.str().c_str());
191  *log_msg = pgr_msg(log.str().c_str());
192  } catch (std::exception &except) {
193  (*return_tuples) = pgr_free(*return_tuples);
194  (*return_count) = 0;
195  err << except.what();
196  *err_msg = pgr_msg(err.str().c_str());
197  *log_msg = pgr_msg(log.str().c_str());
198  } catch(...) {
199  (*return_tuples) = pgr_free(*return_tuples);
200  (*return_count) = 0;
201  err << "Caught unknown exception!";
202  *err_msg = pgr_msg(err.str().c_str());
203  *log_msg = pgr_msg(log.str().c_str());
204  }
205 }
206 
static edge_t edges[22573]
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:79
void check_parameters(int heuristic, double factor, double epsilon)
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
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:67
void reverse()
std::deque< Path > pgr_astar(G &graph, std::vector< int64_t > sources, std::vector< int64_t > targets, int heuristic, double factor, double epsilon, bool only_cost, bool normal)
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:29
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:68
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
graphType
Definition: graph_enum.h:30
void do_pgr_astarManyToMany(Pgr_edge_xy_t *edges, size_t total_edges, 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)