From 90eb0b37302bfc4fa1be4b2fffc881bc262386b5 Mon Sep 17 00:00:00 2001 From: Bernd Bischl Date: Thu, 22 Dec 2016 19:20:32 +0100 Subject: [PATCH 1/3] ... --- R/splitUpVectorParam.R | 36 ++++++++++++++++++++++++++++++++++++ test.R | 7 +++++++ 2 files changed, 43 insertions(+) create mode 100644 R/splitUpVectorParam.R create mode 100644 test.R diff --git a/R/splitUpVectorParam.R b/R/splitUpVectorParam.R new file mode 100644 index 00000000..9fa07c71 --- /dev/null +++ b/R/splitUpVectorParam.R @@ -0,0 +1,36 @@ +#' @title Split up a vector param into a list of ind +#' +#' @description +#' Useful if vectors are included. +#' +#' @template arg_par_or_set +#' @param repeated [\code{logical(1)}]\cr +#' Should ids be repeated length-times if parameter is a vector? +#' Default is \code{FALSE}. +#' @param with.nr [\code{logical(1)}]\cr +#' Should number from 1 to length be appended to id if \code{repeated} is \code{TRUE}? +#' Otherwise ignored. +#' Default is \code{FALSE}. +#' @return [\code{character}]. +#' @export +#' @examples +#' ps = makeParamSet( +#' makeNumericParam("u"), +#' makeIntegerVectorParam("v", len = 2) +#' ) +#' getParamIds(ps) +#' getParamIds(ps, repeated = TRUE) +#' getParamIds(ps, repeated = TRUE, with.nr = TRUE) +splitVectorParam = function(par) { + assertClass(par, "Param") + if (!isVector(par)) + stopf("Function can only be applied to vector params, you passed: '%s'", class(par)[1L]) + pids = getParamIds(par, repeated = TRUE, with.nr = TRUE) + xs = lapply(pids, function(pid) { + x = par + x$type = gsub("vector", "", par$type) + x$len = 1L + return(x) + }) + setNames(xs, pids) +} diff --git a/test.R b/test.R new file mode 100644 index 00000000..100c98f7 --- /dev/null +++ b/test.R @@ -0,0 +1,7 @@ +load_all() + +p = makeNumericVectorParam(id = "x", lower = -1L, upper = 1, len = 2) + +ps = splitVectorParam(p) +print(ps) + From 1ad9f0e5879279502777d284da369b3557158392 Mon Sep 17 00:00:00 2001 From: Bernd Bischl Date: Fri, 23 Dec 2016 18:36:40 +0100 Subject: [PATCH 2/3] ... --- NAMESPACE | 4 +++ R/getTypeStrings.R | 9 +++++ R/splitUpVectorParam.R | 36 -------------------- R/splitVectorParams.R | 45 +++++++++++++++++++++++++ man/getTypeStrings.Rd | 3 ++ man/splitVectorParams.Rd | 22 ++++++++++++ test.R | 7 ---- tests/testthat/test_splitVectorParams.R | 35 +++++++++++++++++++ 8 files changed, 118 insertions(+), 43 deletions(-) delete mode 100644 R/splitUpVectorParam.R create mode 100644 R/splitVectorParams.R create mode 100644 man/splitVectorParams.Rd delete mode 100644 test.R create mode 100644 tests/testthat/test_splitVectorParams.R diff --git a/NAMESPACE b/NAMESPACE index 5b695f54..9fc646fa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -64,6 +64,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) @@ -100,6 +102,7 @@ export(getParamSet) export(getParamTypeCounts) export(getParamTypes) export(getRequirements) +export(getTypeStringsAll) export(getTypeStringsDiscrete) export(getTypeStringsInteger) export(getTypeStringsNumeric) @@ -165,6 +168,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 f3640ebc..ce939822 100644 --- a/R/getTypeStrings.R +++ b/R/getTypeStrings.R @@ -38,3 +38,12 @@ getTypeStringsDiscrete = function(include.logical = TRUE) { c("discrete", "discretevector") } + +#' @export +#' @rdname getTypeStrings +getTypeStringsAll = function() { + c("numeric", "integer", "numericvector", "integervector", "discrete", + "discretevector", "logical", "logicalvector", "character", "charactervector", + "function", "untyped") +} + diff --git a/R/splitUpVectorParam.R b/R/splitUpVectorParam.R deleted file mode 100644 index 9fa07c71..00000000 --- a/R/splitUpVectorParam.R +++ /dev/null @@ -1,36 +0,0 @@ -#' @title Split up a vector param into a list of ind -#' -#' @description -#' Useful if vectors are included. -#' -#' @template arg_par_or_set -#' @param repeated [\code{logical(1)}]\cr -#' Should ids be repeated length-times if parameter is a vector? -#' Default is \code{FALSE}. -#' @param with.nr [\code{logical(1)}]\cr -#' Should number from 1 to length be appended to id if \code{repeated} is \code{TRUE}? -#' Otherwise ignored. -#' Default is \code{FALSE}. -#' @return [\code{character}]. -#' @export -#' @examples -#' ps = makeParamSet( -#' makeNumericParam("u"), -#' makeIntegerVectorParam("v", len = 2) -#' ) -#' getParamIds(ps) -#' getParamIds(ps, repeated = TRUE) -#' getParamIds(ps, repeated = TRUE, with.nr = TRUE) -splitVectorParam = function(par) { - assertClass(par, "Param") - if (!isVector(par)) - stopf("Function can only be applied to vector params, you passed: '%s'", class(par)[1L]) - pids = getParamIds(par, repeated = TRUE, with.nr = TRUE) - xs = lapply(pids, function(pid) { - x = par - x$type = gsub("vector", "", par$type) - x$len = 1L - return(x) - }) - setNames(xs, pids) -} 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/getTypeStrings.Rd b/man/getTypeStrings.Rd index e74d2ebb..ed883f16 100644 --- a/man/getTypeStrings.Rd +++ b/man/getTypeStrings.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/getTypeStrings.R \name{getTypeStrings} \alias{getTypeStrings} +\alias{getTypeStringsAll} \alias{getTypeStringsDiscrete} \alias{getTypeStringsInteger} \alias{getTypeStringsNumeric} @@ -15,6 +16,8 @@ getTypeStringsNumericStrict() getTypeStringsInteger() getTypeStringsDiscrete(include.logical = TRUE) + +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..86eb3fe2 --- /dev/null +++ b/man/splitVectorParams.Rd @@ -0,0 +1,22 @@ +% 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/test.R b/test.R deleted file mode 100644 index 100c98f7..00000000 --- a/test.R +++ /dev/null @@ -1,7 +0,0 @@ -load_all() - -p = makeNumericVectorParam(id = "x", lower = -1L, upper = 1, len = 2) - -ps = splitVectorParam(p) -print(ps) - 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"))) +}) + + From 22d32850d04d22ab0d9bde18359ee969632a9440 Mon Sep 17 00:00:00 2001 From: Jakob Richter Date: Wed, 9 Aug 2017 11:33:26 +0200 Subject: [PATCH 3/3] roxygen --- NAMESPACE | 13 ++++++++++--- man/LearnerParam.Rd | 2 -- man/Param.Rd | 2 -- man/getTypeStrings.Rd | 14 +++++++++++--- man/splitVectorParams.Rd | 1 - 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 9fc646fa..6cea8894 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -37,6 +37,7 @@ S3method(hasRequires,Param) S3method(hasRequires,ParamSet) S3method(hasTrafo,Param) S3method(hasTrafo,ParamSet) +S3method(isCharacter,Param) S3method(isCharacter,ParamSet) S3method(isDiscrete,Param) S3method(isDiscrete,ParamSet) @@ -50,8 +51,6 @@ S3method(isLogical,Param) S3method(isLogical,ParamSet) S3method(isNumeric,Param) S3method(isNumeric,ParamSet) -S3method(isNumericStrict,Param) -S3method(isNumericStrict,ParamSet) S3method(isVector,Param) S3method(isVector,ParamSet) S3method(paramValueToString,Param) @@ -79,6 +78,7 @@ export(filterParams) export(filterParamsDiscrete) export(filterParamsNumeric) export(generateDesign) +export(generateDesignOfDefaults) export(generateGridDesign) export(generateRandomDesign) export(getDefaults) @@ -103,8 +103,10 @@ export(getParamTypeCounts) export(getParamTypes) export(getRequirements) export(getTypeStringsAll) +export(getTypeStringsCharacter) export(getTypeStringsDiscrete) export(getTypeStringsInteger) +export(getTypeStringsLogical) export(getTypeStringsNumeric) export(getTypeStringsNumericStrict) export(getUpper) @@ -120,17 +122,22 @@ export(hasNumeric) export(hasRequires) export(hasTrafo) export(isCharacter) +export(isCharacterTypeString) export(isDiscrete) +export(isDiscreteTypeString) export(isEmpty) export(isFeasible) export(isForbidden) export(isInteger) +export(isIntegerTypeString) export(isLogical) +export(isLogicalTypeString) export(isNumeric) -export(isNumericStrict) +export(isNumericTypeString) export(isRequiresOk) export(isSpecialValue) export(isVector) +export(isVectorTypeString) export(makeCharacterParam) export(makeCharacterVectorParam) export(makeDiscreteLearnerParam) 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 ed883f16..37ad7b92 100644 --- a/man/getTypeStrings.Rd +++ b/man/getTypeStrings.Rd @@ -3,20 +3,29 @@ \name{getTypeStrings} \alias{getTypeStrings} \alias{getTypeStringsAll} -\alias{getTypeStringsDiscrete} -\alias{getTypeStringsInteger} \alias{getTypeStringsNumeric} \alias{getTypeStringsNumericStrict} +\alias{getTypeStringsInteger} +\alias{getTypeStringsCharacter} +\alias{getTypeStringsDiscrete} +\alias{getTypeStringsLogical} +\alias{getTypeStringsAll} \title{Get parameter type-strings.} \usage{ +getTypeStringsAll() + getTypeStringsNumeric(include.int = TRUE) getTypeStringsNumericStrict() getTypeStringsInteger() +getTypeStringsCharacter() + getTypeStringsDiscrete(include.logical = TRUE) +getTypeStringsLogical() + getTypeStringsAll() } \arguments{ @@ -34,4 +43,3 @@ Default is \code{TRUE}.} \description{ Returns type strings used in \code{param$type} for certain groups of parameters. } - diff --git a/man/splitVectorParams.Rd b/man/splitVectorParams.Rd index 86eb3fe2..09eeb5eb 100644 --- a/man/splitVectorParams.Rd +++ b/man/splitVectorParams.Rd @@ -19,4 +19,3 @@ 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. } -