Common interface to all TSP solvers in this package.
solve_TSP(x, method = NULL, control = NULL, ...)
## S3 method for class 'TSP'
solve_TSP(x, method = NULL, control = NULL, ...)
## S3 method for class 'ETSP'
solve_TSP(x, method = NULL, control = NULL, ...)
## S3 method for class 'ATSP'
solve_TSP(x, method = NULL, control = NULL, as_TSP = FALSE, ...)
install.packages("TSP", repo="http://cran.r-project.org", dep=T)
library(TSP)
## solve a simple Euclidean TSP (using the default method)
etsp <- ETSP(data.frame(x = runif(20), y = runif(20)))
tour <- solve_TSP(etsp)
tour
tour_length(tour)
plot(etsp, tour)
## compare methods
data("USCA50")
USCA50
methods <- c("identity", "random", "nearest_insertion",
"cheapest_insertion", "farthest_insertion", "arbitrary_insertion",
"nn", "repetitive_nn", "two_opt")
## calculate tours
tours <- lapply(methods, FUN = function(m) solve_TSP(USCA50, method = m))
names(tours) <- methods
## use the external solver which has to be installed separately
## Not run:
tours$concorde <- solve_TSP(USCA50, method = "concorde")
tours$linkern <- solve_TSP(USCA50, method = "linkern")
## End(Not run)
## register a parallel backend to perform repetitions in parallel
## Not run:
library(doParallel)
registerDoParallel()
## End(Not run)
## add some tours using repetition and two_opt refinements
tours$'nn+two_opt' <- solve_TSP(USCA50, method="nn", two_opt=TRUE)
tours$'nn+rep_10' <- solve_TSP(USCA50, method="nn", rep=10)
tours$'nn+two_opt+rep_10' <- solve_TSP(USCA50, method="nn", two_opt=TRUE, rep=10)
tours$'arbitrary_insertion+two_opt' <- solve_TSP(USCA50)
## show first tour
tours[[1]]
## compare tour lengths
opt <- 14497 # obtained by Concorde
tour_lengths <- c(sort(sapply(tours, tour_length), decreasing = TRUE),
optimal = opt)
dotchart(tour_lengths/opt*100-100, xlab = "percent excess over optimum")
Return Values: An object of class
Details: Treatment of
All heuristics can be used with the control arguments
Some solvers (including Concorde) cannot directly solve
Currently the following methods are available:
return a tour representing the order in the data (identity order) or a random order.
Nearest, farthest, cheapest and arbitrary insertion algorithms for a symmetric and asymmetric TSP (Rosenkrantz et al. 1977).
The distances between cities are stored in a distance matrix D with elements d(i,j). All insertion algorithms start with a tour consisting of an arbitrary city and choose in each step a city k not yet on the tour. This city is inserted into the existing tour between two consecutive cities i and j, such that
d(i,k) + d(k,j) - d(i,j)
is minimized. The algorithms stops when all cities are on the tour.
The nearest insertion algorithm chooses city k in each step as the city which is nearest to a city on the tour.
For farthest insertion, the city k is chosen in each step as the city which is farthest to any city on the tour.
Cheapest insertion chooses the city k such that the cost of inserting the new city (i.e., the increase in the tour's length) is minimal.
Arbitrary insertion chooses the city k randomly from all cities not yet on the tour.
Nearest and cheapest insertion tries to build the tour using cities which fit well into the partial tour constructed so far. The idea behind behind farthest insertion is to link cities far away into the tour fist to establish an outline of the whole tour early.
Additional control options:
index of the first city (default: random city).
Nearest neighbor and repetitive nearest neighbor algorithms for symmetric and asymmetric TSPs (Rosenkrantz et al. 1977).
The algorithm starts with a tour containing a random city. Then the algorithm always adds to the last city on the tour the nearest not yet visited city. The algorithm stops when all cities are on the tour.
Repetitive nearest neighbor constructs a nearest neighbor tour for each city as the starting point and returns the shortest tour found.
Additional control options:
index of the first city (default: random city).
Two edge exchange improvement procedure (Croes 1958).
This is a tour refinement procedure which systematically exchanges two edges in the graph represented by the distance matrix till no improvements are possible. Exchanging two edges is equal to reversing part of the tour. The resulting tour is called 2-optimal.
This method can be applied to tours created by other methods or used as its own method. In this case improvement starts with a random tour.
Additional control options:
an existing tour which should be improved. If no tour is given, a random tour is used.
number of times to try two_opt with a different initial random tour (default: 1).
Concorde algorithm (Applegate et al. 2001).
Concorde is an advanced exact TSP solver for only symmetric TSPs based on branch-and-cut. The program is not included in this package and has to be obtained and installed separately (see
Additional control options:
a character string containing the path to the executable (see
a character string containing command line options for Concorde, e.g.,
an integer which controls the number of decimal places used for the internal representation of distances in Concorde. The values given in
Concorde's Chained Lin-Kernighan heuristic (Applegate et al. 2003).
The Lin-Kernighan (Lin and Kernighan 1973) heuristic uses variable k edge exchanges to improve an initial tour. The program is not included in this package and has to be obtained and installed separately (see
Additional control options: see Concorde above.
See Also: