Skip to content

Commit a79d863

Browse files
committed
refactor: minor refactor changes to proposed async extensions
1 parent a446321 commit a79d863

27 files changed

+977
-399
lines changed

R/AcqFunction.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ AcqFunction = R6Class("AcqFunction",
7373
# FIXME: at some point we may want to make this an AB to a private$.update
7474
},
7575

76+
#' @description
77+
#' Reset the acquisition function.
78+
#'
79+
#' Can be implemented by subclasses.
80+
reset = function() {
81+
# FIXME: at some point we may want to make this an AB to a private$.reset
82+
},
83+
7684
#' @description
7785
#' Evaluates multiple input values on the objective function.
7886
#'

R/AcqFunctionMulti.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#' If acquisition functions have not been initialized with a surrogate, the surrogate passed during construction or lazy initialization
1717
#' will be used for all acquisition functions.
1818
#'
19-
#' For optimization, [AcqOptimizer] can be used as for any other [AcqFunction], however, the [bbotk::Optimizer] wrapped within the [AcqOptimizer]
19+
#' For optimization, [AcqOptimizer] can be used as for any other [AcqFunction], however, the [bbotk::OptimizerBatch] wrapped within the [AcqOptimizer]
2020
#' must support multi-objective optimization as indicated via the `multi-crit` property.
2121
#'
2222
#' @family Acquisition Function

R/AcqFunctionSmsEgo.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#' In the case of being `NULL`, an epsilon vector is maintained dynamically as
1919
#' described in Horn et al. (2015).
2020
#'
21+
#' @section Note:
22+
#' * This acquisition function always also returns its current epsilon values in a list column (`acq_epsilon`).
23+
#' This value will be logged into the [bbotk::ArchiveBatch] of the [bbotk::OptimInstanceBatch] of the [AcqOptimizer] and
24+
#' therefore also in the [bbotk::Archive] of the actual [bbotk::OptimInstance] that is to be optimized.
25+
#'
2126
#' @references
2227
#' * `r format_bib("ponweiser_2008")`
2328
#' * `r format_bib("horn_2015")`
@@ -78,7 +83,7 @@ AcqFunctionSmsEgo = R6Class("AcqFunctionSmsEgo",
7883

7984
#' @field progress (`numeric(1)`)\cr
8085
#' Optimization progress (typically, the number of function evaluations left).
81-
#' Note that this requires the [bbotk::OptimInstance] to be terminated via a [bbotk::TerminatorEvals].
86+
#' Note that this requires the [bbotk::OptimInstanceBatch] to be terminated via a [bbotk::TerminatorEvals].
8287
progress = NULL,
8388

8489
#' @description
@@ -94,7 +99,7 @@ AcqFunctionSmsEgo = R6Class("AcqFunctionSmsEgo",
9499

95100
constants = ps(
96101
lambda = p_dbl(lower = 0, default = 1),
97-
epsilon = p_dbl(lower = 0, default = NULL, special_vals = list(NULL)) # for NULL, it will be calculated dynamically
102+
epsilon = p_dbl(lower = 0, default = NULL, special_vals = list(NULL)) # if NULL, it will be calculated dynamically
98103
)
99104
constants$values$lambda = lambda
100105
constants$values$epsilon = epsilon
@@ -140,6 +145,13 @@ AcqFunctionSmsEgo = R6Class("AcqFunctionSmsEgo",
140145
} else {
141146
self$epsilon = self$constants$values$epsilon
142147
}
148+
},
149+
150+
#' @description
151+
#' Reset the acquisition function.
152+
#' Resets `epsilon`.
153+
reset = function() {
154+
self$epsilon = NULL
143155
}
144156
),
145157

@@ -163,7 +175,7 @@ AcqFunctionSmsEgo = R6Class("AcqFunctionSmsEgo",
163175
# allocate memory for adding points to front for HV calculation in C
164176
front2 = t(rbind(self$ys_front, 0))
165177
sms = .Call("c_sms_indicator", PACKAGE = "mlr3mbo", cbs, self$ys_front, front2, self$epsilon, self$ref_point) # note that the negative indicator is returned from C
166-
data.table(acq_smsego = sms)
178+
data.table(acq_smsego = sms, acq_epsilon = list(self$epsilon))
167179
}
168180
)
169181
)

R/AcqFunctionStochasticCB.R

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,76 @@
88
#'
99
#' @description
1010
#' Lower / Upper Confidence Bound with lambda sampling and decay.
11-
#' The initial lambda value is drawn from an uniform distribution between `min_lambda` and `max_lambda` or from an exponential distribution with rate `1 / lambda`.
12-
#' The lambda value is updated after each evaluation by the formula `lambda * exp(-rate * (t %% period))`.
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].
1316
#'
1417
#' @section Parameters:
1518
#' * `"lambda"` (`numeric(1)`)\cr
16-
#' Lambda value for sampling from the exponential distribution.
19+
#' \eqn{\lambda} value for sampling from the exponential distribution.
1720
#' Defaults to `1.96`.
1821
#' * `"min_lambda"` (`numeric(1)`)\cr
19-
#' Minimum value of lambda for sampling from the uniform distribution.
22+
#' Minimum value of \eqn{\lambda}for sampling from the uniform distribution.
2023
#' Defaults to `0.01`.
2124
#' * `"max_lambda"` (`numeric(1)`)\cr
22-
#' Maximum value of lambda for sampling from the uniform distribution.
25+
#' Maximum value of \eqn{\lambda} for sampling from the uniform distribution.
2326
#' Defaults to `10`.
2427
#' * `"distribution"` (`character(1)`)\cr
25-
#' Distribution to sample lambda from.
28+
#' Distribution to sample \eqn{\lambda} from.
2629
#' One of `c("uniform", "exponential")`.
2730
#' Defaults to `uniform`.
2831
#' * `"rate"` (`numeric(1)`)\cr
2932
#' Rate of the exponential decay.
3033
#' Defaults to `0` i.e. no decay.
3134
#' * `"period"` (`integer(1)`)\cr
3235
#' Period of the exponential decay.
33-
#' Defaults to `NULL` i.e. the decay has no period.
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.
3442
#'
3543
#' @references
3644
#' * `r format_bib("snoek_2012")`
3745
#' * `r format_bib("egele_2023")`
3846
#'
3947
#' @family Acquisition Function
4048
#' @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+
#' }
4181
AcqFunctionStochasticCB = R6Class("AcqFunctionStochasticCB",
4282
inherit = AcqFunction,
4383

@@ -52,7 +92,7 @@ AcqFunctionStochasticCB = R6Class("AcqFunctionStochasticCB",
5292
#' @param max_lambda (`numeric(1)`).
5393
#' @param distribution (`character(1)`).
5494
#' @param rate (`numeric(1)`).
55-
#' @param period (`integer(1)`).
95+
#' @param period (`NULL` | `integer(1)`).
5696
initialize = function(
5797
surrogate = NULL,
5898
lambda = 1.96,
@@ -69,19 +109,17 @@ AcqFunctionStochasticCB = R6Class("AcqFunctionStochasticCB",
69109
private$.distribution = assert_choice(distribution, choices = c("uniform", "exponential"))
70110

71111
if (private$.distribution == "uniform" && (is.null(private$.min_lambda) || is.null(private$.max_lambda))) {
72-
stop("If `distribution` is 'uniform', `min_lambda` and `max_lambda` must be set.")
112+
stop('If `distribution` is "uniform", `min_lambda` and `max_lambda` must be set.')
73113
}
74114

75115
if (private$.distribution == "exponential" && is.null(private$.lambda)) {
76-
stop("If `distribution` is 'exponential', `lambda` must be set.")
116+
stop('If `distribution` is "exponential", `lambda` must be set.')
77117
}
78118

79119
private$.rate = assert_number(rate, lower = 0)
80120
private$.period = assert_int(period, lower = 1, null.ok = TRUE)
81121

82-
constants = ps(
83-
lambda = p_dbl(lower = 0)
84-
)
122+
constants = ps(lambda = p_dbl(lower = 0))
85123

86124
super$initialize("acq_cb",
87125
constants = constants,
@@ -117,8 +155,15 @@ AcqFunctionStochasticCB = R6Class("AcqFunctionStochasticCB",
117155
rate = private$.rate
118156

119157
self$constants$values$lambda = lambda_0 * exp(-rate * t)
120-
private$.t = t + 1
158+
private$.t = t + 1L
121159
}
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
122167
}
123168
),
124169

@@ -129,9 +174,8 @@ AcqFunctionStochasticCB = R6Class("AcqFunctionStochasticCB",
129174
.distribution = NULL,
130175
.rate = NULL,
131176
.period = NULL,
132-
.t = 0,
133177
.lambda_0 = NULL,
134-
178+
.t = 0L,
135179
.fun = function(xdt, lambda) {
136180
p = self$surrogate$predict(xdt)
137181
cb = p$mean - self$surrogate_max_to_min * lambda * p$se

R/AcqFunctionStochasticEI.R

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#'
99
#' @description
1010
#' Expected Improvement with epsilon decay.
11+
#' \eqn{\epsilon} is updated after each update by the formula `epsilon * exp(-rate * (t %% period))` where `t` is the number of times the acquisition function has been updated.
12+
#'
13+
#' While this acquisition function usually would be used within an asynchronous optimizer, e.g., [OptimizerAsyncMbo],
14+
#' it can in principle also be used in synchronous optimizers, e.g., [OptimizerMbo].
1115
#'
1216
#' @section Parameters:
1317
#' * `"epsilon"` (`numeric(1)`)\cr
@@ -20,13 +24,50 @@
2024
#' Defaults to `0.05`.
2125
#' * `"period"` (`integer(1)`)\cr
2226
#' Period of the exponential decay.
23-
#' Defaults to `NULL` i.e. the decay has no period.
27+
#' Defaults to `NULL`, i.e., the decay has no period.
28+
#'
29+
#' @section Note:
30+
#' * This acquisition function always also returns its current (`acq_epsilon`) and original (`acq_epsilon_0`) \eqn{\epsilon}.
31+
#' These values will be logged into the [bbotk::ArchiveBatch] of the [bbotk::OptimInstanceBatch] of the [AcqOptimizer] and
32+
#' therefore also in the [bbotk::Archive] of the actual [bbotk::OptimInstance] that is to be optimized.
2433
#'
2534
#' @references
2635
#' * `r format_bib("jones_1998")`
2736
#'
2837
#' @family Acquisition Function
2938
#' @export
39+
#' @examples
40+
#' if (requireNamespace("mlr3learners") &
41+
#' requireNamespace("DiceKriging") &
42+
#' requireNamespace("rgenoud")) {
43+
#' library(bbotk)
44+
#' library(paradox)
45+
#' library(mlr3learners)
46+
#' library(data.table)
47+
#'
48+
#' fun = function(xs) {
49+
#' list(y = xs$x ^ 2)
50+
#' }
51+
#' domain = ps(x = p_dbl(lower = -10, upper = 10))
52+
#' codomain = ps(y = p_dbl(tags = "minimize"))
53+
#' objective = ObjectiveRFun$new(fun = fun, domain = domain, codomain = codomain)
54+
#'
55+
#' instance = OptimInstanceBatchSingleCrit$new(
56+
#' objective = objective,
57+
#' terminator = trm("evals", n_evals = 5))
58+
#'
59+
#' instance$eval_batch(data.table(x = c(-6, -5, 3, 9)))
60+
#'
61+
#' learner = default_gp()
62+
#'
63+
#' surrogate = srlrn(learner, archive = instance$archive)
64+
#'
65+
#' acq_function = acqf("stochastic_ei", surrogate = surrogate)
66+
#'
67+
#' acq_function$surrogate$update()
68+
#' acq_function$update()
69+
#' acq_function$eval_dt(data.table(x = c(-1, 0, 1)))
70+
#' }
3071
AcqFunctionStochasticEI = R6Class("AcqFunctionStochasticEI",
3172
inherit = AcqFunction,
3273

@@ -43,7 +84,7 @@ AcqFunctionStochasticEI = R6Class("AcqFunctionStochasticEI",
4384
#' @param surrogate (`NULL` | [SurrogateLearner]).
4485
#' @param epsilon (`numeric(1)`).
4586
#' @param rate (`numeric(1)`).
46-
#' @param period (`integer(1)`).
87+
#' @param period (`NULL` | `integer(1)`).
4788
initialize = function(
4889
surrogate = NULL,
4990
epsilon = 0.1,
@@ -55,9 +96,7 @@ AcqFunctionStochasticEI = R6Class("AcqFunctionStochasticEI",
5596
private$.rate = assert_number(rate, lower = 0, finite = TRUE)
5697
private$.period = assert_int(period, lower = 1, null.ok = TRUE)
5798

58-
constants = ps(
59-
epsilon = p_dbl(lower = 0)
60-
)
99+
constants = ps(epsilon = p_dbl(lower = 0, default = 0.1))
61100

62101
super$initialize("acq_ei",
63102
constants = constants,
@@ -82,16 +121,22 @@ AcqFunctionStochasticEI = R6Class("AcqFunctionStochasticEI",
82121
rate = private$.rate
83122

84123
self$constants$values$epsilon = epsilon_0 * exp(-rate * t)
85-
private$.t = t + 1
124+
private$.t = t + 1L
125+
},
126+
127+
#' @description
128+
#' Reset the acquisition function.
129+
#' Resets the private update counter `.t` used within the epsilon decay.
130+
reset = function() {
131+
private$.t = 0L
86132
}
87133
),
88134

89135
private = list(
90136
.rate = NULL,
91137
.period = NULL,
92138
.epsilon_0 = NULL,
93-
.t = 0,
94-
139+
.t = 0L,
95140
.fun = function(xdt, epsilon) {
96141
if (is.null(self$y_best)) {
97142
stop("$y_best is not set. Missed to call $update()?")

0 commit comments

Comments
 (0)