PGROUTING  3.2
pgr_bdAstar.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pgr_bdAstar.hpp
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 
30 #ifndef INCLUDE_BDASTAR_PGR_BDASTAR_HPP_
31 #define INCLUDE_BDASTAR_PGR_BDASTAR_HPP_
32 #pragma once
33 
34 
36 
37 #include <string>
38 #include <queue>
39 #include <utility>
40 #include <vector>
41 #include <limits>
42 #include <functional>
43 
45 
46 namespace pgrouting {
47 namespace bidirectional {
48 
49 template < typename G >
50 class Pgr_bdAstar : public Pgr_bidirectional<G> {
51  typedef typename Pgr_bidirectional<G>::V V;
52  typedef typename Pgr_bidirectional<G>::E E;
54 
59 
65 
71 
72 
74 
75 
76  public:
77  explicit Pgr_bdAstar(G &pgraph) :
78  Pgr_bidirectional<G>(pgraph),
79  m_heuristic(5),
80  m_factor(1.0) {
81  m_log << "pgr_bdAstar constructor\n";
82  }
83 
84  ~Pgr_bdAstar() = default;
85 
86  Path pgr_bdAstar(V start_vertex, V end_vertex,
87  int heuristic,
88  double factor,
89  double epsilon,
90  bool only_cost) {
91  m_log << "pgr_bdAstar\n";
92  v_source = start_vertex;
93  v_target = end_vertex;
95  m_factor = factor * epsilon;
96 
97  return bidirectional(only_cost);
98  }
99 
102 
103  private:
104  void explore_forward(const Cost_Vertex_pair &node) {
105  typename G::EO_i out, out_end;
106 
107  auto current_cost = node.first;
108  auto current_node = node.second;
109 
110  for (boost::tie(out, out_end) = out_edges(current_node, graph.graph);
111  out != out_end; ++out) {
112  auto edge_cost = graph[*out].cost;
113  auto next_node = graph.adjacent(current_node, *out);
114 
115  if (forward_finished[next_node]) continue;
116 
117  if (edge_cost + current_cost < forward_cost[next_node]) {
118  forward_cost[next_node] = edge_cost + current_cost;
119  forward_predecessor[next_node] = current_node;
120  forward_edge[next_node] = graph[*out].id;
121  forward_queue.push({
122  forward_cost[next_node]
123  + heuristic(next_node, v_target),
124  next_node});
125  }
126  }
127  forward_finished[current_node] = true;
128  }
129 
130  void explore_backward(const Cost_Vertex_pair &node) {
131  typename G::EI_i in, in_end;
132 
133  auto current_cost = node.first;
134  auto current_node = node.second;
135 
136  for (boost::tie(in, in_end) = in_edges(current_node, graph.graph);
137  in != in_end; ++in) {
138  auto edge_cost = graph[*in].cost;
139  auto next_node = graph.adjacent(current_node, *in);
140 
141  if (backward_finished[next_node]) continue;
142 
143  if (edge_cost + current_cost < backward_cost[next_node]) {
144  backward_cost[next_node] = edge_cost + current_cost;
145  backward_predecessor[next_node] = current_node;
146  backward_edge[next_node] = graph[*in].id;
147  backward_queue.push({
148  backward_cost[next_node]
149  + heuristic(next_node, v_source),
150  next_node});
151  }
152  }
153  backward_finished[current_node] = true;
154  }
155 
156 
157  double heuristic(V v, V u) {
158  if (m_heuristic == 0) return 0;
159 
160  double dx = graph[v].x() - graph[u].x();
161  double dy = graph[v].y() - graph[u].y();
162  double current;
163 
164  switch (m_heuristic) {
165  case 0:
166  current = 0;
167  break;
168  case 1:
169  current = std::fabs((std::max)(dx, dy)) * m_factor;
170  break;
171  case 2:
172  current = std::fabs((std::min)(dx, dy)) * m_factor;
173  break;
174  case 3:
175  current = (dx * dx + dy * dy) * m_factor * m_factor;
176  break;
177  case 4:
178  current = std::sqrt(dx * dx + dy * dy) * m_factor;
179  break;
180  case 5:
181  current = (std::fabs(dx) + std::fabs(dy)) * m_factor;
182  break;
183  default:
184  current = 0;
185  }
186  return current;
187  }
188 
189  private:
191  double m_factor;
192 };
193 
194 } // namespace bidirectional
195 } // namespace pgrouting
196 
197 #endif // INCLUDE_BDASTAR_PGR_BDASTAR_HPP_
Path
Definition: basePath_SSEC.hpp:47
pgrouting::bidirectional::Pgr_bidirectional::forward_edge
std::vector< int64_t > forward_edge
Definition: pgr_bidirectional.hpp:239
pgrouting::bidirectional::Pgr_bidirectional::backward_predecessor
std::vector< V > backward_predecessor
Definition: pgr_bidirectional.hpp:235
pgrouting::bidirectional::Pgr_bidirectional::forward_queue
Priority_queue forward_queue
Definition: pgr_bidirectional.hpp:230
pgrouting::bidirectional::Pgr_bidirectional::m_log
std::ostringstream m_log
Definition: pgr_bidirectional.hpp:229
pgrouting::bidirectional::Pgr_bdAstar::m_heuristic
int m_heuristic
Definition: pgr_bdAstar.hpp:190
pgrouting::bidirectional::Pgr_bidirectional::v_target
V v_target
target descriptor
Definition: pgr_bidirectional.hpp:221
pgrouting::bidirectional::Pgr_bidirectional::backward_finished
std::vector< bool > backward_finished
Definition: pgr_bidirectional.hpp:233
pgrouting::bidirectional::Pgr_bdAstar::Cost_Vertex_pair
Pgr_bidirectional< G >::Cost_Vertex_pair Cost_Vertex_pair
Definition: pgr_bdAstar.hpp:53
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
pgrouting::bidirectional::Pgr_bidirectional::Cost_Vertex_pair
std::pair< double, V > Cost_Vertex_pair
Definition: pgr_bidirectional.hpp:67
pgrouting::bidirectional::Pgr_bidirectional::v_source
V v_source
source descriptor
Definition: pgr_bidirectional.hpp:220
pgrouting::bidirectional::Pgr_bidirectional
Definition: pgr_bidirectional.hpp:62
pgrouting::bidirectional::Pgr_bidirectional::graph
G & graph
Definition: pgr_bidirectional.hpp:219
pgrouting::bidirectional::Pgr_bidirectional::forward_cost
std::vector< double > forward_cost
Definition: pgr_bidirectional.hpp:241
pgrouting::bidirectional::Pgr_bdAstar::heuristic
double heuristic(V v, V u)
Definition: pgr_bdAstar.hpp:157
pgrouting::bidirectional::Pgr_bidirectional::forward_finished
std::vector< bool > forward_finished
Definition: pgr_bidirectional.hpp:238
pgrouting::bidirectional::Pgr_bdAstar::explore_forward
void explore_forward(const Cost_Vertex_pair &node)
Definition: pgr_bdAstar.hpp:104
pgrouting::bidirectional::Pgr_bidirectional::V
G::V V
Definition: pgr_bidirectional.hpp:64
pgrouting::bidirectional::Pgr_bdAstar::E
Pgr_bidirectional< G >::E E
Definition: pgr_bdAstar.hpp:52
pgrouting::bidirectional::Pgr_bdAstar::~Pgr_bdAstar
~Pgr_bdAstar()=default
pgrouting::bidirectional::Pgr_bidirectional::bidirectional
Path bidirectional(bool only_cost)
Definition: pgr_bidirectional.hpp:122
pgrouting::bidirectional::Pgr_bdAstar::explore_backward
void explore_backward(const Cost_Vertex_pair &node)
Definition: pgr_bdAstar.hpp:130
pgrouting::bidirectional::Pgr_bidirectional::backward_cost
std::vector< double > backward_cost
Definition: pgr_bidirectional.hpp:236
pgrouting::bidirectional::Pgr_bidirectional::backward_queue
Priority_queue backward_queue
Definition: pgr_bidirectional.hpp:231
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
pgrouting::bidirectional::Pgr_bidirectional::E
G::E E
Definition: pgr_bidirectional.hpp:65
pgrouting::bidirectional::Pgr_bdAstar
Definition: pgr_bdAstar.hpp:50
pgr_bidirectional.hpp
pgrouting
Book keeping class for swapping orders between vehicles.
Definition: pgr_alphaShape.cpp:56
pgrouting::bidirectional::Pgr_bdAstar::V
Pgr_bidirectional< G >::V V
Definition: pgr_bdAstar.hpp:51
pgrouting::bidirectional::Pgr_bidirectional::forward_predecessor
std::vector< V > forward_predecessor
Definition: pgr_bidirectional.hpp:240
pgrouting::bidirectional::Pgr_bidirectional::backward_edge
std::vector< int64_t > backward_edge
Definition: pgr_bidirectional.hpp:234
pgrouting::bidirectional::Pgr_bdAstar::Pgr_bdAstar
Pgr_bdAstar(G &pgraph)
Definition: pgr_bdAstar.hpp:77
pgrouting::bidirectional::Pgr_bdAstar::m_factor
double m_factor
Definition: pgr_bdAstar.hpp:191
basePath_SSEC.hpp