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.
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
assert_values
(logical(1)
)
Should values be checked for validity during assigment to active binding $values
?
Default is TRUE
, only switch this off if you know what you are doing.
data
(data.table
) data.table
representation of the ParamSet
.
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()
of character()
)
Can be used to group and subset parameters.
Named with parameter IDs.
params
(named list()
)
data.table
representing the combined Domain
objects used to construct the ParamSet
.
Used for internal purpuses.
Its use by external code is deprecated.
domains
(named list
of Domain
)
List of Domain
objects that could be used to initialize this ParamSet
.
extra_trafo
(function(x, param_set)
)
Transformation function. Settable.
User has to pass a function(x)
, of the form
(named list()
, ParamSet) -> named list()
.
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 named list()
of values and determine whether it satisfies
constraints, returning a scalar logical(1)
value.
deps
(data.table::data.table()
)
Table has cols id
(character(1)
) and on
(character(1)
) and cond
(Condition).
Lists all (direct) dependency parents of a param, through parameter IDs.
Internally created by a call to add_dep
.
Settable, if you want to remove dependencies or perform other changes.
length
(integer(1)
)
Number of contained parameters.
is_empty
(logical(1)
)
Is the ParamSet
empty? Named with parameter IDs.
has_trafo
(logical(1)
)
Whether a trafo
function is present, in parameters or in extra_trafo
.
has_extra_trafo
(logical(1)
)
Whether extra_trafo
is set.
has_deps
(logical(1)
)
Whether the parameter dependencies are present
has_constraint
(logical(1)
)
Whether parameter constraint is set.
all_numeric
(logical(1)
)
Is TRUE
if all parameters are p_dbl()
or p_int()
.
all_categorical
(logical(1)
)
Is TRUE
if all parameters are p_fct()
and p_lgl()
.
all_bounded
(logical(1)
)
Is TRUE
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()
of character
)
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()
of list()
)
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()
)
Whether trafo
is set for any parameter.
is_logscale
(logical()
)
Whether trafo
was set to logscale
during construction.
Note that this only refers to the logscale
flag set during construction, e.g. p_dbl(logscale = TRUE)
.
If the parameter was set to logscale manually, e.g. through p_dbl(trafo = exp)
,
this is_logscale
will be FALSE
.
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 is p_dbl()
or p_int()
. Named with parameter IDs.
is_categ
(named logical()
)
Whether parameter is p_fct()
or p_lgl()
. Named with parameter IDs.
is_bounded
(named logical()
)
Whether parameters have finite bounds. Named with parameter IDs.
new()
Creates a new instance of this R6 class.
ParamSet$new(params = named_list(), allow_dangling_dependencies = FALSE)
params
(named list()
)
List of Domain
, named with their respective ID.
allow_dangling_dependencies
(character(1)
)
Whether dependencies depending on parameters that are not present should be allowed. A parameter x
having
depends = y == 0
if y
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.
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.
ParamSet$ids(class = NULL, tags = NULL, any_tags = NULL)
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.
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.
ParamSet$get_values(
class = NULL,
tags = NULL,
any_tags = NULL,
type = "with_token",
check_required = TRUE,
remove_dependencies = TRUE
)
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)
)
If TRUE
, set values with dependencies that are not fulfilled to NULL
.
Named list()
.
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.
ParamSet$set_values(..., .values = list(), .insert = TRUE)
...
(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.
trafo()
Perform transformation specified by the trafo
of Domain
objects, as well as the $extra_trafo
field.
ParamSet$trafo(x, param_set = self)
x
(named list()
| data.frame
)
The value(s) to be transformed.
param_set
(ParamSet
)
Passed to extra_trafo()
. Note that the extra_trafo
of self
is used, not the extra_trafo
of the
ParamSet
given in the param_set
argument.
In almost all cases, the default param_set = self
should be used.
aggr_internal_tuned_values()
Aggregate parameter values according to their aggregation rules.
ParamSet$aggr_internal_tuned_values(x)
(named list()
)
disable_internal_tuning()
Set the parameter values so that internal tuning for the selected parameters is disabled.
ParamSet$disable_internal_tuning(ids)
ids
(character()
)
The ids of the parameters for which to disable internal tuning.
Self
convert_internal_search_space()
Convert all parameters from the search space to parameter values using the transformation given by
in_tune_fn
.
ParamSet$convert_internal_search_space(search_space)
search_space
(ParamSet
)
The internal search space.
(named list()
)
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
.
ParamSet$test_constraint(x, assert_value = TRUE)
x
(named list()
)
The value to test.
assert_value
(logical(1)
)
Whether to verify that x
satisfies the bounds and types given by this ParamSet
.
Should be TRUE
unless this was already checked before.
logical(1)
: Whether x
satisfies the $constraint
.
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
.
ParamSet$test_constraint_dt(x, assert_value = TRUE)
x
(data.table
)
The values to test.
assert_value
(logical(1)
)
Whether to verify that x
satisfies the bounds and types given by this ParamSet
.
Should be TRUE
unless this was already checked before.
logical
: For each row in x
, whether it satisfies the $constraint
.
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
.
ParamSet$check(xs, check_strict = TRUE, sanitize = FALSE)
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 of sanitize
(depending on the
tolerance
arguments of p_dbl()
and p_int()
) . If sanitize
is TRUE
, the additional effect is that, should checks pass, the
sanitized values of xs
are added to the result as attribute "sanitized"
.
If successful TRUE
, if not a string with an error message.
check_dependencies()
checkmate-like check-function. Takes a named list. Checks that all individual param dependencies are satisfied.
ParamSet$check_dependencies(xs)
xs
(named list()
).
If successful TRUE
, if not a string with an error message.
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
.
ParamSet$test(xs, check_strict = TRUE)
xs
(named list()
).
check_strict
(logical(1)
)
Whether to check that constraints and dependencies are satisfied.
If successful TRUE
, if not FALSE
.
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
.
ParamSet$assert(
xs,
check_strict = TRUE,
.var.name = vname(xs),
sanitize = FALSE
)
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 of sanitize
(depending on the
tolerance
arguments of p_dbl()
and p_int()
) . If sanitize
is TRUE
, the additional effect is that xs
is converted to within bounds.
If successful xs
invisibly, if not an error message.
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
.
ParamSet$check_dt(xdt, check_strict = TRUE)
xdt
check_strict
(logical(1)
)
Whether to check that constraints and dependencies are satisfied.
If successful TRUE
, if not a string with the error message.
test_dt()
checkmate-like test-function (s. $check_dt()
).
ParamSet$test_dt(xdt, check_strict = TRUE)
xdt
check_strict
(logical(1)
)
Whether to check that constraints and dependencies are satisfied.
If successful TRUE
, if not FALSE
.
assert_dt()
checkmate-like assert-function (s. $check_dt()
).
ParamSet$assert_dt(xdt, check_strict = TRUE, .var.name = vname(xdt))
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.
If successful xs
invisibly, if not, an error is generated.
qunif()
Map a matrix
or data.frame
of values between 0 and 1 to proportional values inside the feasible intervals of individual parameters.
ParamSet$qunif(x)
x
(matrix
| data.frame
)
Values to map. Column names must be a subset of the names of parameters.
data.table
.
get_domain()
get the Domain
object that could be used to create a given parameter.
ParamSet$get_domain(id)
id
(character(1)
).
Domain
.
subset()
Create a new ParamSet
restricted to the passed IDs.
ParamSet$subset(
ids,
allow_dangling_dependencies = FALSE,
keep_constraint = TRUE
)
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.
ParamSet
.
subspaces()
Create new one-dimensional ParamSet
s for each dimension.
ParamSet$subspaces(ids = private$.params$id)
ids
(character()
)
IDs for which to create ParamSet
s. Defaults to all IDs.
named list()
of ParamSet
.
flatten()
Create a ParamSet
from this object, even if this object itself is not
a ParamSet
but e.g. a ParamSetCollection
.
ParamSet$flatten()
search_space()
Construct a ParamSet
to tune over. Constructed from TuneToken
in $values
, see to_tune()
.
ParamSet$search_space(values = self$values)
values
(named list
): optional named list of TuneToken
objects to convert, in place of $values
.
add_dep()
Adds a dependency to this set, so that param id
now depends on param on
.
ParamSet$add_dep(id, on, cond, allow_dangling_dependencies = FALSE)
id
(character(1)
).
on
(character(1)
).
cond
(Condition).
allow_dangling_dependencies
(logical(1)
): Whether to allow dependencies on parameters that are not present.
...
(ignored).
print()
Printer.
ParamSet$print(
...,
hide_cols = c("levels", "is_bounded", "special_vals", "tags", "storage_type")
)
...
(ignored).
hide_cols
(character()
)
Which fields should not be printed? Default is "levels"
,
"is_bounded"
, "special_vals"
, "tags"
, and "storage_type"
.
clone()
The objects of this class are cloneable with this method.
ParamSet$clone(deep = FALSE)
deep
Whether to make a deep clone.
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"))
pset$check(list(d = 2.1, f = "d"))
Run the code above in your browser using DataLab