title: Vehicle routing using genetic algorithm
body: <p>I've the following problem: </p>
  
  <p><strong>1 vehicle to collect the maximum profit of the 7 parking meters</strong></p>
  
  <p><strong>The profit of each parking meter are fixed in a vector</strong></p>
  
  <pre><code>profit&lt;-c(0,249,289,381,325,338,216,757)
  </code></pre>
  
  <p>First value means the deport, the vehicle starts from deport and need to finish there.</p>
  
  <p>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.</p>
  
  <p>Here is the distance matrix:</p>
  
  <pre><code>row_names&lt;-c(0,1,2,3,4,5,6,7)
  col_names&lt;-c(0,1,2,3,4,5,6,7)
  d_max&lt;-120 #maximum distance available for the vehicle
  
  Dist&lt;-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)&lt;- list(row_names,col_names)
  </code></pre>
  
  <p>And I've been trying to solve it with a generic algorithm, using the Library(GA) on R.</p>
  
  <p>Here's the rest of my code that I build from some posts that I found here:</p>
  
  <pre><code>#Function to calculate tour length 
  
  tourLength &lt;- function(tour, distMatrix) {
    tour &lt;- c(tour, tour[1])
    route &lt;- embed(tour, 2)[,2:1]
    sum(distMatrix[route])&lt;=120    #maximum distance available 
  }
  
  
  #function to maximize
  func &lt;- function(tour, ...) profit*tourLength(tour, ...)   
  
  That's my fitness function, I guess isn't appropriated. Any idea? 
  
  GA &lt;- 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)
  </code></pre>
  
  <p>I've get the following output:</p>
  
  <p>This is the output. By the value of fitness function, I'm having doughts if it's correct. I've some warnings too...</p>
  
  <pre><code>&gt; 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
  </code></pre>
  
  <p>Could someone help me? please! Perhaps it's better to try a permutation instead of a binary type?</p>
  
  <p>I've an alternative option that is to get a solution by <em>simulated annealing</em>. It's a better way to do it?</p>
  
  <p>Please, try to take a view!
  Thanks to all!</p>