diff --git a/NAMESPACE b/NAMESPACE index 821b2ec2..6c7a1f9c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -136,7 +136,9 @@ export(isRequiresOk) export(isSpecialValue) export(isVector) export(isVectorTypeString) +export(makeCharacterLearnerParam) export(makeCharacterParam) +export(makeCharacterVectorLearnerParam) export(makeCharacterVectorParam) export(makeDiscreteLearnerParam) export(makeDiscreteParam) diff --git a/R/aParam.R b/R/aParam.R index fe38553c..78c4889d 100644 --- a/R/aParam.R +++ b/R/aParam.R @@ -77,8 +77,7 @@ #' Defining a parameter to be not-tunable allows to mark arguments like, e.g., \dQuote{verbose} or #' other purely technical stuff, and allows them to be excluded from later automatic optimization #' procedures that would try to consider all available parameters. -#' Default is \code{TRUE} (except for \code{untyped}, \code{function}, \code{character} and -#' \code{characterVector}) which means it is tunable. +#' Default is \code{TRUE}, which means it is tunable. #' @param special.vals [\code{list()}]\cr #' A list of special values the parameter can except which are outside of the defined range. #' Default is an empty list. diff --git a/R/convertDiscrete.R b/R/convertDiscrete.R index 9c134582..c8bccd40 100644 --- a/R/convertDiscrete.R +++ b/R/convertDiscrete.R @@ -53,7 +53,7 @@ discreteValueToName = function(par, x) { return(NA_character_) assertClass(par, "Param") assertChoice(par$type, c("discrete", "discretevector")) - if (par$type == "discretevector" && length(x) != par$len) + if (par$type == "discretevector" && isTRUE(length(x) != par$len)) stopf("Length of x must be %i!", par$len) ns = names(par$values) getIndex = function(values, v) { @@ -65,6 +65,6 @@ discreteValueToName = function(par, x) { if (par$type == "discrete") { ns[getIndex(par$values, x)] } else if (par$type == "discretevector") { - ns[sapply(x, getIndex, values = par$values)] + vcapply(x, function(x) ns[getIndex(x, values = par$values)]) } } diff --git a/R/makeLearnerParamFuns.R b/R/makeLearnerParamFuns.R index 73800533..ed216081 100644 --- a/R/makeLearnerParamFuns.R +++ b/R/makeLearnerParamFuns.R @@ -64,7 +64,7 @@ makeLogicalLearnerParam = function(id, default, when = "train", requires = NULL, makeLogicalVectorLearnerParam = function(id, len = as.integer(NA), default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) { values = list("TRUE" = TRUE, "FALSE" = FALSE) - makeParam(id = id, type = "logicalvector", learner.param = TRUE, len = len,values = values, + makeParam(id = id, type = "logicalvector", learner.param = TRUE, len = len, values = values, default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) } @@ -83,3 +83,21 @@ makeFunctionLearnerParam = function(id, default, when = "train", requires = NULL } +#' @rdname LearnerParam +#' @export +makeCharacterLearnerParam = function(id, default, when = "train", requires = NULL, tunable = TRUE, + special.vals = list()) { + makeParam(id = id, type = "character", learner.param = TRUE, + default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) +} + +#' @rdname LearnerParam +#' @export +makeCharacterVectorLearnerParam = function(id, len = as.integer(NA), default, when = "train", + requires = NULL, tunable = TRUE, special.vals = list()) { + + makeParam(id = id, type = "charactervector", learner.param = TRUE, len = len, + default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) +} + + diff --git a/R/makeParamFuns.R b/R/makeParamFuns.R index d65c51b0..82928260 100644 --- a/R/makeParamFuns.R +++ b/R/makeParamFuns.R @@ -76,10 +76,11 @@ makeDiscreteVectorParam = function(id, len, values, default, requires = NULL, #' @rdname Param #' @export -makeFunctionParam = function(id, default = default, requires = NULL, special.vals = list()) { +makeFunctionParam = function(id, default = default, requires = NULL, tunable = TRUE, + special.vals = list()) { makeParam(id = id, type = "function", learner.param = FALSE, values = NULL, default = default, trafo = NULL, - requires = requires, tunable = FALSE, special.vals = special.vals) + requires = requires, tunable = tunable, special.vals = special.vals) } #FIXME: what happens if NA is later used for untyped params? because we might interpret this as @@ -94,19 +95,20 @@ makeUntypedParam = function(id, default, requires = NULL, tunable = TRUE, specia #' @rdname Param #' @export -makeCharacterParam = function(id, default, requires = NULL, special.vals = list()) { +makeCharacterParam = function(id, default, requires = NULL, tunable = TRUE, + special.vals = list()) { makeParam(id = id, type = "character", learner.param = FALSE, default = default, trafo = NULL, - requires = requires, tunable = FALSE, special.vals = special.vals) + requires = requires, tunable = tunable, special.vals = special.vals) } #' @rdname Param #' @export makeCharacterVectorParam = function(id, len, cnames = NULL, default, - requires = NULL, special.vals = list()) { + requires = NULL, tunable = TRUE, special.vals = list()) { makeParam(id = id, type = "charactervector", learner.param = FALSE, len = len, cnames = cnames, default = default, - trafo = NULL, requires = requires, tunable = FALSE, special.vals = special.vals) + trafo = NULL, requires = requires, tunable = tunable, special.vals = special.vals) } diff --git a/R/paramValueToString.R b/R/paramValueToString.R index 4e67c919..ea755c0c 100644 --- a/R/paramValueToString.R +++ b/R/paramValueToString.R @@ -53,8 +53,12 @@ paramValueToString.Param = function(par, x, show.missing.values = FALSE, num.for else return("") } - if (isDiscrete(par, include.logical = FALSE)) + if (isDiscrete(par, include.logical = FALSE)) { x = discreteValueToName(par, x) + if (length(x) == 0) { + x = "list()" + } + } s = convertToShortString(x, num.format = num.format) } diff --git a/man/LearnerParam.Rd b/man/LearnerParam.Rd index ee103df7..709a15b6 100644 --- a/man/LearnerParam.Rd +++ b/man/LearnerParam.Rd @@ -12,6 +12,8 @@ \alias{makeLogicalVectorLearnerParam} \alias{makeUntypedLearnerParam} \alias{makeFunctionLearnerParam} +\alias{makeCharacterLearnerParam} +\alias{makeCharacterVectorLearnerParam} \title{Create a description object for a parameter of a machine learning algorithm.} \usage{ makeNumericLearnerParam(id, lower = -Inf, upper = Inf, allow.inf = FALSE, @@ -49,6 +51,13 @@ makeUntypedLearnerParam(id, default, when = "train", requires = NULL, makeFunctionLearnerParam(id, default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) + +makeCharacterLearnerParam(id, default, when = "train", requires = NULL, + tunable = TRUE, special.vals = list()) + +makeCharacterVectorLearnerParam(id, len = as.integer(NA), default, + when = "train", requires = NULL, tunable = TRUE, + special.vals = list()) } \arguments{ \item{id}{[\code{character(1)}]\cr @@ -94,8 +103,7 @@ Is this parameter tunable? Defining a parameter to be not-tunable allows to mark arguments like, e.g., \dQuote{verbose} or other purely technical stuff, and allows them to be excluded from later automatic optimization procedures that would try to consider all available parameters. -Default is \code{TRUE} (except for \code{untyped}, \code{function}, \code{character} and -\code{characterVector}) which means it is tunable.} +Default is \code{TRUE}, which means it is tunable.} \item{special.vals}{[\code{list()}]\cr A list of special values the parameter can except which are outside of the defined range. diff --git a/man/Param.Rd b/man/Param.Rd index ab407a64..56582546 100644 --- a/man/Param.Rd +++ b/man/Param.Rd @@ -43,16 +43,17 @@ makeDiscreteParam(id, values, trafo = NULL, default, requires = NULL, makeDiscreteVectorParam(id, len, values, default, requires = NULL, tunable = TRUE, special.vals = list()) -makeFunctionParam(id, default = default, requires = NULL, +makeFunctionParam(id, default = default, requires = NULL, tunable = TRUE, special.vals = list()) makeUntypedParam(id, default, requires = NULL, tunable = TRUE, special.vals = list()) -makeCharacterParam(id, default, requires = NULL, special.vals = list()) +makeCharacterParam(id, default, requires = NULL, tunable = TRUE, + special.vals = list()) makeCharacterVectorParam(id, len, cnames = NULL, default, requires = NULL, - special.vals = list()) + tunable = TRUE, special.vals = list()) } \arguments{ \item{id}{[\code{character(1)}]\cr @@ -100,8 +101,7 @@ Is this parameter tunable? Defining a parameter to be not-tunable allows to mark arguments like, e.g., \dQuote{verbose} or other purely technical stuff, and allows them to be excluded from later automatic optimization procedures that would try to consider all available parameters. -Default is \code{TRUE} (except for \code{untyped}, \code{function}, \code{character} and -\code{characterVector}) which means it is tunable.} +Default is \code{TRUE}, which means it is tunable.} \item{special.vals}{[\code{list()}]\cr A list of special values the parameter can except which are outside of the defined range. diff --git a/tests/testthat/test_LearnerParam.R b/tests/testthat/test_LearnerParam.R index 3ad450e2..e4186272 100644 --- a/tests/testthat/test_LearnerParam.R +++ b/tests/testthat/test_LearnerParam.R @@ -70,6 +70,32 @@ test_that("disc vec", { p = makeDiscreteVectorLearnerParam(id = "x", len = NA_integer_, values = list("a", "b"), default = list("a", "b", "a")) }) +test_that("function", { + p = makeFunctionLearnerParam("f1") + expect_equal(p$id, "f1") + expect_true(!isFeasible(p, "a")) + expect_true(isFeasible(p, identity)) + expect_true(isFeasible(p, function(x) x^2)) + # defaults + p = makeFunctionLearnerParam(id = "f2", default = sin) + expect_equal(getDefaults(p), sin) +}) + +test_that("character", { + p = makeCharacterLearnerParam("c") + expect_equal(p$id, "c") + expect_true(isFeasible(p, "ab")) + expect_true(!isFeasible(p, c("a","b"))) + expect_true(!isFeasible(p, 1)) +}) + +test_that("character vec", { + p = makeCharacterVectorLearnerParam("cv") + expect_equal(p$id, "cv") + expect_true(isFeasible(p, "a")) + expect_true(isFeasible(p, c("a","b","a"))) + expect_true(!isFeasible(p, 1:3)) +}) test_that("untyped", { p = makeUntypedLearnerParam("x") diff --git a/tests/testthat/test_Param.R b/tests/testthat/test_Param.R index a7293f63..ec9d27a8 100644 --- a/tests/testthat/test_Param.R +++ b/tests/testthat/test_Param.R @@ -279,6 +279,9 @@ test_that("param print works", { expect_output(print(p), "99") p = makeUntypedParam(id = "x", default = c(99, 99)) expect_output(print(p), "untyped") + p = makeDiscreteVectorLearnerParam("test", + default = list(), values = c("a", "b", "c"), len = NA) + expect_output(print(p), "discretevector") }) test_that("normal (not learner) vec param cannot have NA lengths", {