Quick solution of TSP problem in R

For Andy Ford

Install the TSP package

install.packages("TSP")

Invent some dummy data

library(TSP)
x<-round(runif(15,100,1000),0)
y<-round(runif(15,100,1000),0)

d <- data.frame(x, y, row.names = LETTERS[1:15]) ##

Data format

The data just consists of labelled points. Any file in this format can be uploaded into R very easily with read.csv. Shapefiles can also be easily loaded

head(d)
##     x   y
## A 276 380
## B 586 465
## C 516 315
## D 699 475
## E 432 866
## F 851 763
d<-read.csv("Your_file_consisting_of_labelled_coordinates.csv")
etsp <- ETSP(d) ## This stands for EUCLIDEAN TRAVELLING SALESPERSON
tour <- solve_TSP(etsp) ## SOlve the problem
plot(etsp, tour, tour_col = "red") ## Plot it out

Rownames in order of visit

as.integer(tour)
##  [1] 15 13  6  7 10  9  2  4  3 12  1 14 11  8  5
d<-data.frame(d,tour=as.integer(tour))
d<-d[order(d$tour),]
head(d)
##     x   y tour
## K 141 826    1
## G 801 737    2
## I 494 533    3
## H 316 853    4
## O 758 940    5
## C 516 315    6
write.csv(d,"a_new_file_consisting_of_labelled_coordinates_with_ordered_index_of_visits.csv")