【问题标题】:How to adapt a boost::property_map in order to use it like a ublas::vector?如何调整 boost::property_map 以便像 ublas::vector 一样使用它?
【发布时间】:2013-03-22 16:10:29
【问题描述】:

我正在寻找一种将 Boost Graph LibraryBoost uBLAS 结合使用的智能方法。更准确地说,我需要通过使用图 邻接矩阵 和包含其他一些 的向量之间的 标量积 的结果来更新每个顶点的给定顶点属性每个顶点的顶点属性。让我给你一个(不幸的是冗长的)最小的例子来说明这个问题:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost;
namespace ublas = boost::numeric::ublas;


struct Vertex {             //Using bundled vertex properties

        double old_potential; 
        double new_potential; 
};


typedef adjacency_list< listS, vecS, directedS, Vertex > Graph;

int main(){ 

  //[Prepare a graph with two vertex property maps and initialize
  Graph graph; 
  add_edge (0, 1, graph); 
  add_edge (0, 3, graph); 
  add_edge (1, 2, graph); 

  auto  v_old_potential = boost::get( &Vertex::old_potential, graph );
  auto  v_new_potential = boost::get( &Vertex::new_potential, graph );

  unsigned int source_strength = 0;

  BGL_FORALL_VERTICES( v, graph, Graph )    {

    v_old_potential[v] = source_strength++;
    v_new_potential[v] = 0;
  }
  //]  


  //[ Extracting the adjacency matrix by iteration through all edges --> uBLAS matrix
  ublas::zero_matrix<int> zero_matrix( num_vertices(graph) , num_vertices(graph) ); 
  ublas::matrix<int> adjacency_matrix( zero_matrix );       //initialize properly

  BGL_FORALL_EDGES( e, graph, Graph )   {

     adjacency_matrix( source(e,graph), target(e,graph) ) = 1;
     adjacency_matrix( target(e,graph), source(e,graph) ) = 1;
  }
  //]


  //[ Extracting the old potentials by iteration through all vertices --> uBLAS vector
  ublas::zero_vector<double> zero_vector( num_vertices(graph) );
  ublas::vector<double> old_potential_vector( zero_vector );    //initialize properly
  ublas::vector<double> new_potential_vector( zero_vector );    //initialize properly

  BGL_FORALL_VERTICES(v, graph, Graph)    {

        old_potential_vector( vertex(v,graph) ) = v_old_potential[v];
  }
  //]


  //[ Compute new potentials = A . old potentials ! 
  new_potential_vector = ublas::prod ( adjacency_matrix, old_potential_vector );        //     new = A.old
  //]


  //[ Updating the property map for the new potentials with the newly computed values from above
  BGL_FORALL_VERTICES(v, graph, Graph)    {

        v_new_potential[v] = old_potential_vector( vertex(v,graph) );
  }
  //]

  std::cout << adjacency_matrix << std::endl;       //output = [4,4]((0,1,0,1),(1,0,1,0),(0,1,0,0),(1,0,0,0))
  std::cout << old_potential_vector << std::endl;   //output = [4](0,1,2,3)
  std::cout << new_potential_vector << std::endl;   //output = [4](4,2,1,0)

}

现在,虽然我的建议是一个可能的解决方案,但我对此并不十分满意。问题是,(a) 我将整个 old_potential 属性映射复制到关联的 ublas::vector 以计算标量积。 (b) 我还需要遍历new_potential 属性映射,以将新计算的值返回到图表中。 我怀疑这些操作会在我的应用程序中重复很多,这就是为什么我希望从一开始就尽可能干净地完成这部分。

理想情况下,我希望完成所有这些来回复制,而是使用某种适配器boost::property_map在@的调用中作为ublas::vector工作987654327@。使用这样的东西会很棒:

adapter(new_potential) = ublas::prod( adjacency_matrix, adapter(old_potential) );

如果有人知道如何实现此类功能或如何改进我的解决方案,我将不胜感激。

【问题讨论】:

    标签: c++ boost boost-graph boost-ublas


    【解决方案1】:
    #include <iostream>
    #include <memory>
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/iteration_macros.hpp>
    #include <boost/numeric/ublas/matrix.hpp>
    #include <boost/numeric/ublas/io.hpp>
    
    using namespace boost;
    namespace ublas = boost::numeric::ublas;
    
    enum vertex_old_potential_t { vertex_old_potential };
    enum vertex_new_potential_t { vertex_new_potential };
    
    namespace boost
    {
      BOOST_INSTALL_PROPERTY(vertex, new_potential);
      BOOST_INSTALL_PROPERTY(vertex, old_potential);
    }
    
    typedef property<vertex_new_potential_t, double, property<vertex_old_potential_t,double> > Vertex;
    
    
    typedef adjacency_list< listS, vecS, directedS, Vertex > Graph;
    
    struct ublas_vector_map;
    
    namespace boost {
      template<>
      struct property_map< Graph, vertex_new_potential_t > {
        typedef ublas_vector_map type;
        typedef ublas_vector_map const_type;
      };
    
      template<>
      struct property_map< Graph, vertex_old_potential_t > {
        typedef ublas_vector_map type;
        typedef ublas_vector_map const_type;
      };
    }
    
    struct ublas_vector_map : put_get_helper<double&,ublas_vector_map> {
      typedef double value_type;
      typedef value_type& reference;
      typedef typename graph_traits<Graph>::vertex_descriptor key_type;
      typedef boost::lvalue_property_map_tag category;
    
      ublas_vector_map(Graph* g, vertex_old_potential_t&):vec(new ublas::vector<double>(num_vertices(*g),0.0)){}
      ublas_vector_map(Graph* g, vertex_new_potential_t&):vec(new ublas::vector<double>(num_vertices(*g),0.0)){}
    
    
      reference operator[](key_type v) const {
        return (*vec)(v);
      }
    
      ublas::vector<double>& vector() { return *vec; }
    
      private:
       std::unique_ptr<ublas::vector<double> > vec;
    };
    
    int main(){ 
    
      //[Prepare a graph with two vertex property maps and initialize
      Graph graph; 
      add_edge (0, 1, graph); 
      add_edge (0, 3, graph); 
      add_edge (1, 2, graph); 
    
      auto  v_old_potential = boost::get( vertex_old_potential, graph );
      auto  v_new_potential = boost::get( vertex_new_potential, graph );
    
      unsigned int source_strength = 0;
    
      BGL_FORALL_VERTICES( v, graph, Graph )    {
    
        v_old_potential[v] = source_strength++;
      }
      //]  
    
    
      //[ Extracting the adjacency matrix by iteration through all edges --> uBLAS matrix
      ublas::zero_matrix<int> zero_matrix( num_vertices(graph) , num_vertices(graph) ); 
      ublas::matrix<int> adjacency_matrix( zero_matrix );       //initialize properly
    
      BGL_FORALL_EDGES( e, graph, Graph )   {
    
         adjacency_matrix( source(e,graph), target(e,graph) ) = 1;
         adjacency_matrix( target(e,graph), source(e,graph) ) = 1;
      }
      //]
    
    
    
      //[ Compute new potentials = A . old potentials ! 
      v_new_potential.vector() = ublas::prod ( adjacency_matrix, v_old_potential.vector() );        //     new = A.old
      //]
    
    
    
    
      std::cout << adjacency_matrix << std::endl;       //output = [4,4]((0,1,0,1),(1,0,1,0),(0,1,0,0),(1,0,0,0))
      std::cout << v_old_potential.vector() << std::endl;   //output = [4](0,1,2,3)
      std::cout << v_new_potential.vector() << std::endl;   //output = [4](4,2,1,0)
    
      //You must access the properties via v_new_potential and v_old_potential, if you use get... again it resets
      std::cout << v_new_potential[0] << std::endl;
      std::cout << get(vertex_new_potential, graph)[0] << std::endl;
    
    }
    

    【讨论】:

    • 感谢您的解决方案,它可以编译并运行。但是,您能想出一种方法来使此功能也与捆绑属性一起使用吗?在 BGL 文档中,它说属性列表的使用在某些时候会贬值或将贬值。我尝试为捆绑属性调整您的代码,但如果没有可用的vertex_potential_t 标签,我不太知道如何维持生计。
    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多