From 8e41b03ffc54dde0aafd32f3556bdd9fe64f8c4b Mon Sep 17 00:00:00 2001 From: Jakob Bossek Date: Sat, 28 Feb 2015 12:25:43 +0100 Subject: [PATCH] add setupStoppingCondition --- NAMESPACE | 1 + R/setupStoppingConditions.R | 27 +++++++++++++++++++ R/stoppingCondition.max.time.R | 2 +- man/makeMaximumTimeStoppingCondition.Rd | 2 +- man/setupStoppingConditions.Rd | 25 +++++++++++++++++ tests/testthat/test_setupStoppingConditions.R | 15 +++++++++++ 6 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 R/setupStoppingConditions.R create mode 100644 man/setupStoppingConditions.Rd create mode 100644 tests/testthat/test_setupStoppingConditions.R diff --git a/NAMESPACE b/NAMESPACE index dbc4210..6d5adc6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -30,6 +30,7 @@ export(makePermutationGenerator) export(makeStoppingCondition) export(makeUniformGenerator) export(nullRecombinator) +export(setupStoppingConditions) export(swapMutator) import(BBmisc) import(ParamHelpers) diff --git a/R/setupStoppingConditions.R b/R/setupStoppingConditions.R new file mode 100644 index 0000000..c10f437 --- /dev/null +++ b/R/setupStoppingConditions.R @@ -0,0 +1,27 @@ +#' Generator for some frequently used stopping conditions. +#' +#' Setting up stopping conditions is flexible and straight-forward, but +#' it needs a lot of writing. This function generates a list of frequently +#' used stopping conditions with just a single function call. +#' +#' @param max.iter [\code{integer(1)}]\cr +#' Maximal number of iterations. Default ist \code{Inf}. +#' @param max.time [\code{integer(1)}]\cr +#' Time budget in seconds. Default ist \code{Inf}. +#' @return [list] +#' List of \code{ecr_stoppingCondition} objects, which can be passed to \code{ecr}. +#' @export +#FIXME: this is not very flexible. What if we add new stopping conditions. In +# this case we have to change this function every time. +# The stopping condition generators should have attributes 'checkFunction' and +# default value (see ecr operators). This way setupStoppingCondition could +# iterate over all available stopping conditions. +setupStoppingConditions = function(max.iter = Inf, max.time = Inf) { + if (is.infinite(max.iter) && is.infinite(max.time)) { + stopf("At least max.iter or max.time must be finite.") + } + list( + makeMaximumIterationsStoppingCondition(max.iter), + makeMaximumTimeStoppingCondition(max.time) + ) +} diff --git a/R/stoppingCondition.max.time.R b/R/stoppingCondition.max.time.R index c3a4d55..5c19100 100644 --- a/R/stoppingCondition.max.time.R +++ b/R/stoppingCondition.max.time.R @@ -4,7 +4,7 @@ #' Time budget in seconds. Default ist \code{Inf}. #' @return [\code{function}] #' @export -makeMaximumTimeStoppingCondition = function(max.time) { +makeMaximumTimeStoppingCondition = function(max.time = Inf) { assertCount(max.time, positive = TRUE, na.ok = FALSE) force(max.time) diff --git a/man/makeMaximumTimeStoppingCondition.Rd b/man/makeMaximumTimeStoppingCondition.Rd index 550d943..4db629b 100644 --- a/man/makeMaximumTimeStoppingCondition.Rd +++ b/man/makeMaximumTimeStoppingCondition.Rd @@ -4,7 +4,7 @@ \alias{makeMaximumTimeStoppingCondition} \title{Stopping condition: time limit.} \usage{ -makeMaximumTimeStoppingCondition(max.time) +makeMaximumTimeStoppingCondition(max.time = Inf) } \arguments{ \item{max.time}{[\code{integer(1)}]\cr diff --git a/man/setupStoppingConditions.Rd b/man/setupStoppingConditions.Rd new file mode 100644 index 0000000..0802bf1 --- /dev/null +++ b/man/setupStoppingConditions.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/setupStoppingConditions.R +\name{setupStoppingConditions} +\alias{setupStoppingConditions} +\title{Generator for some frequently used stopping conditions.} +\usage{ +setupStoppingConditions(max.iter = Inf, max.time = Inf) +} +\arguments{ +\item{max.iter}{[\code{integer(1)}]\cr +Maximal number of iterations. Default ist \code{Inf}.} + +\item{max.time}{[\code{integer(1)}]\cr +Time budget in seconds. Default ist \code{Inf}.} +} +\value{ +[list] +List of \code{ecr_stoppingCondition} objects, which can be passed to \code{ecr}. +} +\description{ +Setting up stopping conditions is flexible and straight-forward, but +it needs a lot of writing. This function generates a list of frequently +used stopping conditions with just a single function call. +} + diff --git a/tests/testthat/test_setupStoppingConditions.R b/tests/testthat/test_setupStoppingConditions.R new file mode 100644 index 0000000..96d4038 --- /dev/null +++ b/tests/testthat/test_setupStoppingConditions.R @@ -0,0 +1,15 @@ +context("generator for list of stopping conditions") + +test_that("generator for list of stopping conditions works well", { + # at least on of {max.time, max.iter} must be finite + expect_error(setupStoppingConditions()) + + scs = setupStoppingConditions(100L, 1e6) + expect_is(scs, "list") + expect_equal(length(scs), 2L) + lapply(scs, function(sc) { + expect_is(sc, "ecr_stoppingCondition") + }) + +}) +