Skip to content

Commit

Permalink
remove save.population.at stuff, work on autoplot function
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Jan 23, 2016
1 parent 9e0ae74 commit 83ef5a1
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 64 deletions.
50 changes: 35 additions & 15 deletions R/autoplotECRResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ autoplot.ecr_single_objective_result = function(object, xlim = NULL, ylim = NULL
, log.fitness = FALSE, complete.trace = FALSE, ...) {
assertFlag(show.process, na.ok = FALSE)
assertFlag(complete.trace, na.ok = FALSE)
obj.fun = object$task$fitness.fun
n.params = getNumberOfParameters(obj.fun)
assertFlag(log.fitness, na.ok = FALSE)

if (is.null(object$opt.path)) {
stopf("Cannot plot optimization trace, since obviously no logging took place.")
}


op = as.data.frame(object$opt.path)
# we start with the second dob, since otherwise there is not enough info to plot
Expand All @@ -39,24 +43,40 @@ autoplot.ecr_single_objective_result = function(object, xlim = NULL, ylim = NULL
pl.trace = plotTrace(op[which(op$dob <= dob), ], xlim, ylim, log.fitness, ...)
pl.trace = pl.trace + ggtitle(sprintf("Optimization trace for function '%s'", getName(obj.fun)))
if (show.process) {
if (n.params > 2L || isMultiobjective(obj.fun)) {
stopf("Visualization not possible for multi-objective functions or functions with greater than 2 parameters.")
if (object$final.opt.state$control$representation == "custom") {
stopf("Process cannot be visualized if custom representation was used.")
}
obj.fun = object$task$fitness.fun
task = object$task
par.set = smoof::getParamSet(obj.fun)
n.params = getNumberOfParameters(obj.fun)

if (n.params > 2L) {
stopf("Visualization not possible for functions with more than 2 parameters.")
}
if (!length(object$control$save.population.at)) {
stopf("Cannot visualize population since no population was stored! Take a glance a the 'save.population.at' control parameter.")

if (hasDiscrete(par.set)) {
stopf("Visualization for mixed/discrete decision spaces not supported at the moment.")
}

if (isMultiobjective(obj.fun)) {
stopf("Visualization not possible for multi-objective functions at the moment.")
}

# call smoof plot function
pl.fun = autoplot(obj.fun)
population = object$population.storage[[paste0("gen.", dob)]]

# get interesting stuff out of opt.path in ggplot2 friendly format
df.points = getOptPathX(op, dob = dob)
y.name = task$objective.names
df.points[[y.name]] = getOptPathY(op, dob = dob)
x.names = getParamIds(par.set, with.nr = TRUE, repeated = TRUE)
if (n.params == 2L) {
df.points = as.data.frame(do.call(rbind, population$individuals))
colnames(df.points) = paste("x", 1:n.params, sep = "")
df.points$y = as.numeric(population$fitness)
pl.fun = pl.fun + geom_point(data = df.points, aes_string(x = "x1", y = "x2"), colour = "tomato")
pl.fun = pl.fun + geom_point(data = df.points, aes_string(x = x.names[1L], y = x.names[2L]), colour = "tomato")
} else {
fitness = as.numeric(population$fitness)
df.points = data.frame(x = do.call(c, population$individuals), y = fitness)
pl.fun = pl.fun + geom_point(data = df.points, aes_string(x = "x", y = "y"), colour = "tomato")
pl.fun = pl.fun + geom_hline(yintercept = min(fitness), linetype = "dashed", colour = "gray")
pl.fun = pl.fun + geom_point(data = df.points, aes_string(x = x.names, y = y.name), colour = "tomato")
opt.dir.fun = if (task$minimkze) min else max
pl.fun = pl.fun + geom_hline(yintercept = opt.dir.fun(df.points[[y.name]]), linetype = "dashed", colour = "gray")
}

#FIXME: this seems to fail!
Expand Down
14 changes: 0 additions & 14 deletions R/optState.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ setupOptState = function(task, population, control) {
opt.state$best.value = best$fitness
}

# optional logging of population
opt.state$population.storage = namedList(paste0("gen.", control$save.population.at))

if (0 %in% control$save.population.at) {
opt.state$population.storage[[paste0("gen.", as.character(0))]] = population
}

# construct opt path
y.names = paste0("y", seq(task$n.objectives))

Expand Down Expand Up @@ -65,18 +58,11 @@ updateOptState = function(opt.state, population, control) {
# update population
opt.state$population = population

#FIXME/TODO: handle opt.path update!

# save best-so-far solution in single-objective case
if (task$n.objectives == 1L) {
updateOptStateBestIndividual(opt.state)
}

# update populaton storage
if (opt.state$iter %in% control$save.population.at) {
opt.state$population.storage[[paste0("gen.", as.character(opt.state$iter))]] = population
}

invisible()
}

Expand Down
8 changes: 0 additions & 8 deletions R/setupECRControl.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#' Number of fittest individuals of the current generation that shall be copied to the
#' next generation without changing. Default is 0. Keep in mind, that the algorithm
#' does not care about this option if the \code{survival.strategy} is set to 'plus'.
#' @param save.population.at [\code{integer}]\cr
#' Which populations should be saved? Default is none.
#' @param monitor [\code{function}]\cr
#' Monitoring function. Default is \code{consoleMonitor}.
#' @param stopping.conditions [\code{list}]\cr
Expand Down Expand Up @@ -59,7 +57,6 @@ setupECRControl = function(
representation,
survival.strategy = "plus",
n.elite = 0L,
save.population.at = integer(0),
monitor = makeConsoleMonitor(),
stopping.conditions = list(),
logger = makeNullMonitor(),
Expand All @@ -75,10 +72,6 @@ setupECRControl = function(
assertList(custom.constants, unique = TRUE, any.missing = FALSE, all.missing = FALSE)
assertFlag(vectorized.evaluation, na.ok = FALSE)

if (length(save.population.at) > 0) {
assertIntegerish(save.population.at, lower = 0L, any.missing = FALSE)
}

# If the survival strategy is (mu + lambda), than the number of generated offspring in each iteration
# must greater or equal to the population size
if (survival.strategy == "comma" && n.offspring < n.population) {
Expand Down Expand Up @@ -125,7 +118,6 @@ setupECRControl = function(
representation = representation,
survival.strategy = survival.strategy,
n.elite = n.elite,
save.population.at = save.population.at,
stopping.conditions = stopping.conditions,
monitor = monitor,
logger = logger,
Expand Down
4 changes: 2 additions & 2 deletions R/setupResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ setupResult.ecr_single_objective_opt_state = function(opt.state, stop.object, co
control = control,
best.param = opt.state$best.param,
best.value = opt.state$best.value,
opt.path = coalesce(opt.state$opt.path, NA),
opt.path = opt.state$opt.path,
last.population = opt.state$population,
population.storage = opt.state$population.storage,
message = stop.object$message,
Expand Down Expand Up @@ -94,7 +94,7 @@ setupResult.ecr_multi_objective_opt_state = function(opt.state, stop.object, con
final.opt.state = opt.state,
task = opt.state$task,
control = control,
opt.path = coalesce(opt.state$opt.path, NA),
opt.path = opt.state$opt.path,
pareto.idx = pareto.idx,
pareto.front = t(fitness[, pareto.idx, drop = FALSE]),
pareto.set = population[pareto.idx],
Expand Down
3 changes: 2 additions & 1 deletion examples/ex_doTheEvolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ ctrl = setupECRControl(
n.population = 100L,
n.offspring = 10L,
survival.strategy = "plus",
save.population.at = 0:100L,
representation = "float",
logger = makeOptPathLoggingMonitor(),
stopping.conditions = setupStoppingConditions(max.iter = 100L)
)

ctrl = setupEvolutionaryOperators(
ctrl,
parent.selector = makeRouletteWheelSelector(),
Expand Down
15 changes: 8 additions & 7 deletions inst/examples/onesAndZeros_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ load_all(".", reset = TRUE)
# objective function
obj.fn = makeSingleObjectiveFunction(
name = "Number of Ones",
fn = function(x, y) {
(length(x) - sum(x)) + sum(y)
fn = function(x1, x2) {
(length(x1) - sum(x1)) + sum(x2)
},
par.set = makeParamSet(
makeIntegerVectorParam("x", len = 20L, lower = 0, upper = 1),
makeIntegerVectorParam("y", len = 15L, lower = 0, upper = 1)
)
makeIntegerVectorParam("x1", len = 20L, lower = 0, upper = 1),
makeIntegerVectorParam("x2", len = 15L, lower = 0, upper = 1)
),
has.simple.signature = FALSE
)

makeOptimumAppearsStoppingCondition = function(opt.fitness = 0) {
Expand All @@ -36,7 +37,7 @@ control = setupECRControl(
n.offspring = 100L,
n.mating.pool = 100L,
representation = "binary",
save.population.at = c(0L, 10L, 20L, 30L, 40L, 50L, 60L),
logger = makeOptPathLoggingMonitor(step = 10L),
monitor = makeConsoleMonitor(1L),
stopping.conditions = list(
makeOptimumAppearsStoppingCondition()
Expand All @@ -55,4 +56,4 @@ control = setupEvolutionaryOperators(
# names(as.data.frame(res$opt.path))
res = doTheEvolution(obj.fn, control = control)
print(res)
#autoplot(res, complete.trace = TRUE)
autoplot(res, complete.trace = TRUE)
3 changes: 2 additions & 1 deletion man/doTheEvolution.Rd

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

9 changes: 3 additions & 6 deletions man/setupECRControl.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/helper_zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set.seed(1)
set.seed(5453)

makeOneMinFunction = function(dimensions) {
assertInteger(dimensions, len = 1L, lower = 2L, upper = 100L)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test_autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
# representation = "float",
# survival.strategy = "plus",
# # FIXME: Throws an error if not complete population is saved
# save.population.at = 0:15,
# monitor = makeNullMonitor(),
# stopping.conditions = list(makeMaximumIterationsStoppingCondition(max.iter = 15L))
# )
Expand Down
8 changes: 0 additions & 8 deletions tests/testthat/test_ecr.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,11 @@ test_that("ecr can handle initial populations", {
control = setupECRControl(
n.population = 3L,
n.offspring = 1L,
save.population.at = 0L,
representation = "float",
monitor = makeNullMonitor(),
stopping.conditions = setupStoppingConditions(max.iter = 1L)
)

# stop if initial population is to large
expect_error(doTheEvolution(fn, control, c(initial.population, c(2, 2.5))), "exceeds", ignore.case = TRUE)

# check if initial population is indeed used
res = doTheEvolution(fn, control, initial.population)
first.population = res$population.storage[[1L]]
for (i in seq(length(first.population))) {
expect_true(all(first.population$individuals[[i]] == initial.population[[i]]), info = sprintf("%i-th element not equal to intial population.", i))
}
})

0 comments on commit 83ef5a1

Please sign in to comment.