我正在尝试使用最低成本流算法实施一个检测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
- 函数中断言。以下是完整的错误代码(在运行时下打印):
/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.
在邮件的最后,您可以看到断言:algo.is_optimal()失败。我绝对不知道这意味着什么。
在源文件( 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参数上限必须将E中的每个边缘映射为正数 数字,和E ^ T中的每条边到0 。
粗体部分是什么意思?这是否意味着我必须将所有传出边缘的容量从sink-vertex设置为0?
答案 0 :(得分: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;
可能没有这个约束,算法会尝试将这些视为正常边缘。您引用的文档部分描述了这一点,但并不完全明显。
输入图和属性有几个特殊要求 该算法的map参数。首先,有向图G =(V,E) 表示网络必须增加以包含反向 E中每个边的边。也就是说,输入图应该是Gin = (V,{E U E ^ T})。 ReverseEdgeMap参数rev必须映射每个边 原始图形到其反向边缘,即(u,v) - &gt; (v,u)为所有人 (u,v)在E.中,CapacityEdgeMap参数上限必须映射E中的每个边 到正数,并且E ^ T中的每个边到0。
我认为这里E ^ T意味着转置而不是目标。你必须知道有向邻接矩阵的转置实际上是所有边的反转。这就是为什么他们说输入图是G = {V,E U E ^ T}。边缘加上需要添加的反向边缘。
附注:在push-relable example中将long
更改为double
非常合适。