File: updateParVals.R

package info (click to toggle)
r-cran-paramhelpers 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 992 kB
  • sloc: ansic: 102; sh: 13; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 2,406 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#' @title Insert par.vals to old ones with meeting requirements
#' @description Update the values of a given parameter setting with a new
#' parameter setting. Settings that do not meet the requirements anymore will be
#' deleted from the first given parameter setting. Default values of the Param
#' Set are respected to check if the new param settings meet the requirements.
#'
#' @template arg_parset
#' @param old.par.vals [list]\cr
#'   Param Values to be updated.
#' @param new.par.vals [list]\cr
#'   New Param Values to update the `old.par.vals`.
#' @param warn [logical]\cr
#'   Whether a warning should be shown, if a param setting from `old.par.vals`
#'   is dropped. Default is `FALSE`.
#' @return [`list`].
#' @export
updateParVals = function(par.set, old.par.vals, new.par.vals, warn = FALSE) {
  assertList(old.par.vals, names = "named")
  assertList(new.par.vals, names = "named")
  assertClass(par.set, "ParamSet")

  # we might want to check requires with defaults that are not overwritten by new.par.vals
  usable.defaults = getDefaults(par.set)
  usable.defaults = usable.defaults[names(usable.defaults) %nin% names(new.par.vals)]
  repeat {
    # we repeat to include parameters which depend on each other by requirements
    # candidates are params of the old par.vals we might still need.
    candidate.par.names = setdiff(names(old.par.vals), names(new.par.vals))
    for (pn in candidate.par.names) {
      # If all requirement parameters for the candidate are in the new.par.vals and if the requirements are met
      if (all(getRequiredParamNames(par.set$pars[[pn]]) %in% names(new.par.vals)) && requiresOk(par.set$pars[[pn]], new.par.vals)) {
        new.par.vals[pn] = old.par.vals[pn] # keep old.par.val in new.par.vals as it meets the requirements
      } else if (all(getRequiredParamNames(par.set$pars[[pn]]) %in% names(usable.defaults)) && requiresOk(par.set$pars[[pn]], usable.defaults)) {
        new.par.vals[pn] = old.par.vals[pn] # keep old.par.val as it meets requirement via defaults
      } else if (warn) {
        # otherwise we can drop the old par.val because it does not meet the requirements.
        warningf("ParamSetting %s was dropped.", convertToShortString(old.par.vals[pn]))
      }
    }
    # break if no changes were made
    if (identical(candidate.par.names, setdiff(names(old.par.vals), names(new.par.vals)))) break
  }
  return(new.par.vals)
}