在 boost::grid_graph 中增加边缘描述符的自定义权重

Augment custom weights to edge descriptors in boost::grid_graph

我正在使用 BGL 进行自定义 AStar 搜索。基本上,图形的节点对应于地图的单元格,每个单元格都有一个高程。

我创建了一个单元格遍历得分函数 stepTime,它接受两个单元格的高度,并输出一个成本函数。我想将这个成本函数添加到我的提升图中的边缘权重。

我该怎么做?我见过使用

的函数

auto weightmap = make_transform_value_property_map

创建权重图,但如何根据输出更新权重:

double stepTime(const vertex_descriptor& source, const vertex_descriptor& target, const std::vector<uint8_t>& elevation)

but how do I update the weights according to the output of:

 double stepTime(const vertex_descriptor& source, const vertex_descriptor& target, const std::vector<uint8_t>& elevation)

我不知道你从哪里得到高程矢量,但我想那是你的问题。

源和目标顶点很容易从图形本身获得,所以这里是:

auto custom = boost::make_function_property_map<Graph::edge_descriptor>(
        [&g,&elevation](Graph::edge_descriptor e) {
            return stepTime(boost::source(e, g), boost::target(e, g), elevation);
        });

演示

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/astar_search.hpp>
#include <boost/property_map/function_property_map.hpp>
#include <iostream>

using Graph = boost::adjacency_list<>;

double stepTime(const Graph::vertex_descriptor& source, const Graph::vertex_descriptor& target, const std::vector<uint8_t>& elevation) {
    std::cout << __FUNCTION__ << "(" << source << ", " << target << ", {" << elevation.size() << " elements})\n";
    return 42;
}

int main() {
    Graph g(10);
    add_edge(4, 5, g);
    add_edge(2, 8, g);
    add_edge(5, 1, g);
    add_edge(1, 3, g);

    std::vector<uint8_t> const elevation { 1,2,3,4,5,6 }; // or whatevs

    // custom weight map
    auto custom = boost::make_function_property_map<Graph::edge_descriptor>(
            [&g,&elevation](Graph::edge_descriptor e) {
                return stepTime(boost::source(e, g), boost::target(e, g), elevation);
            });

    // pass it to an algorithm directly, or wrap it in a named-parameter object:
    auto param = boost::weight_map(custom);
    param.weight_map2(custom); // or as the alternative weight map
}