Skip to content

Instantly share code, notes, and snippets.

@ytmytm
Last active May 7, 2017 17:39
Show Gist options
  • Save ytmytm/83b9063869f92dbd8d47120189b3cad9 to your computer and use it in GitHub Desktop.
Save ytmytm/83b9063869f92dbd8d47120189b3cad9 to your computer and use it in GitHub Desktop.
TSP problem over major cities in Poland
library(readxl)
setwd("c:/tmp/obchodpolski/")
data <- read_xlsx("miasta.xlsx",sheet = "dist",col_names = TRUE)
# drop city names
data <- data[,-1]
# force distance matrix to be symmetric
library(Matrix)
ds <- as.matrix(forceSymmetric(as.matrix(data)))
library(TSP)
tsp <- TSP(as.matrix(ds))
# suboptimal
tour <- solve_TSP(tsp)
tour_length(tour)
names(tour)
# probably the best
tour2 <- solve_TSP(tsp,method="nn",rep=1000,two_opt=TRUE)
tour_length(tour2)
names(tour2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment