Skip to content

Commit 19695e7

Browse files
mb706berndbischl
authored andcommitted
More concise handling of discrete vector params in infillOptFocus
Parameters that have discrete vector params in their requirement can not be handled by infillOptFocus, so trying to do that now gives an error.
1 parent d752061 commit 19695e7

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

R/infillOptFocus.R

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
# See infillOptCMAES.R for interface explanation.
99
infillOptFocus = function(infill.crit, models, control, par.set, opt.path, design, iter, ...) {
1010
global.y = Inf
11+
12+
allRequirements = extractSubList(par.set$pars, "requires", simplify = FALSE)
13+
allUsedVars = unique(do.call(base::c, sapply(allRequirements, all.vars)))
14+
forbiddenVars = getParamIds(filterParams(par.set, type = c("discretevector", "logicalvector")))
15+
if (any(allUsedVars%in% forbiddenVars)) {
16+
stop("Cannot do focus search when some variables have requirements that depend on discrete or logical vector parameters.")
17+
}
18+
1119

12-
# restart the whole crap some times
20+
# perform multiple starts
1321
for (restart.iter in seq_len(control$infill.opt.restarts)) {
1422
# copy parset so we can shrink it
1523
ps.local = par.set
@@ -21,6 +29,14 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig
2129
# the sampled value (levels 1 to n - #(dropped levels)) to levels with random dropouts.
2230
discreteVectorMapping = lapply(filterParams(par.set, type = c("discretevector", "logicalvector"))$pars,
2331
function(param) rep(list(setNames(names(param$values), names(param$values))), param$len))
32+
discreteVectorMapping = do.call(base::c, discreteVectorMapping)
33+
# the resulting object is NULL if there are no discrete / logical vector params
34+
35+
if (!is.null(discreteVectorMapping)) {
36+
mappedPars = filterParams(par.set, type = c("discretevector", "logicalvector"))
37+
names(discreteVectorMapping) = getParamIds(mappedPars, with.nr = TRUE, repeated = TRUE)
38+
}
39+
2440

2541
# do iterations where we focus the region-of-interest around the current best point
2642
for (local.iter in seq_len(control$infill.opt.focussearch.maxit)) {
@@ -31,13 +47,11 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig
3147
newdesign = convertDataFrameCols(newdesign, ints.as.num = TRUE, logicals.as.factor = TRUE)
3248

3349
# handle discrete vectors
34-
for (dvparam in filterParams(par.set, type = c("discretevector", "logicalvector"))$pars) {
35-
for (dimnum in seq_len(dvparam$len)) {
36-
dfindex = paste0(dvparam$id, dimnum)
37-
mapping = discreteVectorMapping[[dvparam$id]][[dimnum]]
38-
levels(newdesign[[dfindex]]) = mapping[levels(newdesign[[dfindex]])]
39-
}
50+
for (dfindex in names(discreteVectorMapping)) {
51+
mapping = discreteVectorMapping[[dfindex]]
52+
levels(newdesign[[dfindex]]) = mapping[levels(newdesign[[dfindex]])]
4053
}
54+
4155
y = infill.crit(newdesign, models, control, par.set, design, iter, ...)
4256

4357
# get current best value
@@ -77,9 +91,9 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig
7791
if (par$type %nin% c("discretevector", "logicalvector")) {
7892
val.names = names(par$values)
7993
# remove current val from delete options, should work also for NA
80-
val.names = val.names[!sapply(par$values, identical, y=val)] # remember, 'val' may not even be a character
94+
val.names = val.names[!sapply(par$values, identical, y=val)] # remember, 'val' can be any type
8195
to.del = sample(val.names, 1)
82-
par$values[to.del] = NULL
96+
par$values[[to.del]] = NULL
8397
} else {
8498
# we remove the last element of par$values and a random element for
8599
# each dimension in discreteVectorMapping.
@@ -89,13 +103,13 @@ infillOptFocus = function(infill.crit, models, control, par.set, opt.path, desig
89103
val = names(val)
90104
}
91105
for (dimnum in seq_len(par$len)) {
92-
val.names = discreteVectorMapping[[par$id]][[dimnum]]
93-
newmap = val.names
106+
dfindex = paste0(par$id, dimnum)
107+
newmap = val.names = discreteVectorMapping[[dfindex]]
94108
val.names = val.names[val.names != val[dimnum]]
95109
to.del = sample(val.names, 1)
96110
newmap = newmap[newmap != to.del]
97111
names(newmap) = names(par$values)
98-
discreteVectorMapping[[par$id]][[dimnum]] <<- newmap
112+
discreteVectorMapping[[dfindex]] <<- newmap
99113
}
100114
}
101115
}

0 commit comments

Comments
 (0)