|
| 1 | +#' @title Acquisition Function Stochastic Confidence Bound |
| 2 | +#' |
| 3 | +#' @include AcqFunction.R |
| 4 | +#' @name mlr_acqfunctions_stochastic_cb |
| 5 | +#' |
| 6 | +#' @templateVar id stochastic_cb |
| 7 | +#' @template section_dictionary_acqfunctions |
| 8 | +#' |
| 9 | +#' @description |
| 10 | +#' Lower / Upper Confidence Bound with lambda sampling and decay. |
| 11 | +#' The initial \eqn{\lambda} is drawn from an uniform distribution between `min_lambda` and `max_lambda` or from an exponential distribution with rate `1 / lambda`. |
| 12 | +#' \eqn{\lambda} is updated after each update by the formula `lambda * exp(-rate * (t %% period))`, where `t` is the number of times the acquisition function has been updated. |
| 13 | +#' |
| 14 | +#' While this acquisition function usually would be used within an asynchronous optimizer, e.g., [OptimizerAsyncMbo], |
| 15 | +#' it can in principle also be used in synchronous optimizers, e.g., [OptimizerMbo]. |
| 16 | +#' |
| 17 | +#' @section Parameters: |
| 18 | +#' * `"lambda"` (`numeric(1)`)\cr |
| 19 | +#' \eqn{\lambda} value for sampling from the exponential distribution. |
| 20 | +#' Defaults to `1.96`. |
| 21 | +#' * `"min_lambda"` (`numeric(1)`)\cr |
| 22 | +#' Minimum value of \eqn{\lambda}for sampling from the uniform distribution. |
| 23 | +#' Defaults to `0.01`. |
| 24 | +#' * `"max_lambda"` (`numeric(1)`)\cr |
| 25 | +#' Maximum value of \eqn{\lambda} for sampling from the uniform distribution. |
| 26 | +#' Defaults to `10`. |
| 27 | +#' * `"distribution"` (`character(1)`)\cr |
| 28 | +#' Distribution to sample \eqn{\lambda} from. |
| 29 | +#' One of `c("uniform", "exponential")`. |
| 30 | +#' Defaults to `uniform`. |
| 31 | +#' * `"rate"` (`numeric(1)`)\cr |
| 32 | +#' Rate of the exponential decay. |
| 33 | +#' Defaults to `0` i.e. no decay. |
| 34 | +#' * `"period"` (`integer(1)`)\cr |
| 35 | +#' Period of the exponential decay. |
| 36 | +#' Defaults to `NULL`, i.e., the decay has no period. |
| 37 | +#' |
| 38 | +#' @section Note: |
| 39 | +#' * This acquisition function always also returns its current (`acq_lambda`) and original (`acq_lambda_0`) \eqn{\lambda}. |
| 40 | +#' These values will be logged into the [bbotk::ArchiveBatch] of the [bbotk::OptimInstanceBatch] of the [AcqOptimizer] and |
| 41 | +#' therefore also in the [bbotk::Archive] of the actual [bbotk::OptimInstance] that is to be optimized. |
| 42 | +#' |
| 43 | +#' @references |
| 44 | +#' * `r format_bib("snoek_2012")` |
| 45 | +#' * `r format_bib("egele_2023")` |
| 46 | +#' |
| 47 | +#' @family Acquisition Function |
| 48 | +#' @export |
| 49 | +#' @examples |
| 50 | +#' if (requireNamespace("mlr3learners") & |
| 51 | +#' requireNamespace("DiceKriging") & |
| 52 | +#' requireNamespace("rgenoud")) { |
| 53 | +#' library(bbotk) |
| 54 | +#' library(paradox) |
| 55 | +#' library(mlr3learners) |
| 56 | +#' library(data.table) |
| 57 | +#' |
| 58 | +#' fun = function(xs) { |
| 59 | +#' list(y = xs$x ^ 2) |
| 60 | +#' } |
| 61 | +#' domain = ps(x = p_dbl(lower = -10, upper = 10)) |
| 62 | +#' codomain = ps(y = p_dbl(tags = "minimize")) |
| 63 | +#' objective = ObjectiveRFun$new(fun = fun, domain = domain, codomain = codomain) |
| 64 | +#' |
| 65 | +#' instance = OptimInstanceBatchSingleCrit$new( |
| 66 | +#' objective = objective, |
| 67 | +#' terminator = trm("evals", n_evals = 5)) |
| 68 | +#' |
| 69 | +#' instance$eval_batch(data.table(x = c(-6, -5, 3, 9))) |
| 70 | +#' |
| 71 | +#' learner = default_gp() |
| 72 | +#' |
| 73 | +#' surrogate = srlrn(learner, archive = instance$archive) |
| 74 | +#' |
| 75 | +#' acq_function = acqf("stochastic_cb", surrogate = surrogate, lambda = 3) |
| 76 | +#' |
| 77 | +#' acq_function$surrogate$update() |
| 78 | +#' acq_function$update() |
| 79 | +#' acq_function$eval_dt(data.table(x = c(-1, 0, 1))) |
| 80 | +#' } |
| 81 | +AcqFunctionStochasticCB = R6Class("AcqFunctionStochasticCB", |
| 82 | + inherit = AcqFunction, |
| 83 | + |
| 84 | + public = list( |
| 85 | + |
| 86 | + #' @description |
| 87 | + #' Creates a new instance of this [R6][R6::R6Class] class. |
| 88 | + #' |
| 89 | + #' @param surrogate (`NULL` | [SurrogateLearner]). |
| 90 | + #' @param lambda (`numeric(1)`). |
| 91 | + #' @param min_lambda (`numeric(1)`). |
| 92 | + #' @param max_lambda (`numeric(1)`). |
| 93 | + #' @param distribution (`character(1)`). |
| 94 | + #' @param rate (`numeric(1)`). |
| 95 | + #' @param period (`NULL` | `integer(1)`). |
| 96 | + initialize = function( |
| 97 | + surrogate = NULL, |
| 98 | + lambda = 1.96, |
| 99 | + min_lambda = 0.01, |
| 100 | + max_lambda = 10, |
| 101 | + distribution = "uniform", |
| 102 | + rate = 0, |
| 103 | + period = NULL |
| 104 | + ) { |
| 105 | + assert_r6(surrogate, "SurrogateLearner", null.ok = TRUE) |
| 106 | + private$.lambda = assert_number(lambda, lower = .Machine$double.neg.eps, null.ok = TRUE) |
| 107 | + private$.min_lambda = assert_number(min_lambda, lower = .Machine$double.neg.eps, null.ok = TRUE) |
| 108 | + private$.max_lambda = assert_number(max_lambda, lower = .Machine$double.neg.eps, null.ok = TRUE) |
| 109 | + private$.distribution = assert_choice(distribution, choices = c("uniform", "exponential")) |
| 110 | + |
| 111 | + if (private$.distribution == "uniform" && (is.null(private$.min_lambda) || is.null(private$.max_lambda))) { |
| 112 | + stop('If `distribution` is "uniform", `min_lambda` and `max_lambda` must be set.') |
| 113 | + } |
| 114 | + |
| 115 | + if (private$.distribution == "exponential" && is.null(private$.lambda)) { |
| 116 | + stop('If `distribution` is "exponential", `lambda` must be set.') |
| 117 | + } |
| 118 | + |
| 119 | + private$.rate = assert_number(rate, lower = 0) |
| 120 | + private$.period = assert_int(period, lower = 1, null.ok = TRUE) |
| 121 | + |
| 122 | + constants = ps(lambda = p_dbl(lower = 0)) |
| 123 | + |
| 124 | + super$initialize("acq_cb", |
| 125 | + constants = constants, |
| 126 | + surrogate = surrogate, |
| 127 | + requires_predict_type_se = TRUE, |
| 128 | + direction = "same", |
| 129 | + label = "Stochastic Lower / Upper Confidence Bound", |
| 130 | + man = "mlr3mbo::mlr_acqfunctions_stochastic_cb") |
| 131 | + }, |
| 132 | + |
| 133 | + #' @description |
| 134 | + #' Update the acquisition function. |
| 135 | + #' Samples and decays lambda. |
| 136 | + update = function() { |
| 137 | + # sample lambda |
| 138 | + if (is.null(self$constants$values$lambda)) { |
| 139 | + |
| 140 | + if (private$.distribution == "uniform") { |
| 141 | + lambda = runif(1, private$.min_lambda, private$.max_lambda) |
| 142 | + } else { |
| 143 | + lambda = rexp(1, 1 / private$.lambda) |
| 144 | + } |
| 145 | + |
| 146 | + private$.lambda_0 = lambda |
| 147 | + self$constants$values$lambda = lambda |
| 148 | + } |
| 149 | + |
| 150 | + # decay lambda |
| 151 | + if (private$.rate > 0) { |
| 152 | + lambda_0 = private$.lambda_0 |
| 153 | + period = private$.period |
| 154 | + t = if (is.null(period)) private$.t else private$.t %% period |
| 155 | + rate = private$.rate |
| 156 | + |
| 157 | + self$constants$values$lambda = lambda_0 * exp(-rate * t) |
| 158 | + private$.t = t + 1L |
| 159 | + } |
| 160 | + }, |
| 161 | + |
| 162 | + #' @description |
| 163 | + #' Reset the acquisition function. |
| 164 | + #' Resets the private update counter `.t` used within the epsilon decay. |
| 165 | + reset = function() { |
| 166 | + private$.t = 0L |
| 167 | + } |
| 168 | + ), |
| 169 | + |
| 170 | + private = list( |
| 171 | + .lambda = NULL, |
| 172 | + .min_lambda = NULL, |
| 173 | + .max_lambda = NULL, |
| 174 | + .distribution = NULL, |
| 175 | + .rate = NULL, |
| 176 | + .period = NULL, |
| 177 | + .lambda_0 = NULL, |
| 178 | + .t = 0L, |
| 179 | + .fun = function(xdt, lambda) { |
| 180 | + p = self$surrogate$predict(xdt) |
| 181 | + cb = p$mean - self$surrogate_max_to_min * lambda * p$se |
| 182 | + data.table(acq_cb = cb, acq_lambda = lambda, acq_lambda_0 = private$.lambda_0) |
| 183 | + } |
| 184 | + ) |
| 185 | +) |
| 186 | + |
| 187 | +mlr_acqfunctions$add("stochastic_cb", AcqFunctionStochasticCB) |
| 188 | + |
0 commit comments