TSPLIB function

Read and write TSPLIB files

Read and write TSPLIB files

Reads and writes TSPLIB format files. TSPLIB files can be used by most TSP solvers. Sample instances for the TSP in TSPLIB format are available on the TSPLIB homepage (see references).

read_TSPLIB(file, precision = 0) write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL) ## S3 method for class 'TSP' write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL) ## S3 method for class 'ATSP' write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL) ## S3 method for class 'ETSP' write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL)

Arguments

  • file: file name or a connection .
  • precision: controls the number of decimal places used to represent distances (see details). If x already is integer, this argument is ignored and x is used as is.
  • x: an object with a TSP problem. NAs are not allowed.
  • inf: replacement value for Inf (TSPLIB format cannot handle Inf). If inf is NULL, a large value of c("max(x)+2\nmax(x) + 2\n", "range(x)range(x)") (ignoring infinite entries) is used.
  • neg_inf: replacement value for -Inf. If no value is specified, a small value of min(x)2range(x)min(x) - 2 range(x) (ignoring infinite entries) is used.

Returns

returns an object of class TSP or ATSP.

Details

In the TSPLIB format distances are represented by integer values. Therefore, if x contains double values (which is normal in R) the values given in x are multiplied by 10precision10^{precision} before coercion to integer. Note that therefore all results produced by programs using the TSPLIB file as input need to be divided by 10precision10^{precision} (i.e., the decimal point has to be shifted precision placed to the left).

Currently only the following EDGE_WEIGHT_TYPEs are implemented: EXPLICIT, EUC_2D and EUC_3D.

Examples

## Drilling problem from TSP drill <- read_TSPLIB(system.file("examples/d493.tsp", package = "TSP")) drill tour <- solve_TSP(drill, method = "nn", two_opt = TRUE) tour plot(drill, tour, cex=.6, col = "red", pch= 3, main = "TSPLIB: d493") ## Write and read data in TSPLIB format x <- data.frame(x=runif(5), y=runif(5)) ## create TSP, ATSP and ETSP (2D) tsp <- TSP(dist(x)) atsp <- ATSP(dist(x)) etsp <- ETSP(x[,1:2]) write_TSPLIB(tsp, file="example.tsp") #file.show("example.tsp") r <- read_TSPLIB("example.tsp") r write_TSPLIB(atsp, file="example.tsp") #file.show("example.tsp") r <- read_TSPLIB("example.tsp") r write_TSPLIB(etsp, file="example.tsp") #file.show("example.tsp") r <- read_TSPLIB("example.tsp") r ## clean up unlink("example.tsp")

References

TSPLIB home page, http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/

See Also

Other TSP: ATSP(), Concorde, ETSP(), TSP(), insert_dummy(), reformulate_ATSP_as_TSP(), solve_TSP()

Author(s)

Michael Hahsler

  • Maintainer: Michael Hahsler
  • License: GPL-3
  • Last published: 2023-04-04