Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export(isRequiresOk)
export(isSpecialValue)
export(isVector)
export(isVectorTypeString)
export(makeCharacterLearnerParam)
export(makeCharacterParam)
export(makeCharacterVectorLearnerParam)
export(makeCharacterVectorParam)
export(makeDiscreteLearnerParam)
export(makeDiscreteParam)
Expand Down
3 changes: 1 addition & 2 deletions R/aParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions R/convertDiscrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)])
}
}
20 changes: 19 additions & 1 deletion R/makeLearnerParamFuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}


14 changes: 8 additions & 6 deletions R/makeParamFuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

6 changes: 5 additions & 1 deletion R/paramValueToString.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 10 additions & 2 deletions man/LearnerParam.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/Param.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/test_LearnerParam.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test_Param.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down