Skip to content

Commit

Permalink
add setupStoppingCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Feb 28, 2015
1 parent 028a704 commit 8e41b03
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export(makePermutationGenerator)
export(makeStoppingCondition)
export(makeUniformGenerator)
export(nullRecombinator)
export(setupStoppingConditions)
export(swapMutator)
import(BBmisc)
import(ParamHelpers)
Expand Down
27 changes: 27 additions & 0 deletions R/setupStoppingConditions.R
Original file line number Diff line number Diff line change
@@ -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)
)
}
2 changes: 1 addition & 1 deletion R/stoppingCondition.max.time.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion man/makeMaximumTimeStoppingCondition.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions man/setupStoppingConditions.Rd
Original file line number Diff line number Diff line change
@@ -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.
}

15 changes: 15 additions & 0 deletions tests/testthat/test_setupStoppingConditions.R
Original file line number Diff line number Diff line change
@@ -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")
})

})

0 comments on commit 8e41b03

Please sign in to comment.