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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
#' @title Create a description object for a parameter.
#'
#' @description For each parameter type a special constructor function is
#' available, see below.
#'
#' For the following arguments you can also pass an `expression` instead of a
#' concrete value: `default`, `len`, `lower`, `upper`, `values`. These
#' expressions can depend on arbitrary symbols, which are later filled in /
#' substituted from a dictionary, in order to produce a concrete valu, see
#' [evaluateParamExpressions()]. So this enables data / context dependent
#' settings, which is sometimes useful.
#'
#' The S3 class is a list which stores these elements:
#' \describe{
#' \item{id (`character(1)`)}{See argument of same name.}
#' \item{type (`character(1)`)}{Data type of parameter. For all type string see (getTypeStringsAll())}
#' \item{len (`integer(1)` | `expression`)}{See argument of same name.}
#' \item{lower (`numeric` | `expression`)}{See argument of same name. Length of this vector is `len`.}
#' \item{upper (`numeric` | `expression`)}{See argument of same name. Length of this vector is `len`.}
#' \item{values (`list` | `expression`)}{Discrete values, always stored as a named list.}
#' \item{cnames (`character`}{See argument of same name.}
#' \item{allow.inf (`logical(1)`)}{See argument of same name.}
#' \item{trafo (`NULL` | `function(x)`)}{See argument of same name.}
#' \item{requires (`NULL` | `expression`)}{See argument of same name.}
#' \item{default (any concrete value | `expression`)}{See argument of same name.}
#' \item{has.default (`logical(1)`)}{Extra flag to really be able to check whether the user passed a default, to avoid troubles with `NULL` and `NA`.}
#' \item{tunable (`logical(1)`)}{See argument of same name.}
#' \item{special.vals (`list`)}{See argument of same name.}
#' }
#'
#' @param id (`character(1)`)\cr
#' Name of parameter.
#' @param len (`integer(1)` | `expression`)\cr
#' Length of vector parameter.
#' @param lower (`numeric` | `expression`)\cr
#' Lower bounds. A singe value of
#' length 1 is automatically replicated to `len` for vector parameters. If
#' `len = NA` you can only pass length-1 scalars. Default is `-Inf`.
#' @param upper (`numeric` | `expression`)\cr
#' Upper bounds. A singe value of
#' length 1 is automatically replicated to `len` for vector parameters. If
#' `len = NA` you can only pass length-1 scalars. Default is `Inf`.
#' @param values (`vector` | `list` | `expression`)\cr
#' Possible discrete values.
#' Instead of using a vector of atomic values, you are also allowed to pass a
#' list of quite \dQuote{complex} R objects, which are used as discrete
#' choices. If you do the latter, the elements must be uniquely named, so that
#' the names can be used as internal representations for the choice.
#' @param cnames (`character`)\cr
#' Component names for vector params (except
#' discrete). Every function in this package that creates vector values for
#' such a param, will name that vector with `cnames`.
#' @param allow.inf (`logical(1)`)\cr
#' Allow infinite values for numeric and
#' numericvector params to be feasible settings. Default is `FALSE`.
#' @param default (any concrete value | `expression`)\cr
#' Default value used in
#' learner. Note: When this is a discrete parameter make sure to use a VALUE
#' here, not the NAME of the value. If this argument is missing, it means no
#' default value is available.
#' @param trafo (`NULL` | `function(x)`)\cr
#' Function to transform parameter. It
#' should be applied to the parameter value before it is, e.g., passed to a
#' corresponding objective function. Function must accept a parameter value as
#' the first argument and return a transformed one. Default is `NULL` which
#' means no transformation.
#' @param requires (`NULL` | `call` | `expression`)\cr
#' States requirements on
#' other parameters' values, so that setting this parameter only makes sense
#' if its requirements are satisfied (dependent parameter). Can be an object
#' created either with `expression` or `quote`, the former type is
#' auto-converted into the later. Only really useful if the parameter is
#' included in a (ParamSet()). Default is `NULL` which means no requirements.
#' @param tunable (`logical(1)`)\cr
#' Is this parameter tunable? Defining a
#' parameter to be not-tunable allows to mark arguments like, e.g.,
#' \dQuote{verbose} or other purely technical stuff. Note that this flag is
#' most likely not respected by optimizing procedures unless stated otherwise.
#' Default is `TRUE` (except for `untyped`, `function`, `character` and
#' `characterVector`) which means it is tunable.
#' @param special.vals (`list()`)\cr
#' A list of special values the parameter can
#' except which are outside of the defined range. Default is an empty list.
#' @return [[Param()]].
#' @name Param
#' @rdname Param
#' @examples
#' makeNumericParam("x", lower = -1, upper = 1)
#' makeNumericVectorParam("x", len = 2)
#' makeDiscreteParam("y", values = c("a", "b"))
#' makeCharacterParam("z")
NULL
makeParam = function(id, type, learner.param, len = 1L, lower = NULL, upper = NULL, values = NULL, cnames = NULL, allow.inf = FALSE, default,
trafo = NULL, requires = NULL, tunable = TRUE, special.vals = list(), when) {
assertString(id)
assert(
checkCount(len, na.ok = learner.param),
checkClass(len, "expression")
)
if (isNumericTypeString(type, include.int = TRUE)) {
assert(
checkNumeric(lower, any.missing = FALSE),
checkClass(lower, "expression")
)
assert(
checkNumeric(upper, any.missing = FALSE),
checkClass(upper, "expression")
)
# the following check also ensures that if len=NA, the lower and upper must be scalars
if (!is.expression(len) && !is.expression(lower)) {
if (length(lower) %nin% c(1L, len)) {
stopf("For param '%s' length 'lower' must be either 1 or length of param, not: %i", id, length(lower))
}
}
if (!is.expression(len) && !is.expression(upper)) {
if (length(upper) %nin% c(1L, len)) {
stopf("For param '%s' length 'upper' must be either 1 or length of param, not: %i", id, length(upper))
}
}
}
if (isDiscreteTypeString(type)) {
values = checkValuesForDiscreteParam(id, values)
}
# We cannot check default} for NULL or NA as this could be the default value!
if (missing(default)) {
has.default = FALSE
default = NULL
} else {
has.default = TRUE
}
if (!is.null(trafo)) {
assertFunction(trafo)
}
if (!is.null(requires)) {
requires = convertExpressionToCall(requires)
assertSubset(mode(requires), c("call", "name"))
}
assertList(special.vals)
if (isNumericTypeString(type, include.int = TRUE)) {
if (!is.expression(len) && !is.na(len) && len > 1L) {
if (isScalarNumeric(lower)) {
lower = rep(lower, len)
}
if (isScalarNumeric(upper)) {
upper = rep(upper, len)
}
}
if (!is.expression(lower) && !is.expression(upper)) {
if (any(upper < lower)) {
stopf("For param '%s' some component of 'upper' is smaller than the corresponding one in 'lower'", id)
}
}
}
p = makeS3Obj("Param",
id = id,
type = type,
len = len,
lower = lower,
upper = upper,
values = values,
cnames = cnames,
allow.inf = allow.inf,
has.default = has.default,
default = default,
trafo = trafo,
requires = requires,
tunable = tunable,
special.vals = special.vals
)
if (learner.param) {
p = makeLearnerParam(p, when)
}
if (has.default && !is.expression(default)) {
if (!isFeasible(p, default)) {
stop(p$id, " : 'default' must be a feasible parameter setting.")
}
}
return(p)
}
getParPrintData = function(x, trafo = TRUE, used = TRUE, constr.clip = 40L) {
g = function(n) collapse(sprintf("%.3g", n))
if (isNumeric(x, include.int = TRUE)) {
if (!is.expression(x$lower) && !is.expression(x$upper) &&
(length(unique(x$lower)) == 1L) && (length(unique(x$upper)) == 1L)) {
x$lower = unique(x$lower)
x$upper = unique(x$upper)
}
low = if (is.expression(x$lower)) as.character(x$lower) else g(x$lower)
upp = if (is.expression(x$upper)) as.character(x$upper) else g(x$upper)
constr = sprintf("%s to %s", low, upp)
} else if (isDiscrete(x, include.logical = FALSE)) {
vals = if (is.expression(x$values)) as.character(x$values) else collapse(names(x$values))
constr = clipString(vals, constr.clip)
} else {
constr = "-"
}
if (x$has.default) {
if (!is.expression(x$default)) {
def = x$default
def = paramValueToString(x, def)
} else {
def = as.character(x$default)
}
} else {
def = "-"
}
if (isVector(x)) {
if (!is.expression(x$len)) {
len = x$len
} else {
len = as.character(x$len)
}
} else {
len = "-"
}
d = data.frame(
Type = x$type,
len = len,
Def = def,
Constr = constr,
Req = ifelse(is.null(x$requires), "-", "Y"),
Tunable = x$tunable,
stringsAsFactors = FALSE
)
if (trafo) {
d$Trafo = ifelse(is.null(x$trafo), "-", "Y")
}
return(d)
}
#' @export
print.Param = function(x, ..., trafo = TRUE) {
print(getParPrintData(x, trafo = trafo))
}
# helper function to perform sanity checks on values of disctrete param
checkValuesForDiscreteParam = function(id, values) {
if (is.vector(values) && !is.expression(values)) {
values = as.list(values)
}
assert(
checkList(values),
checkClass(values, "expression")
)
if (!is.expression(values)) {
if (length(values) == 0L) {
stopf("No possible value for discrete parameter %s!", id)
}
# check that NA does not occur in values, we use that for "missing state" for dependent params
# make sure that this works for complex object too, cannot be done with simple is.na
if (any(vlapply(values, isScalarNA))) {
stopf("NA is not allowed as a value for discrete parameter %s.\nParamHelpers uses NA as a special value for dependent parameters.", id)
}
n = length(values)
ns = names(values)
# if names missing, set all to ""
if (is.null(ns)) {
ns = rep("", n)
}
# guess missing names
for (i in seq_len(n)) {
v = values[[i]]
if (is.na(ns[i]) || ns[i] == "") {
if (is.character(v) || is.numeric(v)) {
ns[i] = as.character(v)
}
}
}
names(values) = ns
if (!isProperlyNamed(values)) {
stopf("Not all values for parameter '%s' were named and names could not be guessed!", id)
}
# check that NA does not occur in value names, see above
if ("NA" %in% names(values)) {
stopf("NA is not allowed as a value name for discrete parameter %s.\nParamHelpers uses NA as a special value for dependent parameters.", id)
}
}
return(values)
}
|