Tags

, , , , , ,

As mentioned in an earlier TSP post (TSP in R), super simple data manipulation ability of R is just beautiful.  You can input GPS coordinates and accurately calculate the distance matrix based on great-circle distance with a single function spDists().

library(sp)
library(tspmeta)

city <- subset(USCA312_coords, lat <44 & lat>25)
coords.df <- data.frame(long=city$long, lat=city$lat)
coords.mx <- as.matrix(coords.df)

# Compute great-circle distance matrix
dist.mx <- spDists(coords.mx, longlat=TRUE)

# Construct a TSP object
tsp.ins <- tsp_instance(coords.mx, dist.mx )
#
tour <- run_solver(tsp.ins, method="2-opt")
#Plot
autoplot(tsp.ins, tour)

Advertisements