initLogger {ecr}R Documentation

Initialize a log object.


Logging is a central aspect of each EA. Besides the final solution(s) especially in research often we need to keep track of different aspects of the evolutionary process, e.g., fitness statistics. The logger of ecr keeps track of different user-defined statistics and the population. It may also be used to check stopping conditions (see makeECRTerminator). Most important this logger is used internally by the ecr black-box interface.


initLogger(control, log.stats = list(fitness = list("min", "mean", "max")),
  log.extras = NULL, log.pop = FALSE, init.size = 1000L)



Control object.


List of lists for statistic computation on attributes of the individuals of the population. Each entry should be named by the attribute it should be based on, e.g., fitness, and should contain a list of R functions as a character string or a a list with elements fun for the function, and pars for additional parameters which shall be passed to the corresponding function. Each function is required to return a scalar numeric value. By default the minimum, mean and maximum of the fitness values is computed. Since fitness statistics are the most important ones these do not have to be stored as attributes, but can be passed as a matrix to the update function.


Possibility to instruct the logger to store additional scalar values in each generation. Named character vector where the names indicate the value to store and the value indicates the corresponding data types. Currently we support all atomic modes of vector expect “factor” and “raw”.


Shall the entire population be saved in each generation? Default is FALSE.


Initial number of rows of the slot of the logger, where the fitness statistics are stored. The size of the statistics log is doubled each time an overflow occurs. Default is 1000.


[ecr_logger] An S3 object of class ecr_logger with the following components:


The log.stats list.


The log.pop parameter.


Initial size of the log.


The actual log. This is an R environment which ensures, that in-place modification is possible.


Statistics are logged in a data.frame.

See Also

Other logging: getPopulations, getStatistics, updateLogger


control = initECRControl(function(x) sum(x), minimize = TRUE,
  n.objectives = 1L)
control = registerECROperator(control, "mutate", mutBitflip, p = 0.1)
control = registerECROperator(control, "selectForMating", selTournament, k = 2)
control = registerECROperator(control, "selectForSurvival", selGreedy)

log = initLogger(control,
  log.stats = list(
    fitness = list("mean", "myRange" = function(x) max(x) - min(x)),
    age = list("min", "max")
  ), log.pop = TRUE, init.size = 1000L)

 # simply pass stuff down to control object constructor
population = initPopulation(mu = 10L, genBin, n.dim = 10L)
fitness = evaluateFitness(control, population)

# append fitness to individuals and init age
for (i in seq_along(population)) {
  attr(population[[i]], "fitness") = fitness[, i]
  attr(population[[i]], "age") = 1L

for (iter in seq_len(10)) {
  # generate offspring
  offspring = generateOffspring(control, population, fitness, lambda = 5)
  fitness.offspring = evaluateFitness(control, offspring)

  # update age of population
  for (i in seq_along(population)) {
    attr(population[[i]], "age") = attr(population[[i]], "age") + 1L

  # set offspring attributes
  for (i in seq_along(offspring)) {
    attr(offspring[[i]], "fitness") = fitness.offspring[, i]
    # update age
    attr(offspring[[i]], "age") = 1L

  sel = replaceMuPlusLambda(control, population, offspring)

  population = sel$population
  fitness = sel$fitness

  # do some logging
  updateLogger(log, population, n.evals = 5)

[Package ecr version 2.1.0 Index]