|
1 | 1 | context("termination criteria") |
2 | 2 |
|
3 | 3 | test_that("termination criteria works", { |
4 | | - iters = 30L |
5 | | - time.budget = 3L # seconds |
6 | | - exec.time.budget = 0.00001 # seconds |
7 | | - target.fun.value = 0.005 |
8 | | - max.evals = 13L |
9 | 4 |
|
10 | | - f = makeSphereFunction(1L) |
11 | | - x.grid = seq(-2, 2, length.out = 10L) |
12 | | - design = data.frame(x = x.grid, y = vnapply(x.grid, f)) |
13 | | - |
14 | | - learner = makeLearner("regr.randomForest", predict.type = "se", se.method = "sd") |
15 | | - |
16 | | - # time budget |
17 | | - ctrl = makeMBOControl() |
18 | | - ctrl = setMBOControlTermination(ctrl, time.budget = time.budget) |
19 | | - or = mbo(f, design = design, learner = learner, control = ctrl) |
20 | | - |
21 | | - expect_equal(or$final.state, "term.time") |
22 | | - |
23 | | - # exec. time budget |
24 | | - ctrl = makeMBOControl() |
25 | | - ctrl = setMBOControlTermination(ctrl, exec.time.budget = exec.time.budget) |
26 | | - or = mbo(f, design = design, learner = learner, control = ctrl) |
27 | | - |
28 | | - expect_equal(or$final.state, "term.exectime") |
29 | | - |
30 | | - # target fun value |
31 | | - ctrl = makeMBOControl() |
32 | | - ctrl = setMBOControlTermination(ctrl, iters = iters, target.fun.value = target.fun.value) |
33 | | - or = mbo(f, design = design, learner = learner, control = ctrl) |
34 | | - |
35 | | - expect_equal(or$final.state, "term.yval") |
36 | | - expect_lt(or$y, target.fun.value) |
37 | | - |
38 | | - # maximal number of target function evaluations |
39 | | - ctrl = makeMBOControl() |
40 | | - ctrl = setMBOControlTermination(ctrl, max.evals = max.evals) |
41 | | - or = mbo(f, design = design, learner = learner, control = ctrl) |
42 | | - |
43 | | - expect_equal(or$final.state, "term.feval") |
44 | | - expect_equal(getOptPathLength(or$opt.path), max.evals) |
| 5 | + f = testf.fsphere.1d |
| 6 | + f.slow = testf.fsphere.1d.slow |
| 7 | + f.max = convertToMaximization(f) |
| 8 | + design = testd.fsphere.1d |
| 9 | + design.max = design |
| 10 | + design$y = apply(design, 1, f) |
| 11 | + design.max$y = apply(design.max, 1, f.max) |
| 12 | + learner = NULL |
| 13 | + |
| 14 | + term.sets = list( |
| 15 | + list(arg = list(iters = 3L), state = "term.iter"), |
| 16 | + list(arg = list(time.budget = 3L), state = "term.time"), |
| 17 | + list(arg = list(exec.time.budget = 2), state = "term.exectime"), |
| 18 | + list(arg = list(target.fun.value = min(design$y) / 2, iters = 30L), state = "term.yval"), |
| 19 | + list(arg = list(max.evals = nrow(design) + 2L), state = "term.feval") |
| 20 | + ) |
| 21 | + |
| 22 | + for (term.set in term.sets) { |
| 23 | + ctrl = makeMBOControl() |
| 24 | + ctrl = do.call(setMBOControlTermination, c(list(control = ctrl), term.set$arg)) |
| 25 | + |
| 26 | + if (term.set$state == "term.exectime") { |
| 27 | + this.f = f.slow |
| 28 | + } else { |
| 29 | + this.f = f |
| 30 | + } |
| 31 | + |
| 32 | + or = mbo(this.f, design = design, control = ctrl, learner = learner) |
| 33 | + expect_equal(or$final.state, term.set$state) |
| 34 | + |
| 35 | + if (term.set$state == "term.iter") { |
| 36 | + expect_equal(getOptPathLength(or$opt.path), term.set$arg$iters + nrow(design)) |
| 37 | + } |
| 38 | + if (term.set$state == "term.yval") { |
| 39 | + expect_lt(or$y, term.set$arg$target.fun.value) |
| 40 | + term.set$arg$target.fun.value = term.set$arg$target.fun.value * (-1) |
| 41 | + ctrl = do.call(setMBOControlTermination, c(list(control = ctrl), term.set$arg)) |
| 42 | + or.max = mbo(f.max, design = design.max, control = ctrl, learner = learner) |
| 43 | + expect_lt(abs(getOptPathLength(or$opt.path)-getOptPathLength(or.max$opt.path)), 2) |
| 44 | + } |
| 45 | + if (term.set$state == "term.feval") { |
| 46 | + expect_equal(getOptPathLength(or$opt.path), term.set$arg$max.evals) |
| 47 | + } |
| 48 | + } |
45 | 49 | }) |
0 commit comments