Skip to content

Commit

Permalink
selectors now know if they are specified for single- or multi-objecti…
Browse files Browse the repository at this point in the history
…ve funs
  • Loading branch information
jakobbossek committed Apr 20, 2015
1 parent 0e248db commit afcbc5e
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 32 deletions.
10 changes: 10 additions & 0 deletions R/doTheEvolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ doTheEvolution = function(objective.fun, control) {
par.set = makeParamSet(makeNumericParam("dummy", lower = 0, upper = 1))
}

# check compatibility of selectors and #objectives
selectors = c(control$parent.selector, control$survival.selector)
desired.tag = if (n.objectives == 1L) "single-objective" else "multi-objective"
lapply(selectors, function(selector) {
if (desired.tag %nin% attr(selector, "supported.objectives")) {
stopf("Selector '%s' cannot be applied to problem with %i objectives.",
getOperatorName(selector), n.objectives)
}
})

y.names = paste0("y", seq(n.objectives))

n.population = control$n.population
Expand Down
5 changes: 1 addition & 4 deletions R/getSupportedRepresentations.R → R/getOperatorsTags.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#' Returns the representations which a specific operator supports.
#'
#' Operators and representation types are not mandatory compatible. This function
#' detemines the types an operator can operate on.
#' Returns the character vector of tags which describe a specific operator.
#'
#' @param operator [\code{ecr_operator}]\cr
#' Operator object.
Expand Down
11 changes: 9 additions & 2 deletions R/makeSelector.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
# @param supported [\code{character}]\cr
# Vector of strings/names of supported parameter representations. For example
# 'permutation', 'float', 'binary'.
# @param supported.objectives [\code{character}]\cr
# At least one of \dQuote{single-objective} or \dQuote{multi-objective}.
# @return [\code{ecr_selector}]
# selector object.
makeSelector = function(selector, name, description,
supported = getAvailableRepresentations()) {
makeSelector = function(
selector,
name, description,
supported = getAvailableRepresentations(),
supported.objectives) {
assertFunction(selector, args = c("population", "n.select", "control"), ordered = TRUE)
assertSubset(supported.objectives, c("single-objective", "multi-objective"))
selector = makeOperator(selector, name, description, supported)
selector = setAttribute(selector, "supported.objectives", supported.objectives)
selector = addClasses(selector, c("ecr_selector"))
return(selector)
}
3 changes: 2 additions & 1 deletion R/selector.greedy.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ makeGreedySelector = function() {
makeSelector(
selector = selector,
name = "Greedy selector",
description = "Return the best individuals regarding the fitness value."
description = "Return the best individuals regarding the fitness value.",
supported.objectives = c("single-objective")
)
}
3 changes: 2 additions & 1 deletion R/selector.k-tournament.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ makeTournamentSelector = function(k = 3L) {
makeSelector(
selector = selector,
name = "k-Tournament selector",
description = "Select k individuals at random, choose the best, repeat until mating pool is filled."
description = "Select k individuals at random, choose the best, repeat until mating pool is filled.",
supported.objectives = c("single-objective")
)
}
3 changes: 2 additions & 1 deletion R/selector.roulettewheel.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ makeRouletteWheelSelector = function() {
makeSelector(
selector = selector,
name = "Roulette-Wheel selector",
description = "Selects individuals in a fitness-proportional fashion."
description = "Selects individuals in a fitness-proportional fashion.",
supported.objectives = c("single-objective")
)
}
3 changes: 2 additions & 1 deletion R/selector.simple.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ makeSimpleSelector = function() {
makeSelector(
selector = selector,
name = "Simple selector",
description = "Simply returns the entire population."
description = "Simply returns the entire population.",
supported.objectives = c("single-objective", "multi-objective")
)
}
2 changes: 1 addition & 1 deletion R/setupECRControl.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ setupECRControl = function(
representation = representation,
survival.strategy = survival.strategy,
n.elite = n.elite,
n.targets = NULL, # we set this by hand here
#n.targets = n.targets,
save.population.at = save.population.at,
target.name = target.name,
stopping.conditions = stopping.conditions,
Expand Down
40 changes: 23 additions & 17 deletions inst/examples/sms_emoa_example.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
library(smoof)
library(ecr)

# THIS FILE IS WORK-IN-PROGRESS/EXPERIMENAL

# COLLECTION OF FIXME
#FIXME: integrate NondominatedSetSelector as ecr_selector
#FIXME: we need to check in setupECRControl or in ecr if the operators can work
# on multi-objective stuff
#

# Get set of dominated individuals.
#
# @param x [list]
# Set of 2D fitness values.
# @param fn [function]
# Fitness function.
# @return [list]
#FIXME: formulate this for matrices where each column describes one vector
#FIXME: formulate this for matrices where each column describes one vector. However,
# this is just a naive imolementation for testing purpose. Will be replaced
getDominatedSet = function(x) {
n = length(x)

Expand Down Expand Up @@ -44,41 +53,38 @@ makeNondominatedSetSelector = function() {
survive.idx = setdiff(seq(n), idx)
return(makePopulation(inds[survive.idx], population$fitness[, survive.idx, drop = FALSE]))
},
supported = "float",
supported.objectives = "multi-objective",
name = "Nondominated set selector",
description = "description"
)
}

ctrl = setupECRControl(
n.population = 10L,
n.offspring = 1L,
n.population = 20L,
n.offspring = 10L,
representation = "float",
monitor = makeConsoleMonitor()
)

ctrl = setupEvolutionaryOperators(
ctrl,
# tournamend and roulette wheel work on scalar fitness values only
# Thus, simply select two random elements for reproduction
parent.selector = makeSimpleSelector(),
mutator = makeGaussMutator(),
recombinator = makeCrossoverRecombinator(),
# everything can be used, we just needed a multi-objective-specific survival selector
survivalSelector = makeNondominatedSetSelector()
)

obj.fn = smoof::makeZDT1Function(2L)

res = doTheEvolution(obj.fn, ctrl)
stop("FIN")


# Determine the number of elements for each individual by which it is dominated.
#
# @param x [list]
# Set of fitness values.
# @return [integer]
# Each position contains the number of individuals by which this one is dominated.
#getDominanceNumber = function()
inds = list(c(1, 2), c(2, 1), c(2, 2), c(1, 3), c(1.5, 1.3))

print(getDominatedSet(inds))

pl = smoof::visualizeParetoOptimalFront(obj.fn)
pf = res$pareto.front
pf = as.data.frame(t(pf))
names(pf) = paste0("x", 1:2)
pl = pl + geom_point(data = pf, aes(x = x1, y = x2), colour = "green")
print(pl)
stop("FIN")
7 changes: 3 additions & 4 deletions man/getSupportedRepresentations.Rd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/getSupportedRepresentations.R
% Please edit documentation in R/getOperatorsTags.R
\name{getSupportedRepresentations}
\alias{getSupportedRepresentations}
\title{Returns the representations which a specific operator supports.}
\title{Returns the character vector of tags which describe a specific operator.}
\usage{
getSupportedRepresentations(operator)
}
Expand All @@ -15,7 +15,6 @@ Operator object.}
Vector of representation types.
}
\description{
Operators and representation types are not mandatory compatible. This function
detemines the types an operator can operate on.
Returns the character vector of tags which describe a specific operator.
}

0 comments on commit afcbc5e

Please sign in to comment.