Boost Graph LibraryをBoost 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
、プロパティ マップ全体を関連付けられたマップにコピーすることです。(b)新しく計算された値をグラフに戻すには、プロパティ マップもublas::vector
トラバースする必要があります。私のアプリケーションではこれらの操作が何度も繰り返されると思われるため、最初からこの部分をできるだけクリーンに実行したいと考えています。new_potential
理想的には、このすべてのコピーを行ったり来たりして、代わりにある種のアダプターを使用して、 の呼び出しでboost::property_map
として動作するようにしたいと考えています。次のようなものを使用すると素晴らしいでしょう。ublas::vector
prod()
adapter(new_potential) = ublas::prod( adjacency_matrix, adapter(old_potential) );
そのような機能を実現する方法や、私のソリューションを改善する方法を誰かが知っていれば、とても感謝しています。