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

查看:137
本文介绍了如何使用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) code>

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 ()只能应用于整数而不是double

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)

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

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

这也适用于其他方法。

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

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