-
-
Notifications
You must be signed in to change notification settings - Fork 48
Fix #201: focussearch now handles discrete vector parameters #202
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,41 @@ | |
| # See infillOptCMAES.R for interface explanation. | ||
| infillOptFocus = function(infill.crit, models, control, par.set, opt.path, design, iter, ...) { | ||
| global.y = Inf | ||
|
|
||
| discreteVectorPars = filterParams(par.set, type = c("discretevector", "logicalvector")) | ||
|
|
||
| allRequirements = extractSubList(par.set$pars, "requires", simplify = FALSE) | ||
| allRequirementVars = unique(unlist(lapply(allRequirements, all.vars))) | ||
| forbiddenRequirementVars = getParamIds(discreteVectorPars) | ||
| if (any(allRequirementVars %in% forbiddenRequirementVars)) { | ||
| 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 (and logical vectors): | ||
| # The problem is that for discrete vectors, we can't adjust the range dimension-wise. | ||
| # Instead we store the range of each discrete vectorparameter dimension in the list of named characters | ||
| # `discreteVectorMapping`. In each iteration a random value (that does not contain | ||
| # the optimum) is dropped from each vector on this list. The $values of the parameters in the parameterset also | ||
| # need to be modified to reflect the reduced range: from them, always the last value is dropped. | ||
| # Then `discreteVectorMapping` is a mapping that maps, for each discrete vector param dimension | ||
| # with originally n values, from the sampled value (levels 1 to n - #(dropped levels)) to the acutal levels with | ||
| # random dropouts. | ||
| # | ||
| # Since the requirements of the param set are queried while generating the design, this breaks if | ||
| # there are requirements depending on discrete vector parameters. | ||
| discreteVectorMapping = lapply(discreteVectorPars$pars, | ||
| function(param) rep(list(setNames(names(param$values), names(param$values))), param$len)) | ||
| discreteVectorMapping = unlist(discreteVectorMapping, recursive=FALSE) | ||
| if (!isEmpty(discreteVectorPars)) { | ||
| names(discreteVectorMapping) = getParamIds(discreteVectorPars, 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)) { | ||
|
|
@@ -21,13 +51,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, ...) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
@@ -39,24 +76,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 | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?