mlr3mbo

Package website: release | dev

r-cmd-check CRANstatus StackOverflow Mattermost

A new R6 and much more modular implementation for single- and multi-objective Bayesian Optimization.

Get Started

An overview and gentle introduction is given in this vignette.

Design

mlr3mbo is built modular relying on the following R6 classes:

Based on these, Bayesian Optimization loops can be written, see, e.g., bayesopt_ego for sequential single-objective BO.

mlr3mbo also provides an OptimizerMbo class behaving like any other Optimizer from the bbotk package as well as a TunerMbo class behaving like any other Tuner from the mlr3tuning package.

mlr3mbo uses sensible defaults for the Surrogate, AcqFunction, AcqOptimizer, and even the loop_function. See ?mbo_defaults for more details.

Simple Optimization Example

Minimize f(x) = x^2 via sequential single-objective BO using a GP as surrogate and EI optimized via random search as acquisition function:

library(bbotk)
library(mlr3mbo)
library(mlr3learners)
set.seed(1)

obfun = ObjectiveRFun$new(
  fun = function(xs) list(y1 = xs$x ^ 2),
  domain = ps(x = p_dbl(lower = -10, upper = 10)),
  codomain = ps(y1 = p_dbl(tags = "minimize")))

instance = OptimInstanceSingleCrit$new(
  objective = obfun,
  terminator = trm("evals", n_evals = 10))

surrogate = srlrn(lrn("regr.km", control = list(trace = FALSE)))
acqfun = acqf("ei")
acqopt = acqo(opt("random_search", batch_size = 100),
  terminator = trm("evals", n_evals = 100))

optimizer = opt("mbo",
  loop_function = bayesopt_ego,
  surrogate = surrogate,
  acq_function = acqfun,
  acq_optimizer = acqopt)

optimizer$optimize(instance)
##             x  x_domain          y1
## 1: 0.03897209 <list[1]> 0.001518824

Note that you can also use bb_optimize as a shorthand:

library(bbotk)
library(mlr3mbo)
library(mlr3learners)
set.seed(1)

fun = function(xs) list(y1 = xs$x ^ 2)

surrogate = srlrn(lrn("regr.km", control = list(trace = FALSE)))
acqfun = acqf("ei")
acqopt = acqo(opt("random_search", batch_size = 100),
  terminator = trm("evals", n_evals = 100))

optimizer = opt("mbo",
  loop_function = bayesopt_ego,
  surrogate = surrogate,
  acq_function = acqfun,
  acq_optimizer = acqopt)

result = bb_optimize(
  fun,
  method = optimizer,
  lower = c(x = -10),
  upper = c(x = 10),
  max_evals = 10)

Simple Tuning Example

library(mlr3)
library(mlr3learners)
library(mlr3tuning)
library(mlr3mbo)
set.seed(1)

task = tsk("pima")

learner = lrn("classif.rpart", cp = to_tune(lower = 1e-04, upper = 1, logscale = TRUE))

instance = tune(
  tuner = tnr("mbo"),
  task = task,
  learner = learner,
  resampling = rsmp("holdout"),
  measure = msr("classif.ce"),
  term_evals = 10)

instance$result
##           cp learner_param_vals  x_domain classif.ce
## 1: -4.594102          <list[2]> <list[1]>  0.2109375