Home > Software design >  How can I get solutions and fitness values at each iteration with the GA: Genetic Algorithms package
How can I get solutions and fitness values at each iteration with the GA: Genetic Algorithms package

Time:10-03

I am using the GA: Genetic Algorithms package (https://cran.r-project.org/web/packages/GA/index.html) and have the following ga object :

obj <- GA::ga(
  type = "binary",
  nBits = N,
  run = 300,
  maxiter = 5000,
  popSize = popSize,
  fitness = fitness,
  m = m,
  size = size,
  suggestions = initialPopulation(popSize, N, size),
  mutation = mutation,
  crossover = sizeCrossover(size)
)

I can get the solution for the last iteration with :

obj@solution

and the fitness with

obj@fitnessValue

I also know I can easily plot the fitness for every iteration using

plot(obj)

But how can I retrieve the best solutions at each iteration (and not only the last one) ?

CodePudding user response:

Here is a way. The code below is an adaptation from the 2nd example in help("ga").
Write a monitor function to be called at each iteration. The monitor will update the index into a best solutions list and assign the current best to the current list member. Assignments are done with <<-, because the object best_solutions is defined outside the function and is not destroyed when returning from it.

library(GA)
#> Loading required package: foreach
#> Loading required package: iterators
#> Package 'GA' version 3.2.2
#> Type 'citation("GA")' for citing this R package in publications.
#> 
#> Attaching package: 'GA'
#> The following object is masked from 'package:utils':
#> 
#>     de

# 2) one-dimensional function
f <- function(x)  (x^2 x)*cos(x) # -10 < x < 10
curve(f, -10, 10)


monitor <- function(obj) {
  i_best <<- i_best   1L
  best_solutions[[i_best]] <<- obj@bestSol[[1]]
}

niter <- 100L
best_solutions <- vector("list", length = niter)
i_best <- 0L

GA <- ga(type = "real-valued", 
         fitness = f, 
         lower = -10, upper = 10, 
         maxiter = niter,    # better to explicitly set it
         keepBest = TRUE,    # here, to have bestSol available
         monitor = monitor)  # call the function above
summary(GA)
#> ── Genetic Algorithm ─────────────────── 
#> 
#> GA settings: 
#> Type                  =  real-valued 
#> Population size       =  50 
#> Number of generations =  100 
#> Elitism               =  2 
#> Crossover probability =  0.8 
#> Mutation probability  =  0.1 
#> Search domain = 
#>        x1
#> lower -10
#> upper  10
#> 
#> GA results: 
#> Iterations             = 100 
#> Fitness function value = 47.70562 
#> Solution = 
#>           x1
#> [1,] 6.56054

head(best_solutions)
#> [[1]]
#>          [,1]
#> [1,] 6.426683
#> 
#> [[2]]
#>          [,1]
#> [1,] 6.426683
#> 
#> [[3]]
#>          [,1]
#> [1,] 6.426683
#> 
#> [[4]]
#>          [,1]
#> [1,] 6.426683
#> 
#> [[5]]
#>          [,1]
#> [1,] 6.426683
#> 
#> [[6]]
#>          [,1]
#> [1,] 6.426683

Created on 2022-10-02 with reprex v2.0.2

  •  Tags:  
  • r
  • Related