Skip to content

Commit

Permalink
add trace (forgot it in the last commit), update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Apr 21, 2015
1 parent 12fd0f7 commit 82ce1ba
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 63 deletions.
63 changes: 1 addition & 62 deletions R/doTheEvolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,74 +148,13 @@ doTheEvolution = function(objective.fun, control) {
# generate result object
if (n.objectives == 1L) {
makeECRSingleObjectiveResult(objective.fun, trace$best, trace$opt.path, control,
population.storage = NULL, stop.object)
population.storage, stop.object)
} else {
makeECRMultiObjectiveResult(objective.fun, trace$opt.path, control,
population.storage, stop.object)
}
}


initTrace = function(control, population, n.objectives, y.names) {
par.set = control$par.set
opt.path = makeOptPathDF(par.set, y.names = y.names, minimize = rep(TRUE, n.objectives), include.extra = TRUE, include.exec.time = TRUE)

if (n.objectives == 1L) {
best = getBestIndividual(population)
return(makeS3Obj(
opt.path = opt.path,
best = best,
classes = c("ecr_single_objective_trace")
))
}
return(makeS3Obj(
opt.path = opt.path,
classes = c("ecr_multi_objective_trace")
))
}

updateTrace = function(trace, iter, population, start.time, pop.gen.time, control) {
UseMethod("updateTrace")
}

updateTrace.ecr_single_objective_trace = function(trace, iter, population, start.time, pop.gen.time, control) {
par.set = control$par.set
best = getBestIndividual(population)
extras = getListOfExtras(iter, population, start.time, control)
if (length(par.set$pars) == 1L) {
best.param.values = list(best$individual)
names(best.param.values) = getParamIds(par.set)
} else {
best.param.values = as.list(best$individual)
names(best.param.values) = getParamIds(par.set, repeated = TRUE, with.nr = TRUE)
}
#FIXME: dummy value for custom representation
if (control$representation == "custom") {
best.param.values = list("x" = 0.5)
}
addOptPathEl(trace$opt.path, x = best.param.values, y = unlist(best$fitness), dob = iter,
exec.time = 0, extra = extras, check.feasible = FALSE)
trace$best = best
return(trace)
}

updateTrace.ecr_multi_objective_trace = function(trace, iter, population, start.time, pop.gen.time, control) {
par.set = control$par.set
extras = getListOfExtras(iter, population, start.time, control)
if (control$representation == "custom") {
stopf("Multi-objective optimization with custom genotypes is not yet finished.")
best.param.values = list("x" = 0.5)
}
n.population = length(population$individuals)
for (i in seq(n.population)) {
x = as.list(population$individuals[[i]])
names(x) = getParamIds(control$par.set, with.nr = TRUE, repeated = TRUE)
addOptPathEl(trace$opt.path, x = x, y = population$fitness[, i], dob = iter,
exec.time = 0, extra = extras, check.feasible = FALSE)
}
return(trace)
}

#' Print the result of an ecr run.
#'
#' @param x [\code{ecr_result}]\cr
Expand Down
3 changes: 2 additions & 1 deletion R/makeECRResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ NULL
makeECRMultiObjectiveResult = function(
objective.fun, opt.path, control,
population.storage, stop.object) {
pareto.inds = getOptPathParetoFront(opt.path, index = TRUE)
max.dob = max(getOptPathDOB(opt.path))
pareto.inds = getOptPathParetoFront(opt.path, index = TRUE, dob = max.dob)
makeS3Obj(
objective.fun = objective.fun,
control = control,
Expand Down
23 changes: 23 additions & 0 deletions R/notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Four cases:
How to solve that elegantly?
1. Standard genotype, single-objective
-> x and **best** y can be saved in optPath as well as entire population
2. Standard genotype, multi-objective
-> x and y can be saved in optPath, but there is no *best*
3. Custom genotype, single-objective
-> **only** (best) y can be saved (x values not scalar or vector)
4. Custom genotype, multi-objective
-> **only** y values can be saved (there is no best and x values not scalar)

Possible solutions (these are just some considerations)
- Principally do not use optPath to store x-values. Always save them in a list -> this way we need not distinguish between all the genotype variants
- setup 'ecr templates': one for custom representations and one for standard representations. This way we could always store all the stuff in the opt.path for default without hindering and we would still have a mess for custom representations :-(
-

Moreover!
optPath is passed to terminator objects.
-> Terminators need to become ecr_operators with supported.objectives field (like selectors). E.g. stop if global optimum is approximated is not suitable as an emoa stopping condition.



:-(
99 changes: 99 additions & 0 deletions R/trace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Helper - Generator for trace objects.
#
# Trace objects are kind of a wrapper for optPath and other stuff which is specific
# to single- and multiobjective tracing respectively.
#
# @param control [ecr_control]
# Control object.
# @param population [ecr_population]
# Current population.
# @param n.objecjtive [integer(1)]
# Number of targets/objectives.
# @param y.names [character(1)]
# Names for the y-columns in the opt.path.
# @return [ecr_single_objective_trace | ecr_multi_objective_trace]
initTrace = function(control, population, n.objectives, y.names) {
par.set = control$par.set
opt.path = makeOptPathDF(par.set, y.names = y.names, minimize = rep(TRUE, n.objectives), include.extra = TRUE, include.exec.time = TRUE)

if (n.objectives == 1L) {
best = getBestIndividual(population)
return(makeS3Obj(
opt.path = opt.path,
best = best,
classes = c("ecr_single_objective_trace")
))
}
return(makeS3Obj(
opt.path = opt.path,
classes = c("ecr_multi_objective_trace")
))
}

# Helper - Updates a trace with the information of the current population.
#
# @param trace [ecr_{single,multi}_objective_trace]
# Trace to update.
# @param iter [integer(1)]
# Current iteration/generation.
# @param population [ecr_population]
# Population object.
# @param start.time [POSIXct]
# Beginning of the optimization process.
# @param exec.time [numeric(1)]
# Time it took to generate the initial population or set up the new population
# respectively.
# @param control [ecr_control]
# Control object.
# @return [ecr_{single,multi}_objective_trace] Modified trace.
updateTrace = function(trace, iter, population, start.time, exec.time, control) {
UseMethod("updateTrace")
}

# see generic updateTrace
updateTrace.ecr_single_objective_trace = function(trace, iter, population, start.time, exec.time, control) {
par.set = control$par.set
best = getBestIndividual(population)
extras = getListOfExtras(iter, population, start.time, control)
if (length(par.set$pars) == 1L) {
best.param.values = list(best$individual)
names(best.param.values) = getParamIds(par.set)
} else {
best.param.values = as.list(best$individual)
names(best.param.values) = getParamIds(par.set, repeated = TRUE, with.nr = TRUE)
}
#FIXME: dummy value for custom representation
if (control$representation == "custom") {
best.param.values = list("x" = 0.5)
}
addOptPathEl(trace$opt.path, x = best.param.values, y = unlist(best$fitness), dob = iter,
exec.time = exec.time, extra = extras, check.feasible = FALSE)
trace$best = best
return(trace)
}

# see generic updateTrace
updateTrace.ecr_multi_objective_trace = function(trace, iter, population, start.time, exec.time, control) {
par.set = control$par.set
extras = getListOfExtras(iter, population, start.time, control)
#FIXME: handle this specific stuff here.
if (control$representation == "custom") {
stopf("Multi-objective optimization with custom genotypes is not yet finished.")
best.param.values = list("x" = 0.5)
}
n.population = length(population$individuals)
#FIXME: since we store the entire population anew, we set the eol stuff
# if (iter > 1L) {
# dobs = getOptPathDOB(trace$opt.path)
# idx = which(dobs == (iter - 1L))
# setOptPathElEOL(trace$opt.path, index = idx, eol = iter)
# }

for (i in seq(n.population)) {
x = as.list(population$individuals[[i]])
names(x) = getParamIds(control$par.set, with.nr = TRUE, repeated = TRUE)
addOptPathEl(trace$opt.path, x = x, y = population$fitness[, i], dob = iter,
exec.time = 0, extra = extras, check.feasible = FALSE)
}
return(trace)
}
1 change: 1 addition & 0 deletions inst/examples/smoof_multi_objective_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ for (i in 1:n.reps) {
sum(res * w)
}
attributes(fitness.fun) = attributes(obj.fun)
fitness.fun = setAttribute(fitness.fun, "n.objectives", 1L)

# do the evolutionary magic
res = doTheEvolution(fitness.fun, control = control)
Expand Down

0 comments on commit 82ce1ba

Please sign in to comment.