Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
larskotthoff opened this issue Feb 7, 2024 · 3 comments

Comments

@larskotthoff
Copy link
Member

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).

@sumny
Copy link
Member

sumny commented Feb 12, 2024

@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
Copy link
Member

be-marc commented Feb 12, 2024

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
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants