mlr-org / mlr3mbo

Flexible Bayesian Optimization in R
https://mlr3mbo.mlr-org.com
25 stars 1 forks source link

Evaluating an initial design while providing a callback causes errors from unrelated callback #141

Open larskotthoff opened 9 months ago

larskotthoff commented 9 months ago
require(data.table)
require(mlr3mbo)
require(bbotk)

callback_plot = callback_optimization("plot",
  on_optimizer_after_eval = function(callback, context) {
  }
)

obfun = ObjectiveRFun$new(
  fun = function(xs) 2 * xs$x * sin(14 * xs$x),
  domain = ps(x = p_dbl(lower = 0, upper = 1)),
  codomain = ps(y = p_dbl(tags = "minimize")))

instance = OptimInstanceSingleCrit$new(
  objective = obfun,
  terminator = trm("evals", n_evals = 20),
  callbacks = callback_plot)
initial_design = data.table(x = c(0, 0.5, 1))
instance$eval_batch(initial_design)

gives me

Error in call_back("on_optimizer_before_eval", self$callbacks, private$.context) : 
  Assertion on 'context' failed: Must inherit from class 'Context', but has class 'NULL'.
Calls: <Anonymous> ... call_back -> assert_class -> makeAssertion -> mstop
Execution halted

Note that the callback specified is on_optimizer_after_eval and the error comes from on_optimizer_before_eval (the same error would probably come from the specified callback as well though).

sumny commented 9 months ago

@be-marc this looks like a more general issue with callbacks within an optim instance and evaluating points outside of optimize (probably the context just does not exist at this point and is NULL).

Do you have any ideas how to tackle this?

be-marc commented 9 months ago

The context is only available when optimizer$optimize(instance) is called because the context contains the optimizer and the instance. I already had the same problem and solved it with a callback :D

 callback_tuning("mlr3tuning.initial_design",
    label = "Initial Design Callback",
    on_optimization_begin = function(callback, context) {
      assert_data_table(callback$state$design)
      context$instance$eval_batch(callback$state$design)
    }
  )
larskotthoff commented 9 months ago

Sounds like this could be documented better and the error message improved :)