I am working with the R programming language.
I found this link over here which shows how to recreate the "Travelling Salesman Problem" in R using the Genetic Algorithm (Optimized Delivery Route using Genetic Algorithm: Cost cutting for e-commerce)
library(GA)
data("eurodist", package = "datasets")
D <- as.matrix(eurodist)
tourLength <- function(tour, distMatrix) {
tour <- c(tour, tour[1])
route <- embed(tour, 2)[,2:1]
sum(distMatrix[route])
}
#Firness function to be maximized
tspFitness <- function(tour, ...) 1/tourLength(tour, ...)
GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
min = 1, max = attr(eurodist, "Size"), popSize = 50, maxiter = 5000,
run = 500, pmutation = 0.2)
We can then see the results:
summary(GA)
## Solutions =
## x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19
## [1,] 20 7 11 3 4 5 18 13 12 9 14 2 15 8 16 19 1 21 17
## [2,] 6 10 20 7 11 3 4 5 18 13 12 9 14 2 15 8 16 19 1
## [3,] 10 6 17 21 1 19 16 8 15 2 14 9 12 13 18 5 4 3 11
## x20 x21
## [1,] 6 10
## [2,] 21 17
## [3,] 7 20
I want to try and repeat this using my own data set.
Part 1: For instance, suppose I create a dataset that contains the longitude and latitude of cities :
set.seed(123)
data_1 = data.frame(id = c(1,2,3), long = rnorm(3, -74, 1 ), lat = rnorm(3, 40, 1 ))
data_2 = data.frame(id = c(4,5,6), long = rnorm(3, -78, 1 ), lat = rnorm(3, 42, 1 ))
final_data = rbind(data_1, data_2)
N <- nrow(final_data) # just for repeated convenience
final_data
# id long lat
# 1 1 -74.56048 40.07051
# 2 2 -74.23018 40.12929
# 3 3 -72.44129 41.71506
# 4 4 -77.53908 41.55434
# 5 5 -79.26506 43.22408
# 6 6 -78.68685 42.35981
Part 2: Then, I create a "distance matrix" that shows the distance between each pair of cities:
library(geoshpere)
dists <- outer(seq_len(N), seq_len(N), function(a,b) {
geosphere::distHaversine(final_data[a,2:3], final_data[b,2:3]) # Notes 1, 2
})
dists
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 0.00 28876.24 255554.4 300408.5 525566.9 429264.3
# [2,] 28876.24 0.00 231942.7 320616.0 541980.9 448013.6
# [3,] 255554.43 231942.67 0.0 424449.9 584761.5 521210.7
# [4,] 300408.47 320616.03 424449.9 0.0 233840.9 130640.9
# [5,] 525566.87 541980.93 584761.5 233840.9 0.0 107178.2
# [6,] 429264.34 448013.57 521210.7 130640.9 107178.2 0.0
Part 3: Finally, I tried to run the Genetic Algorithm on this problem:
GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = dists,
min = 1, max = attr(eurodist, "Size"), popSize = 50, maxiter = 5000,
run = 500, pmutation = 0.2)
My Problem: Unfortunately, this last line of code produces the following error:
Error in distMatrix[route] : subscript out of bounds
In addition: Warning messages:
1: In ga(type = "permutation", fitness = tspFitness, distMatrix = dists, :
'min' arg is deprecated. Use 'lower' instead.
2: In ga(type = "permutation", fitness = tspFitness, distMatrix = dists, :
Error in distMatrix[route] : subscript out of bounds
Can someone please show me what am I doing wrong and how can I fix this?
Thanks!