Skip to content

Commit

Permalink
introduce event stuff in doTheEvolution
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Jan 23, 2016
1 parent 98e5b6b commit 52b6795
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export(makeUniformGenerator)
export(makeUniformMutator)
export(nsga2)
export(onePlusOneGA)
export(registerAction)
export(rescalePoints)
export(setupECRControl)
export(setupEvolutionaryOperators)
Expand Down
11 changes: 8 additions & 3 deletions R/doTheEvolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ doTheEvolution = function(task, control, initial.population = NULL) {
population = buildInitialPopulation(control$n.population, task, control, initial.population)
population$fitness = evaluateFitness(population, task$fitness.fun, task, control)
opt.state = setupOptState(task, population, control)
fireEvent("onEAInitialized", control, opt.state)

monitor$before()
repeat {
matingPool = selectForMating(opt.state, control)
fireEvent("onMatingPoolGenerated", control, opt.state)

offspring = generateOffspring(opt.state, matingPool, control)
population = getNextGeneration(opt.state, offspring, control)
fireEvent("onOffspringGenerated", control, opt.state)

population = getNextGeneration(opt.state, offspring, control)
updateOptState(opt.state, population, control)
fireEvent("onPopulationUpdated", control, opt.state)

monitor$step()

Expand All @@ -60,9 +65,9 @@ doTheEvolution = function(task, control, initial.population = NULL) {
break
}
}
fireEvent("onEAFinished", control, opt.state)
monitor$after()
setupResult(opt.state, stop.object, control)
# end of new doTheEvolution
return(setupResult(opt.state, stop.object, control))
}

# @title
Expand Down
36 changes: 32 additions & 4 deletions R/eventDispatcher.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,36 @@ setupEventDispatcher = function(name) {
#' @export
getAvailableEventNames = function() {
return(c(
"onGeneratedOffspring",
"onFinishedOptimization",
"onInitializedOptimization")
)
"onEAInitialized",
"onMatingPoolGenerated",
"onOffspringGenerated",
"onPopulationUpdated",
"onEAFinished"
))
}

fireEvent = function(event.name, control, opt.state, ...) {
control$event.dispatcher$fireEvent(event.name, opt.state, ...)
}

#' @title
#' Register an action.
#'
#' @description
#' Register an action for a specific event in the EA circle.
#' See \code{\link{getAvailableEventNames}} for a list of available event names.
#'
#' @param control [\code{ecr_control}]\cr
#' Control object.
#' @param event.name [\code{character(1)}]\cr
#' Name of the event.
#' @param fun [\code{function()}]\cr
#' Function with arguments \code{opt.state} and \code{...}.
#' @return [\code{ecr_control}] Updated control object.
#' @seealso \code{\link{getAvailableEventNames}}
#' @export
registerAction = function(control, event.name, fun) {
assertClass(control, "ecr_control")
control$event.dispatcher$registerAction(event.name, fun)
return(control)
}
1 change: 1 addition & 0 deletions R/setupECRControl.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ setupECRControl = function(
extras.fun = extras.fun,
custom.constants = custom.constants,
vectorized.evaluation = vectorized.evaluation,
event.dispatcher = setupEventDispatcher(),
classes = "ecr_control"
)

Expand Down
29 changes: 29 additions & 0 deletions man/registerAction.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test_eventDispatcher.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ test_that("event dispatcher works well", {

# now register some events
expect_error(eventDispatcher$registerAction("onNotExistentEvent", function(opt.state, ...) "foo"))
eventDispatcher$register("onInitializedOptimization", function(opt.state, ...) {
eventDispatcher$register("onEAInitialized", function(opt.state, ...) {
catf("init")
})
eventDispatcher$register("onInitializedOptimization", function(opt.state, ...) {
eventDispatcher$register("onEAInitialized", function(opt.state, ...) {
x <<- 10L # modify test var
})

action.list = eventDispatcher$getActionList()
expect_equal(length(action.list[["onInitializedOptimization"]]), 2L) # added two actions
expect_true(all(sapply(action.list[["onInitializedOptimization"]], is.function)))
expect_output(eventDispatcher$fireEvent("onInitializedOptimization", opt.state = NULL), regexp = "init")
expect_equal(length(action.list[["onEAInitialized"]]), 2L) # added two actions
expect_true(all(sapply(action.list[["onEAInitialized"]], is.function)))
expect_output(eventDispatcher$fireEvent("onEAInitialized", opt.state = NULL), regexp = "init")
expect_equal(x, 10L)
})

0 comments on commit 52b6795

Please sign in to comment.