C++ 使用捆绑属性作为dijkstra_最短路径中的权重映射
也许这是一个愚蠢的问题,但我正在尝试使用BGL的C++ 使用捆绑属性作为dijkstra_最短路径中的权重映射,c++,boost-graph,C++,Boost Graph,也许这是一个愚蠢的问题,但我正在尝试使用BGL的dijkstra_最短路径,尤其是使用我的Edge bundled属性的字段作为权重映射。我的尝试目前已经导致数十页的编译器错误,所以我希望有人知道如何帮助我。这就是我的代码的本质: struct GraphEdge { float length; // other cruft }; struct GraphVertex { ... }; typedef boost::adjacency_list <boost::ve
dijkstra_最短路径
,尤其是使用我的Edge bundled属性的字段作为权重映射。我的尝试目前已经导致数十页的编译器错误,所以我希望有人知道如何帮助我。这就是我的代码的本质:
struct GraphEdge {
float length;
// other cruft
};
struct GraphVertex {
...
};
typedef boost::adjacency_list
<boost::vecS, boost::vecS, boost::directedS,
GraphVertex, GraphEdge> GraphType;
这样,weightmap将以某种方式将我的图形的特定边与属性中相应的length
字段相关联。我相信有一种简单的方法可以做到这一点,但是BGL的文档对我来说是非常不透明的。如果你能告诉我这个例子在文档中的什么地方被描述,我也会很高兴
提前谢谢你 如果有人关心这一点,使用命名参数版本的调用似乎已经奏效,如下所示:
dijkstra_shortest_paths(m_graph, vertex_from,
weight_map(get(&TrafficGraphEdge::length, m_graph))
.distance_map(make_iterator_property_map(distances.begin(),
get(vertex_index, m_graph))));
这在文档中。不过,我仍然不知道如何使用调用的“非命名参数”版本。好的,我只是在这个问题上浪费了太多时间。以下是子孙后代的解决方案:
/**
* @brief Example concerning bundled properties.
* @author Pierre-Andre Noel
* @date September 10 2012
*/
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
/// The type of the field we are interested in.
typedef int interesting_type;
/// The struct whose elements will be bundled in each vertex.
struct bundled_in_vertex_type
{
/// Something interesting.
interesting_type something;
};
int main()
{
typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, bundled_in_vertex_type > graph_type;
typedef graph_type::vertex_descriptor vertex_descriptor_type;
/// Create a graph of two vertices.
graph_type g(2);
/// Name the two nodes.
const vertex_descriptor_type v1(*boost::vertices(g).first), v2(*(++boost::vertices(g).first));
// Store some stuff in the two nodes, the "easy" way.
g[v1].something = interesting_type(42);
g[v2].something = interesting_type(999);
// Now what you came here for.
/// An handle providing direct access to the field "something".
boost::property_map< graph_type, interesting_type bundled_in_vertex_type::* >::type handle_to_something( boost::get(&bundled_in_vertex_type::something, g) );
// You can now use "handle_to_something" for whatever deed you are interested in.
// Just checking that it works.
std::cout << "Vertex v1's ""something"" field is: " << handle_to_something[v1] << std::endl;
std::cout << "Vertex v2's ""something"" field is: " << handle_to_something[v2] << std::endl;
// Thank you and have a nice day.
return 0;
}
虽然BGL可能很强大,但不幸的是,在我看来,它并不容易使用。要实现这一点需要大量的尝试和错误,但这里有一个使用Boost 1.53.0编译的工作版本[我们想对uu edge_数据中的“rate”变量使用Dijkstra算法]:
struct __edge_data
{
double rate;
double edge_thickness;
size_t colour;
};
struct __vertex_data
{
size_t colour;
size_t shape_code;
string name;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, __vertex_data, __edge_data> DIgraph;
typedef boost::graph_traits<DIgraph>::vertex_descriptor vertexx;
typedef boost::graph_traits<DIgraph>::vertex_iterator vertexx_iter;
typedef boost::graph_traits<DIgraph>::edge_descriptor edgee;
// functor
template<typename T>
struct combine_min : public std::binary_function<T, T, T>
{
T const operator()(const T& a, const T& b) const
{
return b < a ? (b) : (a);
}
};
// functor
template<typename T>
struct compare_less_than : public std::binary_function<T, T, bool>
{
bool const operator()(const T& a, const T& b) const
{
return a < b;
}
};
void graph_analysis()
{
...
std::vector<vertexx> parents(num_vertices(G));
std::vector<double> distances(num_vertices(G));
auto p_map = boost::make_iterator_property_map(&parents[0], boost::get(boost::vertex_index, G));
auto d_map = boost::make_iterator_property_map(&distances[0], boost::get(boost::vertex_index, G));
auto w_map = boost::get(&__edge_data::rate_rate, G); // <=== THIS IS THE TRICK!!!
auto n_map = boost::get(&__vertex_data::name, G);
boost::dijkstra_shortest_paths(G, start_vertex_vector,
boost::weight_map(w_map).
predecessor_map(p_map).
distance_map(d_map).
distance_combine(combine_min<double>()).
distance_compare(compare_less_than<double>()) );
...
}
struct\uuuu edge\u数据
{
双倍费率;
双边缘厚度;
大小和颜色;
};
结构顶点数据
{
大小和颜色;
尺寸、形状和代码;
字符串名;
};
typedef boost::邻接列表有向图;
typedef boost::graph_traits::vertex_描述符vertexx;
typedef boost::graph_traits::vertex_迭代器vertexx_iter;
typedef boost::graph_traits::edge_描述符edgee;
//函子
模板
结构组合_min:public std::binary_函数
{
常量运算符()(常量T&a,常量T&b)常量
{
返回b auto handle_to_something( boost::get(&bundled_in_vertex_type::something, g) );
struct __edge_data
{
double rate;
double edge_thickness;
size_t colour;
};
struct __vertex_data
{
size_t colour;
size_t shape_code;
string name;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, __vertex_data, __edge_data> DIgraph;
typedef boost::graph_traits<DIgraph>::vertex_descriptor vertexx;
typedef boost::graph_traits<DIgraph>::vertex_iterator vertexx_iter;
typedef boost::graph_traits<DIgraph>::edge_descriptor edgee;
// functor
template<typename T>
struct combine_min : public std::binary_function<T, T, T>
{
T const operator()(const T& a, const T& b) const
{
return b < a ? (b) : (a);
}
};
// functor
template<typename T>
struct compare_less_than : public std::binary_function<T, T, bool>
{
bool const operator()(const T& a, const T& b) const
{
return a < b;
}
};
void graph_analysis()
{
...
std::vector<vertexx> parents(num_vertices(G));
std::vector<double> distances(num_vertices(G));
auto p_map = boost::make_iterator_property_map(&parents[0], boost::get(boost::vertex_index, G));
auto d_map = boost::make_iterator_property_map(&distances[0], boost::get(boost::vertex_index, G));
auto w_map = boost::get(&__edge_data::rate_rate, G); // <=== THIS IS THE TRICK!!!
auto n_map = boost::get(&__vertex_data::name, G);
boost::dijkstra_shortest_paths(G, start_vertex_vector,
boost::weight_map(w_map).
predecessor_map(p_map).
distance_map(d_map).
distance_combine(combine_min<double>()).
distance_compare(compare_less_than<double>()) );
...
}