Skip to content

Commit

Permalink
add autoplot function for ecr result object
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Feb 13, 2015
1 parent 8ce157c commit 5ee4238
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Depends:
Imports:
ggplot2,
checkmate (>= 1.1),
parallelMap (>= 1.1)
parallelMap (>= 1.1),
reshape2 (>= 1.4.1)
Suggests:
testthat,
soobench
2 changes: 2 additions & 0 deletions 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 @@ -30,3 +31,4 @@ import(checkmate)
import(ggplot2)
import(otf)
import(parallelMap)
import(reshape2)
34 changes: 34 additions & 0 deletions R/autoplotECRResult.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Plot 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 ... [any]\cr
#' Not used.
#' @return [\code{\link[ggplot2]{ggplot}}]
#' @export
autoplot.ecr_result = function(object, xlim = NULL, ylim = NULL, ...) {
ggdf = as.data.frame(object$opt.path)
ggdf = ggdf[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)
assertNumeric(xlim, len = 2L, any.missing = FALSE)

requirePackages("reshape2", why = "ecr")
ggdf = melt(ggdf, c("dob"))
ggdf$variable = as.factor(ggdf$variable)

pl = ggplot(data = ggdf, mapping = aes_string(x = "dob", y = "value", linetype = "variable"))
pl = pl + geom_line()
pl = pl + xlab("Generation") + ylab("Fitness")
pl = pl + xlim(xlim) + ylim(ylim)
pl = pl + scale_linetype_discrete(name = "Type")
pl = pl + ggtitle(sprintf("Optimization trace for function '%s'", getName(object$objective.fun)))

return(pl)
}
1 change: 1 addition & 0 deletions R/ecr.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ ecr = function(objective.fun, control) {

return(
structure(list(
objective.fun = objective.fun,
best.param = setColNames(t(data.frame(best$individual)), getParamIds(par.set, repeated = TRUE, with.nr = TRUE)),
best.value = best$fitness,
opt.path = opt.path,
Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
#' @import ggplot2
#' @import checkmate
#' @import parallelMap
#' @import reshape2
NULL
28 changes: 28 additions & 0 deletions man/autoplot.ecr_result.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/autoplotECRResult.R
\name{autoplot.ecr_result}
\alias{autoplot.ecr_result}
\title{Plot optimization trace.}
\usage{
\method{autoplot}{ecr_result}(object, xlim = NULL, ylim = NULL, ...)
}
\arguments{
\item{object}{[\code{ecr_result}]\cr
ecr result object.}

\item{xlim}{[\code{numeric(2)} | NULL]\cr
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{...}{[any]\cr
Not used.}
}
\value{
[\code{\link[ggplot2]{ggplot}}]
}
\description{
Plot optimization trace.
}

0 comments on commit 5ee4238

Please sign in to comment.