c++ - 在 boost::grid_graph 中将自定义权重 boost 到边缘描述符

标签 c++ boost a-star boost-graph

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

我创建了一个单元格遍历得分函数stepTime,它接收两个单元格的海拔,并输出一个成本函数。我想将此成本函数添加到我的 boost 图中的边权重中。

我该如何解决这个问题?我见过使用

的函数

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)

我不知道你从哪里获得高程 vector ,但我想这就是你的问题。

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

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
}

关于c++ - 在 boost::grid_graph 中将自定义权重 boost 到边缘描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45966930/

相关文章:

C++ - 在 std::thread 中调用重载函数时编译失败

c++ - 一般来说,boost bind 在幕后是如何工作的?

特定方格子图中的长路径算法

c++ - 为什么来自 boost::smart_ptr 的 &this_type::px 返回 1?

c++ - Python 到 C++ : From Deriv, 到 Base,再到 Deriv

android - 大 map 实现A星(A*)路径算法,性能低

python - 为什么 A star 比 Dijkstra 更快,即使启发式在网络中设置为 Nonex

c++ - Linux 和 C++ 编程错误 :\194 and error:\168

C++:声明、定义和调用返回模板类对象的函数?

c++ - 将 size_t 转换为整数 (c++)