1 #include "network/network_graph.hpp"
2 #include "network/heap.hpp"
3 #include "network/network.hpp"
4 #include "util/debug.hpp"
9 #include <unordered_map>
15 NetworkGraph::NetworkGraph(
const Network &network_arg) : network(network_arg) {
17 SPDLOG_INFO(
"Construct graph from network edges start");
22 for (
int i = 0; i < N; ++i) {
23 const Edge &edge = edges[i];
24 boost::tie(e, inserted) = boost::add_edge(edge.
source, edge.
target,
g);
30 SPDLOG_INFO(
"Graph nodes {} edges {}",
num_vertices, boost::num_edges(
g));
31 SPDLOG_INFO(
"Construct graph from network edges end");
35 boost::graph_traits<Graph_T>::edge_iterator it, end;
36 for (boost::tie(it, end) = boost::edges(
g); it != end; ++it)
37 std::cout <<
" index " <<
g[*it].index <<
" edge " <<
56 SPDLOG_TRACE(
"Shortest path starts");
57 if (source == target)
return {};
63 pmap.insert({source, source});
64 dmap.insert({source, 0});
72 if (u == target)
break;
73 for (boost::tie(out_i, out_end) = boost::out_edges(u,
g);
74 out_i != out_end; ++out_i) {
77 temp_dist = node.
value +
g[e].length;
78 auto iter = dmap.find(v);
79 if (iter != dmap.end()) {
81 if (iter->second > temp_dist) {
91 dmap.insert({v, temp_dist});
101 return sqrt((p2.get<0>() - p1.get<0>()) * (p2.get<0>() - p1.get<0>()) +
102 (p2.get<1>() - p1.get<1>()) * (p2.get<1>() - p1.get<1>()));
107 SPDLOG_TRACE(
"Shortest path astar starts");
108 if (source == target)
return {};
109 const std::vector<Point> &vertex_points =
116 vertex_points[target]);
118 pmap.insert({source, source});
119 dmap.insert({source, 0});
121 double temp_dist = 0;
127 if (u == target)
break;
128 for (boost::tie(out_i, out_end) = boost::out_edges(u,
g);
129 out_i != out_end; ++out_i) {
132 temp_dist = dmap.at(u) +
g[e].length;
134 auto iter = dmap.find(v);
135 if (iter != dmap.end()) {
137 if (iter->second > temp_dist) {
145 Q.
push(v, temp_dist + h);
150 Q.
push(v, temp_dist + h);
152 dmap.insert({v, temp_dist});
157 return back_track(source, target, pmap, dmap);
163 SPDLOG_TRACE(
"Backtrack starts");
164 if (dmap.find(target) == dmap.end()) {
167 std::vector<EdgeIndex> path;
170 while (v != source) {
171 double cost = dmap.at(v) - dmap.at(u);
172 SPDLOG_TRACE(
"u {} d {} v {} d {} cost {}",
179 std::reverse(path.begin(), path.end());
190 SPDLOG_TRACE(
"Find edge from {} to {} cost {}", source, target, cost);
194 for (boost::tie(out_i, out_end) = boost::out_edges(source,
g);
195 out_i != out_end; ++out_i) {
197 SPDLOG_TRACE(
" Check Edge from {} to {} cost {}",
198 source, boost::target(e,
g),
g[e].length);
199 if (target == boost::target(e,
g) &&
201 SPDLOG_TRACE(
" Found edge idx {} id {}",
206 SPDLOG_ERROR(
"Edge not found");
216 pmap->insert({s, s});
217 dmap->insert({s, 0});
219 double temp_dist = 0;
225 if (node.
value > delta)
break;
226 for (boost::tie(out_i, out_end) = boost::out_edges(u,
g);
227 out_i != out_end; ++out_i) {
230 temp_dist = node.
value +
g[e].length;
231 auto iter = dmap->find(v);
232 if (iter != dmap->end()) {
234 if (iter->second > temp_dist) {
237 (*dmap)[v] = temp_dist;
242 if (temp_dist <= delta) {
243 Q.
push(v, temp_dist);
244 pmap->insert({v, u});
245 dmap->insert({v, temp_dist});
Graph_T g
The member storing a boost graph.
Heap data structure used in the routing query
boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProperty > Graph_T
Boost graph type.
Graph_T::edge_descriptor EdgeDescriptor
Boost graph edge type.
void print_graph() const
Print graph information.
void decrease_key(NodeIndex index, double value)
Decrease a node in the heap.
const std::vector< Edge > & get_edges() const
Get edges in the network.
HeapNode top()
Get a copy of the top node in the heap.
void push(NodeIndex index, double value)
Push a node into the heap.
Node in the heap structure.
const Network & network
Road network.
NodeIndex index
Index of a node in the heap.
const std::vector< FMM::CORE::Point > & get_vertex_points() const
Get all node geometry.
std::vector< EdgeIndex > shortest_path_astar(NodeIndex source, NodeIndex target) const
AStar Shortest path query from source to target.
EdgeIndex index
Index of an edge, which is continuous [0,N-1].
std::unordered_map< NodeIndex, double > DistanceMap
Distance map.
void single_source_upperbound_dijkstra(NodeIndex source, double delta, PredecessorMap *pmap, DistanceMap *dmap) const
Single source shortest path query with an uppper bound.
int get_edge_id(EdgeIndex idx) const
Get the edge ID from edge index.
NodeID get_node_id(NodeIndex index) const
Get node ID from index.
const Network & get_network()
Get inner network reference.
const Graph_T & get_boost_graph() const
Get inner boost graph.
EdgeID get_edge_id(EdgeIndex index) const
Get edge ID from index.
static constexpr double DOUBLE_MIN
A value used in checking edge from source,target and cost.
unsigned int get_num_vertices() const
Get number of vertices in the graph.
void pop()
Pop a node from the heap.
bool empty()
Check if the heap is empty.
unsigned int num_vertices
number of vertices
boost::graph_traits< Graph_T >::out_edge_iterator OutEdgeIterator
Boost graph out edge iterator.
Classes related with network and graph.
int get_edge_index(NodeIndex source, NodeIndex target, double cost) const
Find the edge ID given a pair of nodes and its cost, if not found, return -1.
bool contain_node(NodeIndex index)
Check if the heap contains a node.
int get_node_id(NodeIndex idx) const
Get node ID from a node index.
std::vector< EdgeIndex > shortest_path_dijkstra(NodeIndex source, NodeIndex target) const
Dijkstra Shortest path query from source to target.
NodeIndex source
source node index
unsigned int NodeIndex
Node Index in the network, range from [0,num_vertices-1 ].
double calc_heuristic_dist(const CORE::Point &p1, const CORE::Point &p2) const
Calculate heuristic distance from p1 to p2,which is used in Astar routing.
double length
length of the edge polyline
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point
Point class.
std::unordered_map< NodeIndex, NodeIndex > PredecessorMap
Predecessor Map.
double value
Value of a node in the heap.
NodeIndex target
target node index
std::vector< EdgeIndex > back_track(NodeIndex source, NodeIndex target, const PredecessorMap &pmap, const DistanceMap &dmap) const
Backtrack the routing result to find a path from source to target.