Description Usage Arguments Details Value Author(s) References Examples

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).

1 2 | ```
write_TSPLIB(x, file, precision = 6, inf = NULL, neg_inf = NULL)
read_TSPLIB(file, precision = 0)
``` |

`x` |
an object of class |

`file` |
file name or a |

`precision` |
controls the number of decimal places used to represent
distances (see details). If |

`inf` |
replacement value for |

`neg_inf` |
replacement value for |

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 *10^{precision}* before coercion to
`integer`

. Note that therefore all results produced by programs using the
TSPLIB file as input need to be divided by *10^{precision}* (i.e., the
decimal point has to be shifted `precision`

placed to the left).

Currently only the following `EDGE_WEIGHT_TYPE`

s are implemented:
`EXPLICIT`

, `EUC_2D`

and `EUC_3D`

.

`read_TSPLIB`

returns an object of class `TSP`

or `ATSP`

.

Michael Hahsler

TSPLIB home page, http://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | ```
## 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")
``` |

```
object of class 'ETSP'
493 cities (Euclidean TSP)
object of class 'TOUR'
result of method 'nn+two_opt' for 493 cities
tour length: 37960.29
object of class 'TSP'
5 cities (distance 'unknown')
object of class 'ATSP' (asymmetric TSP)
5 cities (distance 'unknown')
object of class 'ETSP'
5 cities (Euclidean TSP)
```

TSP documentation built on May 23, 2019, 1:03 a.m.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.