How to specify a starting city using the TSP packa

2019-05-17 00:31发布

问题:

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))

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

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)

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

回答1:

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)

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

This also works for other methods.



标签: r dataset