PGROUTING  3.2
basePath_SSEC.hpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 File: basePath_SSEC.hpp
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
27 #ifndef INCLUDE_CPP_COMMON_BASEPATH_SSEC_HPP_
28 #define INCLUDE_CPP_COMMON_BASEPATH_SSEC_HPP_
29 #pragma once
30 
31 
32 #include <boost/config.hpp>
33 #include <boost/graph/adjacency_list.hpp>
34 
35 
36 #include <deque>
37 #include <vector>
38 #include <iostream>
39 #include <algorithm>
40 
42 #include "cpp_common/path_t.h"
44 #include "cpp_common/rule.h"
45 
46 
47 class Path {
48  typedef std::deque< Path_t >::iterator pthIt;
49  typedef std::deque< Path_t >::const_iterator ConstpthIt;
50 
51  private:
52  std::deque< Path_t > path;
53  int64_t m_start_id;
54  int64_t m_end_id;
55  double m_tot_cost;
56 
57  public:
59  {}
60  Path(int64_t s_id, int64_t e_id)
61  : m_start_id(s_id), m_end_id(e_id), m_tot_cost(0)
62  {}
63  Path(const Path&) = default;
64 
65  int64_t start_id() const {return m_start_id;}
66  void start_id(int64_t value) {m_start_id = value;}
67  int64_t end_id() const {return m_end_id;}
68  void end_id(int64_t value) {m_end_id = value;}
69  double tot_cost() const {return m_tot_cost;}
70 
71  size_t size() const {return path.size();}
72  bool empty() const {return path.empty();}
73  size_t countInfinityCost() const;
74 
75  void push_front(Path_t data);
76  void push_back(Path_t data);
77  const Path_t& operator[](size_t i) const {return path[i];}
78  Path_t& operator[](size_t i) {return path[i];}
79  Path& renumber_vertices(int64_t value);
80 
81  pthIt begin() {return path.begin();}
82  pthIt end() {return path.end();}
83  ConstpthIt begin() const {return path.begin();}
84  ConstpthIt end() const {return path.end();}
85 
86 
87  void erase(pthIt pos) {path.erase(pos);}
88  const Path_t& back() const {return path.back();}
89  Path_t& back() {return path.back();}
90  const Path_t& front() const {return path.front();}
91  Path_t& front() {return path.front();}
92  void sort_by_node_agg_cost();
93 
94  void recalculate_agg_cost();
95 
102  bool has_restriction(const pgrouting::trsp::Rule &rule) const;
104 
106  int64_t d_from,
107  int64_t d_to,
108  int64_t d_vertex,
109  int64_t d_edge,
110  double d_cost,
111  double d_tot_cost);
112 
113  void push_front(
114  int64_t d_vertex,
115  int64_t d_edge,
116  double d_cost,
117  double d_tot_cost);
118  void clear();
119 
120  friend std::ostream& operator<<(std::ostream &log, const Path &p);
121 
122 
123  void reverse();
124 
125  Path getSubpath(unsigned int j) const;
126 
127 
128  bool isEqual(const Path &subpath) const;
129  void appendPath(const Path &o_path);
130  void append(const Path &other);
131  void empty_path(unsigned int d_vertex);
132 
133  void get_pg_dd_path(
134  General_path_element_t **ret_path,
135  size_t &sequence) const;
136 
137  void get_pg_ksp_path(
138  General_path_element_t **ret_path,
139  size_t &sequence, int routeId) const;
140 
142  General_path_element_t **ret_path,
143  size_t &sequence, int routeId) const;
144 
146  General_path_element_t **postgres_data,
147  size_t &sequence) const;
148 
149  friend size_t collapse_paths(
150  General_path_element_t **ret_path,
151  const std::deque< Path > &paths);
152 
153 
155  friend void equi_cost(std::deque< Path > &paths);
157  friend size_t count_tuples(const std::deque< Path > &paths);
158 
159  /*
160  * TEMPLATES
161  */
162  template <typename G , typename V> Path(
163  G &graph,
164  int64_t source,
165  double distance,
166  const std::vector<V> &predecessors,
167  const std::vector<double> &distances) :
168  m_start_id(source),
169  m_end_id(source) {
170  for (V i = 0; i < distances.size(); ++i) {
171  if (distances[i] <= distance) {
172  auto cost = distances[i] - distances[predecessors[i]];
173  auto edge_id = graph.get_edge_id(predecessors[i], i, cost);
174  push_back(
175  {graph[i].id,
176  edge_id, cost,
177  distances[i]});
178  }
179  }
180  }
181 
182 
183  template <typename G> Path(
184  const G &graph,
185  const Path &original,
186  bool only_cost) :
187  m_start_id(original.m_start_id),
188  m_end_id(original.m_end_id),
189  m_tot_cost(0) {
190  if (original.path.empty()) return;
191 
192  typename G::EO_i ei, ei_end;
193 
194 // auto last_node = m_start_id;
195  for (const auto &p : original.path) {
196  boost::tie(ei, ei_end) = out_edges(
197  graph.get_V(p.node),
198  graph.graph);
199 
200  if (p.edge == -1) {
201  path.push_back({m_end_id, -1, 0, 0});
202  } else {
203  for ( ; ei != ei_end; ++ei) {
204  if (graph[*ei].id == p.edge) {
205  auto cost = graph[*ei].cost;
206  push_back({p.node, p.edge, cost, 0});
207  }
208  }
209  }
210 // last_node = p.node;
211  }
213 
214  if (only_cost) {
215  path.clear();
216  path.push_back(
217  {m_end_id,
218  -1,
219  m_tot_cost,
220  m_tot_cost});
221  }
222  }
223 
224  template <typename G , typename V> Path(
225  G &graph,
226  V v_source,
227  double distance,
228  const std::vector<V> &predecessors,
229  const std::vector<double> &distances) :
230  m_start_id(graph.graph[v_source].id),
231  m_end_id(graph.graph[v_source].id) {
232  for (V i = 0; i < distances.size(); ++i) {
233  if (distances[i] <= distance) {
234  auto cost = distances[i] - distances[predecessors[i]];
235  auto edge_id = graph.get_edge_id(predecessors[i], i, cost);
236  push_back(
237  {graph[i].id,
238  edge_id, cost,
239  distances[i]});
240  }
241  }
242  }
243 
244 
245 
246  template <typename G , typename V> Path(
247  const G &graph,
248  const V v_source,
249  const V v_target,
250  const std::vector<V> &predecessors,
251  const std::vector<double> &distances,
252  bool only_cost,
253  bool normal = true) :
254  m_start_id(graph.graph[v_source].id),
255  m_end_id(graph.graph[v_target].id) {
256  if (!only_cost) {
257  complete_path(graph,
258  v_source,
259  v_target,
260  predecessors,
261  distances,
262  normal);
263  return;
264  }
265  /*
266  * only_cost
267  */
268  if (v_target != predecessors[v_target]) {
269  push_front(
270  {graph.graph[v_target].id,
271  -1,
272  distances[v_target],
273  distances[v_target]});
274  }
275  return;
276  }
277 
282  template <typename G , typename V> void complete_path(
283  const G &graph,
284  const V v_source,
285  const V v_target,
286  const std::vector<V> &predecessors,
287  const std::vector<double> &distances,
288  bool normal) {
289  // no path was found
290  if (v_target == predecessors[v_target]) {
291  return;
292  }
293 
294  /*
295  * set the target
296  */
297  auto target = v_target;
298 
299  /*
300  * the last stop is the target
301  */
302  push_front(
303  {graph.graph[target].id, -1,
304  0, distances[target]});
305 
306  /*
307  * get the path
308  */
309  while (target != v_source) {
310  /*
311  * done when the predecesor of the target is the target
312  */
313  if (target == predecessors[target]) break;
314 
315  /*
316  * Inserting values in the path
317  */
318  auto cost = distances[target] - distances[predecessors[target]];
319  auto vertex_id = graph.graph[predecessors[target]].id;
320  auto edge_id = normal?
321  graph.get_edge_id(predecessors[target], target, cost)
322  : graph.get_edge_id(target, predecessors[target], cost);
323 
324  push_front({
325  vertex_id,
326  edge_id,
327  cost,
328  distances[target] - cost});
329  target = predecessors[target];
330  }
331 
332  return;
333  }
334 };
335 
336 
337 #endif // INCLUDE_CPP_COMMON_BASEPATH_SSEC_HPP_
Path::end_id
int64_t end_id() const
Definition: basePath_SSEC.hpp:67
Path::operator[]
Path_t & operator[](size_t i)
Definition: basePath_SSEC.hpp:78
Path
Definition: basePath_SSEC.hpp:47
Path::path
std::deque< Path_t > path
Definition: basePath_SSEC.hpp:52
Path::start_id
int64_t start_id() const
Definition: basePath_SSEC.hpp:65
Path::inf_cost_on_restriction
Path inf_cost_on_restriction(const pgrouting::trsp::Rule &rule)
Definition: basePath_SSEC.cpp:105
Path::push_back
void push_back(Path_t data)
Definition: basePath_SSEC.cpp:52
pgr_base_graph.hpp
Path_t
Definition: path_t.h:36
Path::begin
ConstpthIt begin() const
Definition: basePath_SSEC.hpp:83
Path::end
pthIt end()
Definition: basePath_SSEC.hpp:82
Path::set_data
Path_t set_data(int64_t d_from, int64_t d_to, int64_t d_vertex, int64_t d_edge, double d_cost, double d_tot_cost)
Path::empty_path
void empty_path(unsigned int d_vertex)
Path::countInfinityCost
size_t countInfinityCost() const
Definition: basePath_SSEC.cpp:133
Path::erase
void erase(pthIt pos)
Definition: basePath_SSEC.hpp:87
Path::operator[]
const Path_t & operator[](size_t i) const
Definition: basePath_SSEC.hpp:77
Path::reverse
void reverse()
Definition: basePath_SSEC.cpp:57
Path::empty
bool empty() const
Definition: basePath_SSEC.hpp:72
Path::getSubpath
Path getSubpath(unsigned int j) const
Definition: basePath_SSEC.cpp:141
pgrouting::trsp::Rule
Definition: rule.h:39
Path::push_front
void push_front(Path_t data)
Definition: basePath_SSEC.cpp:47
Path::pthIt
std::deque< Path_t >::iterator pthIt
Definition: basePath_SSEC.hpp:48
Path::appendPath
void appendPath(const Path &o_path)
Definition: basePath_SSEC.cpp:164
Path::Path
Path(const G &graph, const V v_source, const V v_target, const std::vector< V > &predecessors, const std::vector< double > &distances, bool only_cost, bool normal=true)
Definition: basePath_SSEC.hpp:246
Path::start_id
void start_id(int64_t value)
Definition: basePath_SSEC.hpp:66
Path::back
const Path_t & back() const
Definition: basePath_SSEC.hpp:88
Path::end_id
void end_id(int64_t value)
Definition: basePath_SSEC.hpp:68
Path::Path
Path(const G &graph, const Path &original, bool only_cost)
Definition: basePath_SSEC.hpp:183
Path::Path
Path()
Definition: basePath_SSEC.hpp:58
Path::has_restriction
bool has_restriction(const pgrouting::trsp::Rule &rule) const
Definition: basePath_SSEC.cpp:100
Path::m_end_id
int64_t m_end_id
Definition: basePath_SSEC.hpp:54
Path::sort_by_node_agg_cost
void sort_by_node_agg_cost()
Sorts a path by node, aggcost ascending.
Definition: basePath_SSEC.cpp:295
Path::Path
Path(G &graph, V v_source, double distance, const std::vector< V > &predecessors, const std::vector< double > &distances)
Definition: basePath_SSEC.hpp:224
rule.h
Path::get_pg_dd_path
void get_pg_dd_path(General_path_element_t **ret_path, size_t &sequence) const
Definition: basePath_SSEC.cpp:239
Path::get_pg_ksp_path
void get_pg_ksp_path(General_path_element_t **ret_path, size_t &sequence, int routeId) const
Definition: basePath_SSEC.cpp:255
Path::ConstpthIt
std::deque< Path_t >::const_iterator ConstpthIt
Definition: basePath_SSEC.hpp:49
Path::front
Path_t & front()
Definition: basePath_SSEC.hpp:91
General_path_element_t
Definition: general_path_element_t.h:37
Path::Path
Path(int64_t s_id, int64_t e_id)
Definition: basePath_SSEC.hpp:60
Path::complete_path
void complete_path(const G &graph, const V v_source, const V v_target, const std::vector< V > &predecessors, const std::vector< double > &distances, bool normal)
constructs a path based on results
Definition: basePath_SSEC.hpp:282
Path::size
size_t size() const
Definition: basePath_SSEC.hpp:71
Path::front
const Path_t & front() const
Definition: basePath_SSEC.hpp:90
Path::generate_postgres_data
void generate_postgres_data(General_path_element_t **postgres_data, size_t &sequence) const
Definition: basePath_SSEC.cpp:221
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
Path::m_tot_cost
double m_tot_cost
Definition: basePath_SSEC.hpp:55
Path::back
Path_t & back()
Definition: basePath_SSEC.hpp:89
Path::m_start_id
int64_t m_start_id
Definition: basePath_SSEC.hpp:53
Path::end
ConstpthIt end() const
Definition: basePath_SSEC.hpp:84
Path::collapse_paths
friend size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
Path::count_tuples
friend size_t count_tuples(const std::deque< Path > &paths)
counts the tuples to be returned
Definition: basePath_SSEC.cpp:387
Path::equi_cost
friend void equi_cost(std::deque< Path > &paths)
discards common vertices with greater agg_cost
Definition: basePath_SSEC.cpp:336
general_path_element_t.h
Path::operator<<
friend std::ostream & operator<<(std::ostream &log, const Path &p)
Definition: basePath_SSEC.cpp:118
Path::get_pg_turn_restricted_path
void get_pg_turn_restricted_path(General_path_element_t **ret_path, size_t &sequence, int routeId) const
Definition: basePath_SSEC.cpp:273
pgrouting::alphashape::V
boost::graph_traits< BG >::vertex_descriptor V
Definition: pgr_alphaShape.h:58
Path::append
void append(const Path &other)
Path: 2 -> 9 seq node edge cost agg_cost 0 2 4 1 0 1 5 8 1 1 2 6 9 1 2 3 9 -1 0 3 Path: 9 -> 3 seq no...
Definition: basePath_SSEC.cpp:192
path_t.h
Path::Path
Path(G &graph, int64_t source, double distance, const std::vector< V > &predecessors, const std::vector< double > &distances)
Definition: basePath_SSEC.hpp:162
Path::renumber_vertices
Path & renumber_vertices(int64_t value)
Definition: basePath_SSEC.cpp:38
Path::begin
pthIt begin()
Definition: basePath_SSEC.hpp:81
Path::isEqual
bool isEqual(const Path &subpath) const
Definition: basePath_SSEC.cpp:153
Path::recalculate_agg_cost
void recalculate_agg_cost()
Definition: basePath_SSEC.cpp:77
Path::clear
void clear()
Definition: basePath_SSEC.cpp:87
Path::tot_cost
double tot_cost() const
Definition: basePath_SSEC.hpp:69
Path::find_restriction
ConstpthIt find_restriction(const pgrouting::trsp::Rule &rule) const
get the iterator of the path where the (restriction) rule starts
Definition: basePath_SSEC.cpp:94