Skip to content

Commit 4888abe

Browse files
committed
new paradox adaption
1 parent 325ddea commit 4888abe

10 files changed

+50
-34
lines changed

R/helper.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,22 @@ evaluate_default = function(inst) {
2323
xss = default_values(inst$objective$learner, inst$search_space, inst$objective$task)
2424

2525
# parameters with exp transformation and log inverse transformation
26-
has_logscale = map_lgl(inst$search_space$params, function(param) get_private(param)$.has_logscale)
2726
# parameters with unknown inverse transformation
28-
has_trafo = map_lgl(inst$search_space$params, function(param) get_private(param)$.has_trafo)
2927
# parameter set with trafo
30-
has_extra_trafo = get_private(inst$search_space)$.has_extra_trafo
28+
if ("set_id" %in% names(ps())) {
29+
# old paradox
30+
has_logscale = map_lgl(inst$search_space$params, function(param) get_private(param)$.has_logscale)
31+
32+
has_trafo = map_lgl(inst$search_space$params, function(param) get_private(param)$.has_trafo)
33+
34+
has_extra_trafo = get_private(inst$search_space)$.has_extra_trafo
35+
} else {
36+
has_logscale = map_lgl(inst$search_space$params$.trafo, function(x) identical(x, exp))
37+
38+
has_trafo = map_lgl(inst$search_space$params$.trafo, function(x) !is.null(x) && !identical(x, exp))
39+
40+
has_extra_trafo = !is.null(inst$search_space$extra_trafo)
41+
}
3142

3243
if (any(has_trafo) || has_extra_trafo) {
3344
stop("Cannot evaluate default hyperparameter values. Search space contains transformation functions with unknown inverse function.")

tests/testthat/helper.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ library(R6)
77

88
lapply(list.files(system.file("testthat", package = "mlr3"), pattern = "^helper.*\\.[rR]", full.names = TRUE), source)
99
lapply(list.files(system.file("testthat", package = "mlr3tuning"), pattern = "^helper.*\\.[rR]", full.names = TRUE), source)
10+
11+
sortnames = function(x) {
12+
if (!is.null(names(x))) {
13+
x <- x[order(names(x), decreasing = TRUE)]
14+
}
15+
x
16+
}

tests/testthat/test_ArchiveTuning.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ test_that("ArchiveTuning access methods work", {
6969
# learner param values
7070
walk(instance$archive$data$uhash, function(uhash) {
7171
expect_list(instance$archive$learner_param_vals(uhash = uhash))
72-
expect_named(instance$archive$learner_param_vals(uhash = uhash), c("xval" ,"cp"))
72+
expect_named(instance$archive$learner_param_vals(uhash = uhash), c("xval" ,"cp"), ignore.order = TRUE)
7373
})
7474

7575
walk(seq_row(instance$archive$data), function(i) {
7676
expect_list(instance$archive$learner_param_vals(i))
77-
expect_named(instance$archive$learner_param_vals(i), c("xval" ,"cp"))
77+
expect_named(instance$archive$learner_param_vals(i), c("xval" ,"cp"), ignore.order = TRUE)
7878
})
7979

8080
# learners

tests/testthat/test_AutoTuner.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ test_that("AutoTuner / train+predict", {
88
expect_learner(at)
99
at$train(task)
1010
expect_learner(at)
11-
expect_equal(at$learner$param_set$values, list(xval = 0, cp = 0.2))
11+
12+
expect_equal(sortnames(at$learner$param_set$values), list(xval = 0, cp = 0.2))
1213
inst = at$tuning_instance
1314
a = at$archive$data
1415
expect_data_table(a, nrows = 3L)
1516
r = at$tuning_result
1617
expect_equal(r$x_domain[[1]], list(cp = 0.2))
17-
expect_equal(r$learner_param_vals[[1]], list(xval = 0, cp = 0.2))
18+
expect_equal(sortnames(r$learner_param_vals[[1]]), list(xval = 0, cp = 0.2))
1819
prd = at$predict(task)
1920
expect_prediction(prd)
2021
expect_s3_class(at$learner$model, "rpart")
@@ -39,11 +40,12 @@ test_that("AutoTuner / resample", {
3940

4041
rr = resample(tsk("iris"), at, r_outer, store_models = TRUE)
4142

43+
4244
# check tuning results of all outer folds
4345
expect_equal(length(rr$learners), outer_folds)
4446
lapply(rr$learners, function(ll) {
4547
assert_r6(ll, "AutoTuner")
46-
expect_equal(ll$learner$param_set$values, list(xval = 0, cp = 0.2))
48+
expect_equal(sortnames(ll$learner$param_set$values), list(xval = 0, cp = 0.2))
4749
inst = ll$tuning_instance
4850
assert_r6(inst, "TuningInstanceSingleCrit")
4951
expect_data_table(inst$archive$data, nrows = inner_evals)

tests/testthat/test_Tuner.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test_that("we get a result when some subordinate params are not fulfilled", {
5151
})
5252

5353
test_that("print method workds", {
54-
param_set = ParamSet$new(list(ParamLgl$new("p1")))
54+
param_set = ps(p1 = p_lgl())
5555
param_set$values$p1 = TRUE
5656
param_classes = "ParamLgl"
5757
properties = "single-crit"
@@ -70,7 +70,7 @@ test_that("print method workds", {
7070
})
7171

7272
test_that("optimize does not work in abstract class", {
73-
param_set = ParamSet$new(list(ParamLgl$new("p1")))
73+
param_set = ps(p1 = p_lgl())
7474
param_set$values$p1 = TRUE
7575
param_classes = "ParamDbl"
7676
properties = "single-crit"
@@ -145,7 +145,7 @@ test_that("Tuner works with instantiated resampling", {
145145
})
146146

147147
test_that("Tuner active bindings work", {
148-
param_set = ParamSet$new(list(ParamLgl$new("p1")))
148+
param_set = ps(p1 = p_lgl())
149149
param_set$values$p1 = TRUE
150150
param_classes = "ParamLgl"
151151
properties = "single-crit"
@@ -163,7 +163,7 @@ test_that("Tuner active bindings work", {
163163
expect_equal(tuner$properties, properties)
164164
expect_subset(packages, tuner$packages)
165165

166-
expect_error({tuner$param_set = ParamSet$new(list(ParamLgl$new("p2")))},
166+
expect_error({tuner$param_set = ps(p2 = p_lgl())},
167167
regexp = "$param_set is read-only",
168168
fixed = TRUE)
169169

tests/testthat/test_TunerCmaes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ expect_tuner(tnr("cmaes"))
1818
)
1919

2020
expect_equal(instance$archive$n_evals, 10)
21-
expect_named(instance$result_x_domain, c("cp", "minsplit", "minbucket"))
21+
expect_named(instance$result_x_domain, c("cp", "minsplit", "minbucket"), ignore.order = TRUE)
2222
})

tests/testthat/test_TunerGenSA.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ test_that("TunerGenSA", {
1515
test_that("TunerGenSA with int params and trafo", {
1616
ps = ps(
1717
cp = p_dbl(lower = 0.001, upper = 0.1),
18-
minsplit = p_dbl(lower = 1, upper = 10)
18+
minsplit = p_dbl(lower = 1, upper = 10, trafo = function(x) as.integer(round(x)))
1919
)
20-
ps$trafo = function(x, param_set) {
21-
x$minsplit = as.integer(round(x$minsplit))
22-
return(x)
23-
}
2420
te = trm("evals", n_evals = 2)
2521
inst = TuningInstanceSingleCrit$new(tsk("iris"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), te, ps)
2622
tt = TunerGenSA$new()

tests/testthat/test_TunerGridSearch.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test_that("TunerGridSearch with TerminatorNone", {
2626

2727
test_that("TunerGridSearch works with forward hotstart parameter", {
2828
task = tsk("pima")
29-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
29+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
3030

3131
instance = tune(
3232
tuner = tnr( "grid_search", batch_size = 5, resolution = 5),
@@ -40,12 +40,12 @@ test_that("TunerGridSearch works with forward hotstart parameter", {
4040

4141
ids = map(extract_benchmark_result_learners(instance$archive$benchmark_result), function(l) l$model$id)
4242
expect_equal(length(unique(ids)), 5)
43-
expect_equal(unique(instance$archive$data$iter), c(1, 25, 50, 75, 100))
43+
expect_equal(unique(instance$archive$data$iter), c(1, 26, 51, 76, 101))
4444
})
4545

4646
test_that("TunerGridSearch works with forward hotstart parameter", {
4747
task = tsk("pima")
48-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
48+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
4949
learner$properties[learner$properties %in% "hotstart_forward"] = "hotstart_backward"
5050

5151
instance = tune(
@@ -60,12 +60,12 @@ test_that("TunerGridSearch works with forward hotstart parameter", {
6060

6161
ids = map(extract_benchmark_result_learners(instance$archive$benchmark_result), function(l) l$model$id)
6262
expect_equal(length(unique(ids)), 5)
63-
expect_equal(unique(instance$archive$data$iter), c(100, 75, 50, 25, 1))
63+
expect_equal(unique(instance$archive$data$iter), c(101, 76, 51, 26, 1))
6464
})
6565

6666
test_that("TunerGridSearch works with forward and backward hotstart parameter", {
6767
task = tsk("pima")
68-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
68+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
6969
learner$properties = c(learner$properties, "hotstart_backward")
7070

7171
instance = tune(
@@ -80,5 +80,5 @@ test_that("TunerGridSearch works with forward and backward hotstart parameter",
8080

8181
ids = map(extract_benchmark_result_learners(instance$archive$benchmark_result), function(l) l$model$id)
8282
expect_equal(length(unique(ids)), 5)
83-
expect_equal(unique(instance$archive$data$iter), c(100, 75, 50, 25, 1))
83+
expect_equal(unique(instance$archive$data$iter), c(101, 76, 51, 26, 1))
8484
})

tests/testthat/test_TuningInstanceMultiCrit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ test_that("TuningInstanceMultiCrit and empty search space works", {
143143
)
144144

145145
expect_data_table(instance$result)
146-
expect_equal(instance$result$learner_param_vals[[1]], list(xval = 0, cp = 0.1))
146+
expect_equal(sortnames(instance$result$learner_param_vals[[1]]), list(xval = 0, cp = 0.1))
147147
expect_equal(instance$result$x_domain[[1]], list())
148148

149149
# no constant
@@ -182,5 +182,5 @@ test_that("assign_result works", {
182182
expect_equal(res$cp, c(0.1, 0.01))
183183
expect_equal(res$classif.fpr, c(0.8, 0.7))
184184
expect_equal(res$classif.tpr, c(0.3, 0.2))
185-
expect_equal(res$learner_param_vals[[1]], list(xval = 0, cp = 0.1))
185+
expect_equal(sortnames(res$learner_param_vals[[1]]), list(xval = 0, cp = 0.1))
186186
})

tests/testthat/test_hotstart.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_that("hotstart works forwards", {
22
task = tsk("pima")
3-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
3+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
44

55
instance = tune(
66
tuner = tnr("grid_search", batch_size = 5, resolution = 5),
@@ -14,13 +14,13 @@ test_that("hotstart works forwards", {
1414

1515
ids = map(extract_benchmark_result_learners(instance$archive$benchmark_result), function(l) l$model$id)
1616
expect_equal(length(unique(ids)), 5)
17-
expect_equal(unique(instance$archive$data$iter), c(1, 25, 50, 75, 100))
17+
expect_equal(unique(instance$archive$data$iter), c(1, 26, 51, 76, 101))
1818
expect_null(instance$archive$data$resample_result)
1919
})
2020

2121
test_that("hotstart works backwards", {
2222
task = tsk("pima")
23-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
23+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
2424
learner$properties[learner$properties %in% "hotstart_forward"] = "hotstart_backward"
2525

2626
instance = tune(
@@ -35,13 +35,13 @@ test_that("hotstart works backwards", {
3535

3636
ids = map(extract_benchmark_result_learners(instance$archive$benchmark_result), function(l) l$model$id)
3737
expect_equal(length(unique(ids)), 5)
38-
expect_equal(unique(instance$archive$data$iter), c(100, 75, 50, 25, 1))
38+
expect_equal(unique(instance$archive$data$iter), c(101, 76, 51, 26, 1))
3939
expect_null(instance$archive$data$resample_result)
4040
})
4141

4242
test_that("hotstart works forwards and backwards", {
4343
task = tsk("pima")
44-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
44+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
4545
learner$properties = c(learner$properties, "hotstart_backward")
4646

4747
instance = tune(
@@ -56,7 +56,7 @@ test_that("hotstart works forwards and backwards", {
5656

5757
ids = map(extract_benchmark_result_learners(instance$archive$benchmark_result), function(l) l$model$id)
5858
expect_equal(length(unique(ids)), 5)
59-
expect_equal(unique(instance$archive$data$iter), c(100, 75, 50, 25, 1))
59+
expect_equal(unique(instance$archive$data$iter), c(101, 76, 51, 26, 1))
6060
expect_null(instance$archive$data$resample_result)
6161
})
6262

@@ -79,7 +79,7 @@ test_that("hotstart flag is not set to TRUE if learners does not support hotstar
7979

8080
test_that("models are discarded after storing them in the stack", {
8181
task = tsk("pima")
82-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
82+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
8383

8484
instance = tune(
8585
tuner = tnr("grid_search", batch_size = 5, resolution = 5),
@@ -102,7 +102,7 @@ test_that("models are discarded after storing them in the stack", {
102102

103103
test_that("objects are cloned", {
104104
task = tsk("pima")
105-
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 100))
105+
learner = lrn("classif.debug", x = to_tune(), iter = to_tune(1, 101))
106106

107107
instance = tune(
108108
tuner = tnr("grid_search", batch_size = 5, resolution = 5),

0 commit comments

Comments
 (0)