【问题标题】:Boost.Graph - algo.is_optimal() assertionBoost.Graph - algo.is_optimal() 断言
【发布时间】:2016-06-03 22:44:14
【问题描述】:

我正在尝试实现一个程序,该程序使用 最小成本流 算法检测arbitrage trading 机会。该算法在 Boost.Graph 中以boost::push_relabel_max_flow() 的形式实现,然后调用boost::cycle_canceling()。

以下是我已经拥有的代码,省略了boost::cycle_canceling-部分,因为我的程序在到达函数之前就死了。

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/property_map/property_map.hpp>
    #include <boost/graph/push_relabel_max_flow.hpp>
    #include <boost/graph/cycle_canceling.hpp>
    #include <boost/graph/directed_graph.hpp>
    #include <boost/config.hpp>
    #include <iostream>
    #include <string>



    typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS> Traits;

    struct st_order {
        double price;
        double amount;
        std::string type;
    };

    struct VertexProps {
        unsigned int id;
    };

    struct EdgeProps {
        double capacity;
        double residual_capacity;
        double weight;
        Traits::edge_descriptor reverse;
    };

    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProps, EdgeProps > DirectedGraph;

    int main() {


        DirectedGraph g;

        // ETH / BTC
        std::vector<st_order> trades{
            st_order{0.0101,10.0,"SELL"},
            st_order{0.01,3.0,"BUY"},
            st_order{0.0102,5.0,"SELL"},
            st_order{0.2,42.0,"BUY"},
        };


        std::vector<boost::graph_traits<DirectedGraph>::vertex_descriptor> vertices;
        for(unsigned int vertex_index = 0; vertex_index < trades.size(); vertex_index++)
        {
            vertices.push_back(boost::add_vertex({vertex_index}, g));
        }


        std::map<DirectedGraph::vertex_descriptor, std::map<DirectedGraph::vertex_descriptor, Traits::edge_descriptor>> edges;


int ifirst = 0;
for(auto& first : vertices)
{
    int isecond = 0;
    for(auto& second : vertices)
    {

        if(first == second || trades[ifirst].type.compare(trades[isecond].type) == 0)
        {
            isecond++;
            continue;
        }

        double amount = trades[isecond].amount;

        if(trades[isecond].type.compare("SELL") == 0)
            amount = amount * trades[isecond].price;

        edges[first][second] = boost::add_edge(first, second, {amount, amount, (trades[isecond].price / trades[ifirst].price)} , g).first;
        std::cout << "Add Edge: From " << first << " to " << second << std::endl;

        isecond++;
    }
    ifirst++;
}


for(auto& i : vertices)
{
    for(auto& j : vertices)
    {
        if(i == j)
            continue;

        if(edges.find(i) != edges.end() && edges[i].find(j) != edges[i].end())
        {
            if(edges.find(j) == edges.end() || edges[j].find(i) == edges[j].end())
            {
                throw std::runtime_error("No return edge found: "+std::to_string(i)+" "+std::to_string(j));
            }

            auto edge = boost::edge(i,j,g).first;
            g[edge].reverse = edges[i][j];

        }
    }
}



        double flow = boost::push_relabel_max_flow(g, vertices[0], vertices[1],
                boost::get(&EdgeProps::capacity, g),
                boost::get(&EdgeProps::residual_capacity, g),
                boost::get(&EdgeProps::reverse, g),
                boost::get(boost::vertex_index, g)
            );

// Now boost::cycle_canceling() would follow

    std::cout << "END << std::endl;
    return 0;
    };

我的程序的“正常”输出是:

Add Edge: From 0 to 1
Add Edge: From 0 to 3
Add Edge: From 1 to 0
Add Edge: From 1 to 2
Add Edge: From 2 to 1
Add Edge: From 2 to 3
Add Edge: From 3 to 0
Add Edge: From 3 to 2

作为流程图:

我的程序在push_relabel_max_flow-函数中断言。以下是完整的错误代码(在runtime打印):

    /usr/local/include/boost/graph/push_relabel_max_flow.hpp:707: typename
 boost::property_traits<IndexMap>::value_type 
boost::push_relabel_max_flow(Graph&, typename 
boost::graph_traits<Graph>::vertex_descriptor, typename 
boost::graph_traits<Graph>::vertex_descriptor, CapacityEdgeMap, 
ResidualCapacityEdgeMap, ReverseEdgeMap, VertexIndexMap) [with Graph = 
boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, 
VertexProps, EdgeProps>; CapacityEdgeMap = 
boost::adj_list_edge_property_map<boost::directed_tag, double, double&, long 
unsigned int, EdgeProps, double EdgeProps::*>; ResidualCapacityEdgeMap = 
boost::adj_list_edge_property_map<boost::directed_tag, double, double&, long 
unsigned int, EdgeProps, double EdgeProps::*>; ReverseEdgeMap = 
boost::adj_list_edge_property_map<boost::directed_tag, 
boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>, 
boost::detail::edge_desc_impl<boost::directed_tag, long unsigned int>&, long
 unsigned int, EdgeProps, boost::detail::edge_desc_impl<boost::directed_tag, 
long unsigned int> EdgeProps::*>; VertexIndexMap = 
boost::vec_adj_list_vertex_id_map<VertexProps, long unsigned int>; typename 
boost::property_traits<IndexMap>::value_type = double; typename 
boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]: Assertion
 `algo.is_optimal()' failed.

在消息的最后,您可以看到 Assertion: algo.is_optimal() failed。我完全不知道这意味着什么。

在源文件(boost/graph/push_relabel_max_flow.hpp)中定义为:

bool is_optimal() {
        // check if mincut is saturated...
        global_distance_update();
        return get(distance, src) >= n;
      }

我用谷歌搜索了它,但没有找到任何东西。我是否以错误的方式传递参数?是因为我使用double 作为容量(尽管,如果我没记错的话,“documentation”描述它尽可能使用double 作为容量)? 另外,我在documentation中发现了这句话:

CapacityEdgeMap 参数 cap 必须将 E 中的每条边映射到一个正数 数,E^T 中的每条边为 0。

粗体部分是什么意思?这是否意味着我必须将所有来自 sink-vertex 的出边的容量设置为 0?

【问题讨论】:

  • 我不熟悉这个特定的算法。我运行了你的代码,看起来is_optimal 是推送重新标记算法的一部分,而不是我最初认为的提升。我的猜测是它可能与您的图形的拓扑有关。我没有时间浏览您的图形构建代码。您确定此拓扑对于此类问题是正确的吗?您可能会考虑查看算法的最小工作示例。
  • 我用更多信息和图表编辑了我的帖子。我很确定我可以将这个算法用于我的图表。

标签: c++ boost boost-graph


【解决方案1】:

您需要将反向边缘的容量设置为 0。

所以你需要:

auto edge = boost::edge(i,j,g).first;
g[edge].reverse = edges[i][j];
g[edges[i][j]].capacity = 0;

我不知道为什么会这样。查看read_dimacs.hpp 我注意到他们创建了反向边缘并给了他们0容量。大约在页面下方的 3/4:

capacity[e1] = cap;
capacity[e2] = 0;
reverse_edge[e1] = e2;
reverse_edge[e2] = e1;

可能没有这个约束,算法会尝试将这些视为正常边缘。您引用的文档部分对此进行了描述,但并不完全清楚。

对输入图和属性有几个特殊要求 该算法的映射参数。一、有向图G=(V,E) 表示网络必须被扩充以包括反向 E 中每条边的边。也就是说,输入图应该是 Gin = (V,{E U E^T})。 ReverseEdgeMap 参数 rev 必须映射每个边 原始图到它的反向边缘,即 (u,v) -> (v,u) 对于所有 (u,v) 在 E 中。CapacityEdgeMap 参数 cap 必须映射 E 中的每条边 为正数,E^T 中的每条边为 0。

我认为这里的 E^T 表示 transpose 不是目标。您必须知道,有向邻接矩阵的转置实际上是所有边的反转。这就是为什么他们说输入图是 G = {V, E U E^T}。边加上需要添加的反向边。

旁注:将push-relable example 中的long 更改为double 效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多