Skip to content

Commit

Permalink
add objective.names parameter to makeOptimizationTask
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Jan 20, 2016
1 parent c18dbe1 commit 3294968
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
24 changes: 15 additions & 9 deletions R/makeOptimizationTask.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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.")
Expand All @@ -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")
)

Expand Down
2 changes: 2 additions & 0 deletions R/setupResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions man/makeOptimizationTask.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test_makeOptimizationTask.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 3294968

Please sign in to comment.