Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 73 additions & 20 deletions R/infillOptFocus.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,35 @@
# See infillOptCMAES.R for interface explanation.
infillOptFocus = function(infill.crit, models, control, par.set, opt.path, design, iter, ...) {
global.y = Inf

allRequirements = extractSubList(par.set$pars, "requires", simplify = FALSE)
allUsedVars = unique(do.call(base::c, sapply(allRequirements, all.vars)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is all.vars in this context? I feel stupid, but I cannot find it.
I also find it strange to use do.call(base::c, ...). Won't unlist do the trick?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base::all.vars returning a vector of all variables used in the expression? Unfortunately it is not perfect (ideally I would want a list of all free variables) but it is the best R is giving me as it seems.

forbiddenVars = getParamIds(filterParams(par.set, type = c("discretevector", "logicalvector")))
if (any(allUsedVars%in% forbiddenVars)) {
stop("Cannot do focus search when some variables have requirements that depend on discrete or logical vector parameters.")
}


# restart the whole crap some times
# perform multiple starts
for (restart.iter in seq_len(control$infill.opt.restarts)) {
# copy parset so we can shrink it
ps.local = par.set

# handle discrete vectors:
# The problem is that for discrete vectors, we can't adjust the values dimension-wise.
# Therefore, for discrete vectors, we always drop the last level and instead have a
# mapping that maps, for each discrete vector param and for each dimension, from
# the sampled value (levels 1 to n - #(dropped levels)) to levels with random dropouts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only maybe understand what you mean here 😃

discreteVectorMapping = lapply(filterParams(par.set, type = c("discretevector", "logicalvector"))$pars,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO you can store filterParams(par.set, type = c("discretevector", "logicalvector")) as ps.disic.log.vec or something, as you use it twice here. Then you could also put everything up to line 39 in if (!isEmpty(ps.disc.log.vec) if i am not mistaken.

function(param) rep(list(setNames(names(param$values), names(param$values))), param$len))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment would help "For each discrete/logic vector of length n create n replications of vector, each containing all possible values"?

discreteVectorMapping = do.call(base::c, discreteVectorMapping)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above: unlist(..., recursive = FALSE)

# the resulting object is NULL if there are no discrete / logical vector params

if (!is.null(discreteVectorMapping)) {
mappedPars = filterParams(par.set, type = c("discretevector", "logicalvector"))
names(discreteVectorMapping) = getParamIds(mappedPars, with.nr = TRUE, repeated = TRUE)
}


# do iterations where we focus the region-of-interest around the current best point
for (local.iter in seq_len(control$infill.opt.focussearch.maxit)) {
Expand All @@ -21,13 +45,20 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig

# convert to param encoding our model was trained on and can use
newdesign = convertDataFrameCols(newdesign, ints.as.num = TRUE, logicals.as.factor = TRUE)
y = infill.crit(newdesign, models, control, ps.local, design, iter, ...)

# handle discrete vectors
for (dfindex in names(discreteVectorMapping)) {
mapping = discreteVectorMapping[[dfindex]]
levels(newdesign[[dfindex]]) = mapping[levels(newdesign[[dfindex]])]
}

y = infill.crit(newdesign, models, control, par.set, design, iter, ...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially slow and doesn't make use of our helpers. have a look at

par.set.disc.logic.v = filterParams(par.set, type = c("discretevector", "logicalvector"))
dfindex = getParamIds(par.set.disc.logic.v, with.nr = TRUE, repeated = TRUE)


# get current best value
local.index = getMinIndex(y, ties.method = "random")
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)
local.x.list = dfRowToList(recodeTypes(local.x.df, par.set), par.set, 1)

# if we found a new best value, store it
if (local.y < global.y) {
Expand All @@ -39,24 +70,46 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig
ps.local$pars = lapply(ps.local$pars, function(par) {
# only shrink when there is a value
val = local.x.list[[par$id]]
if (!isScalarNA(val)) {
if (isNumeric(par)) {
# shrink to range / 2, centered at val
range = par$upper - par$lower
par$lower = pmax(par$lower, val - (range / 4))
par$upper = pmin(par$upper, val + (range / 4))
if (isInteger(par)) {
par$lower = floor(par$lower)
par$upper = ceiling(par$upper)
if (isScalarNA(val)) {
return(par)
}
if (isNumeric(par)) {
# shrink to range / 2, centered at val
range = par$upper - par$lower
par$lower = pmax(par$lower, val - (range / 4))
par$upper = pmin(par$upper, val + (range / 4))
if (isInteger(par)) {
par$lower = floor(par$lower)
par$upper = ceiling(par$upper)
}
} else if (isDiscrete(par)) {
# randomly drop a level, which is not val
if (length(par$values) <= 1L) {
return(par)
}
# need to do some magic to handle discrete vectors
if (par$type %nin% c("discretevector", "logicalvector")) {
val.names = names(par$values)
# remove current val from delete options, should work also for NA
val.names = val.names[!sapply(par$values, identical, y=val)] # remember, 'val' can be any type
to.del = sample(val.names, 1)
par$values[[to.del]] = NULL
} else {
# we remove the last element of par$values and a random element for
# each dimension in discreteVectorMapping.
par$values = par$values[-length(par$values)]
if (par$type != "logicalvector") {
# for discretevectorparam val would be a list; convert to character vector
val = names(val)
}
} else if (isDiscrete(par)) {
# randomly drop a level, which is not val
if (length(par$values) > 1L) {
val.names = names(par$values)
# remove current val from delete options, should work also for NA
val.names = setdiff(val.names, val)
to.del = sample(seq_along(val.names), 1)
par$values = par$values[-to.del]
for (dimnum in seq_len(par$len)) {
dfindex = paste0(par$id, dimnum)
newmap = val.names = discreteVectorMapping[[dfindex]]
val.names = val.names[val.names != val[dimnum]]
to.del = sample(val.names, 1)
newmap = newmap[newmap != to.del]
names(newmap) = names(par$values)
discreteVectorMapping[[dfindex]] <<- newmap
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_infill_opt_focus.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ test_that("complex param space, dependencies, focusing, restarts", {
if(x$disc2 == 'a') tmp3 = log(x$realA) + x$intA^4 + ifelse(x$discA == 'm', 5, 0)
if(x$disc2 == 'b') tmp3 = exp(x$realB) + ifelse(x$discB == 'R', sin(x$realBR), sin(x$realBNR))
if(x$disc2 == "c") tmp3 = 500
assert(is.list(x$discVec))
assert(x$discVec[[1]] %in% c("a", "b", "c"))
assert(x$discScal %in% c("x", "y", "z"))
tmp1 + tmp2 + tmp3
},
par.set = makeParamSet(
makeNumericParam("real1", lower = 0, upper = 1000),
makeIntegerParam("int1", lower = -100, upper = 100),
makeNumericVectorParam("realVec", len = 10, lower = -50, upper = 50),
makeIntegerVectorParam("intVec", len = 3, lower = 0, upper = 100),
makeDiscreteVectorParam("discVec", len = 3, c(x = "a", y = "b", z = "c")),
makeDiscreteParam("discScal", c(a = "x", b = "y", c = "z")),
makeNumericParam("real2", lower = -1, upper = 1),
makeDiscreteParam("disc1", values = c("foo", "bar"), requires = quote(real2 < 0)),
makeNumericParam("real3", lower = -100, upper = 100, requires = quote(real2 > 0)),
Expand Down