I've the following problem:
1 vehicle to collect the maximum profit of the 7 parking meters
The profit of each parking meter are fixed in a vector
profit<-c(0,249,289,381,325,338,216,757)
First value means the deport, the vehicle starts from deport and need to finish there.
My objective is to maximize the profit respecting the constraint of maximum distance available. It's not required to pass for all the parking meters.
Here is the distance matrix:
row_names<-c(0,1,2,3,4,5,6,7)
col_names<-c(0,1,2,3,4,5,6,7)
d_max<-120 #maximum distance available for the vehicle
Dist<-matrix(c(0,17,19,50,64,33,57,97,
+ 15,0,2,43,72,20,64,92,
+ 20,2,0,42,74,18,70,89,
+ 53,43,42,0,63,28,67,51,
+ 64,72,74,63,0,76,18,68,
+ 36,20,18,28,74,0,76,79,
+ 57,68,72,70,18,76,0,84,
+ 92,92,92,51,68,80,84,0),nrow=8, byrow=T)
dimnames(Dist)<- list(row_names,col_names)
And I've been trying to solve it with a generic algorithm, using the Library(GA) on R.
Here's the rest of my code that I build from some posts that I found here:
#Function to calculate tour length
tourLength <- function(tour, distMatrix) {
tour <- c(tour, tour[1])
route <- embed(tour, 2)[,2:1]
sum(distMatrix[route])<=120 #maximum distance available
}
#function to maximize
func <- function(tour, ...) profit*tourLength(tour, ...)
That's my fitness function, I guess isn't appropriated. Any idea?
GA <- ga(type = "binary", fitness = func, distMatrix = Dist,
min = 1, max = sqrt(length(Dist)), popSize = 50, maxiter = 5000,
run = 500, pmutation = 0.2, nBits=8)
summary(GA)
I've get the following output:
This is the output. By the value of fitness function, I'm having doughts if it's correct. I've some warnings too...
> summary(GA)
+-----------------------------------+
| Genetic Algorithm |
+-----------------------------------+
GA settings:
Type = binary
Population size = 50
Number of generations = 5000
Elitism = 2
Crossover probability = 0.8
Mutation probability = 0.2
GA results:
Iterations = 500
Fitness function value = 0
Solutions =
x1 x2 x3 x4 x5 x6 x7 x8
[1,] 0 1 0 1 0 0 1 0
[2,] 0 1 1 1 0 1 0 0
[3,] 1 0 1 1 0 1 1 0
[4,] 0 1 1 1 0 1 1 1
[5,] 1 1 0 1 0 1 1 0
[6,] 1 0 1 0 1 0 1 0
[7,] 0 1 1 1 1 1 0 1
[8,] 0 0 1 0 1 1 0 1
[9,] 1 1 1 1 0 0 1 1
[10,] 1 0 1 0 1 1 0 1
...
[43,] 1 1 1 1 0 0 1 0
Could someone help me? please! Perhaps it's better to try a permutation instead of a binary type?
I've an alternative option that is to get a solution by simulated annealing. It's a better way to do it?
Please, try to take a view! Thanks to all!