Open swaheera opened 2 years ago
The code you provided works on my machine:
> res <- ga(
type = "permutation",
fitness = fitness,
distMatrix = D_perm,
lower = 1,
upper = nrow(D_perm),
mutation = mutation(nrow(D_perm), fixed_points),
crossover = gaperm_pmxCrossover,
suggestions = feasiblePopulation(nrow(D_perm), popSize, fixed_points),
popSize = popSize,
maxiter = 5000,
run = 500,
pmutation = 0.2
)
> summary(res)
── Genetic Algorithm ───────────────────
GA settings:
Type = permutation
Population size = 100
Number of generations = 5000
Elitism = 5
Crossover probability = 0.8
Mutation probability = 0.2
Suggestions =
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 ... x19 x20
1 1 15 14 3 16 9 13 17 2 7 8 11
2 1 2 9 12 16 10 14 7 17 5 18 15
3 1 12 8 18 14 10 11 13 7 19 9 4
4 1 15 18 7 9 17 16 19 4 2 12 10
5 1 5 18 10 11 3 9 7 19 15 4 13
6 1 8 18 17 3 19 2 20 14 4 15 9
7 1 11 3 7 20 5 17 9 8 13 4 6
8 1 14 11 13 9 4 20 16 7 8 15 3
9 1 2 19 17 11 14 4 3 6 15 12 7
10 1 15 2 5 13 9 3 12 19 8 4 6
...
99 1 8 4 16 7 20 13 12 19 6 15 18
100 1 18 16 6 14 3 12 19 13 5 8 9
GA results:
Iterations = 1042
Fitness function value = 0.0000006683507
Solution =
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 ... x19 x20
[1,] 1 9 2 15 10 6 19 14 7 17 3 5
> colnames(D_perm)[res@solution[1,]]
[1] "Location 1" "Location 6" "Location 2" "Location 19" "Location 3" "Location 20" "Location 17"
[8] "Location 15" "Location 14" "Location 8" "Location 10" "Location 18" "Location 5" "Location 16"
[15] "Location 12" "Location 7" "Location 4" "Location 9" "Location 11" "Location 13"
Suppose I have 20 cities and the Longitude/Latitude for each of these cities :
I can also make a distance matrix for these cities that contains the distance between each pair of cities:
In the end, I would like to use the above matrix as input for a customized Travelling Salesman Problem (R: Customizing the Travelling Salesman Problem) - e.g. Try to find the optimal path when you are forced to start at "city 4" and the third city should be "city 5":
This results in the following error:
How can I fix this?
Thanks!