Repeat a ParamSet
a given number of times and thus create a larger ParamSet
.
By default, the resulting parameters are prefixed with the string "repX.", where
Xcounts up from 1. It is also possible to tag parameters by their original name and by their prefix, making grouped retrieval e.g. using
$get_values()` easier.
Arguments
- set
- times
(
integer(1)
)
Number of times to repeatset
. Should not be given ifprefixes
is provided.- prefixes
(
character
)
Acharacter
vector indicating the prefixes to use for each repetition ofset
. If this is given,times
is inferred fromlength(prefixes)
and should not be given separately. Iftimes
is given, this defaults to"repX"
, withX
counting up from 1.- tag_sets
(
logical(1)
)
Whether to add a tag of the form"set_<prefixes[[i]]>"
to each parameter in the result, indicating the repetition each parameter belongs to.- tag_params
(
logical(1)
)
Whether to add a tag of the form"param_<id>"
to each parameter in the result, indicating the original parameter ID insideset
.
Examples
pset = ps(
i = p_int(),
z = p_lgl()
)
ps_replicate(pset, 3)
#> <ParamSet(6)>
#> id class lower upper nlevels default value
#> <char> <char> <num> <num> <num> <list> <list>
#> 1: rep1.i ParamInt -Inf Inf Inf <NoDefault[0]>
#> 2: rep1.z ParamLgl NA NA 2 <NoDefault[0]>
#> 3: rep2.i ParamInt -Inf Inf Inf <NoDefault[0]>
#> 4: rep2.z ParamLgl NA NA 2 <NoDefault[0]>
#> 5: rep3.i ParamInt -Inf Inf Inf <NoDefault[0]>
#> 6: rep3.z ParamLgl NA NA 2 <NoDefault[0]>
ps_replicate(pset, prefixes = c("first", "last"))
#> <ParamSet(4)>
#> id class lower upper nlevels default value
#> <char> <char> <num> <num> <num> <list> <list>
#> 1: first.i ParamInt -Inf Inf Inf <NoDefault[0]>
#> 2: first.z ParamLgl NA NA 2 <NoDefault[0]>
#> 3: last.i ParamInt -Inf Inf Inf <NoDefault[0]>
#> 4: last.z ParamLgl NA NA 2 <NoDefault[0]>
pset$values = list(i = 1, z = FALSE)
psr = ps_replicate(pset, 2, tag_sets = TRUE, tag_params = TRUE)
# observe the effect of tag_sets, tag_params:
psr$tags
#> $rep1.i
#> [1] "set_rep1" "param_i"
#>
#> $rep1.z
#> [1] "set_rep1" "param_z"
#>
#> $rep2.i
#> [1] "set_rep2" "param_i"
#>
#> $rep2.z
#> [1] "set_rep2" "param_z"
#>
# note that values are repeated as well
psr$values
#> $rep1.i
#> [1] 1
#>
#> $rep1.z
#> [1] FALSE
#>
#> $rep2.i
#> [1] 1
#>
#> $rep2.z
#> [1] FALSE
#>
psr$set_values(rep1.i = 10, rep2.z = TRUE)
psr$values
#> $rep1.i
#> [1] 10
#>
#> $rep1.z
#> [1] FALSE
#>
#> $rep2.i
#> [1] 1
#>
#> $rep2.z
#> [1] TRUE
#>
# use `any_tags` to get subset of values.
# `any_tags = ` is preferable to `tags = `, since parameters
# could also have other tags. `tags = ` would require the
# selected params to have the given tags exclusively.
# get all values associated with the original parameter `i`
psr$get_values(any_tags = "param_i")
#> $rep1.i
#> [1] 10
#>
#> $rep2.i
#> [1] 1
#>
# get all values associated with the first repetition "rep1"
psr$get_values(any_tags = "set_rep1")
#> $rep1.i
#> [1] 10
#>
#> $rep1.z
#> [1] FALSE
#>