From 32949682297fb20d21ce17d97ee3f11be14bc235 Mon Sep 17 00:00:00 2001 From: Jakob Bossek Date: Wed, 20 Jan 2016 16:12:20 +0100 Subject: [PATCH] add objective.names parameter to makeOptimizationTask --- R/makeOptimizationTask.R | 24 ++++++++++++++-------- R/setupResult.R | 2 ++ man/makeOptimizationTask.Rd | 13 +++++++++--- tests/testthat/test_makeOptimizationTask.R | 9 ++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/R/makeOptimizationTask.R b/R/makeOptimizationTask.R index 2c6d997..63b7964 100644 --- a/R/makeOptimizationTask.R +++ b/R/makeOptimizationTask.R @@ -3,8 +3,9 @@ #' #' @description #' An optimization task consists of the fitness/objective function, the -#' number of objectives and the \dQuote{direction} of optimization, i.e., -#' which objectives should be minimized/maximized. +#' number of objectives, the \dQuote{direction} of optimization, i.e., +#' which objectives should be minimized/maximized and the names of the +#' objectives. #' #' @param fun [\code{function} | \code{smoof_function}]\cr #' Fitness/objective function. @@ -14,10 +15,13 @@ #' @param minimize [\code{logical}]\cr #' A logical vector indicating which objectives to minimize/maximize. By default #' all objectives are assumed to be minimized. +#' @param objective.names [\code{character}]\cr +#' Names for the objectuves. +#' Default is \code{NULL}. In this case the names are set to y1, ..., yn with +#' n equal to \code{n.objectives} and simply y in the single-objective case. #' @return [\code{ecr_optimization_task}] #' @export -makeOptimizationTask = function(fun, n.objectives = NULL, minimize = NULL) { - #FIXME: what parameters do we force it to have? +makeOptimizationTask = function(fun, n.objectives = NULL, minimize = NULL, objective.names = NULL) { assertFunction(fun) if (isSmoofFunction(fun)) { if (is.null(n.objectives)) { @@ -36,11 +40,10 @@ makeOptimizationTask = function(fun, n.objectives = NULL, minimize = NULL) { but the passed smoof function '%s' exhibits these.", getName(fun)) } } - if (!is.null(n.objectives)) { - assertInt(n.objectives, lower = 1L, na.ok = FALSE) - } - if (!is.null(minimize)) { - assertLogical(minimize, any.missing = FALSE) + assertInt(n.objectives, lower = 1L, na.ok = FALSE) + + if (is.null(objective.names)) { + objective.names = if (n.objectives == 1L) "y" else paste0("y", seq(n.objectives)) } if (is.null(minimize)) { @@ -50,6 +53,8 @@ makeOptimizationTask = function(fun, n.objectives = NULL, minimize = NULL) { minimize = rep(TRUE, n.objectives) } } + assertLogical(minimize, any.missing = FALSE) + assertCharacter(objective.names, len = n.objectives, any.missing = FALSE, all.missing = FALSE) if (n.objectives != length(minimize)) { stopf("Number of objectives does not correspond to the length of the minimize argument.") @@ -66,6 +71,7 @@ makeOptimizationTask = function(fun, n.objectives = NULL, minimize = NULL) { fitness.fun = fun, n.objectives = n.objectives, minimize = minimize, + objective.names = objective.names, classes = c("ecr_optimization_task") ) diff --git a/R/setupResult.R b/R/setupResult.R index c284e43..ab8a7b9 100644 --- a/R/setupResult.R +++ b/R/setupResult.R @@ -64,6 +64,8 @@ setupResult.ecr_multi_objective_opt_state = function(opt.state, stop.object, con population = opt.state$population fitness = population$fitness pareto.idx = which.nondominated(fitness) + pareto.front = t(fitness[, pareto.idx, drop = FALSE]) + colnames(pareto.front) = opt.state$task$objective.names makeS3Obj( final.opt.state = opt.state, task = opt.state$task, diff --git a/man/makeOptimizationTask.Rd b/man/makeOptimizationTask.Rd index ae7bdd2..2498184 100644 --- a/man/makeOptimizationTask.Rd +++ b/man/makeOptimizationTask.Rd @@ -4,7 +4,8 @@ \alias{makeOptimizationTask} \title{Creates an optimization task.} \usage{ -makeOptimizationTask(fun, n.objectives = NULL, minimize = NULL) +makeOptimizationTask(fun, n.objectives = NULL, minimize = NULL, + objective.names = NULL) } \arguments{ \item{fun}{[\code{function} | \code{smoof_function}]\cr @@ -17,13 +18,19 @@ is of type \code{smoof_function}.} \item{minimize}{[\code{logical}]\cr A logical vector indicating which objectives to minimize/maximize. By default all objectives are assumed to be minimized.} + +\item{objective.names}{[\code{character}]\cr +Names for the objectuves. +Default is \code{NULL}. In this case the names are set to y1, ..., yn with +n equal to \code{n.objectives} and simply y in the single-objective case.} } \value{ [\code{ecr_optimization_task}] } \description{ An optimization task consists of the fitness/objective function, the -number of objectives and the \dQuote{direction} of optimization, i.e., -which objectives should be minimized/maximized. +number of objectives, the \dQuote{direction} of optimization, i.e., +which objectives should be minimized/maximized and the names of the +objectives. } diff --git a/tests/testthat/test_makeOptimizationTask.R b/tests/testthat/test_makeOptimizationTask.R index 272f83d..9de1561 100644 --- a/tests/testthat/test_makeOptimizationTask.R +++ b/tests/testthat/test_makeOptimizationTask.R @@ -13,6 +13,15 @@ test_that("optimization tasks are properly generated", { expect_output(print(task), "optimization task", ignore.case = TRUE) } + # now generate a task by hand + fn = makeZDT2Function(2L) + task = makeOptimizationTask(fn, minimize = c(TRUE, FALSE), objective.names = c("warmth", "elasticity")) + expect_true(any(task$minimize)) + expect_true(any(!task$minimize)) + expect_true(all(task$objective.names == c("warmth", "elasticity"))) + expect_equal(task$n.objectives, 2L) + + # now check for errors fn = makeSphereFunction(2L) # wrong number of objectives expect_error(makeOptimizationTask(fn, n.objectives = 2L))