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