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