Parallelize 'TSP' functions
Henrik Bengtsson
Source:vignettes/futurize-81-TSP.md
futurize-81-TSP.Rmd+
=

The futurize package allows you to easily turn
sequential code into parallel code by piping the sequential code to the
futurize() function. Easy!
Introduction
The TSP package provides algorithms for solving the traveling salesperson problem (TSP).
Example:
Example adopted from
help("solve_RSP", package = "TSP"):
library(futurize)
plan(multisession)
library(TSP)
data("USCA50")
methods <- c(
"identity", "random", "nearest_insertion", "cheapest_insertion",
"farthest_insertion", "arbitrary_insertion", "nn", "repetitive_nn",
"two_opt", "sa"
)
## calculate tours - each tour in parallel
tours <- lapply(methods, FUN = function(m) {
solve_TSP(USCA50, rep = 10L, method = m) |> futurize()
})
names(tours) <- methodsThis will parallelize the computations, given that we have set up parallel workers, e.g.
plan(multisession)The built-in multisession backend parallelizes on your
local computer and works on all operating systems. There are other parallel
backends to choose from, including alternatives to parallelize
locally as well as distributed across remote machines, e.g.
plan(future.mirai::mirai_multisession)and
plan(future.batchtools::batchtools_slurm)