Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
normandabroad opened this issue Mar 1, 2020 · 2 comments
Closed

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

normandabroad opened this issue Mar 1, 2020 · 2 comments

Comments

@normandabroad
Copy link

normandabroad commented Mar 1, 2020

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
Copy link
Owner

luca-scr commented Mar 2, 2020

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().

@luca-scr luca-scr closed this as completed Mar 2, 2020
@normandabroad
Copy link
Author

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants