Skip to content

mlr-org/bbotk

Repository files navigation

bbotk - Black-Box Optimization Toolkit

Package website: release | dev

r-cmd-check CRAN Status Badge Mattermost

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, Grid Search, Iterated Racing, Bayesian Optimization (in mlr3mbo) and Hyperband (in mlr3hyperband). bbotk is the base package of mlr3tuning, mlr3fselect and miesmuschel.

Resources

There are several sections about black-box optimization in the mlr3book. Often the sections about tuning are also relevant for general black-box optimization.

Installation

Install the latest release from CRAN.

install.packages("bbotk")

Install the development version from GitHub.

pak::pkg_install("mlr-org/bbotk")

Example

# define the objective function
fun = function(xs) {
  - (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 = 20)
)

# load optimizer
optimizer = opt("gensa")

# trigger optimization
optimizer$optimize(instance)
##    x1 x2  x_domain  y
## 1:  2 -3 <list[2]> 10
# best performing configuration
instance$result
##    x1 x2  x_domain  y
## 1:  2 -3 <list[2]> 10
# 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 2024-08-13 17:52:54        1   -4.689827   -1.278761
##  2: -5.930364 -4.400474 -54.851999 2024-08-13 17:52:54        2   -5.930364   -4.400474
##  3:  7.170817 -1.519948 -18.927907 2024-08-13 17:52:54        3    7.170817   -1.519948
##  4:  2.045200 -1.519948   7.807403 2024-08-13 17:52:54        4    2.045200   -1.519948
##  5:  2.045200 -2.064742   9.123250 2024-08-13 17:52:54        5    2.045200   -2.064742
## ---                                                                                    
## 16:  2.000000 -3.000000  10.000000 2024-08-13 17:52:54       16    2.000000   -3.000000
## 17:  2.000001 -3.000000  10.000000 2024-08-13 17:52:54       17    2.000001   -3.000000
## 18:  1.999999 -3.000000  10.000000 2024-08-13 17:52:54       18    1.999999   -3.000000
## 19:  2.000000 -2.999999  10.000000 2024-08-13 17:52:54       19    2.000000   -2.999999
## 20:  2.000000 -3.000001  10.000000 2024-08-13 17:52:54       20    2.000000   -3.000001