luca-scr / GA

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

gaperm_oxCrossover_Rcpp(object, parents) : index error on apple M1 #52

Closed cpanse closed 2 years ago

cpanse commented 3 years ago

Dear Luca;

I am happy that you provide the GA package.

CRAN reports an index error for my recmap package using GA. Please see: https://www.stats.ox.ac.uk/pub/bdr/M1mac/recmap.out

I also ran into the same error executing the following code snippet r recmap::recmapGA(recmap::checkerboard(4)) on my brand-new apple M1 device.

0 cp@leda:~ % R                                                                                                               21-05-28|9:56:04

R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: aarch64-apple-darwin20 (64-bit)

R ist freie Software und kommt OHNE JEGLICHE GARANTIE.
Sie sind eingeladen, es unter bestimmten Bedingungen weiter zu verbreiten.
Tippen Sie 'license()' or 'licence()' für Details dazu.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Tippen Sie 'demo()' für einige Demos, 'help()' für on-line Hilfe, oder
'help.start()' für eine HTML Browserschnittstelle zur Hilfe.
Tippen Sie 'q()', um R zu verlassen.

R> recmap::recmapGA(recmap::checkerboard(4))
GA | iter = 1 | Mean = 0.3971563 | Best = 0.9981481
Fehler in gaperm_oxCrossover_Rcpp(object, parents) : index error
Zusätzlich: Es gab 28 Warnungen (Anzeige mit warnings())
R> 
R> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.4

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] C/UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] recmap_1.0.9     sp_1.4-5         Rcpp_1.0.6      
[4] GA_3.2.1         iterators_1.0.13 foreach_1.5.1   

loaded via a namespace (and not attached):
[1] compiler_4.1.0   cli_2.5.0        crayon_1.4.1    
[4] codetools_0.2-18 grid_4.1.0       lattice_0.20-44 
R> 

At the moment it is not urgent but it would be nice to investigate the reason.

Thanks and best wishes,

Christian

luca-scr commented 3 years ago

Hi Christian, I replicated your short R code

recmap::recmapGA(recmap::checkerboard(4))

without any issues on my Mac (no M1). Perhaps this is an issue with C++ code via Rcpp.

cpanse commented 3 years ago

Luca; it is definitely an issue on the M1 only. C

dkibalnikov commented 2 years ago

I experience the same issue on Apple M1 .
Code:

library(igraph)
library(GA)
# TSP problem example this is the data of 21 europian cities
data("eurodist", package = "datasets")
D <- as.matrix(eurodist)

# given a tour, calculate the total distance
tourLength <- function(tour, distMatrix) {
  tour <- c(tour, tour[1])
  route <- embed(tour, 2)
  sum(distMatrix[route])
}
# inverse of thetotal distance is the fitness
tpsFitness <- function(tour, ...) 1/tourLength(tour, ...)

undebug(tourLength)
# run a GA algorithm
GA.fit <- ga(type = "permutation", fitness = tpsFitness, distMatrix = D, lower = 1, 
             upper = 21, popSize = 100, maxiter = 100, run = 10, pmutation = 0.2)

returns error:

GA | iter = 1 | Mean = 3.196610e-05 | Best = 3.812283e-05
Error in gaperm_oxCrossover_Rcpp(object, parents) : index error

Session info:

R version 4.1.1 (2021-08-10)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.6

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] igraph_1.2.6     GA_3.2.2         iterators_1.0.13 foreach_1.5.1   

loaded via a namespace (and not attached):
[1] compiler_4.1.1   magrittr_2.0.1   cli_3.0.1        tools_4.1.1      crayon_1.4.1     Rcpp_1.0.7       codetools_0.2-18 pkgconfig_2.0.3 

The same code works well on linux

dkibalnikov commented 2 years ago

some update regarding my example above, using setting:

gaControl("useRcpp" = FALSE)

works fine

luca-scr commented 2 years ago

Yes of course, this option forces the use of standard R function for the genetic operators. Not using the Rcpp versions you paid a bit in terms of speed but not too much in my experience.