Skip to content

Commit

Permalink
compatibility: work with new irace 4.0.0 (#435)
Browse files Browse the repository at this point in the history
* compatibility: work with new irace 4.0.0

* fix: irace

* ...

* ...
  • Loading branch information
be-marc authored Nov 28, 2024
1 parent 218887e commit b0f74e2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Depends:
paradox (>= 1.0.1),
R (>= 3.1.0)
Imports:
bbotk (>= 1.4.0),
bbotk (>= 1.4.1),
checkmate (>= 2.0.0),
data.table,
lgr,
Expand All @@ -39,7 +39,7 @@ Suggests:
adagio,
future,
GenSA,
irace,
irace (>= 4.0.0),
knitr,
mlflow,
mlr3learners (>= 0.7.0),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ perf: save models on worker only when requested in `ObjectiveTuningAsync`.
* refactor: Rename stage `on_result` to `on_result_end` in `CallbackAsyncTuning` and `CallbackBatchTuning`.
* docs: Extend the `CallbackAsyncTuning` and `CallbackBatchTuning` documentation.
* compatibility: mlr3 0.22.0
* compatibility: Work with new irace 4.0.0

# mlr3tuning 1.1.0

Expand Down
6 changes: 3 additions & 3 deletions R/TunerBatchIrace.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ TunerBatchIrace = R6Class("TunerBatchIrace",
)
)

target_runner_tuning = function(experiment, exec.target.runner, scenario, target.runner) {# nolint
target_runner_tuning = function(experiment, exec_target_runner, scenario, target_runner) {# nolint
tuning_instance = scenario$targetRunnerData$inst

xdt = map_dtr(experiment, function(e) {
configuration = as.data.table(e$configuration)
# add configuration and instance id to archive
set(configuration, j = "configuration", value = e$id.configuration)
set(configuration, j = "instance", value = e$id.instance)
set(configuration, j = "configuration", value = e$id_configuration)
set(configuration, j = "instance", value = e$id_instance)
configuration
})
# fix logicals
Expand Down
47 changes: 34 additions & 13 deletions tests/testthat/test_TunerBatchIrace.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
skip_if_not_installed("irace")
skip_on_cran()

test_that("TunerIrace", {
x = capture.output({z = test_tuner("irace", term_evals = 42, real_evals = 39)})
z = test_tuner("irace", term_evals = 42)
instance = z$inst
archive = instance$archive$data
tuner = z$tuner
Expand All @@ -12,7 +11,7 @@ test_that("TunerIrace", {

# check optimization direction
# first elite of the first race should have the lowest average performance
load(tuner$param_set$values$logFile)
iraceResults = irace::read_logfile(tuner$param_set$values$logFile)
elites = iraceResults$allElites
aggr = archive[race == 1, .(classif.ce = mean(classif.ce)), by = configuration]
expect_equal(aggr[which.min(classif.ce), configuration], elites[[1]][1])
Expand All @@ -27,8 +26,13 @@ test_that("TunerIrace works with dependencies", {
cp = p_dbl(lower = 0.001, upper = 0.1),
minsplit = p_int(lower = 1, upper = 10, depends = cp == 0.005)
)
instance = TuningInstanceBatchSingleCrit$new(tsk("iris"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"),
trm("evals", n_evals = 96), search_space)
instance = ti(
task = tsk("mtcars"),
learner = lrn("regr.rpart"),
resampling = rsmp("holdout"),
measures = msr("regr.mse"),
terminator = trm("evals", n_evals = 96),
search_space = search_space)
tuner = tnr("irace")
x = capture.output({tuner$optimize(instance)})

Expand All @@ -38,27 +42,44 @@ test_that("TunerIrace works with dependencies", {
})

test_that("TunerIrace works with logical parameters", {
search_space = ps(keep_model = p_lgl())
instance = TuningInstanceBatchSingleCrit$new(tsk("mtcars"), lrn("regr.rpart"), rsmp("holdout"), msr("regr.mse"),
trm("evals", n_evals = 42), search_space)
search_space = ps(
cp = p_dbl(lower = 0.001, upper = 0.1),
keep_model = p_lgl())
instance = ti(
task = tsk("mtcars"),
learner = lrn("regr.rpart"),
resampling = rsmp("holdout"),
measures = msr("regr.mse"),
terminator = trm("evals", n_evals = 96),
search_space = search_space)
tuner = tnr("irace")
x = capture.output({tuner$optimize(instance)})
expect_logical(instance$archive$best()$keep_model)
})

test_that("TunerIrace uses digits", {
search_space = ps(cp = p_dbl(lower = pi * 1e-20, upper = 5.242e12 / 1e13))
instance = TuningInstanceBatchSingleCrit$new(tsk("iris"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"),
trm("evals", n_evals = 30), search_space)
instance = ti(
task = tsk("mtcars"),
learner = lrn("regr.rpart"),
resampling = rsmp("holdout"),
measures = msr("regr.mse"),
terminator = trm("evals", n_evals = 96),
search_space = search_space)
tuner = tnr("irace", nbIterations = 1L, minNbSurvival = 1)
x = capture.output({expect_data_table(tuner$optimize(instance))})
})

test_that("TunerIrace works with unnamed discrete values", {
# we had a bug here, see (mlr) issue #627
search_space = ps(minsplit = p_int(lower = 2L, upper = 7L))
inst = TuningInstanceBatchSingleCrit$new(tsk("iris"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"),
trm("evals", n_evals = 50), search_space)
instance = ti(
task = tsk("mtcars"),
learner = lrn("regr.rpart"),
resampling = rsmp("holdout"),
measures = msr("regr.mse"),
terminator = trm("evals", n_evals = 96),
search_space = search_space)
tuner = tnr("irace")
x = capture.output({expect_data_table(tuner$optimize(inst))})
x = capture.output({expect_data_table(tuner$optimize(instance))})
})

0 comments on commit b0f74e2

Please sign in to comment.