在 edmonds_karp_max_flow() 中使用命名参数和捆绑属性
Using Named parameters and Bundled properties with edmonds_karp_max_flow()
我正在尝试在 Boost-graph 库的 edmonds_karp_max_flow
算法中使用 命名参数 和 捆绑属性 。
为了说明我的问题,我采用了现有的 edmonds-karp example 并将内部属性转换为捆绑属性(我将我的属性命名为 edge_t
和 node_t
),并将命名参数转换为非命名参数。
这里是可以正常编译的非命名参数版本:
// The "non-named parameters version" (works fine)
edmonds_karp_max_flow(g, s, t,capacity,residual_capacity,rev,col,pred);
这是无法编译并触发大量模板错误的命名参数版本:
// The "named parameters version" (produces errors)
flow = edmonds_karp_max_flow(g, s, t,
capacity_map(capacity)
.residual_capacity_map(residual_capacity)
.reverse_edge_map(rev)
.color_map(col)
.predecessor_map(pred));
完整 edmonds-karp-eg_modified.cpp
来源:
#include <boost/config.hpp>
#include <iostream>
#include <string>
#include <boost/graph/edmonds_karp_max_flow.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/read_dimacs.hpp>
#include <boost/graph/graph_utility.hpp>
using namespace boost;
typedef adjacency_list_traits<vecS,vecS,directedS> traits;
struct edge_t {
double capacity;
float cost;
float residual_capacity;
traits::edge_descriptor reversed_edge;
};
struct node_t {
std::string name;
boost::default_color_type color;
traits::edge_descriptor predecessor;
};
typedef adjacency_list < listS, vecS, directedS, node_t, edge_t > Graph;
int main()
{
Graph g;
property_map < Graph, double edge_t::* >::type capacity = get(&edge_t::capacity, g);
property_map < Graph, float edge_t::* >::type cost = get(&edge_t::cost, g);
property_map < Graph, float edge_t::* >::type residual_capacity = get(&edge_t::residual_capacity, g);
property_map < Graph, traits::edge_descriptor edge_t::* >::type rev = get(&edge_t::reversed_edge, g);
property_map < Graph, std::string node_t::* >::type name = get(&node_t::name, g);
property_map < Graph, boost::default_color_type node_t::* >::type col = get(&node_t::color, g);
property_map < Graph, traits::edge_descriptor node_t::* >::type pred = get(&node_t::predecessor, g);
traits::vertex_descriptor s, t;
read_dimacs_max_flow(g, capacity, rev, s, t);
long flow;
// XXX The "non-named parameters version" (works fine)
// flow = edmonds_karp_max_flow(g, s, t,capacity,residual_capacity,rev,col,pred);
// XXX The "named parameters version" (produces errors)
flow = edmonds_karp_max_flow(g, s, t,
capacity_map(capacity)
.residual_capacity_map(residual_capacity)
.reverse_edge_map(rev)
.color_map(col)
.predecessor_map(pred));
std::cout << "c The total flow:" << std::endl;
std::cout << "s " << flow << std::endl << std::endl;
std::cout << "c flow values:" << std::endl;
graph_traits < Graph >::vertex_iterator u_iter, u_end;
graph_traits < Graph >::out_edge_iterator ei, e_end;
for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
if (capacity[*ei] > 0)
std::cout << "a " << *u_iter << " " << target(*ei, g) << " "
<< (capacity[*ei] - residual_capacity[*ei]) << std::endl;
return EXIT_SUCCESS;
}
以下是返回的错误:http://pastebin.com/Vra8ZWHG。
它可以很好地处理非命名参数...但不适用于命名参数。
更新: 有人似乎遇到了完全相同的问题:svn.boost.org/trac/boost/ticket/8791。他使用 Boost 1.50
而不是 1.55
.
修复了它
- Boost-graph 版本:1.58.0
- 编译器:g++ 5.0
它看起来像一个图形错误:named_function_params.hpp,第 326 行,get_param_type 的参数顺序相反。改变这个让它为我编译。
似乎没有任何带有命名参数的 Edmonds-Karp 测试用例,显然需要...
我正在尝试在 Boost-graph 库的 edmonds_karp_max_flow
算法中使用 命名参数 和 捆绑属性 。
为了说明我的问题,我采用了现有的 edmonds-karp example 并将内部属性转换为捆绑属性(我将我的属性命名为 edge_t
和 node_t
),并将命名参数转换为非命名参数。
这里是可以正常编译的非命名参数版本:
// The "non-named parameters version" (works fine)
edmonds_karp_max_flow(g, s, t,capacity,residual_capacity,rev,col,pred);
这是无法编译并触发大量模板错误的命名参数版本:
// The "named parameters version" (produces errors)
flow = edmonds_karp_max_flow(g, s, t,
capacity_map(capacity)
.residual_capacity_map(residual_capacity)
.reverse_edge_map(rev)
.color_map(col)
.predecessor_map(pred));
完整 edmonds-karp-eg_modified.cpp
来源:
#include <boost/config.hpp>
#include <iostream>
#include <string>
#include <boost/graph/edmonds_karp_max_flow.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/read_dimacs.hpp>
#include <boost/graph/graph_utility.hpp>
using namespace boost;
typedef adjacency_list_traits<vecS,vecS,directedS> traits;
struct edge_t {
double capacity;
float cost;
float residual_capacity;
traits::edge_descriptor reversed_edge;
};
struct node_t {
std::string name;
boost::default_color_type color;
traits::edge_descriptor predecessor;
};
typedef adjacency_list < listS, vecS, directedS, node_t, edge_t > Graph;
int main()
{
Graph g;
property_map < Graph, double edge_t::* >::type capacity = get(&edge_t::capacity, g);
property_map < Graph, float edge_t::* >::type cost = get(&edge_t::cost, g);
property_map < Graph, float edge_t::* >::type residual_capacity = get(&edge_t::residual_capacity, g);
property_map < Graph, traits::edge_descriptor edge_t::* >::type rev = get(&edge_t::reversed_edge, g);
property_map < Graph, std::string node_t::* >::type name = get(&node_t::name, g);
property_map < Graph, boost::default_color_type node_t::* >::type col = get(&node_t::color, g);
property_map < Graph, traits::edge_descriptor node_t::* >::type pred = get(&node_t::predecessor, g);
traits::vertex_descriptor s, t;
read_dimacs_max_flow(g, capacity, rev, s, t);
long flow;
// XXX The "non-named parameters version" (works fine)
// flow = edmonds_karp_max_flow(g, s, t,capacity,residual_capacity,rev,col,pred);
// XXX The "named parameters version" (produces errors)
flow = edmonds_karp_max_flow(g, s, t,
capacity_map(capacity)
.residual_capacity_map(residual_capacity)
.reverse_edge_map(rev)
.color_map(col)
.predecessor_map(pred));
std::cout << "c The total flow:" << std::endl;
std::cout << "s " << flow << std::endl << std::endl;
std::cout << "c flow values:" << std::endl;
graph_traits < Graph >::vertex_iterator u_iter, u_end;
graph_traits < Graph >::out_edge_iterator ei, e_end;
for (boost::tie(u_iter, u_end) = vertices(g); u_iter != u_end; ++u_iter)
for (boost::tie(ei, e_end) = out_edges(*u_iter, g); ei != e_end; ++ei)
if (capacity[*ei] > 0)
std::cout << "a " << *u_iter << " " << target(*ei, g) << " "
<< (capacity[*ei] - residual_capacity[*ei]) << std::endl;
return EXIT_SUCCESS;
}
以下是返回的错误:http://pastebin.com/Vra8ZWHG。
它可以很好地处理非命名参数...但不适用于命名参数。
更新: 有人似乎遇到了完全相同的问题:svn.boost.org/trac/boost/ticket/8791。他使用 Boost 1.50
而不是 1.55
.
- Boost-graph 版本:1.58.0
- 编译器:g++ 5.0
它看起来像一个图形错误:named_function_params.hpp,第 326 行,get_param_type 的参数顺序相反。改变这个让它为我编译。
似乎没有任何带有命名参数的 Edmonds-Karp 测试用例,显然需要...