权重映射作为 Boost Graph Dijkstra 算法中的函数
Weight map as function in Boost Graph Dijkstra algorithm
我正在使用 Boost Graph Libraries 并且需要使用一个权重图,它不是常数,而是参数 K 的函数(即边成本取决于 K)。在实践中,给定以下代码:
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
struct Edge {
Edge(float weight_) : weight(weight_) {}
float weight;
float getWeight(int K)
{
return K*weight;
}
};
int main(int, char**){
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::no_property, Edge > graph_t;
typedef boost::graph_traits < graph_t >::vertex_descriptor vertex_t;
graph_t g;
vertex_t a = boost::add_vertex(g);
vertex_t b = boost::add_vertex(g);
vertex_t c = boost::add_vertex(g);
vertex_t d = boost::add_vertex(g);
boost::add_edge(a, b, Edge(3), g);
boost::add_edge(b, c, Edge(3), g);
boost::add_edge(a, d, Edge(1), g);
boost::add_edge(d, c, Edge(4), g);
std::vector<vertex_t> preds(4);
// Traditional dijsktra (sum)
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::weight,g)));
return 0;
}
我想调用Dijkstra算法如下:
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::getWeight(2),g)));
但是报错如下
cannot call member function ‘float Edge::getWeight(int)’ without
object
有人知道怎么解决吗?
有许多 属性 地图风格。特别是 transform_value_property_map
可以在这里使用。
简单方法 C++03
假设你写的是 c++03:
#include <boost/property_map/transform_value_property_map.hpp>
#include <boost/bind.hpp>
// ...
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(
boost::make_transform_value_property_map(
boost::bind(&Edge::getWeight ,_1, 2),
boost::get(boost::edge_bundle, g))
));
更干净的 C++11
auto wmap = make_transform_value_property_map([](Edge& e) { return e.getWeight(2); }, get(boost::edge_bundle, g));
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(wmap));
您可以删除 boost/bind.hpp
包含。
奖励:删除 getWeight()
成员函数
你实际上并不需要它。您可以就地编写 Phoenix 演员:
#include <boost/phoenix.hpp>
using boost::phoenix::arg_names::arg1;
auto wmap = make_transform_value_property_map(2 * (&arg1->*&Edge::weight), get(boost::edge_bundle, g));
或者再次使用c++11:
auto wmap = make_transform_value_property_map([](Edge& e) { return e.weight * 2; }, get(boost::edge_bundle, g));
我正在使用 Boost Graph Libraries 并且需要使用一个权重图,它不是常数,而是参数 K 的函数(即边成本取决于 K)。在实践中,给定以下代码:
#include <boost/config.hpp>
#include <iostream>
#include <fstream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/adjacency_list.hpp>
struct Edge {
Edge(float weight_) : weight(weight_) {}
float weight;
float getWeight(int K)
{
return K*weight;
}
};
int main(int, char**){
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS, boost::no_property, Edge > graph_t;
typedef boost::graph_traits < graph_t >::vertex_descriptor vertex_t;
graph_t g;
vertex_t a = boost::add_vertex(g);
vertex_t b = boost::add_vertex(g);
vertex_t c = boost::add_vertex(g);
vertex_t d = boost::add_vertex(g);
boost::add_edge(a, b, Edge(3), g);
boost::add_edge(b, c, Edge(3), g);
boost::add_edge(a, d, Edge(1), g);
boost::add_edge(d, c, Edge(4), g);
std::vector<vertex_t> preds(4);
// Traditional dijsktra (sum)
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::weight,g)));
return 0;
}
我想调用Dijkstra算法如下:
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(boost::get(&Edge::getWeight(2),g)));
但是报错如下
cannot call member function ‘float Edge::getWeight(int)’ without object
有人知道怎么解决吗?
有许多 属性 地图风格。特别是 transform_value_property_map
可以在这里使用。
简单方法 C++03
假设你写的是 c++03:
#include <boost/property_map/transform_value_property_map.hpp>
#include <boost/bind.hpp>
// ...
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(
boost::make_transform_value_property_map(
boost::bind(&Edge::getWeight ,_1, 2),
boost::get(boost::edge_bundle, g))
));
更干净的 C++11
auto wmap = make_transform_value_property_map([](Edge& e) { return e.getWeight(2); }, get(boost::edge_bundle, g));
boost::dijkstra_shortest_paths(g, a, boost::predecessor_map(&preds[0]).weight_map(wmap));
您可以删除 boost/bind.hpp
包含。
奖励:删除 getWeight()
成员函数
你实际上并不需要它。您可以就地编写 Phoenix 演员:
#include <boost/phoenix.hpp>
using boost::phoenix::arg_names::arg1;
auto wmap = make_transform_value_property_map(2 * (&arg1->*&Edge::weight), get(boost::edge_bundle, g));
或者再次使用c++11:
auto wmap = make_transform_value_property_map([](Edge& e) { return e.weight * 2; }, get(boost::edge_bundle, g));