Skip to content

Commit bbb91c3

Browse files
authored
feat: add asynchronous decentralized bayesian optimization (#145)
1 parent ddbb5ef commit bbb91c3

File tree

86 files changed

+3665
-184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3665
-184
lines changed

.lintr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ linters: linters_with_defaults(
55
object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names
66
cyclocomp_linter = NULL, # do not check function complexity
77
commented_code_linter = NULL, # allow code in comments
8-
line_length_linter = line_length_linter(120)
8+
line_length_linter = line_length_linter(120),
9+
indentation_linter(indent = 2L, hanging_indent_style = "never")
910
)

DESCRIPTION

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ License: LGPL-3
3939
URL: https://mlr3mbo.mlr-org.com, https://github.com/mlr-org/mlr3mbo
4040
BugReports: https://github.com/mlr-org/mlr3mbo/issues
4141
Depends:
42+
mlr3tuning (>= 1.1.0),
4243
R (>= 3.1.0)
4344
Imports:
4445
bbotk (>= 1.2.0),
4546
checkmate (>= 2.0.0),
4647
data.table,
4748
lgr (>= 0.3.4),
48-
mlr3 (>= 0.21.0),
49+
mlr3 (>= 0.21.1),
4950
mlr3misc (>= 0.11.0),
50-
mlr3tuning (>= 1.0.2),
51-
paradox (>= 1.0.0),
51+
paradox (>= 1.0.1),
5252
spacefillr,
5353
R6 (>= 2.4.1)
5454
Suggests:
@@ -62,6 +62,8 @@ Suggests:
6262
ranger,
6363
rgenoud,
6464
rpart,
65+
redux,
66+
rush,
6567
stringi,
6668
testthat (>= 3.0.0)
6769
ByteCompile: no
@@ -85,8 +87,12 @@ Collate:
8587
'AcqFunctionPI.R'
8688
'AcqFunctionSD.R'
8789
'AcqFunctionSmsEgo.R'
90+
'AcqFunctionStochasticCB.R'
91+
'AcqFunctionStochasticEI.R'
8892
'AcqOptimizer.R'
8993
'aaa.R'
94+
'OptimizerADBO.R'
95+
'OptimizerAsyncMbo.R'
9096
'OptimizerMbo.R'
9197
'mlr_result_assigners.R'
9298
'ResultAssigner.R'
@@ -95,6 +101,8 @@ Collate:
95101
'Surrogate.R'
96102
'SurrogateLearner.R'
97103
'SurrogateLearnerCollection.R'
104+
'TunerADBO.R'
105+
'TunerAsyncMbo.R'
98106
'TunerMbo.R'
99107
'mlr_loop_functions.R'
100108
'bayesopt_ego.R'

NAMESPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ export(AcqFunctionMulti)
1616
export(AcqFunctionPI)
1717
export(AcqFunctionSD)
1818
export(AcqFunctionSmsEgo)
19+
export(AcqFunctionStochasticCB)
20+
export(AcqFunctionStochasticEI)
1921
export(AcqOptimizer)
22+
export(OptimizerADBO)
23+
export(OptimizerAsyncMbo)
2024
export(OptimizerMbo)
2125
export(ResultAssigner)
2226
export(ResultAssignerArchive)
2327
export(ResultAssignerSurrogate)
2428
export(Surrogate)
2529
export(SurrogateLearner)
2630
export(SurrogateLearnerCollection)
31+
export(TunerADBO)
32+
export(TunerAsyncMbo)
2733
export(TunerMbo)
2834
export(acqf)
2935
export(acqfs)
@@ -58,6 +64,7 @@ importFrom(R6,R6Class)
5864
importFrom(stats,dnorm)
5965
importFrom(stats,pnorm)
6066
importFrom(stats,quantile)
67+
importFrom(stats,rexp)
6168
importFrom(stats,runif)
6269
importFrom(stats,setNames)
6370
importFrom(utils,bibentry)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# mlr3mbo (development version)
22

3+
* refactor: refactored `SurrogateLearner` and `SurrogateLearnerCollection` to allow updating on an asynchronous `Archive`
4+
* feat: added experimental `OptimizerAsyncMbo`, `OptimizerADBO`, `TunerAsyncMbo`, and `TunerADBO` that allow for asynchronous optimization
5+
* feat: added `AcqFunctionStochasticCB` and `AcqFunctionStochasticEI` that are useful for asynchronous optimization
6+
* doc: minor changes to highlight differences between batch and asynchronous objects related to asynchronous support
7+
* refactor: `AcqFunction`s and `AcqOptimizer` gained a `reset()` method.
8+
39
# mlr3mbo 0.2.6
410

511
* refactor: Extract internal tuned values in instance.

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+
#' These values 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: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

Comments
 (0)