| 1 |
### R code from vignette source 'TSP.Rnw' |
| 2 |
|
| 3 |
################################################### |
| 4 |
### code chunk number 1: TSP.Rnw:80-83 |
| 5 |
################################################### |
| 6 |
options(width = 75, useFancyQuotes=FALSE, prompt="R> ") |
| 7 |
### for sampling |
| 8 |
set.seed(1234) |
| 9 |
|
| 10 |
|
| 11 |
################################################### |
| 12 |
### code chunk number 2: TSP.Rnw:644-647 |
| 13 |
################################################### |
| 14 |
library("TSP") |
| 15 |
data("USCA50") |
| 16 |
USCA50 |
| 17 |
|
| 18 |
|
| 19 |
################################################### |
| 20 |
### code chunk number 3: TSP.Rnw:660-661 |
| 21 |
################################################### |
| 22 |
set.seed(1234) |
| 23 |
|
| 24 |
|
| 25 |
################################################### |
| 26 |
### code chunk number 4: dotchart_USCA50 |
| 27 |
################################################### |
| 28 |
methods <- c("nearest_insertion", "farthest_insertion", "cheapest_insertion", |
| 29 |
"arbitrary_insertion", "nn", "repetitive_nn", "two_opt") |
| 30 |
|
| 31 |
tours <- sapply(methods, FUN = function(m) solve_TSP(USCA50, method = m), |
| 32 |
simplify = FALSE) |
| 33 |
## tours$concorde <- solve_TSP(tsp, method = "concorde") |
| 34 |
|
| 35 |
tours[[1]] |
| 36 |
|
| 37 |
dotchart(sort(c(sapply(tours, tour_length), optimal = 14497)), |
| 38 |
xlab = "tour length", xlim = c(0, 20000)) |
| 39 |
|
| 40 |
|
| 41 |
################################################### |
| 42 |
### code chunk number 5: TSP.Rnw:702-703 |
| 43 |
################################################### |
| 44 |
set.seed(1234) |
| 45 |
|
| 46 |
|
| 47 |
################################################### |
| 48 |
### code chunk number 6: TSP.Rnw:706-710 |
| 49 |
################################################### |
| 50 |
library("TSP") |
| 51 |
data("USCA312") |
| 52 |
tsp <- insert_dummy(USCA312, label = "cut") |
| 53 |
tsp |
| 54 |
|
| 55 |
|
| 56 |
################################################### |
| 57 |
### code chunk number 7: TSP.Rnw:716-718 |
| 58 |
################################################### |
| 59 |
tour <- solve_TSP(tsp, method="farthest_insertion") |
| 60 |
tour |
| 61 |
|
| 62 |
|
| 63 |
################################################### |
| 64 |
### code chunk number 8: TSP.Rnw:727-730 |
| 65 |
################################################### |
| 66 |
path <- cut_tour(tour, "cut") |
| 67 |
head(labels(path)) |
| 68 |
tail(labels(path)) |
| 69 |
|
| 70 |
|
| 71 |
################################################### |
| 72 |
### code chunk number 9: map1 |
| 73 |
################################################### |
| 74 |
if(require(maps)) { |
| 75 |
library("maps") |
| 76 |
|
| 77 |
data("USCA312_GPS") |
| 78 |
|
| 79 |
plot_path <- function(path) { |
| 80 |
plot((USCA312_GPS[, c("long", "lat")]), cex = .3, col = "red") |
| 81 |
map("world", col = "gray", add = TRUE) |
| 82 |
lines(USCA312_GPS[, c("long", "lat")][path,], col = "black") |
| 83 |
points(USCA312_GPS[c(head(path, 1), tail(path, 1)), c("long", "lat")], pch = 19, col = "black") |
| 84 |
} |
| 85 |
|
| 86 |
plot_path(path) |
| 87 |
|
| 88 |
} else { |
| 89 |
plot(NA, xlim= c(0,1), ylim = c(0,1)) |
| 90 |
text(.5, .5, "Suggested packages not available") |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
################################################### |
| 95 |
### code chunk number 10: TSP.Rnw:791-792 |
| 96 |
################################################### |
| 97 |
set.seed(1234) |
| 98 |
|
| 99 |
|
| 100 |
################################################### |
| 101 |
### code chunk number 11: TSP.Rnw:795-806 |
| 102 |
################################################### |
| 103 |
atsp <- as.ATSP(USCA312) |
| 104 |
ny <- which(labels(USCA312) == "New York, NY") |
| 105 |
atsp[, ny] <- 0 |
| 106 |
initial_tour <- solve_TSP(atsp, method="nn") |
| 107 |
initial_tour |
| 108 |
tour <- solve_TSP(atsp, method ="two_opt", control = list(tour = initial_tour)) |
| 109 |
tour |
| 110 |
path <- cut_tour(tour, ny, exclude_cut = FALSE) |
| 111 |
|
| 112 |
head(labels(path)) |
| 113 |
tail(labels(path)) |
| 114 |
|
| 115 |
|
| 116 |
################################################### |
| 117 |
### code chunk number 12: map2 |
| 118 |
################################################### |
| 119 |
plot_path(path) |
| 120 |
|
| 121 |
|
| 122 |
################################################### |
| 123 |
### code chunk number 13: TSP.Rnw:830-832 |
| 124 |
################################################### |
| 125 |
tsp <- reformulate_ATSP_as_TSP(atsp) |
| 126 |
tsp |
| 127 |
|
| 128 |
|
| 129 |
################################################### |
| 130 |
### code chunk number 14: TSP.Rnw:844-846 (eval = FALSE) |
| 131 |
################################################### |
| 132 |
## tour <- solve_TSP(tsp, method = "concorde") |
| 133 |
## tour <- as.TOUR(tour[tour <= n_of_cities(atsp)]) |
| 134 |
|
| 135 |
|
| 136 |
################################################### |
| 137 |
### code chunk number 15: TSP.Rnw:868-869 |
| 138 |
################################################### |
| 139 |
set.seed(1234) |
| 140 |
|
| 141 |
|
| 142 |
################################################### |
| 143 |
### code chunk number 16: TSP.Rnw:872-882 |
| 144 |
################################################### |
| 145 |
m <- as.matrix(USCA312) |
| 146 |
ny <- which(labels(USCA312) == "New York, NY") |
| 147 |
la <- which(labels(USCA312) == "Los Angeles, CA") |
| 148 |
|
| 149 |
atsp <- ATSP(m[-c(ny,la), -c(ny,la)]) |
| 150 |
atsp <- insert_dummy(atsp, label = "LA/NY") |
| 151 |
|
| 152 |
la_ny <- which(labels(atsp) == "LA/NY") |
| 153 |
atsp[la_ny, ] <- c(m[-c(ny,la), ny], 0) |
| 154 |
atsp[, la_ny] <- c(m[la, -c(ny,la)], 0) |
| 155 |
|
| 156 |
|
| 157 |
################################################### |
| 158 |
### code chunk number 17: TSP.Rnw:887-896 |
| 159 |
################################################### |
| 160 |
tour <- solve_TSP(atsp, method ="nearest_insertion") |
| 161 |
tour |
| 162 |
|
| 163 |
path_labels <- c("New York, NY", |
| 164 |
labels(cut_tour(tour, la_ny)), "Los Angeles, CA") |
| 165 |
path_ids <- match(path_labels, labels(USCA312)) |
| 166 |
|
| 167 |
head(path_labels) |
| 168 |
tail(path_labels) |
| 169 |
|
| 170 |
|
| 171 |
################################################### |
| 172 |
### code chunk number 18: map3 |
| 173 |
################################################### |
| 174 |
plot_path(path_ids) |
| 175 |
|
| 176 |
|
| 177 |
################################################### |
| 178 |
### code chunk number 19: TSP.Rnw:940-941 |
| 179 |
################################################### |
| 180 |
set.seed(4444) |
| 181 |
|
| 182 |
|
| 183 |
################################################### |
| 184 |
### code chunk number 20: TSP.Rnw:943-947 |
| 185 |
################################################### |
| 186 |
data("iris") |
| 187 |
tsp <- TSP(dist(iris[-5]), labels = iris[, "Species"]) |
| 188 |
tsp_dummy <- insert_dummy(tsp, n = 3, label = "boundary") |
| 189 |
tour <- solve_TSP(tsp_dummy) |
| 190 |
|
| 191 |
|
| 192 |
################################################### |
| 193 |
### code chunk number 21: clustering |
| 194 |
################################################### |
| 195 |
## plot the distance matrix |
| 196 |
image(tsp_dummy, tour, xlab = "objects", ylab ="objects") |
| 197 |
|
| 198 |
## draw lines where the dummy cities are located |
| 199 |
abline(h = which(labels(tour)=="boundary"), col = "red") |
| 200 |
abline(v = which(labels(tour)=="boundary"), col = "red") |
| 201 |
|
| 202 |
|
| 203 |
################################################### |
| 204 |
### code chunk number 22: TSP.Rnw:985-989 |
| 205 |
################################################### |
| 206 |
out <- rle(labels(tour)) |
| 207 |
data.frame(Species = out$values, |
| 208 |
Lenghts = out$lengths, |
| 209 |
Pos = cumsum(out$lengths)) |
| 210 |
|
| 211 |
|
| 212 |
################################################### |
| 213 |
### code chunk number 23: clustering2 |
| 214 |
################################################### |
| 215 |
prc <- prcomp(iris[1:4]) |
| 216 |
plot(prc$x, pch = as.numeric(iris[,5]), col = as.numeric(iris[,5])) |
| 217 |
|
| 218 |
paths <- cut_tour(tour, cut = "boundary") |
| 219 |
for(p in paths) lines(prc$x[p, ]) |
| 220 |
|
| 221 |
|