luca-scr / GA

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

print current solution in monitor function #26

Closed maxrodkin closed 5 years ago

maxrodkin commented 5 years ago

hello! i`d liketo debug my ga. cancerning that, i tryed to print current solution in monitor function

monitor <- function(obj) 
{ 
  #gaMonitor(obj)
  browser()
  print(c("iteration =", obj@iter , "solution =" , obj@solution))
}

but obj@solution is empty

Called from: monitor(object)
Browse[1]> obj@solution
<0 x 0 matrix>

Call: ga(type = "real-valued", fitness = fitness, lower = rep(0, nrow(UsingInstr)), upper = rep(1, nrow(UsingInstr)), pcrossover = 0.05, maxiter = 50000, run = 100, names = files, suggestions = rep(1/nrow(UsingInstr), nrow(UsingInstr)), parallel = TRUE, monitor = monitor, seed = 1) How to print solution in iteration?

luca-scr commented 5 years ago

Please update the package with the current version available in GitHub. Then

library(GA)

f <- function(x)  (x^2+x)*cos(x) # -10 < x < 10

monitor <- function(obj) 
{ 
  cat(paste("iteration =", obj@iter , "solution =" , obj@bestSol[[obj@iter]]), "\n")
}

GA <- ga(type = "real-valued", fitness = f, lower = -10, upper = 10, 
         keepBest = TRUE, monitor = monitor)
maxrodkin commented 5 years ago

@luca-scr , thanks for support! unfortunatelly, i got unsuccessfull resuls. My actions:

> remove.packages("GA")
Removing package from ‘C:/Users/mrodkin/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
> devtools::install_github("luca-scr/GA", build_vignettes = TRUE)
Downloading GitHub repo luca-scr/GA@master
....
* DONE (GA)
In R CMD INSTALL
Warning messages:
1: In untar2(tarfile, files, list, exdir) :
  skipping pax global extended headers
2: In untar2(tarfile, files, list, exdir) :
  skipping pax global extended headers

After that, i ran this code, and obj@bestSol[[obj@iter]]) are still empty...

> GA <- ga(type = "real-valued", fitness = f, lower = -10, upper = 10, 
+          keepBest = TRUE, monitor = monitor)
iteration = 1 solution =  
iteration = 2 solution =  
iteration = 3 solution =

may be i miss something important in GA updating? Will glad with your answer. Respect, Maxim

luca-scr commented 5 years ago

Please try the following and check that your output matches:

> packageDescription("GA")$Version
[1] "3.2.1"
> packageDescription("GA")$Date
[1] "2019-05-29"
maxrodkin commented 5 years ago

@luca-scr , you was right! now i got desired monitoring

gokhale616 commented 4 years ago

@luca-scr - Extending the following thread. Is it possible to store the population at every evolutionary iteration rather than have it printed? I am thinking of a user defined slot in ga to store a list of population matrices from first to last iteration?