diff --git a/NAMESPACE b/NAMESPACE index 821b2ec2..6cea8894 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -63,6 +63,8 @@ S3method(sampleValue,Param) S3method(sampleValue,ParamSet) S3method(setValueCNames,Param) S3method(setValueCNames,ParamSet) +S3method(splitVectorParams,Param) +S3method(splitVectorParams,ParamSet) export(addOptPathEl) export(checkParamSet) export(convertParamSetToIrace) @@ -173,6 +175,7 @@ export(sampleValues) export(setOptPathElDOB) export(setOptPathElEOL) export(setValueCNames) +export(splitVectorParams) export(trafoOptPath) export(trafoValue) export(updateParVals) diff --git a/R/getTypeStrings.R b/R/getTypeStrings.R index 030f5d63..aef69730 100644 --- a/R/getTypeStrings.R +++ b/R/getTypeStrings.R @@ -58,3 +58,13 @@ getTypeStringsLogical = function() { c("logical", "logicalvector") } + + +#' @export +#' @rdname getTypeStrings +getTypeStringsAll = function() { + c("numeric", "integer", "numericvector", "integervector", "discrete", + "discretevector", "logical", "logicalvector", "character", "charactervector", + "function", "untyped") +} + diff --git a/R/splitVectorParams.R b/R/splitVectorParams.R new file mode 100644 index 00000000..9d32efc2 --- /dev/null +++ b/R/splitVectorParams.R @@ -0,0 +1,45 @@ +#' @title Split up a vector param into a list of length-1 params. +#' +#' @description +#' Splits up vector params into a multiple length-1 params. +#' Sometimes a useful conversion if it is nicer to operate on +#' the individual normal params. +#' +#' @template arg_par_or_set +#' @return [list of \code{\link{Param}} | \code{\link{ParamSet}}]. +#' Return a list for single params and a (converted) param set for param sets. +#' @export +splitVectorParams = function(par) { + UseMethod("splitVectorParams") +} + +#' @export +splitVectorParams.Param = function(par) { + pids = getParamIds(par, repeated = TRUE, with.nr = TRUE) + xs = lapply(seq_along(pids), function(i) { + pid = pids[i] + x = par + x$id = pid + x$type = gsub("vector", "", par$type) + x$len = 1L + if (isNumeric(par)) { + x$lower = par$lower[i] + x$upper = par$upper[i] + } + if (isDiscrete(par)) { + x$values = par$values + } + return(x) + }) + setNames(xs, pids) +} + +#' @export +splitVectorParams.ParamSet = function(par) { + ps = lapply(par$pars, splitVectorParams.Param) + ps = unlist(ps, recursive = FALSE) + names(ps) = extractSubList(ps, "id") + par$pars = ps + return(par) +} + diff --git a/man/LearnerParam.Rd b/man/LearnerParam.Rd index ee103df7..9dde21a6 100644 --- a/man/LearnerParam.Rd +++ b/man/LearnerParam.Rd @@ -85,8 +85,6 @@ this parameter only makes sense if its requirements are satisfied (dependent par Can be an object created either with \code{expression} or \code{quote}, the former type is auto-converted into the later. Only really useful if the parameter is included in a \code{\link{ParamSet}}. -Note that if your dependent parameter is a logical Boolean you need to verbosely write -\code{requires = quote(a == TRUE)} and not \code{requires = quote(a)}. Default is \code{NULL} which means no requirements.} \item{tunable}{[\code{logical(1)}]\cr diff --git a/man/Param.Rd b/man/Param.Rd index ab407a64..3c9498fa 100644 --- a/man/Param.Rd +++ b/man/Param.Rd @@ -91,8 +91,6 @@ this parameter only makes sense if its requirements are satisfied (dependent par Can be an object created either with \code{expression} or \code{quote}, the former type is auto-converted into the later. Only really useful if the parameter is included in a \code{\link{ParamSet}}. -Note that if your dependent parameter is a logical Boolean you need to verbosely write -\code{requires = quote(a == TRUE)} and not \code{requires = quote(a)}. Default is \code{NULL} which means no requirements.} \item{tunable}{[\code{logical(1)}]\cr diff --git a/man/getTypeStrings.Rd b/man/getTypeStrings.Rd index 75cd42c4..37ad7b92 100644 --- a/man/getTypeStrings.Rd +++ b/man/getTypeStrings.Rd @@ -9,6 +9,7 @@ \alias{getTypeStringsCharacter} \alias{getTypeStringsDiscrete} \alias{getTypeStringsLogical} +\alias{getTypeStringsAll} \title{Get parameter type-strings.} \usage{ getTypeStringsAll() @@ -24,6 +25,8 @@ getTypeStringsCharacter() getTypeStringsDiscrete(include.logical = TRUE) getTypeStringsLogical() + +getTypeStringsAll() } \arguments{ \item{include.int}{[\code{logical(1)}]\cr diff --git a/man/splitVectorParams.Rd b/man/splitVectorParams.Rd new file mode 100644 index 00000000..09eeb5eb --- /dev/null +++ b/man/splitVectorParams.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/splitVectorParams.R +\name{splitVectorParams} +\alias{splitVectorParams} +\title{Split up a vector param into a list of length-1 params.} +\usage{ +splitVectorParams(par) +} +\arguments{ +\item{par}{[\code{\link{Param}} | \code{\link{ParamSet}}]\cr +Parameter or parameter set.} +} +\value{ +[list of \code{\link{Param}} | \code{\link{ParamSet}}]. + Return a list for single params and a (converted) param set for param sets. +} +\description{ +Splits up vector params into a multiple length-1 params. +Sometimes a useful conversion if it is nicer to operate on +the individual normal params. +} diff --git a/tests/testthat/test_splitVectorParams.R b/tests/testthat/test_splitVectorParams.R new file mode 100644 index 00000000..cd1c6e36 --- /dev/null +++ b/tests/testthat/test_splitVectorParams.R @@ -0,0 +1,35 @@ +context("splitVectorParams") + +test_that("splitVectorParams", { + p1 = makeNumericParam("p1", lower = 1) + expect_equal(splitVectorParams(p1), list(p1 = p1)) + + p2 = makeNumericVectorParam(id = "p2", lower = -1, upper = 1, len = 2) + expect_equal(splitVectorParams(p2), list( + p21 = makeNumericParam("p21", lower = -1, upper = 1), + p22 = makeNumericParam("p22", lower = -1, upper = 1) + )) + + p3 = makeDiscreteVectorParam("p3", len = 3, values = c("a", "b")) + expect_equal(splitVectorParams(p3), list( + p31 = makeDiscreteParam("p31", values = c("a", "b")), + p32 = makeDiscreteParam("p32", values = c("a", "b")), + p33 = makeDiscreteParam("p33", values = c("a", "b")) + )) + + ps = makeParamSet(p1) + expect_equal(splitVectorParams(ps), makeParamSet(p1)) + + ps = makeParamSet(p1, p2, p3) + ps2 = splitVectorParams(ps) + expect_equal(ps2, makeParamSet(p1, + p21 = makeNumericParam("p21", lower = -1, upper = 1), + p22 = makeNumericParam("p22", lower = -1, upper = 1), + p31 = makeDiscreteParam("p31", values = c("a", "b")), + p32 = makeDiscreteParam("p32", values = c("a", "b")), + p33 = makeDiscreteParam("p33", values = c("a", "b")) + )) + expect_true(isFeasible(ps2, list(p1 = 1, p21 = 0, p22 = 0, p31 = "a", p32 = "a", p33 = "a"))) +}) + +