fstermann / mlr-mini

MIT License
0 stars 2 forks source link

Swap fields for active bindings #37

Open fstermann opened 1 year ago

fstermann commented 1 year ago

For the Hyperparameter class, the user might want to change the range after instantiating it, e.g.

a <- p_dbl(0, 1)
a$range <- c(5, 9)
#or
a$range[2] <- 99

which is currently possible, but also allows for

a$range <- c("a, "b")

which should not be allowed for a numeric Hyperparamter.

Idea

Turn the range field into an active binding, which is validated like this (e.g. for Double):

range.validator <- function(value) {
  if (!missing(value)) {
    checkmate::assertNumeric(unclass(value), len = 2, sorted = TRUE)
    self$.range <- structure(value, class=c("NumericRange", "Range"))
  }
  self$.range
}
# Can maybe be simplified with some `NumericRange` instantiator which does the check

# Note that 
a$range[2] <- 99
# passes the `value` to the binding like: c(a$range[1], 99).
# So the validator for numeric ranges should always check for a len of 2. 

Tasks