Skip to content

Commit

Permalink
add possibility to visualize optimization process of numeric functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Mar 4, 2015
1 parent 332ee94 commit cdf6e67
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ License: BSD_2_clause + file LICENSE
Depends:
ParamHelpers (>= 1.3),
BBmisc (>= 1.6),
smoof
smoof (>= 1.0),
ggplot2 (>= 1.0.0)
Imports:
ggplot2,
checkmate (>= 1.1),
parallelMap (>= 1.1),
reshape2 (>= 1.4.1),
gridExtra (>= 0.9.1)
Suggests:
testthat
testthat (>= 0.9.1)
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2 (4.1.0): do not edit by hand

S3method(autoplot,ecr_result)
S3method(getOperatorCheckFunction,ecr_operator)
S3method(getOperatorDefaultParameters,ecr_operator)
S3method(getOperatorName,ecr_operator)
Expand Down Expand Up @@ -36,7 +37,7 @@ import(BBmisc)
import(ParamHelpers)
import(checkmate)
import(ggplot2)
import(gridExtra)
import(parallelMap)
import(reshape2)
import(smoof)
importFrom(gridExtra,arrangeGrob)
62 changes: 36 additions & 26 deletions R/autoplotECRResult.R
Original file line number Diff line number Diff line change
@@ -1,52 +1,62 @@
#' Plot optimization trace.
#'
#' Call this function on the result object of an \code{\link{ecr}} function call
#' to visualize the optimization trace.
#'
#' @param object [\code{ecr_result}]\cr
#' ecr result object.
#' @param xlim [\code{numeric(2)} | NULL]\cr
#' Lower and upper bound for generation. If \code{NULL}, this is set automatically.
#' @param ylim [\code{numeric(2)} | NULL]\cr
#' Lower and upper bound for fitness. If \code{NULL}, this is set automatically.
#' @param show.process [\code{logical(1)}]\cr
#' Should the function itself with the population be plotted as well? Default is
#' \code{FALSE}.
#' @param log.fitness [\code{logical(1)}]\cr
#' Log-transform fitness values? Default is \code{FALSE}.
#' @param ... [any]\cr
#' Not used.
#' @return [\code{\link[ggplot2]{ggplot}}]
# @export
#' @return Nothing.
#' @export
autoplot.ecr_result = function(object, xlim = NULL, ylim = NULL, show.process = FALSE, log.fitness = FALSE, ...) {
assertFlag(show.process, na.ok = FALSE)
obj.fun = object$objective.fun
n.params = getNumberOfParameters(obj.fun)

if (show.process) {
if (n.params > 2L || isMultiobjective(obj.fun)) {
stopf("Visualization not possible for multi-objective functions or function with greater than 2 parameters.")
}
pl.fun = autoplot(obj.fun)
if (length(object$control$save.population.at)) {
population = object$population.storage[["0"]]
op = as.data.frame(object$opt.path)
unique.dobs = unique(op$dob)
# we start with the second dob, since otherwise there is not enough info to plot
for (dob in unique.dobs[2:length(unique.dobs)]) {
pl.trace = plotTrace(op[which(op$dob <= dob), ], xlim, ylim, log.fitness, ...)
pl.trace = pl.trace + ggtitle(sprintf("Optimization trace for function '%s'", getName(object$objective.fun)))
if (show.process) {
if (n.params > 2L || isMultiobjective(obj.fun)) {
stopf("Visualization not possible for multi-objective functions or functions with greater than 2 parameters.")
}
if (!length(object$control$save.population.at)) {
stopf("Cannot visualize population since no population was stored! Take a glance a the 'save.population.at' control parameter.")
}
pl.fun = autoplot(obj.fun)
population = object$population.storage[[as.character(dob)]]
df.points = data.frame(x = population$individuals, y = population$fitness)
pl.fun = pl.fun + geom_point(data = df.points, aes(x = x, y = y), colour = "tomato")
pl.fun = pl.fun + geom_point(data = df.points, aes_string(x = "x", y = "y"), colour = "tomato")
pl.fun = pl.fun + geom_hline(yintercept = min(population$fitness), linetype = "dashed", colour = "gray")
}
if (show.process) {
#FIXME: this seems to fail!
BBmisc::requirePackages(c("grid", "gridExtra"), why = "ecr")
pl = do.call(gridExtra::arrangeGrob, list(pl.fun, pl.trace, ncol = 1))
} else {
pl = pl.trace
}
pause()
print(pl)
}

pl.trace = autoplot(object$opt.path, xlim, ylim, log.fitness, ...)
pl.trace = pl.trace + ggtitle(sprintf("Optimization trace for function '%s'", getName(object$objective.fun)))

if (show.process) {
requirePackages("gridExtra", why = "ecr")
pl = do.call(arrangeGrob, list(pl.fun, pl.trace, ncol = 1))
} else {
pl = pl.trace
}
return (pl)
}

# autoplot function for opt.path used by ecr
autoplot.OptPath = function(object, xlim, ylim, log.fitness, ...) {
ggdf = as.data.frame(object)
ggdf = ggdf[c("dob", "pop.min.fitness", "pop.mean.fitness", "pop.median.fitness", "pop.max.fitness")]

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)))
assertNumeric(ylim, len = 2L, any.missing = FALSE)
Expand All @@ -69,4 +79,4 @@ autoplot.OptPath = function(object, xlim, ylim, log.fitness, ...) {
}

return(pl)
}
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#' @import checkmate
#' @import parallelMap
#' @import reshape2
#' @importFrom gridExtra arrangeGrob
#' @import gridExtra
NULL
9 changes: 7 additions & 2 deletions man/autoplot.ecr_result.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ Lower and upper bound for generation. If \code{NULL}, this is set automatically.
\item{ylim}{[\code{numeric(2)} | NULL]\cr
Lower and upper bound for fitness. If \code{NULL}, this is set automatically.}

\item{show.process}{[\code{logical(1)}]\cr
Should the function itself with the population be plotted as well? Default is
\code{FALSE}.}

\item{log.fitness}{[\code{logical(1)}]\cr
Log-transform fitness values? Default is \code{FALSE}.}

\item{...}{[any]\cr
Not used.}
}
\value{
[\code{\link[ggplot2]{ggplot}}]
Nothing.
}
\description{
Plot optimization trace.
Call this function on the result object of an \code{\link{ecr}} function call
to visualize the optimization trace.
}

0 comments on commit cdf6e67

Please sign in to comment.