C++ boost图度量_tsp_近似解不跟随图边
我试图在使用boost::adjacence_list创建的图上解决旅行推销员问题。我正在使用解决tsp的方法。 我面临的问题是,解决方案不遵循图的边。该解决方案连接图中未直接连接的顶点。我想知道这是图书馆的工作方式还是我做错了什么。这个解决方案看起来也不正确。我有4个顶点组成一个正方形,解应该沿着周长,但它是沿着对角线。对角线上没有边 这是我的邻接列表:C++ boost图度量_tsp_近似解不跟随图边,c++,boost,traveling-salesman,boost-graph,C++,Boost,Traveling Salesman,Boost Graph,我试图在使用boost::adjacence_list创建的图上解决旅行推销员问题。我正在使用解决tsp的方法。 我面临的问题是,解决方案不遵循图的边。该解决方案连接图中未直接连接的顶点。我想知道这是图书馆的工作方式还是我做错了什么。这个解决方案看起来也不正确。我有4个顶点组成一个正方形,解应该沿着周长,但它是沿着对角线。对角线上没有边 这是我的邻接列表: boost::邻接列表 添加顶点和添加边函数: boost::添加_顶点(id,图形); boost::添加_边(id1、id2、权重、图
boost::邻接列表
添加顶点和添加边函数:
boost::添加_顶点(id,图形);
boost::添加_边(id1、id2、权重、图形);//权重是欧几里得距离
TSP求解器:
std::vector<VertexDescriptor> tsp_path; //VertexDescriptor is adjacency_list::vertex_descriptor
metric_tsp_approx_tour(graph, back_inserter(tsp_path));
std::向量tsp\u路径//VertexDescriptor是邻接列表::顶点描述符
公制tsp近似图(图,背面插入器(tsp路径));
我还尝试将权重映射传递到metric\u tsp\u approw\u tour
,但同样的问题仍然存在
有人能帮我解决这个问题吗?如果Boost MeTimeTSPTA近似xTURE不考虑图的边,有没有办法考虑它们?< P>:DOCs:
这是一个旅行推销员启发式算法,用于生成一个完全连通的无向图的顶点之旅,该图的加权边服从三角形不等式
(强调矿山)
该子句确实声明假定所有顶点都是连接的
另外请注意,假设顶点索引映射到[0,num_顶点(图))
奖金
作为奖励,我尝试为该算法设计一个最小的工作示例。它似乎确实如广告所宣传的那样工作:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/metric_tsp_approx.hpp>
using Graph =
boost::adjacency_list<boost::setS, boost::listS, boost::undirectedS,
boost::property<boost::vertex_index_t, int>,
boost::property<boost::edge_weight_t, double>,
boost::no_property>;
using VertexDescriptor = Graph::vertex_descriptor;
int main() {
std::vector const points { std::pair
{ 4., 9. }, // these can be randomized
{ 2., 6. },
{ 4., 1. },
{ 1., 1. },
};
Graph graph;
for (auto i = 0u; i < points.size(); ++i) {
add_vertex(i, graph);
}
for (auto i = 0u; i < points.size(); ++i) {
auto va = vertex(i, graph);
// undirected, so only need traverse higher vertices for connections
for (auto j = i+1; j < points.size(); ++j) {
auto vb = vertex(j, graph);
auto const [ax, ay] = points.at(i);
auto const [bx, by] = points.at(j);
auto const dx = bx - ax;
auto const dy = by - ay;
add_edge(va, vb, sqrt(dx*dx + dy*dy), graph); // weight is euclidean distance
}
}
print_graph(graph);
std::vector<VertexDescriptor> tsp_path(num_vertices(graph)); //VertexDescriptor is adjacency_list::vertex_descriptor
metric_tsp_approx_tour(graph, back_inserter(tsp_path));
auto idmap = get(boost::vertex_index, graph);
for (auto vd : tsp_path) {
if (vd != graph.null_vertex()) {
auto [x,y] = points.at(idmap[vd]);
std::cout << " {" << x << "," << y << "}";
}
}
}
#包括
#包括
#包括
使用图形=
boost::邻接列表;
使用VertexDescriptor=Graph::vertex\u描述符;
int main(){
std::向量常量点{std::对
{4,9.},//这些可以随机化
{ 2., 6. },
{ 4., 1. },
{ 1., 1. },
};
图形;
用于(自动i=0u;i0 <--> 1 2 3
1 <--> 0 2 3
2 <--> 0 1 3
3 <--> 0 1 2
{4,9} {2,6} {1,1} {4,1} {4,9}