我不熟悉boost库,正在尝试学习。 我已经使用boost图形库调用dijstra的最短路径函数,在地图上查找到达目的地的路径。顶点是交叉口,边缘是街道段。
我通过最小时间找到最短路径。为此,我定义了边缘权重作为时间,时间= st段长度*其速度/限制。这确实给了我最短路径(按时间计算)但是我还要考虑转弯并将每个转弯的总时间增加15秒钟。如何检测转弯是指如果第二条街道段(边)的st名称与第一条不相等,则为转弯。
基本上,我想动态分配权重(不仅像我在这里所做的那样在开始时设置)。当程序在搜索过程中访问边缘时,我希望它在这个阶段检查父项(或前任者)。如何传递一个函数或其他参数来执行此操作?
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;