mies_aggregate_single_generation {miesmuschel}R Documentation

Aggregate a Value for a given Generation

Description

Applies a fitness_aggregator function to the values that were alive in the archive at a given generation. The function is supplied with the fitness values, and optionally other data, of all individuals that are alive at that point.

Usage

mies_aggregate_single_generation(
  archive,
  fitness_aggregator,
  generation = NA,
  include_previous_generations = FALSE
)

Arguments

archive

(Archive)
The archive over which to aggregate.

fitness_aggregator

(function)
Aggregation function, called with information about alive individuals of each generation. See details.

generation

(numeric(1))
Generation for which to aggregate the value. If include_previous_generations is FALSE, then an individual is considered to be alive at generation i if its dob is smaller or equal to i, and if its eol is either NA or greater than i. If include_previous_generations is TRUE, then all individuals with dob smaller or equal to i are considered. If this is NA, the currently alive (include_previous_generations FALSE) or all (include_previous_generations TRUE) individuals are aggregated. If multiple individuals considered "alive" with the same x_id are found, then only the last individual is used. This excludes previous individuals that were re-evaluated with a different fidelity.

include_previous_generations

(logical(1))
Aggregate all individuals that were alive at generation or at any point before that. Duplicates with the same x_id are removed, meaning that if an individual was re-evaluated with different fidelity, only the last re-evaluation is counted. However, note that individuals from different generations may still have been evaluated with different fidelity, so if Default FALSE.

Details

The fitness_aggregator function may have any of the following arguments, which will be given the following information when fitness_aggregator is called:

Not all of these arguments need to present, but at least one of fitnesses, objectives_unscaled, or xdt must be.

fitness_aggregator will never be called for an empty generation.

Value

The value returned by fitness_aggregator when applied to individuals alive at generation generation. If no individuals of the requested generation are present, fitness_aggregator is not called and mies_aggregate_single_generation() returns NULL instead.

See Also

Other aggregation methods: mies_aggregate_generations(), mies_get_generation_results()

Examples

library("bbotk")
lgr::threshold("warn")

objective <- ObjectiveRFun$new(
  fun = function(xs) {
    list(y1 = xs$x1, y2 = xs$x2)
  },
  domain = ps(x1 = p_dbl(0, 1), x2 = p_dbl(-1, 0)),
  codomain = ps(y1 = p_dbl(0, 1, tags = "maximize"),
    y2 = p_dbl(-1, 0, tags = "minimize"))
)

oi <- OptimInstanceMultiCrit$new(objective, terminator = trm("none"))

try(mies_aggregate_single_generation(oi$archive, identity), silent = TRUE)

mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses)


mies_init_population(oi, 2, budget_id = "x1", fidelity = .5)

oi$archive$data

mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses)

# Notice how fitnesses are positive, since x2 is scaled with -1.
# To get the original objective-values, use objectives_unscaled:
mies_aggregate_single_generation(oi$archive,
  function(objectives_unscaled) objectives_unscaled)

# When `...` is used, all information is passed:
mies_aggregate_single_generation(oi$archive, function(...) names(list(...)))

# Generation 10 is not present, but individuals with eol `NA` are still
# considered alive:
mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses,
  generation = 10)

# Re-evaluating points with higher "fidelity" (x1)
mies_step_fidelity(oi, budget_id = "x1", fidelity = 0.7)

oi$archive$data
# Lower-fidelity values are considered dead now, even for generation 1:
mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses,
  generation = 1)

# This adds two new alive individuals at generation 2.
# Also the individuals from gen 1 are reevaluated with fidelity 0.8
mies_evaluate_offspring(oi, offspring = data.frame(x2 = c(-0.1, -0.2)),
  budget_id = "x1", fidelity = 0.9, reevaluate_fidelity = 0.8)

oi$archive$data

mies_aggregate_single_generation(oi$archive, function(budget, ...) budget)

mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses,
  generation = 1)

mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses,
  generation = 2)

# No individuals were killed, but some were fidelity-reevaluated.
# These are not present with include_previous_generations:
mies_aggregate_single_generation(oi$archive, function(fitnesses) fitnesses,
  generation = 2, include_previous_generations = TRUE)

# Typical use-case: get dominated hypervolume
mies_aggregate_single_generation(oi$archive, function(fitnesses) domhv(fitnesses))

# Get generation-wise mean fitness values
mies_aggregate_single_generation(oi$archive, function(fitnesses) {
  apply(fitnesses, 2, mean)
})

[Package miesmuschel version 0.0.3 Index]