Skip to content

Commit

Permalink
add shortcuts: recombinate, mutate, selectForMating, selectForSurvival
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Aug 25, 2015
1 parent 59f0d1d commit 2c6935d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
5 changes: 2 additions & 3 deletions R/doTheEvolution.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ doTheEvolution = function(task, control) {

# extract generator and selector
populationGenerator = control$generator
parentSelector = control$parent.selector

# init some vars
iter = 1L
Expand Down Expand Up @@ -98,12 +97,12 @@ doTheEvolution = function(task, control) {
off.gen.start.time = Sys.time()

# actually create offspring
matingPool = parentSelector(population, STORAGE, n.mating.pool)
matingPool = selectForMating(control, population, STORAGE, n.mating.pool)
offspring = generateOffspring(matingPool, STORAGE, task$fitness.fun, control, trace$opt.path)
n.evals = n.evals + n.offspring

# apply survival selection and set up the (i+1)-th generation
population = selectForSurvival(
population = getNextGeneration(
population,
offspring,
STORAGE,
Expand Down
4 changes: 2 additions & 2 deletions R/generateOffspring.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ generateOffspring = function(matingPool, STORAGE, objective.fun, control, opt.pa

offspring = lapply(seq(n.offspring), function(idx) {
parents = getParents(matingPool)
child = recombinator(parents, control)
child = mutator(child, control$mutator.control, control)
child = recombine(control, parents)
child = mutate(control, child)
return(child)
})
offspring.fitness = computeFitness(makePopulation(offspring), objective.fun)
Expand Down
9 changes: 4 additions & 5 deletions R/selectForSurvival.R → R/getNextGeneration.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
#' @param control [\code{ecr_control}]\cr
#' Control object.
#' @return [\code{setOfIndividuals}]
selectForSurvival = function(population, offspring, STORAGE, n.population, strategy = "plus", n.elite = 0L, control) {
getNextGeneration = function(population, offspring, STORAGE, n.population, strategy = "plus", n.elite = 0L, control) {
elite = NULL
new.population = NULL
survivalSelector = control$survival.selector
if (strategy == "plus") {
source.population = mergePopulations(population, offspring)
new.population = survivalSelector(source.population, STORAGE, n.population, control)
new.population = selectForSurvival(control, source.population, STORAGE, n.population)
} else if (strategy == "comma") {
source.population = offspring
elite = list()
Expand All @@ -52,8 +51,8 @@ selectForSurvival = function(population, offspring, STORAGE, n.population, strat
# Adapt number of individuals taken from the offspring and select non-elite individuals
n.population = n.population - n.elite
}
new.population = survivalSelector(offspring, STORAGE, n.population, control)
if (length(elite) > 0L) {
new.population = selectForSurvival(control, offspring, STORAGE, n.population)
if (n.elite > 0L) {
new.population = mergePopulations(new.population, elite)
}
}
Expand Down
16 changes: 16 additions & 0 deletions R/setupECRControl.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,19 @@ getParametersAsString = function(parameters) {
}
return(x)
}

recombine = function(ctrl, parents) {
ctrl$recombinator(parents, ctrl$recombinator.control)
}

mutate = function(ctrl, parent) {
ctrl$mutator(parent, ctrl$mutator.control, ctrl)
}

selectForMating = function(ctrl, population, storage, n.select) {
ctrl$parent.selector(population, storage, n.select, ctrl)
}

selectForSurvival = function(ctrl, population, storage, n.select) {
ctrl$survival.selector(population, storage, n.select, ctrl)
}
8 changes: 4 additions & 4 deletions man/selectForSurvival.Rd → man/getNextGeneration.Rd
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/selectForSurvival.R
\name{selectForSurvival}
\alias{selectForSurvival}
% Please edit documentation in R/getNextGeneration.R
\name{getNextGeneration}
\alias{getNextGeneration}
\title{Performs survival selection.}
\usage{
selectForSurvival(population, offspring, STORAGE, n.population,
getNextGeneration(population, offspring, STORAGE, n.population,
strategy = "plus", n.elite = 0L, control)
}
\arguments{
Expand Down

0 comments on commit 2c6935d

Please sign in to comment.