使用捆绑属性作为 dijkstra_shortest_paths 中的权重图
也许这是一个愚蠢的问题,但我正在尝试使用 BGL 的 dijkstra_shortest_paths
,特别是使用我的 Edge 捆绑属性的字段作为权重图。我的尝试目前导致了数十页的编译器错误,所以我希望有人知道如何帮助我。这基本上就是我的代码的样子:
struct GraphEdge {
float length;
// other cruft
};
struct GraphVertex {
...
};
typedef boost::adjacency_list
<boost::vecS, boost::vecS, boost::directedS,
GraphVertex, GraphEdge> GraphType;
我可以毫无问题地填充图表,但是当调用 dijkstra_shortest_paths
时,我遇到了麻烦。我想使用 length
字段。具体来说,我想知道需要多少 boost voodoo 才能适合这样的调用:
GraphType m_graph;
vector<int> predecessor(num_vertices(m_graph));
vector<float> distances(num_vertices(m_graph), 0.0f);
vector<int> vertex_index_map(num_vertices(m_graph));
for (size_t i=0; i<vertex_index_map.size(); ++i) {
vertex_index_map[i] = i;
}
dijkstra_shortest_paths(m_graph, vertex_from, predecessor, distances,
weightmap, vertex_index_map,
std::less<float>(), closed_plus<float>(),
(std::numeric_limits<float>::max)(), 0.0f,
default_dijkstra_visitor());
// How do I write the right version of weightmap here?
这样权重图将以某种方式将我的图形的特定边缘与相应的 length
字段相关联财产。我确信有一种简单的方法可以做到这一点,但 BGL 的文档对我来说非常不透明。如果您能告诉我文档中描述该示例的位置,我也会非常高兴。
先感谢您!
Perhaps this is a stupid question, but I'm trying to use BGL's dijkstra_shortest_paths
, and, in particular, to use a field of my Edge bundled property as the weightmap. My attempts have currently led to tens of pages of compiler errors, so I'm hoping that someone knows how to help me. This is essentially what my code looks like:
struct GraphEdge {
float length;
// other cruft
};
struct GraphVertex {
...
};
typedef boost::adjacency_list
<boost::vecS, boost::vecS, boost::directedS,
GraphVertex, GraphEdge> GraphType;
I can populate the graph without any problems, however when it comes to calling dijkstra_shortest_paths
, I get in trouble. I'd like to use the length
field. Specifically, I'd like to know what's the bit of needed boost voodoo to go fit in a call like this:
GraphType m_graph;
vector<int> predecessor(num_vertices(m_graph));
vector<float> distances(num_vertices(m_graph), 0.0f);
vector<int> vertex_index_map(num_vertices(m_graph));
for (size_t i=0; i<vertex_index_map.size(); ++i) {
vertex_index_map[i] = i;
}
dijkstra_shortest_paths(m_graph, vertex_from, predecessor, distances,
weightmap, vertex_index_map,
std::less<float>(), closed_plus<float>(),
(std::numeric_limits<float>::max)(), 0.0f,
default_dijkstra_visitor());
// How do I write the right version of weightmap here?
such that weightmap will somehow associate a particular edge of my graph with the corresponding length
field in the property. I'm sure there's an easy way to do this, but the documentation for BGL is incredibly opaque to me. If you can tell me where in the documentation the example is described, I'd be very happy as well.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果有人关心这一点,使用调用的命名参数版本似乎有效,如下所示:
这在文档中, 此处。不过,我仍然不知道如何使用“非命名参数”版本的调用。
In case anyone cares about this, using the named parameter version of the call seems to have worked, as follows:
This is in the documentation, here. I still don't know how to use the "non-named parameter" version of the call, though.
好吧,我只是在这个问题上浪费了太多时间。这是后代的解决方案:
说真的,这个库很棒,但是 。这应该是一件小事。
编辑
如果您使用的是 C++11,那么您可能更喜欢以下替代方案。
Ok, I just wasted too much time on this problem. Here is the solution for the posterity:
Seriously, this library is great, but the documentation is definitively lacking. This should be a trivial matter.
EDIT
If you are using C++11, then you may prefer the following alternative.
不幸的是,尽管 BGL 功能强大,但在我看来,它并不是很容易使用。让它工作需要一些相当多的试验和错误,但这里是一个用 Boost 1.53.0 编译的工作版本[我们想在 __edge_data 中的“rate”变量上使用 Dijkstra 算法]:
我真诚地希望这会有所帮助!我在这里的尝试是展示如何访问算法可用的所有主要“功能”。
As powerful as the BGL may be, unfortunately, it is not very easy to use in my honest opinion. Getting this to work took some considerable trial and error, but here is a working version compiled with Boost 1.53.0 [we want to use Dijkstra's algorithm on the 'rate' variable in __edge_data]:
I sincerely hope this helps! My attempt here was to show how to access all the main 'features' available to the algorithm.