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)
)
mies_init_population(inst = oi, mu = 3)
oi$archive
mies_get_fitnesses(oi, c(2, 3))
###
# Multi-objective, and automatic maximization:
objective2 <- ObjectiveRFun$new(
fun = function(xs) list(Obj1 = xs$x^2, Obj2 = -xs$y^2),
domain = ps(x = p_dbl(-2, 4), y = p_dbl(-2, 4)),
codomain = ps(
Obj1 = p_dbl(tags = "minimize"),
Obj2 = p_dbl(tags = "maximize")
)
)
# Using MultiCrit!
oi <- OptimInstanceMultiCrit$new(objective2,
terminator = trm("evals", n_evals = 100)
)
mies_init_population(inst = oi, mu = 3)
oi$archive
# Note Obj1 has a different sign than in the archive.
mies_get_fitnesses(oi, c(2, 3))
Run the code above in your browser using DataLab