Evolutionary-Optimization-Laboratory / rmoo

An R package for multi/many-objective optimization with non-dominated genetic algorithms' family
GNU General Public License v2.0
29 stars 7 forks source link

how to print the progress of the algorithm? #8

Closed mytarmail closed 3 years ago

mytarmail commented 3 years ago

Hi, thanks for the great package, I have a question: how to print the progress of the algorithm? as in the "GA" package for example

> library(GA)
> f <- function(x)  (x^2+x)*cos(x)
> lbound <- -10; ubound <- 10
> GA <- ga(type = "real-valued", fitness = f, lower = c(th = lbound), upper = ubound)

GA | iter = 1 | Mean = -7.413866 | Best = 47.704785 GA | iter = 2 | Mean = 5.062077 | Best = 47.704785 GA | iter = 3 | Mean = 17.42209 | Best = 47.70479 GA | iter = 4 | Mean = 13.61758 | Best = 47.70523 GA | iter = 5 | Mean = 5.147661 | Best = 47.705230 GA | iter = 6 | Mean = 12.09697 | Best = 47.70523 GA | iter = 7 | Mean = 18.24898 | Best = 47.70523 GA | iter = 8 | Mean = 25.15846 | Best = 47.70561 GA | iter = 9 | Mean = 31.19939 | Best = 47.70561 GA | iter = 10 | Mean = 32.15691 | Best = 47.70561 GA | iter = 11 | Mean = 29.43683 | Best = 47.70561 GA | iter = 12 | Mean = 35.59874 | Best = 47.70561 GA | iter = 13 | Mean = 33.73205 | Best = 47.70561 ............... ........ ..

benitezfj commented 3 years ago

Thank you for using our package.

Regarding your question: The package rmoo dont print iteration by iteration the process of algorithm. Because the best results make up a matrix and printing them by iteration would pile up on the print screen. But the monitor argument and the nsgaMonitor function are in charge of making the interactive impressions of the package. You could create your own R function that takes the algorithm object as a parameter and print the values you like by iterates, such that:

print_iter <- function(object, number_objectives, ...) {
  #Extract the indices that form the first front
  first <- object@f[[1]]
  first_front_fit <- object@fitness[first, ]
  first_front_pop <- object@population[first, ]

  cat(paste("NSGA-III | iter =", object@iter, "\n"))
  cat(paste("First Front Fitness =", "\n"))
  print(first_front_fit)
  cat(paste("First Front Objective Value =", "\n"))
  print(first_front_pop)
  cat("\n")
}

#In addition to the other arguments necessary for the execution 
# of the algorithm, pass as a parameter the R function created
nsga3(...,
          monitor = print_iter)
mytarmail commented 3 years ago

Thank you! It's just convenient to quickly estimate how long it takes to iterate in the algorithm.