Skip to content

Commit

Permalink
pass opt path to stopping conditions instead of envir
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Mar 16, 2015
1 parent b28bc67 commit c970e58
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion R/autoplotECRResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ autoplot.ecr_result = function(object, xlim = NULL, ylim = NULL, show.process =
plotTrace = function(df, xlim, ylim, log.fitness, ...) {
ggdf = df[c("dob", "pop.min.fitness", "pop.mean.fitness", "pop.median.fitness", "pop.max.fitness")]
xlim = BBmisc::coalesce(xlim, c(0, max(ggdf$dob)))
ylim = BBmisc::coalesce(ylim, c(0, max(ggdf$pop.max.fitness)))
ylim = BBmisc::coalesce(ylim, c(min(ggdf$pop.min.fitness), max(ggdf$pop.max.fitness)))
assertNumeric(ylim, len = 2L, any.missing = FALSE)
assertNumeric(xlim, len = 2L, any.missing = FALSE)
assertFlag(log.fitness)
Expand Down
26 changes: 13 additions & 13 deletions R/doTerminate.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
# @param envir [environment]
# Environment in which to look for some maybe neccessary variables.
# @return [stopObject]
doTerminate = function(stopping.funs, envir = parent.frame()) {
stopObject = list()
doTerminate = function(stopping.funs, opt.path) {
stopObject = list()

# if we have not specified any stopping conditions always return the empty object
if (!length(stopping.funs)) {
return(stopObject)
}
if (!length(stopping.funs)) {
return(stopObject)
}

# otherwise iterate over stopping conditions and check
for (stopping.fun in stopping.funs) {
shouldStop = stopping.fun(envir = envir)
if (shouldStop) {
stopObject$name = attr(stopping.fun, "name")
stopObject$message = attr(stopping.fun, "message")
break
}
for (stopping.fun in stopping.funs) {
shouldStop = stopping.fun(opt.path = opt.path)
if (shouldStop) {
stopObject$name = attr(stopping.fun, "name")
stopObject$message = attr(stopping.fun, "message")
break
}
return(stopObject)
}
return(stopObject)
}
39 changes: 22 additions & 17 deletions R/ecr.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,28 @@ ecr = function(objective.fun, control) {
population$fitness = computeFitness(population, objective.fun)
best = getBestIndividual(population)

opt.path = makeOptPathDF(par.set, y.names = "y", minimize = TRUE, include.extra = TRUE)
opt.path = addBestToOptPath(opt.path, par.set, best, population$fitness, 0)
buildExtras = function(iter, start.time, fitness) {
return(list(
past.time = as.numeric(Sys.time() - start.time),
iter = iter,
pop.min.fitness = min(fitness),
pop.mean.fitness = mean(fitness),
pop.median.fitness = median(fitness),
pop.max.fitness = max(fitness)
))
}

iter = 1L
start.time = Sys.time()

opt.path = makeOptPathDF(par.set, y.names = "y", minimize = TRUE, include.extra = TRUE, include.exec.time = TRUE)
opt.path = addBestToOptPath(opt.path, par.set, best, population$fitness, generation = iter, extra = buildExtras(iter, start.time, population$fitness), exec.time = 0.0)

population.storage = namedList(control$save.population.at)
if (0 %in% control$save.population.at) {
population.storage[[as.character(0)]] = population
}

iter = 1L
start.time = Sys.time()

monitor$before()

repeat {
Expand All @@ -111,9 +122,9 @@ ecr = function(objective.fun, control) {
}

best = getBestIndividual(population)
opt.path = addBestToOptPath(opt.path, par.set, best, population$fitness, iter)
opt.path = addBestToOptPath(opt.path, par.set, best, population$fitness, generation = iter, exec.time = 0.0, extra = buildExtras(iter, start.time, population$fitness))

stop.object = doTerminate(control$stopping.conditions)
stop.object = doTerminate(control$stopping.conditions, opt.path)
if (length(stop.object) > 0L) {
break
}
Expand All @@ -132,8 +143,8 @@ ecr = function(objective.fun, control) {
opt.path = opt.path,
population.storage = population.storage,
message = stop.object$message
), class = "ecr_result")
)
), class = "ecr_result")
)
}

#' Print the result of an ecr run.
Expand Down Expand Up @@ -163,19 +174,13 @@ print.ecr_result = function(x, ...) {
# @param generation [\code{integer(1)}]\cr
# Current generation.
# @return [\code{\link[ParamHelpers]{OptPathDF}}]
addBestToOptPath = function(opt.path, par.set, best, fitness, generation) {
addBestToOptPath = function(opt.path, par.set, best, fitness, generation, exec.time, extra) {
if (length(par.set$pars) == 1L) {
best.param.values = list(best$individual)
} else {
best.param.values = as.list(best$individual)
names(best.param.values) = getParamIds(par.set, repeated = TRUE, with.nr = TRUE)
}
extras = list(
pop.min.fitness = min(fitness),
pop.mean.fitness = mean(fitness),
pop.median.fitness = median(fitness),
pop.max.fitness = max(fitness)
)
addOptPathEl(opt.path, x = best.param.values, y = best$fitness, dob = generation, extra = extras)
addOptPathEl(opt.path, x = best.param.values, y = best$fitness, dob = generation, exec.time = exec.time, extra = extra)
return(opt.path)
}
6 changes: 3 additions & 3 deletions R/makeStoppingCondition.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Wrap a function within a stopping condition object.
#'
#' @param condition.fun [\code{function}]\cr
#' Function which takes an environment \code{envir} as its only parameter and return
#' a single logical.
#' Function which takes an environment \code{\link[ParamHelpers]{OptPath}} as its
#' only parameter and return a single logical.
#' @param name [\code{character(1)}]\cr
#' Identifier for the stopping condition.
#' @param message [\code{character(1)}]\cr
Expand All @@ -11,7 +11,7 @@
#' @return [\code{ecr_stoppingCondition}]
#' @export
makeStoppingCondition = function(condition.fun, name, message) {
assertFunction(condition.fun, args = c("envir"))
assertFunction(condition.fun, args = c("opt.path"))
assertCharacter(name, len = 1L, any.missing = FALSE)
assertCharacter(message, len = 1L, any.missing = FALSE)

Expand Down
27 changes: 14 additions & 13 deletions R/stoppingCondition.max.iter.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
#' @return [\code{function}]
#' @export
makeMaximumIterationsStoppingCondition = function(max.iter = NULL) {
if (!is.null(max.iter)) {
assertInt(max.iter, lower = 1L, na.ok = FALSE)
} else {
max.iter = Inf
}
force(max.iter)
if (!is.null(max.iter)) {
assertInt(max.iter, lower = 1L, na.ok = FALSE)
} else {
max.iter = Inf
}
force(max.iter)

condition.fun = function(envir = parent.frame()) {
envir$iter > max.iter
}
condition.fun = function(opt.path) {
iter.vector = getOptPathCol(opt.path, "iter")
max(iter.vector) > max.iter
}

makeStoppingCondition(
condition.fun,
name = "IterLimit",
message = sprintf("Maximum number of iterations reached: '%i'", max.iter)
makeStoppingCondition(
condition.fun,
name = "IterLimit",
message = sprintf("Maximum number of iterations reached: '%i'", max.iter)
)
}
31 changes: 14 additions & 17 deletions R/stoppingCondition.max.time.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
#' @return [\code{function}]
#' @export
makeMaximumTimeStoppingCondition = function(max.time = NULL) {
if (!is.null(max.time)) {
assertInt(max.time, lower = 1L, na.ok = FALSE)
} else {
max.time = Inf
}
force(max.time)
if (!is.null(max.time)) {
assertInt(max.time, lower = 1L, na.ok = FALSE)
} else {
max.time = Inf
}
force(max.time)

condition.fun = function(envir = parent.frame()) {
time = Sys.time()
#FIXME: hmm, somehow this is ugly with the environments. We need to
# believe, that this stuff (like start.time) exists.
timediff = difftime(time, envir$start.time, units = "secs")
timediff >= max.time
}
condition.fun = function(opt.path) {
times.vec = getOptPathCol(opt.path, "past.time")
max(times.vec) >= max.time
}

makeStoppingCondition(
condition.fun,
name = "TimeLimit",
message = sprintf("Time limit reached: '%s' [seconds]", max.time)
makeStoppingCondition(
condition.fun,
name = "TimeLimit",
message = sprintf("Time limit reached: '%s' [seconds]", max.time)
)
}
4 changes: 2 additions & 2 deletions man/makeStoppingCondition.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ makeStoppingCondition(condition.fun, name, message)
}
\arguments{
\item{condition.fun}{[\code{function}]\cr
Function which takes an environment \code{envir} as its only parameter and return
a single logical.}
Function which takes an environment \code{\link[ParamHelpers]{OptPath}} as its
only parameter and return a single logical.}

\item{name}{[\code{character(1)}]\cr
Identifier for the stopping condition.}
Expand Down

0 comments on commit c970e58

Please sign in to comment.