Package website: release | dev
bbotk is a black-box optimization framework for R. It features highly configurable search spaces via the paradox package and optimizes every user-defined objective function. The package includes several optimization algorithms e.g. Random Search, Iterated Racing, Bayesian Optimization (in mlr3mbo) and Hyperband (in mlr3hyperband). bbotk is the base package of mlr3tuning, mlr3fselect and miesmuschel.
The package includes the basic building blocks of optimization:
Optimizer
: Objects of this class allow you to optimize
an object of the class OptimInstance
.OptimInstance
: Defines the optimization problem,
consisting of an Objective
, the search_space
,
and a Terminator
. All evaluations on the
OptimInstance
will be automatically stored in its own
Archive
.Objective
: Objects of this class contain the objective
function. The class ensures that the objective function is called in the
right way and defines, whether the function should be minimized or
maximized.Terminator
: Objects of this class control the
termination of the optimization independent of the optimizer.Install the last release from CRAN:
install.packages("bbotk")
Install the development version from GitHub:
::install_github("mlr-org/bbotk") remotes
# define the objective function
= function(xs) {
fun - (xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10
}
# set domain
= ps(
domain x1 = p_dbl(-10, 10),
x2 = p_dbl(-5, 5)
)
# set codomain
= ps(
codomain y = p_dbl(tags = "maximize")
)
# create Objective object
= ObjectiveRFun$new(
objective fun = fun,
domain = domain,
codomain = codomain,
properties = "deterministic"
)
# Define termination criterion
= trm("evals", n_evals = 10)
terminator
# create optimization instance
= OptimInstanceSingleCrit$new(
instance objective = objective,
terminator = terminator
)
# load optimizer
= opt("gensa")
optimizer
# trigger optimization
$optimize(instance) optimizer
## x1 x2 x_domain y
## 1: 2.0452 -2.064743 <list[2]> 9.123252
# best performing configuration
$result instance
## x1 x2 x_domain y
## 1: 2.0452 -2.064743 <list[2]> 9.123252
# all evaluated configuration
as.data.table(instance$archive)
## x1 x2 y timestamp batch_nr x_domain_x1 x_domain_x2
## 1: -4.689827 -1.278761 -37.716445 2022-11-18 11:17:17 1 -4.689827 -1.278761
## 2: -5.930364 -4.400474 -54.851999 2022-11-18 11:17:17 2 -5.930364 -4.400474
## 3: 7.170817 -1.519948 -18.927907 2022-11-18 11:17:17 3 7.170817 -1.519948
## 4: 2.045200 -1.519948 7.807403 2022-11-18 11:17:17 4 2.045200 -1.519948
## 5: 2.045200 -2.064742 9.123250 2022-11-18 11:17:17 5 2.045200 -2.064742
## 6: 2.045200 -2.064742 9.123250 2022-11-18 11:17:17 6 2.045200 -2.064742
## 7: 2.045201 -2.064742 9.123250 2022-11-18 11:17:17 7 2.045201 -2.064742
## 8: 2.045199 -2.064742 9.123250 2022-11-18 11:17:17 8 2.045199 -2.064742
## 9: 2.045200 -2.064741 9.123248 2022-11-18 11:17:17 9 2.045200 -2.064741
## 10: 2.045200 -2.064743 9.123252 2022-11-18 11:17:17 10 2.045200 -2.064743
bb_optimize
library(bbotk)
# define the objective function
= function(xs) {
fun c(y1 = - (xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10)
}
# optimize function with random search
= bb_optimize(fun, method = "random_search", lower = c(-10, -5), upper = c(10, 5),
result max_evals = 100)
# optimized parameters
$par result
## x1 x2
## 1: -7.982537 4.273021
# optimal outcome
$value result
## y1
## -142.5479