Skip to content

Commit eb27ebb

Browse files
be-marcsumny
andauthored
compatibility: mlr3 0.21.0 (#159)
* compatibility: mlr3 0.21.0 * ... * cleanup tests --------- Co-authored-by: Lennart Schneider <[email protected]>
1 parent ba8297f commit eb27ebb

File tree

5 files changed

+21
-35
lines changed

5 files changed

+21
-35
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ Suggests:
6464
rpart,
6565
stringi,
6666
testthat (>= 3.0.0)
67-
Remotes: mlr-org/bbotk
67+
Remotes:
68+
mlr-org/mlr3
6869
ByteCompile: no
6970
Encoding: UTF-8
7071
Config/testthat/edition: 3

R/mbo_defaults.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,13 @@ default_surrogate = function(instance, learner = NULL, n_learner = NULL) {
161161
default_rf(noisy)
162162
}
163163
# stability: evaluate and add a fallback
164-
learner$encapsulate[c("train", "predict")] = "evaluate"
165164
require_namespaces("ranger")
166165
fallback = mlr3learners::LearnerRegrRanger$new()
167166
fallback$param_set$values = insert_named(
168167
fallback$param_set$values,
169168
list(num.trees = 10L, keep.inbag = TRUE, se.method = "jack")
170169
)
171-
learner$fallback = fallback
170+
learner$encapsulate("evaluate", fallback)
172171

173172
if (has_deps) {
174173
require_namespaces("mlr3pipelines")
@@ -184,8 +183,7 @@ default_surrogate = function(instance, learner = NULL, n_learner = NULL) {
184183
learner
185184
)
186185
)
187-
learner$encapsulate[c("train", "predict")] = "evaluate"
188-
learner$fallback = LearnerRegrFeatureless$new()
186+
learner$encapsulate("evaluate", lrn("regr.featureless"))
189187
}
190188
}
191189

tests/testthat/helper.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ MAKE_DESIGN = function(instance, n = 4L) {
101101

102102
if (requireNamespace("mlr3learners") && requireNamespace("DiceKriging") && requireNamespace("rgenoud")) {
103103
library(mlr3learners)
104-
REGR_KM_NOISY = lrn("regr.km", covtype = "matern3_2", optim.method = "gen", control = list(trace = FALSE, max.generations = 2), nugget.estim = TRUE, jitter = 1e-12)
105-
REGR_KM_NOISY$encapsulate = c(train = "callr", predict = "callr")
106-
REGR_KM_DETERM = lrn("regr.km", covtype = "matern3_2", optim.method = "gen", control = list(trace = FALSE, max.generations = 2), nugget.stability = 10^-8)
107-
REGR_KM_DETERM$encapsulate = c(train = "callr", predict = "callr")
104+
REGR_KM_NOISY = lrn("regr.km", covtype = "matern3_2", optim.method = "gen", control = list(trace = FALSE), nugget.estim = TRUE, jitter = 1e-12)
105+
REGR_KM_NOISY$encapsulate("callr", lrn("regr.featureless"))
106+
REGR_KM_DETERM = lrn("regr.km", covtype = "matern3_2", optim.method = "gen", control = list(trace = FALSE), nugget.stability = 10^-8)
107+
REGR_KM_DETERM$encapsulate("callr", lrn("regr.featureless"))
108108
}
109109
REGR_FEATURELESS = lrn("regr.featureless")
110-
REGR_FEATURELESS$encapsulate = c(train = "callr", predict = "callr")
110+
REGR_FEATURELESS$encapsulate("callr", lrn("regr.featureless"))
111111

112112
OptimizerError = R6Class("OptimizerError",
113113
inherit = OptimizerBatch,

tests/testthat/test_bayesopt_ego.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ test_that("stable bayesopt_ego", {
109109
expect_true(nrow(instance$archive$data) == 5L)
110110
expect_number(acq_function$surrogate$assert_insample_perf, upper = 1)
111111
lines = readLines(f)
112-
# expect_true(sum(grepl("Optimizer Error", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 1L)
112+
expect_true(sum(grepl("Optimizer Error", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 1L)
113113
expect_true(sum(grepl("Proposing a randomly sampled point", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 2L)
114114

115115
# Surrogate using LearnerRegrError as Learner that will fail during train

tests/testthat/test_mbo_defaults.R

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_that("default_surrogate", {
2323
expect_r6(surrogate$learner, "LearnerRegrKM")
2424
expect_equal_sorted(surrogate$learner$param_set$values,
2525
list(covtype = "matern5_2", optim.method = "gen", control = list(trace = FALSE), nugget.stability = 1e-08))
26-
expect_equal(surrogate$learner$encapsulate, c(train = "evaluate", predict = "evaluate"))
26+
expect_equal(surrogate$learner$encapsulation, c(train = "evaluate", predict = "evaluate"))
2727
expect_r6(surrogate$learner$fallback, "LearnerRegrRanger")
2828

2929
# singlecrit all numeric, noisy
@@ -32,7 +32,7 @@ test_that("default_surrogate", {
3232
expect_r6(surrogate$learner, "LearnerRegrKM")
3333
expect_equal_sorted(surrogate$learner$param_set$values,
3434
list(covtype = "matern5_2", optim.method = "gen", control = list(trace = FALSE), nugget.estim = TRUE, jitter = 1e-12))
35-
expect_equal(surrogate$learner$encapsulate, c(train = "evaluate", predict = "evaluate"))
35+
expect_equal(surrogate$learner$encapsulation, c(train = "evaluate", predict = "evaluate"))
3636
expect_r6(surrogate$learner$fallback, "LearnerRegrRanger")
3737

3838
# twocrit all numeric, deterministic
@@ -41,10 +41,10 @@ test_that("default_surrogate", {
4141
expect_list(surrogate$learner, types = "LearnerRegrKM")
4242
expect_equal_sorted(surrogate$learner[[1L]]$param_set$values,
4343
list(covtype = "matern5_2", optim.method = "gen", control = list(trace = FALSE), nugget.stability = 1e-08))
44-
expect_equal(surrogate$learner[[1L]]$encapsulate, c(train = "evaluate", predict = "evaluate"))
44+
expect_equal(surrogate$learner[[1L]]$encapsulation, c(train = "evaluate", predict = "evaluate"))
4545
expect_r6(surrogate$learner[[1L]]$fallback, "LearnerRegrRanger")
4646
expect_equal(surrogate$learner[[1L]]$param_set$values, surrogate$learner[[2L]]$param_set$values)
47-
expect_equal(surrogate$learner[[1L]]$encapsulate, surrogate$learner[[2L]]$encapsulate)
47+
expect_equal(surrogate$learner[[1L]]$encapsulation, surrogate$learner[[2L]]$encapsulation)
4848
expect_equal(surrogate$learner[[1L]]$fallback, surrogate$learner[[2L]]$fallback)
4949

5050
# twocrit all numeric, noisy
@@ -53,10 +53,10 @@ test_that("default_surrogate", {
5353
expect_list(surrogate$learner, types = "LearnerRegrKM")
5454
expect_equal_sorted(surrogate$learner[[1L]]$param_set$values,
5555
list(covtype = "matern5_2", optim.method = "gen", control = list(trace = FALSE), nugget.estim = TRUE, jitter = 1e-12))
56-
expect_equal(surrogate$learner[[1L]]$encapsulate, c(train = "evaluate", predict = "evaluate"))
56+
expect_equal(surrogate$learner[[1L]]$encapsulation, c(train = "evaluate", predict = "evaluate"))
5757
expect_r6(surrogate$learner[[1L]]$fallback, "LearnerRegrRanger")
5858
expect_equal(surrogate$learner[[1L]]$param_set$values, surrogate$learner[[2L]]$param_set$values)
59-
expect_equal(surrogate$learner[[1L]]$encapsulate, surrogate$learner[[2L]]$encapsulate)
59+
expect_equal(surrogate$learner[[1L]]$encapsulation, surrogate$learner[[2L]]$encapsulation)
6060
expect_equal(surrogate$learner[[1L]]$fallback, surrogate$learner[[2L]]$fallback)
6161

6262
# singlecrit mixed input
@@ -65,7 +65,7 @@ test_that("default_surrogate", {
6565
expect_r6(surrogate$learner, "LearnerRegrRanger")
6666
expect_equal_sorted(surrogate$learner$param_set$values,
6767
list(num.threads = 1L, num.trees = 100L, keep.inbag = TRUE, se.method = "jack"))
68-
expect_equal(surrogate$learner$encapsulate, c(train = "evaluate", predict = "evaluate"))
68+
expect_equal(surrogate$learner$encapsulation, c(train = "evaluate", predict = "evaluate"))
6969
expect_r6(surrogate$learner$fallback, "LearnerRegrRanger")
7070

7171
# twocrit mixed input
@@ -74,10 +74,10 @@ test_that("default_surrogate", {
7474
expect_list(surrogate$learner, types = "LearnerRegrRanger")
7575
expect_equal_sorted(surrogate$learner[[1L]]$param_set$values,
7676
list(num.threads = 1L, num.trees = 100L, keep.inbag = TRUE, se.method = "jack"))
77-
expect_equal(surrogate$learner[[1L]]$encapsulate, c(train = "evaluate", predict = "evaluate"))
77+
expect_equal(surrogate$learner[[1L]]$encapsulation, c(train = "evaluate", predict = "evaluate"))
7878
expect_r6(surrogate$learner[[1L]]$fallback, "LearnerRegrRanger")
7979
expect_equal(surrogate$learner[[1L]]$param_set$values, surrogate$learner[[2L]]$param_set$values)
80-
expect_equal(surrogate$learner[[1L]]$encapsulate, surrogate$learner[[2L]]$encapsulate)
80+
expect_equal(surrogate$learner[[1L]]$encapsulation, surrogate$learner[[2L]]$encapsulation)
8181
expect_equal(surrogate$learner[[1L]]$fallback, surrogate$learner[[2L]]$fallback)
8282

8383
# singlecrit mixed input deps
@@ -152,12 +152,11 @@ test_that("stability and defaults", {
152152
# this should trigger a mbo_error
153153
instance = MAKE_INST_1D(terminator = trm("evals", n_evals = 5L))
154154
learner = LearnerRegrError$new()
155-
learner$encapsulate[c("train", "predict")] = "evaluate"
156-
learner$fallback = lrn("regr.ranger", num.trees = 10L, keep.inbag = TRUE, se.method = "jack")
155+
learner$encapsulate("evaluate", lrn("regr.ranger", num.trees = 10L, keep.inbag = TRUE, se.method = "jack"))
157156
surrogate = default_surrogate(instance, learner = learner, n_learner = 1L)
158157
expect_r6(surrogate, "SurrogateLearner")
159158
expect_r6(surrogate$learner, "LearnerRegrError")
160-
expect_equal(surrogate$learner$encapsulate, c(train = "evaluate", predict = "evaluate"))
159+
expect_equal(surrogate$learner$encapsulation, c(train = "evaluate", predict = "evaluate"))
161160
expect_r6(surrogate$learner$fallback, "LearnerRegrRanger")
162161
acq_function = default_acqfunction(instance)
163162
expect_r6(acq_function, "AcqFunctionEI")
@@ -172,17 +171,5 @@ test_that("stability and defaults", {
172171
lines = readLines(f)
173172
# Nothing should happen here due to the fallback learner
174173
expect_true(sum(grepl("Surrogate Train Error", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 0L)
175-
176-
acq_function$surrogate$learner$reset()
177-
acq_function$surrogate$learner$fallback = NULL
178-
instance$archive$clear()
179-
bayesopt_ego(instance, surrogate = surrogate, acq_function = acq_function, acq_optimizer = acq_optimizer)
180-
expect_true(nrow(instance$archive$data) == 5L)
181-
lines = readLines(f)
182-
# Training fails but this error is not logged due to the "evaluate" encapsulate
183-
expect_equal(acq_function$surrogate$learner$errors, "Surrogate Train Error.")
184-
expect_true(sum(grepl("Surrogate Train Error", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 0L)
185-
expect_true(sum(grepl("Cannot predict", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 1L)
186-
expect_true(sum(grepl("Proposing a randomly sampled point", unlist(map(strsplit(lines, "\\[bbotk\\] "), 2L)))) == 1L)
187174
})
188175

0 commit comments

Comments
 (0)