سؤال

I am trying to implement the TSP package. I need to understand the input data structure so I can input my data. But apparently I am not able to display it.

The introduction paper ( link ) describes how to run the algorithm on a data set USCA50 that comes with the TSP package. I attempted to display the dataset using the lines of code below:

library("TSP")
data("USCA50")
USCA50

#to run the solver 
solve_TSP(USCA50)

It produces the output below but does not display the data set.

object of class ‘TSP’ 
50 cities (distance ‘euclidean’) 

I am trying to understand dataset/input so I can run my own data/input with the solve_TSP() function.

هل كانت مفيدة؟

المحلول 2

In a nutshell:

class(USCA50)

Shows that the input is an object of the following classes: TSP, dist.

So any input must be fist a distance matrix. And that can be construct using dist() by inputting locations and coordinates. Secondly, use TSP() constructor.

distance_matrix <- dist(coordinates)
tsp <- TSP(distance_matrix)
solve_TSP(tsp)

نصائح أخرى

To see it as a matrix:

as.matrix(USCA50)

If you just want to have a look at what it contains, I suggest you Hmisc::describe

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top