Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to check for constant model while focussearch #259

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions R/doc_mbo_OptPath.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#' }
#' }
#' \item{parego.weight}{Weight vector sampled for multipoint ParEGO}
#' \item{constant.model}{If CMAES or Focussearch is used to optimize the infill crit, they can automatically determine if the model is constant in this iteration.}
#' \item{...}{Depending on the chosen infill criterion there will be additional columns, e.g. \code{se} and \code{mean} for the Expected Improvement)}
#' }
#'
Expand Down
2 changes: 2 additions & 0 deletions R/getExtras.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ getExtras = function(n, prop, train.time, control) {
w = setNames(as.list(weight.mat[i, ]), paste0("parego.weight.", seq_col(weight.mat)))
ex = c(ex, w)
}
ex$constant.model = isTRUE(attr(prop$prop.points, "constant.model"))
# if we use asyn MBO store node information and evaluation starte
ex$train.time = if (i == 1) train.time else NA_real_
ex$prop.type = prop$prop.type[i]
ex$propose.time = NA_real_
Expand Down
5 changes: 4 additions & 1 deletion R/infillOptCMAES.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ infillOptCMAES = function(infill.crit, models, control, par.set, opt.path, desig
if (is.infinite(result$best.fitness)) {
warningf("Infill optimizer CMA-ES crashed. Random point generated instead.")
res = t(sampleValue(par.set))
constant.model = TRUE
} else {
res = t(result$best.param)
constant.model = FALSE
}
setColNames(as.data.frame(res), rep.pids)
res = setColNames(as.data.frame(res), rep.pids)
setAttribute(res, "constant.model", constant.model)
}
9 changes: 7 additions & 2 deletions R/infillOptFocus.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# See infillOptCMAES.R for interface explanation.
infillOptFocus = function(infill.crit, models, control, par.set, opt.path, design, iter, ...) {
global.y = Inf
constant.model = FALSE

# restart the whole crap some times
for (restart.iter in seq_len(control$infill.opt.restarts)) {
Expand All @@ -26,11 +27,15 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig


# get current best value
local.index = getMinIndex(y, ties.method = "random")
local.index = getMinIndex(y, ties.method = "random", na.rm = TRUE)
local.y = y[local.index]
local.x.df = newdesign[local.index, , drop = FALSE]
local.x.list = dfRowToList(recodeTypes(local.x.df, ps.local), ps.local, 1)

# check if the model just gives constant values
if (local.iter == 1 && length(unique(y)) == 1)
constant.model = TRUE

# if we found a new best value, store it
if (local.y < global.y) {
global.x.df = local.x.df
Expand Down Expand Up @@ -64,7 +69,7 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig
})
}
}
recodeTypes(global.x.df, par.set)
setAttribute(recodeTypes(global.x.df, par.set), "constant.model", constant.model)
}

# as we operate on other types for the learner (ints are nums, logs are factors),
Expand Down
2 changes: 1 addition & 1 deletion R/makeMBOControl.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ makeMBOControl = function(n.objectives = 1L,
assertList(resample.measures, types = "Measure")

assertString(output.num.format)

control = makeS3Obj("MBOControl",
n.objectives = n.objectives,
propose.points = propose.points,
Expand Down
1 change: 1 addition & 0 deletions man/mbo_OptPath.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test_mbo_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ test_that("mbo works with impute and failure model", {
expect_true(!is.na(op$error.model[13L]))
expect_true(!is.na(op$error.model[14L]))
})

test_that("mbo recognizes constant model prediction", {
des = testd.fsphere.2d
des$y = mean(apply(des, 1, testf.fsphere.2d))
# make sure model does not break, and we get a failure model
learner = makeLearner("regr.km", predict.type = "se")
ctrl = makeMBOControl()
ctrl = setMBOControlTermination(ctrl, iters = 4L)
ctrl = setMBOControlInfill(ctrl, opt.focussearch.points = 100L)
or = mbo(testf.fsphere.2d, des, learner = learner, control = ctrl)
expect_equal(getOptPathLength(or$opt.path), 14)
op = as.data.frame(or$opt.path)
expect_true(sum(op$constant.model)<5)
})