Skip to contents

Returns shortest cycle or path via tsp solver from package TSP

Usage

order_tsp(d, method = "nearest", cycle=FALSE,improve=FALSE,path_dir = path_cor,...)

Arguments

d

A dist, used to provide edge weights.

method

Options are nearest_insertion, farthest_insertion, cheapest_insertion, arbitrary_insertion, nn, repetitive_nn, 2-opt and if concorde package is loaded, concorde. See solve_TSP for details.

improve

if TRUE, attempts to improve the solution using "2-opt".

cycle

If TRUE, finds the shortest cycle, otherwise the shortest open path.

path_dir

If a function is provided, used to re-orient the cycle/path. Default function is path_cor.

...

passed to solve_tsp

Value

A vector containing a permutation of 1..n

Details

Requires package TSP. When path_dir is non NULL, the returned hamiltonian is also optimally oriented using best_orientation, which compares orientations via path_dir.

References

See package TSP.

Author

C.B. Hurley and R.W. Oldford

See also

Examples

require(PairViz)


rdist <- function(n) {
  d <- matrix(0,n,n)
  d[lower.tri(d)] <- runif(n*(n-1)/2)
  return(as.dist(d))
  }


order_tsp(rdist(7))
#> [1] 1 3 6 5 2 4 7




edist <- as.dist(as.matrix(eurodist))
order_tsp(edist)
#>  [1] 20  7 10 11  6  3  4 18  5 14 12  9  2 15 13  8 16 17 21 19  1