luca-scr / GA

An R package for optimization using genetic algorithms
http://luca-scr.github.io/GA/
91 stars 29 forks source link

Upper value must be greater than lower value - Error #38

Closed normandabroad closed 4 years ago

normandabroad commented 4 years ago

Hi,

First of all, thank you for the package. I am trying to learn how to use it properly and perhaps my doubt is a simple one.

Considering the code below:

# https://rpubs.com/karthy1988/TSP_GA

library(GA)
data("eurodist", package = "datasets")
D <- as.matrix(eurodist)

#Function to calculate tour length

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,
         lower = 1, upper = attr(eurodist, "Size"), popSize = 50, maxiter = 5000,
         run = 500, pmutation = 0.2)

show(summary(GA))

It runs smoothly. But if I would change the upper parameter lower than 4, I get an error.


> GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
+          lower = 1, upper = 3, popSize = 50, maxiter = 5000,
+          run = 500, pmutation = 0.2)
GA | iter = 1 | Mean = 0.0001316829 | Best = 0.0001316829
Error in gaperm_oxCrossover_Rcpp(object, parents) : 
  Sample size must be <= n when not using replacement!

But my problem is happening with a slight modified version of that program.

tourLength <- function(tour, distMatrix) {
  tour <- c(1, tour + 1) # I would like the tour to start at site 1 and not return to it at the end
  route <- embed(tour, 2)[, 2:1]
  sum(distMatrix[route])
}

GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
         lower = 1, upper = 2, popSize = 50, maxiter = 5000,
         run = 500, pmutation = 0.2)
> GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
+          lower = 1, upper = 2, popSize = 50, maxiter = 5000,
+          run = 500, pmutation = 0.2)
GA | iter = 1 | Mean = 0.0002247632 | Best = 0.0002335903
Error in gaperm_oxCrossover_Rcpp(object, parents) : 
  upper value must be greater than lower value

OR

tourLength <- function(tour, distMatrix) {
  tour <- c(1, tour) # I would like the tour to start at site 1 and not return to it at the end
  route <- embed(tour, 2)[, 2:1]
  sum(distMatrix[route])
}

GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
         lower = 2, upper = 3, popSize = 50, maxiter = 5000,
         run = 500, pmutation = 0.2)
> GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
+          lower = 2, upper = 3, popSize = 50, maxiter = 5000,
+          run = 500, pmutation = 0.2)
GA | iter = 1 | Mean = 0.0002244101 | Best = 0.0002335903
Error in gaperm_oxCrossover_Rcpp(object, parents) : 
  upper value must be greater than lower value

But both codes work if the difference between lower and upper bounds are higher than 2. If the difference is 1, the error mentioned above. If the difference is 2, the error is:

> GA2 <- ga(type = "permutation", fitness = tspFitness, distMatrix = D,
+          lower = 2, upper = 4, popSize = 50, maxiter = 5000,
+          run = 500, pmutation = 0.2)
GA | iter = 1 | Mean = 0.0002015671 | Best = 0.0002225684
Error in gaperm_oxCrossover_Rcpp(object, parents) : 
  Sample size must be <= n when not using replacement!

I would appreciate any help you could provide.

Best Regards, R

luca-scr commented 4 years ago

You cannot use the crossover genetic operator gaperm_oxCrossover() (the default one for permutation searches) with upper <=3. But you can use one of the alternative, such as gaperm_cxCrossover() or gaperm_pmxCrossover()or gaperm_pbxCrossover().

normandabroad commented 4 years ago

Thank you.