The TSP package for R

The traveling salesperson problem (TSP) is a well known and important combinatorial optimization problem. The goal is to find the shortest tour that visits each city in a given list exactly once and then returns to the starting city.

The TSP package offers a simple infrastructure to handle and solve TSPs.



A simple example using the data set of 312 cities in North America (USA and Canada) which comes with the package. The result is visualized with the R-package map (code is available in the introductory paper below).

R> ## load library and read data
R> library("TSP")
R> data("USCA312")
R> ## create a TSP object from the data 
R> tsp <- TSP(USCA312)
R> tsp
object of class 'TSP'
312 cities (distance   'euclidean')
R> ## find a 2-optimal solution 
R> tour <- solve_TSP(tsp, method = "2-opt")
R> tour
object of class 'TOUR'
result of method '2-opt' for 312 cities
tour length: 41381

Download & Installation

Or install directly in R via: R> install.packages("TSP")

Further Information

© Michael Hahsler
Last update: