gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:25 24 4
gpt4 key购买 nike

我正在使用 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 中增加边缘描述符的自定义权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45966930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com