Skip to content

Commit

Permalink
add cross over probability and recombinator.control for all recombina…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
surmann committed Jun 29, 2015
1 parent faeea3d commit 57888e1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion R/generateOffspring.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ generateOffspring = function(matingPool, objective.fun, control, opt.path) {
mutationStrategyAdaptor = control$mutationStrategyAdaptor
mutator.control = control$mutator.control
recombinator = control$recombinator
recombinator.control = control$recombinator.control
n.offspring = control$n.offspring

offspring = vector("list", n.offspring)
Expand All @@ -23,7 +24,7 @@ generateOffspring = function(matingPool, objective.fun, control, opt.path) {
parents = getParents(matingPool)
#print(parents)
# pass just the individuals and get a single individual
child = recombinator(parents, control)
child = recombinator(parents, recombinator.control, control)
#catf("Child %i", i)
#print(child)
mutator.control = mutationStrategyAdaptor(mutator.control, opt.path)
Expand Down
29 changes: 23 additions & 6 deletions R/recombinator.crossover.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#' Generator of the One-point crossover recombination operator.
#'
#' @param recombinator.crossover.prob [\code{numeric(1)}]\cr
#' Cross over probability to form an offspring. Default is \code{1}.
#' @return [\code{ecr_recombinator}]
#' @export
makeCrossoverRecombinator = function() {
recombinator = function(inds, control = list()) {
makeCrossoverRecombinator = function(recombinator.crossover.prob = 1) {
recombinatorCheck = function(operator.control) {
assertNumber(operator.control$recombinator.crossover.prob
, lower = 0, upper = 1, na.ok = FALSE
)
}

force(recombinator.crossover.prob)
defaults = list(recombinator.crossover.prob = recombinator.crossover.prob)
recombinatorCheck(defaults)

recombinator = function(inds, args = defaults, control = list()) {
parent1 = inds[[1]]
parent2 = inds[[2]]
n = length(parent1)
Expand All @@ -12,12 +24,15 @@ makeCrossoverRecombinator = function() {
if (n == 1L) {
stopf("Crossover recombinator requires genes to have length > 1.")
}
idx = sample(1:(n - 1), size = 1L)
# at least one allele of each parent should be contained
child1 = parent1
child2 = parent2
child1[(idx + 1L):n] = parent2[(idx + 1L):n]
child2[1:idx] = parent1[1:idx]
do.recombinate = runif(1L) < args$recombinator.crossover.prob
if (do.recombinate) {
idx = sample(1:(n - 1), size = 1L)
child1[(idx + 1L):n] = parent2[(idx + 1L):n]
child2[1:idx] = parent1[1:idx]
}
#FIXME: here we just return one offspring for now
return(child1)
}
Expand All @@ -27,6 +42,8 @@ makeCrossoverRecombinator = function() {
name = "Crossover recombinator",
description = "Performs classical one-point crossover.",
n.parents = 2L,
supported = c("float", "binary")
supported = c("float", "binary"),
defaults = defaults,
checker = recombinatorCheck
)
}
2 changes: 1 addition & 1 deletion R/recombinator.intermediate.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @return [\code{ecr_recombinator}]
#' @export
makeIntermediateRecombinator = function() {
recombinator = function(inds, control = list()) {
recombinator = function(inds, args = list(), control = list()) {
n = length(inds[[1]])
child = rep(0, n)
for (i in 1:length(inds)) {
Expand Down
2 changes: 1 addition & 1 deletion R/recombinator.null.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @return [\code{ecr_recombinator}]
#' @export
makeNullRecombinator = function() {
recombinator = function(inds, control=list()) {
recombinator = function(inds, args = list(), control=list()) {
return(inds[[1L]])
}

Expand Down
2 changes: 1 addition & 1 deletion R/recombinator.pmx.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @return [\code{ecr_recombinator}]
#' @export
makePMXRecombinator = function() {
recombinator = function(inds, control = list()) {
recombinator = function(inds, args = list(), control = list()) {
p1 = inds[[1]]
p2 = inds[[2]]
n = length(p1)
Expand Down
6 changes: 5 additions & 1 deletion man/makeCrossoverRecombinator.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
\alias{makeCrossoverRecombinator}
\title{Generator of the One-point crossover recombination operator.}
\usage{
makeCrossoverRecombinator()
makeCrossoverRecombinator(recombinator.crossover.prob = 1)
}
\arguments{
\item{recombinator.crossover.prob}{[\code{numeric(1)}]\cr
Cross over probability to form an offspring. Default is \code{1}.}
}
\value{
[\code{ecr_recombinator}]
Expand Down

0 comments on commit 57888e1

Please sign in to comment.