使用捆绑属性作为Dijkstra最短路径算法中的权重映射

9
也许这是一个愚蠢的问题,但我正在尝试使用BGL的dijkstra_shortest_paths算法,并且特别是想使用我的边捆绑属性的一个字段作为权重图。目前我的尝试导致编译器错误的页数达到了十几页,所以我希望有人能帮助我。以下是我的代码示例:
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?

需要让weightmap将我的图形的特定边与属性中相应的length字段相关联。我相信有一种简单的方法可以做到这一点,但BGL的文档对我来说非常晦涩难懂。如果您能告诉我在文档的哪个部分描述了示例,我也会非常高兴。

提前感谢您!

3个回答

11

如果有人关心的话,使用调用的命名参数版本似乎已经奏效,如下:

    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))));

这在文档中有说明,在这里。尽管如此,我仍然不知道如何使用“无名称参数”版本的调用。


6

好的,我在这个问题上浪费了太多时间。以下是解决方案:

/**
 * @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;
}

说真的,这个库很棒,但是文档明显不足。这应该是一个微不足道的问题。


编辑

如果您正在使用C++11,则可以使用以下替代方案。

    auto handle_to_something( boost::get(&bundled_in_vertex_type::something, g) );

6

虽然BGL非常强大,但在我看来,它并不是很易于使用。要使其正常工作需要进行一些重要的尝试和错误,但是这里提供了一个可行的版本,编译时使用的是Boost 1.53.0 [我们希望在__edge_data上使用Dijkstra算法中的“rate”变量]:

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>()) );

    ...
}

我真诚地希望这能帮到你!我在这里的尝试是展示如何访问算法可用的所有主要“功能”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接