mlr3mbo
mlr3mbo copied to clipboard
Evaluating an initial design while providing a callback causes errors from unrelated callback
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
````R
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).
@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?
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)
}
)
Sounds like this could be documented better and the error message improved :)