如何使用 R 中的 TSP 包指定起始城市 [英] How to specify a starting city using the TSP package in R

查看:16
本文介绍了如何使用 R 中的 TSP 包指定起始城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 R 中的 TSP 包来解决 TSP 问题.我创建了一个大型对称距离矩阵,其中沿前导对角线有 0 个条目.我希望能够将第一个城市指定为 nearest_insertion 等方法的起始城市.我已成功使用 "nn" 方法并使用以下代码指定了起始城市:

I have been trying to solve a TSP problem using the TSP package in R. I have created a large symmetric distance matrix with 0 entries along the leading diagonal. I want to be able to specify the first city as the starting city for methods such as nearest_insertion. I have successfully used the "nn" method and specified the starting city using the code below:

tsp1=TSP(distance_matrix)
solve_TSP(tsp1,method="nn",control=list(start=1))

但是参数 control=list(start=1)) 不适用于 "nearest_insertion" 方法.相反,我收到错误消息:

However the argument control=list(start=1)) doesn't work for the "nearest_insertion" method. Instead I get the error message:

x[is.na(x)] <- Inf 中的错误:INTEGER() 只能应用于整数",不能应用于双精度"

Error in x[is.na(x)] <- Inf : INTEGER() can only be applied to a 'integer', not a 'double`

对于样本数据,我一直在使用以下距离矩阵:

For sample data I have been using the following distance matrix:

distance_matrix=matrix(c(0,1,2,1,0,5,2,5,0),3,3)

如何将第一个城市指定为 "nearest_insertion" 方法的起始城市?

How can I specify the first city as the starting city for the "nearest_insertion" method?

推荐答案

我知道有点晚了,但我遇到了同样的问题,这就是你需要做的:

I know it's a bit late but I had the same problem, and that's how you need to do:

solve_TSP(prova, method = "nearest_insertion", start=3L)

这将 3 声明为整数,与 as.integer(3) 相同,但避免了函数调用.

This declares 3 as an integer, same as as.integer(3), yet avoiding the function call.

这也适用于其他方法.

这篇关于如何使用 R 中的 TSP 包指定起始城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆