Skip to contents

The ps() short form constructor uses Domain objects (p_dbl, p_fct, ...) to construct ParamSets in a succinct and readable way.

For more specifics also see the documentation of Domain.

Usage

ps(
  ...,
  .extra_trafo = NULL,
  .constraint = NULL,
  .allow_dangling_dependencies = FALSE
)

Arguments

...

(Domain)
Named arguments of Domain objects. The ParamSet will be constructed of the given Domains, The names of the arguments will be used as $id() in the resulting ParamSet.

.extra_trafo

(function(x, param_set))
Transformation to set the resulting ParamSet's $trafo value to. This is in addition to any trafo of Domain objects given in ..., and will be run after transformations of individual parameters were performed.

.constraint

(function(x))
Constraint function. When given, this function must evaluate a named list() of values and determine whether it satisfies constraints, returning a scalar logical(1) value.

.allow_dangling_dependencies

(logical)
Whether dependencies depending on parameters that are not present should be allowed. A parameter x having depends = y == 0 if y is not present in the ps() call would usually throw an error, but if dangling dependencies are allowed, the dependency is added regardless. This is usually a bad idea and mainly for internal use. Dependencies between ParamSets when using to_tune() can be realized using this.

Value

A ParamSet object.

See also

Other ParamSet construction helpers: Domain(), to_tune()

Examples

pars = ps(
  a = p_int(0, 10),
  b = p_int(upper = 20),
  c = p_dbl(),
  e = p_fct(letters[1:3]),
  f = p_uty(custom_check = checkmate::check_function)
)
print(pars)
#> <ParamSet(5)>
#>        id    class lower upper nlevels        default  value
#>    <char>   <char> <num> <num>   <num>         <list> <list>
#> 1:      a ParamInt     0    10      11 <NoDefault[0]>       
#> 2:      b ParamInt  -Inf    20     Inf <NoDefault[0]>       
#> 3:      c ParamDbl  -Inf   Inf     Inf <NoDefault[0]>       
#> 4:      e ParamFct    NA    NA       3 <NoDefault[0]>       
#> 5:      f ParamUty    NA    NA     Inf <NoDefault[0]>       

pars = ps(
  a = p_dbl(0, 1, trafo = exp),
  b = p_dbl(0, 1, trafo = exp),
  .extra_trafo = function(x, ps) {
    x$c <- x$a + x$b
    x
  }
)

# See how the addition happens after exp()ing:
pars$trafo(list(a = 0, b = 0))
#> $a
#> [1] 1
#> 
#> $b
#> [1] 1
#> 
#> $c
#> [1] 2
#> 

pars$values = list(
  a = to_tune(ps(x = p_int(0, 1),
    .extra_trafo = function(x, param_set) list(a = x$x)
  )),
  # make 'y' depend on 'x', but they are defined in different ParamSets
  # Therefore we need to allow dangling dependencies here.
  b = to_tune(ps(y = p_int(0, 1, depends = x == 1),
    .extra_trafo = function(x, param_set) list(b = x$y),
    .allow_dangling_dependencies = TRUE
  ))
)

pars$search_space()
#> <ParamSet(2)>
#> Key: <id>
#>        id    class lower upper nlevels        default parents  value
#>    <char>   <char> <num> <num>   <num>         <list>  <list> <list>
#> 1:      x ParamInt     0     1       2 <NoDefault[0]>               
#> 2:      y ParamInt     0     1       2 <NoDefault[0]>       x       
#> Trafo is set.