我对boost库不熟悉,正在努力学习。我已经使用 boost 图形库调用 dijstra 的最短路径函数来在 map 中找到到目的地的路径。顶点是交叉点,边是街道段。
我正在用最短时间找到最短路径。为此,我将边缘权重定义为时间,时间 = st 段长度 * 其速度/限制。这确实给了我最短的路径(按时间)。 但是,我还要计算一个转弯,并为每个转弯的总时间增加 15 秒。给定两个街道段(边缘),我如何检测转弯,如果第二个的 st 名称不等于第一个的 st 名称,则转弯。
基本上,我想动态分配权重(而不是像我在这里所做的那样在开始时设置它们)。当程序在搜索过程中访问边缘时,我希望它在这个阶段检查 parent (或这里的前辈)。我如何在参数中传递一个函数或一些可以做到这一点的东西?
vector<unsigned> OurGraph::find_awesome_path(unsigned start, unsigned finish)
{
// start and finish are intersection IDs,
// Get the corresponding Vertices in the graph.
Vertex start_node = vertex_map[start];
Vertex dest_node = vertex_map[finish];
std::vector<Vertex> predecessors(boost::num_vertices(my_graph)); // To store parents
std::vector<float> distances(boost::num_vertices(my_graph)); // To store dijkstra distances
IndexMap indexMap = boost::get(boost::vertex_index, my_graph);
PredecessorMap predecessorMap(&predecessors[0], indexMap);
DistanceMap distanceMap(&distances[0], indexMap);
boost::dijkstra_shortest_paths(my_graph, start_node, boost::distance_map(distanceMap).predecessor_map(predecessorMap));
vector<Edge> path;
path = get_edge_path(dest_node, predecessorMap); // Extracts edges from edge descriptors in predecessor map
// and piles them in a vector of Edge.
return segment_list_from_edges(path); // Convert edges to street segment IDs and return.
}
my_graph 是类型 Graph 而 Graph 、Vertex、Edge、IndexMap、PredecessorMap 和 DistanceMap 是类型定义如下:
typedef boost::property<boost::edge_weight_t, float> WeightProperty;
typedef boost::property<boost::vertex_name_t, unsigned> IntersectionProperty;
typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS,
IntersectionProperty, WeightProperty > Graph;
typedef boost::graph_traits < Graph >::vertex_descriptor Vertex;
typedef boost::graph_traits < Graph >::edge_descriptor Edge;
typedef boost::property_map < Graph, boost::vertex_index_t >::type IndexMap;
typedef boost::iterator_property_map < Vertex*, IndexMap, Vertex, Vertex& > PredecessorMap;
typedef boost::iterator_property_map < float*, IndexMap, float, float& > DistanceMap;
最佳答案
执行此操作的一种方法是使用微分图。在那种情况下,微分图中的每个顶点都相当于当前图中的一条边。例如,一个顶点会封装一个转弯。然后,您可以将转弯权重添加到涉及街道名称更改的边缘(或您用来确定转弯的任何机制。)
关于c++ - Boost BGL Dijkstra 最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085139/