R/mies_methods.R
mies_generate_offspring.Rd
Generate new proposal individuals to be evaluated using mies_evaluate_offspring()
.
Parent individuals are selected using parent_selector
, then mutated using mutator
, and thend
recombined using recombinator
. If only a subset of these operations is desired, then
it is possible to set mutator
or recombinator
to the respective "null"-operators.
mies_generate_offspring(
inst,
lambda,
parent_selector = NULL,
mutator = NULL,
recombinator = NULL,
budget_id = NULL
)
(OptimInstance
)
Optimization instance to evaluate.
(integer(1)
)
Number of new individuals to generate. This is not necessarily the number with which parent_selector
gets called, because recombinator
could in principle need more than lambda
input individuals to
generate lambda
output individuals.
(Selector
| NULL
)Selector
operator that selects parent individuals depending on configuration values
and objective results. When parent_selector$operate()
is called, then objectives that
are being minimized are multiplied with -1 (through mies_get_fitnesses()
), since Selector
s always try to maximize fitness.
When this is NULL
(default), then a SelectorBest
us used.
The Selector
must be primed on a superset of inst$search_space
; this includes the "budget" component
when performing multi-fidelity optimization. All components on which selector
is primed on must occur in the archive.
The given Selector
may return duplicates.
(Mutator
| NULL
)Mutator
operation to apply to individuals selected out of inst
using parent_selector
.
The Mutator
must be primed on a ParamSet
similar to inst$search_space
,
but without the "budget" component when budget_id
is given (multi-fidelity optimization). Such a
ParamSet
can be generated for example using mies_prime_operators
.
When this is NULL
(default), then a MutatorNull
is used, effectively disabling mutation.
(Recombinator
| NULL
)Recombinator
operation to apply to individuals selected out of int
using parent_selector
after mutation using mutator
.
The Recombinator
must be primed on a ParamSet
similar to inst$search_space
,
but without the "budget" component when budget_id
is given (multi-fidelity optimization). Such a
ParamSet
can be generated for example using mies_prime_operators
.
When this is NULL
(default), then a RecombinatorNull
is used, effectively disabling recombination.
(character(1)
| NULL
)
Budget compnent when doing multi-fidelity optimization. This component of the search space is removed from
individuals sampled from the archive in inst
before giving it to mutator
and recombinator
.
Should be NULL
when not doing multi-fidelity.
data.table
: A table of configurations proposed as offspring to be evaluated
using mies_evaluate_offspring()
.
Other mies building blocks:
mies_evaluate_offspring()
,
mies_get_fitnesses()
,
mies_init_population()
,
mies_select_from_archive()
,
mies_step_fidelity()
,
mies_survival_comma()
,
mies_survival_plus()
set.seed(1)
library("bbotk")
lgr::threshold("warn")
# Define the objective to optimize
objective <- ObjectiveRFun$new(
fun = function(xs) {
z <- exp(-xs$x^2 - xs$y^2) + 2 * exp(-(2 - xs$x)^2 - (2 - xs$y)^2)
list(Obj = z)
},
domain = ps(x = p_dbl(-2, 4), y = p_dbl(-2, 4)),
codomain = ps(Obj = p_dbl(tags = "maximize"))
)
# Get a new OptimInstance
oi <- OptimInstanceSingleCrit$new(objective,
terminator = trm("evals", n_evals = 100)
)
# Demo operators
m = mut("gauss", sdev = 0.1)
r = rec("xounif")
s = sel("random")
# Operators must be primed
mies_prime_operators(objective$domain, list(m), list(r), list(s))
# We would normally call mies_init_population, but for reproducibility
# we are going to evaluate three given points
oi$eval_batch(data.table::data.table(x = 0:2, y = 2:0, dob = 1, eol = NA_real_))
# Evaluated points:
oi$archive
#> <Archive>
#> x y dob eol Obj timestamp batch_nr
#> <int> <int> <num> <num> <num> <POSc> <int>
#> 1: 0 2 1 NA 0.055 2024-05-13 04:41:06 1
#> 2: 1 1 1 NA 0.406 2024-05-13 04:41:06 1
#> 3: 2 0 1 NA 0.055 2024-05-13 04:41:06 1
# Use default operators: no mutation, no recombination, parent_selctor is
# sel("best") --> get one individual, the one with highest performance in the
# archive (x = 1, y = 1).
# (Note 'mies_generate_offspring()' does not modify 'oi')
mies_generate_offspring(oi, lambda = 1)
#> x y
#> <int> <int>
#> 1: 1 1
# Mutate the selected individual after selection. 'm' has 'sdev' set to 0.1,
# so the (x = 1, y = 1) is slightly permuted.
mies_generate_offspring(oi, lambda = 1, mutator = m)
#> x y
#> <num> <num>
#> 1: 1.197705 0.507719
# Recombination, then mutation.
# Even though lambda is 1, there will be two individuals selected with
# sel("best") and recombined, because rec("xounif") needs two parents. One
# of the crossover results is discarded (respecting that 'lambda' is 1),
# the other is mutated and returned.
mies_generate_offspring(oi, lambda = 1, mutator = m, recombinator = r)
#> x y
#> <num> <num>
#> 1: 2.458156 0.5205945
# General application: select, recombine, then mutate.
mies_generate_offspring(oi, lambda = 5, parent_selector = s, mutator = m, recombinator = r)
#> x y
#> <num> <num>
#> 1: 2.26140998 -0.7425231
#> 2: -0.13456073 2.2264374
#> 3: 0.08000182 1.4825137
#> 4: 0.96573594 2.3021648
#> 5: 2.65146162 1.5854277