Skip to content

Commit 3dcd47b

Browse files
committed
improve tests, terms
1 parent 4d57844 commit 3dcd47b

File tree

4 files changed

+77
-57
lines changed

4 files changed

+77
-57
lines changed

R/term_conds.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ makeMBOTerminationTargetFunValue = function(target.fun.value) {
7676
control = getOptProblemControl(opt.problem)
7777
opt.path = getOptStateOptPath(opt.state)
7878
opt.dir = if (control$minimize) 1L else -1L
79-
init.best = min(getOptPathY(opt.path)[getOptPathDOB(opt.path) == 0])
79+
init.best = getOptPathY(opt.path)[getOptPathBestIndex(opt.path, dob = 0)]
8080
current.best = getOptPathY(opt.path)[getOptPathBestIndex(opt.path)]
8181
term = (current.best * opt.dir <= target.fun.value * opt.dir)
8282
message = if (!term) NA_character_ else sprintf("Target function value %f reached.", target.fun.value)
83-
return(list(term = term, message = message, code = "term.yval", progress = (init.best - current.best)/(init.best - target.fun.value)))
83+
return(list(term = term, message = message, code = "term.yval", progress = abs(init.best - current.best)/abs(init.best - target.fun.value)))
8484
}
8585
}
8686

tests/testthat/helper_objects.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ testfmco2 = makeMultiObjectiveFunction(
3535
testdesmco2 = generateTestDesign(10L, getParamSet(testfmco2))
3636

3737
# slow test function
38-
testf.fsphere.2d.slow = makeSingleObjectiveFunction(
38+
testf.fsphere.1d.slow = makeSingleObjectiveFunction(
3939
name = "slow.function",
4040
fn = function(...) {
4141
Sys.sleep(0.25)
42-
testf.fsphere.2d(...)
42+
testf.fsphere.1d(...)
4343
},
4444
has.simple.signature = TRUE,
45-
vectorized = isVectorized(testf.fsphere.2d),
46-
noisy = isNoisy(testf.fsphere.2d),
47-
par.set = getParamSet(testf.fsphere.2d),
48-
minimize = shouldBeMinimized(testf.fsphere.2d),
49-
fn.mean = getMeanFunction(testf.fsphere.2d),
50-
tags = getTags(testf.fsphere.2d),
51-
global.opt.params = getGlobalOptimum(testf.fsphere.2d)$param,
52-
global.opt.value = getGlobalOptimum(testf.fsphere.2d)$value
45+
vectorized = isVectorized(testf.fsphere.1d),
46+
noisy = isNoisy(testf.fsphere.1d),
47+
par.set = getParamSet(testf.fsphere.1d),
48+
minimize = shouldBeMinimized(testf.fsphere.1d),
49+
fn.mean = getMeanFunction(testf.fsphere.1d),
50+
tags = getTags(testf.fsphere.1d),
51+
global.opt.params = getGlobalOptimum(testf.fsphere.1d)$param,
52+
global.opt.value = getGlobalOptimum(testf.fsphere.1d)$value
5353
)
5454

5555
# mixed space test functio

tests/testthat/test_adaptive_infillcrits.R

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
context("adaptive infill crits")
22

3-
test_that("adaptive infill crits", {
3+
test_that("adaptive infill crit works with all termination criteria", {
4+
45
terminations = list(
56
list(iters = 3L),
67
list(time.budget = 1L),
78
list(exec.time.budget = 1L, iters = 13, use.for.adaptive.infill = "exec.time.budget"),
89
list(target.fun.value = 0.025, iters = 13, use.for.adaptive.infill = "target.fun.value"),
10+
list(target.fun.value = -0.025, iters = 13, use.for.adaptive.infill = "target.fun.value"),
911
list(max.evals = 13L)
1012
)
11-
des = testd.fsphere.2d
12-
des$y = apply(des, 1, testf.fsphere.2d)
13+
14+
f = testf.fsphere.1d
15+
f.slow = testf.fsphere.1d.slow
16+
f.max = convertToMaximization(f)
17+
design = testd.fsphere.1d
18+
design.max = design
19+
design$y = apply(design, 1, f)
20+
design.max$y = apply(design.max, 1, f.max)
21+
1322
ctrl = makeMBOControl()
1423
ctrl = setMBOControlInfill(ctrl, crit = makeMBOInfillCritAdaCB())
24+
1525
for (i in seq_along(terminations)) {
1626
ctrl2 = do.call(setMBOControlTermination, c(list(control = ctrl), terminations[[i]]))
1727
if (i %in% 2:3) {
18-
fun = testf.fsphere.2d.slow
28+
fun = f.slow
29+
des = design
30+
} else if (i == 5) {
31+
fun = f.max
32+
des = design.max
1933
} else {
20-
fun = testf.fsphere.2d
34+
fun = f
35+
des = design
2136
}
2237
or = mbo(fun, des, control = ctrl2)
2338
expect_number(or$y)
2439
df = as.data.frame(or$opt.path)
2540
expect_true(any(df$lambda > 1))
2641
expect_true(length(unique(df$lambda))>2)
42+
expect_true(all(diff(df$lambda[!is.na(df$lambda)])<0))
2743
expect_true(all(df$prop.type %in% c("infill_adacb", "initdesign")))
2844
expect_numeric(df$adacb)
2945
}
Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
11
context("termination criteria")
22

33
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
94

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+
}
4549
})

0 commit comments

Comments
 (0)