An object representing the space of possible parametrizations of a function or another object.
ParamSet
s are used on the side of objects being parameterized, where they function as a configuration space determining the set of possible configurations accepted by these objects.
They can also be used to specify search spaces for optimization, indicating the set of legal configurations to try out.
It is often convenient to generate search spaces from configuration spaces, which can be done using the $search_space()
method in combination with to_tune()
/ TuneToken
objects.
Individual dimensions of a ParamSet
are specified by Domain
objects, created as p_dbl()
, p_lgl()
etc.
The field $values
can be used to store an active configuration or to partially fix
some parameters to constant values – the precise effect can be determined by the object being parameterized.
Constructing a ParamSet
can be done using ParamSet$new()
in combination with a named list of Domain
objects.
This route is recommended when the set of dimensions (i.e. the members of this named list) is dynamically created, such as when the number of parameters is variable.
ParamSet
s can also be created using the ps()
shorthand, which is the recommended way when the set of parameters is fixed.
In practice, the majority of cases where a ParamSet
is created, the ps()
should be used.
S3 methods and type converters
as.data.table()
ParamSet
->data.table::data.table()
Compact representation as datatable. Col types are:id: character
class: character
lower, upper: numeric
levels: list col, with NULL elements
nlevels: integer valued numeric
is_bounded: logical
special_vals: list col of list
default: list col
storage_type: character
tags: list col of character vectors
Public fields
assert_values
(
logical(1)
)
Should values be checked for validity during assigment to active binding$values
? Default isTRUE
, only switch this off if you know what you are doing.
Active bindings
data
(
data.table
)data.table
representation of theParamSet
.values
(named
list()
)
Currently set / fixed parameter values. Settable, and feasibility of values will be checked when you set them. You do not have to set values for all parameters, but only for a subset. When you set values, all previously set values will be unset / removed.tags
(named
list()
ofcharacter()
)
Can be used to group and subset parameters. Named with parameter IDs.params
(named
list()
)data.table
representing the combinedDomain
objects used to construct theParamSet
. Used for internal purpuses. Its use by external code is deprecated.domains
(named
list
ofDomain
) List ofDomain
objects that could be used to initialize thisParamSet
.extra_trafo
(
function(x, param_set)
)
Transformation function. Settable. User has to pass afunction(x)
, of the form
(namedlist()
, ParamSet) -> namedlist()
.
The function is responsible to transform a feasible configuration into another encoding, before potentially evaluating the configuration with the target algorithm. For the output, not many things have to hold. It needs to have unique names, and the target algorithm has to accept the configuration. For convenience, the self-paramset is also passed in, if you need some info from it (e.g. tags). Is NULL by default, and you can set it to NULL to switch the transformation off.constraint
(
function(x)
)
Constraint function. Settable. This function must evaluate a namedlist()
of values and determine whether it satisfies constraints, returning a scalarlogical(1)
value.deps
(
data.table::data.table()
)
Table has colsid
(character(1)
) andon
(character(1)
) andcond
(Condition). Lists all (direct) dependency parents of a param, through parameter IDs. Internally created by a call toadd_dep
. Settable, if you want to remove dependencies or perform other changes.length
(
integer(1)
)
Number of contained parameters.is_empty
(
logical(1)
)
Is theParamSet
empty? Named with parameter IDs.has_trafo
(
logical(1)
)
Whether atrafo
function is present, in parameters or inextra_trafo
.has_extra_trafo
(
logical(1)
)
Whetherextra_trafo
is set.has_deps
(
logical(1)
)
Whether the parameter dependencies are presenthas_constraint
(
logical(1)
)
Whether parameter constraint is set.all_numeric
(
logical(1)
)
IsTRUE
if all parameters arep_dbl()
orp_int()
.all_categorical
(
logical(1)
)
IsTRUE
if all parameters arep_fct()
andp_lgl()
.all_bounded
(
logical(1)
)
IsTRUE
if all parameters are bounded.class
(named
character()
)
Classes of contained parameters. Named with parameter IDs.lower
(named
double()
)
Lower bounds of numeric parameters (NA
for non-numerics). Named with parameter IDs.upper
(named
double()
)
Upper bounds of numeric parameters (NA
for non-numerics). Named with parameter IDs.levels
(named
list()
ofcharacter
)
Allowed levels of categorical parameters (NULL
for non-categoricals). Named with parameter IDs.storage_type
(
character()
)
Data types of parameters when stored in tables. Named with parameter IDs.special_vals
(named
list()
oflist()
)
Special values for all parameters. Named with parameter IDs.default
(named
list()
)
Default values of all parameters. If no default exists, element is not present. Named with parameter IDs.has_trafo_param
(
logical()
)
Whethertrafo
is set for any parameter.is_logscale
(
logical()
)
Whethertrafo
was set tologscale
during construction.
Note that this only refers to thelogscale
flag set during construction, e.g.p_dbl(logscale = TRUE)
. If the parameter was set to logscale manually, e.g. throughp_dbl(trafo = exp)
, thisis_logscale
will beFALSE
.nlevels
(named
integer()
)
Number of distinct levels of parameters.Inf
for double parameters or unbounded integer parameters. Named with param IDs.is_number
(named
logical()
)
Whether parameter isp_dbl()
orp_int()
. Named with parameter IDs.is_categ
(named
logical()
)
Whether parameter isp_fct()
orp_lgl()
. Named with parameter IDs.is_bounded
(named
logical()
)
Whether parameters have finite bounds. Named with parameter IDs.
Methods
Method new()
Creates a new instance of this R6 class.
Usage
ParamSet$new(params = named_list(), allow_dangling_dependencies = FALSE)
Arguments
params
(named
list()
)
List ofDomain
, named with their respective ID.allow_dangling_dependencies
(
character(1)
)
Whether dependencies depending on parameters that are not present should be allowed. A parameterx
havingdepends = y == 0
ify
is not present would usually throw an error, but if dangling dependencies are allowed, the dependency is added regardless. This is mainly for internal use.
Method ids()
Retrieves IDs of contained parameters based on some filter criteria
selections, NULL
means no restriction.
Only returns IDs of parameters that satisfy all conditions.
Arguments
class
(
character()
)
Typically a subset of"ParamDbl"
,"ParamInt"
,"ParamFct"
,"ParamLgl"
,"ParamUty"
. Other classes are possible if implemented by 3rd party packages. Return only IDs of dimensions with the given class.tags
(
character()
). Return only IDs of dimensions that have all tags given in this argument.any_tags
(
character()
). Return only IDs of dimensions that have at least one of the tags given in this argument.
Method get_values()
Retrieves parameter values based on some selections, NULL
means no
restriction and is equivalent to $values
.
Only returns values of parameters that satisfy all conditions.
Usage
ParamSet$get_values(
class = NULL,
tags = NULL,
any_tags = NULL,
type = "with_token",
check_required = TRUE,
remove_dependencies = TRUE
)
Arguments
class
(
character()
). See$ids()
.tags
(
character()
). See$ids()
.any_tags
(
character()
). See$ids()
.type
(
character(1)
)
Return values"with_token"
(i.e. all values),check_required
(
logical(1)
)
Check if all required parameters are set?remove_dependencies
(
logical(1)
)
IfTRUE
, set values with dependencies that are not fulfilled toNULL
.
Returns
Named list()
.
Method set_values()
Allows to to modify (and overwrite) or replace the parameter values. Per default already set values are being kept unless new values are being provided.
Usage
ParamSet$set_values(..., .values = list(), .insert = TRUE)
Arguments
...
(any)
Named parameter values..values
(named
list()
)
Named list with parameter values. Names must not already appear in...
..insert
(
logical(1)
)
Whether to insert the values (old values are being kept, if not overwritten), or to replace all values. Default is TRUE.
Method trafo()
Perform transformation specified by the trafo
of Domain
objects, as well as the $extra_trafo
field.
Arguments
x
(named
list()
|data.frame
)
The value(s) to be transformed.param_set
(
ParamSet
)
Passed toextra_trafo()
. Note that theextra_trafo
ofself
is used, not theextra_trafo
of theParamSet
given in theparam_set
argument. In almost all cases, the defaultparam_set = self
should be used.
Method aggr_internal_tuned_values()
Aggregate parameter values according to their aggregation rules.
Returns
(named list()
)
Method disable_internal_tuning()
Set the parameter values so that internal tuning for the selected parameters is disabled.
Arguments
ids
(
character()
)
The ids of the parameters for which to disable internal tuning.
Method convert_internal_search_space()
Convert all parameters from the search space to parameter values using the transformation given by
in_tune_fn
.
Returns
(named list()
)
Method test_constraint()
checkmate-like test-function. Takes a named list.
Return FALSE
if the given $constraint
is not satisfied, TRUE
otherwise.
Note this is different from satisfying the bounds or types given by the ParamSet
itself:
If x
does not satisfy these, an error will be thrown, given that assert_value
is TRUE
.
Arguments
x
(named
list()
)
The value to test.assert_value
(
logical(1)
)
Whether to verify thatx
satisfies the bounds and types given by thisParamSet
. Should beTRUE
unless this was already checked before.
Method test_constraint_dt()
checkmate-like test-function. Takes a data.table
.
For each row, return FALSE
if the given $constraint
is not satisfied, TRUE
otherwise.
Note this is different from satisfying the bounds or types given by the ParamSet
itself:
If x
does not satisfy these, an error will be thrown, given that assert_value
is TRUE
.
Method check()
checkmate-like check-function. Takes a named list.
A point x is feasible, if it configures a subset of params,
all individual param constraints are satisfied and all dependencies are satisfied.
Params for which dependencies are not satisfied should not be part of x
.
Constraints and dependencies are not checked when check_strict
is FALSE
.
Arguments
xs
(named
list()
).check_strict
(
logical(1)
)
Whether to check that constraints and dependencies are satisfied.sanitize
(
logical(1)
)
Whether to move values that are slightly outside bounds to valid values. These values are accepted independent ofsanitize
(depending on thetolerance
arguments ofp_dbl()
andp_int()
) . Ifsanitize
isTRUE
, the additional effect is that, should checks pass, the sanitized values ofxs
are added to the result as attribute"sanitized"
.
Method check_dependencies()
checkmate-like check-function. Takes a named list. Checks that all individual param dependencies are satisfied.
Arguments
xs
(named
list()
).
Method test()
checkmate-like test-function. Takes a named list.
A point x is feasible, if it configures a subset of params,
all individual param constraints are satisfied and all dependencies are satisfied.
Params for which dependencies are not satisfied should not be part of x
.
Constraints and dependencies are not checked when check_strict
is FALSE
.
Arguments
xs
(named
list()
).check_strict
(
logical(1)
)
Whether to check that constraints and dependencies are satisfied.
Method assert()
checkmate-like assert-function. Takes a named list.
A point x is feasible, if it configures a subset of params,
all individual param constraints are satisfied and all dependencies are satisfied.
Params for which dependencies are not satisfied should not be part of x
.
Constraints and dependencies are not checked when check_strict
is FALSE
.
Arguments
xs
(named
list()
).check_strict
(
logical(1)
)
Whether to check that constraints and dependencies are satisfied..var.name
(
character(1)
)
Name of the checked object to print in error messages.
Defaults to the heuristic implemented in vname.sanitize
(
logical(1)
)
Whether to move values that are slightly outside bounds to valid values. These values are accepted independent ofsanitize
(depending on thetolerance
arguments ofp_dbl()
andp_int()
) . Ifsanitize
isTRUE
, the additional effect is thatxs
is converted to within bounds.
Method check_dt()
checkmate-like check-function. Takes a data.table::data.table
where rows are points and columns are parameters.
A point x is feasible, if it configures a subset of params,
all individual param constraints are satisfied and all dependencies are satisfied.
Params for which dependencies are not satisfied should not be part of x
.
Constraints and dependencies are not checked when check_strict
is FALSE
.
Method test_dt()
checkmate-like test-function (s. $check_dt()
).
Method assert_dt()
checkmate-like assert-function (s. $check_dt()
).
Arguments
xdt
check_strict
(
logical(1)
)
Whether to check that constraints and dependencies are satisfied..var.name
(
character(1)
)
Name of the checked object to print in error messages.
Defaults to the heuristic implemented in vname.
Method qunif()
Map a matrix
or data.frame
of values between 0 and 1 to proportional values inside the feasible intervals of individual parameters.
Method subset()
Create a new ParamSet
restricted to the passed IDs.
Arguments
ids
(
character()
).allow_dangling_dependencies
(
logical(1)
)
Whether to allow subsets that cut across parameter dependencies. Dependencies that point to dropped parameters are kept (but will be "dangling", i.e. their"on"
will not be present).keep_constraint
(
logical(1)
)
Whether to keep the$constraint
function.
Method subspaces()
Create new one-dimensional ParamSet
s for each dimension.
Arguments
ids
(
character()
)
IDs for which to createParamSet
s. Defaults to all IDs.
Returns
named list()
of ParamSet
.
Method flatten()
Create a ParamSet
from this object, even if this object itself is not
a ParamSet
but e.g. a ParamSetCollection
.
Method search_space()
Construct a ParamSet
to tune over. Constructed from TuneToken
in $values
, see to_tune()
.
Arguments
values
(
named list
): optional named list ofTuneToken
objects to convert, in place of$values
.
Method add_dep()
Adds a dependency to this set, so that param id
now depends on param on
.
Arguments
id
(
character(1)
).on
(
character(1)
).cond
(Condition).
allow_dangling_dependencies
(
logical(1)
): Whether to allow dependencies on parameters that are not present.
Method print()
Printer.
Usage
ParamSet$print(
...,
hide_cols = c("levels", "is_bounded", "special_vals", "tags", "storage_type")
)
Arguments
...
(ignored).
hide_cols
(
character()
)
Which fields should not be printed? Default is"levels"
,"is_bounded"
,"special_vals"
,"tags"
, and"storage_type"
.
Examples
pset = ParamSet$new(
params = list(
d = p_dbl(lower = -5, upper = 5, default = 0, trafo = function(x) 2^x),
f = p_fct(levels = letters[1:3])
)
)
# alternative, recommended way of construction in this case since the
# parameter list is not dynamic:
pset = ps(
d = p_dbl(lower = -5, upper = 5, default = 0, trafo = function(x) 2^x),
f = p_fct(levels = letters[1:3])
)
pset$check(list(d = 2.1, f = "a"))
#> [1] TRUE
pset$check(list(d = 2.1, f = "d"))
#> [1] "f: Must be element of set {'a','b','c'}, but is 'd'"