# example only runs if GenSA is available
if (mlr3misc::require_namespaces("GenSA", quietly = TRUE)) {
# define the objective function
fun = function(xs) {
list(y = - (xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10)
}
# set domain
domain = ps(
x1 = p_dbl(-10, 10),
x2 = p_dbl(-5, 5)
)
# set codomain
codomain = ps(
y = p_dbl(tags = "maximize")
)
# create objective
objective = ObjectiveRFun$new(
fun = fun,
domain = domain,
codomain = codomain,
properties = "deterministic"
)
# initialize instance
instance = oi(
objective = objective,
terminator = trm("evals", n_evals = 10)
)
# load optimizer
optimizer = opt("chain",
optimizers = list(opt("random_search"), opt("grid_search")),
terminators = list(trm("evals", n_evals = 5), trm("evals", n_evals = 5))
)
# trigger optimization
optimizer$optimize(instance)
# all evaluated configurations
instance$archive
# best performing configuration
instance$result
}
Run the code above in your browser using DataLab