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)
)
mies_init_population(inst = oi, mu = 3)
# Initial state:
oi$archive
# 'offspring' is just a data.frame of values to evaluate.
# In general it should be created using 'mies_generate_offspring()'.
offspring = data.frame(x = 1:2, y = 2:1)
mies_evaluate_offspring(oi, offspring = offspring)
# This evaluated the given points and assigned them 'dob' 2.
oi$archive
# Note that at this point one would ordinarily call a 'mies_survival_*()'
# function.
###
# Advanced demo, making use of additional components and doing multi-fidelity
##
# declare 'y' the budget parameter. It will not be in the 'offspring'
# table any more.
budget_id = "y"
# but: offspring may contain any other value that is appended to 'oi'. These
# are ignored by the objective.
offspring = data.frame(x = 0:1, z = 3)
mies_evaluate_offspring(oi, offspring = offspring, budget_id = budget_id,
fidelity = 1)
# This now has the additional column 'z'. Values of y for the new evaluations
# are 1.
oi$archive
offspring = data.frame(x = 2, z = 3)
# Increasing the fidelity will not cause re-evaluation of existing individuals
# when `reevaluate_fidelity` is not given.
mies_evaluate_offspring(oi, offspring = offspring, budget_id = budget_id,
fidelity = 2)
oi$archive
offspring = data.frame(x = 3, z = 3)
# Depending on the effect of fidelity, this may however have a biasing effect,
# so it may be desirable to re-evaluate surviving individuals from previous
# generations. The 'reevaluate_fidelity' may even be different from 'fidelity'
mies_evaluate_offspring(oi, offspring = offspring, budget_id = budget_id,
fidelity = 3, reevaluate_fidelity = 2)
# In this example, only individuals with 'y = 1' were re-evaluated, since
# 'fidelity_monotonic' is TRUE.
oi$archive
Run the code above in your browser using DataLab