Skip to content

Commit

Permalink
issue #102: export 'makeMutator', 'makeRecombinator' and 'makeSelector'
Browse files Browse the repository at this point in the history
  • Loading branch information
surmann committed Jul 2, 2015
1 parent b83339f commit afc6441
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 67 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ export(makeInversionMutator)
export(makeMaximumIterationsStoppingCondition)
export(makeMaximumTimeStoppingCondition)
export(makeMonitor)
export(makeMutator)
export(makeNullMonitor)
export(makeNullRecombinator)
export(makePMXRecombinator)
export(makePermutationGenerator)
export(makeRecombinator)
export(makeRouletteWheelSelector)
export(makeScrambleMutator)
export(makeSelector)
export(makeSimpleSelector)
export(makeStoppingCondition)
export(makeSwapMutator)
Expand Down
48 changes: 28 additions & 20 deletions R/makeMutator.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
# Helper function which constructs a mutator, i. e., a mutation operator.
#
# @param mutator [\code{function}]\cr
# Actual mutation operator.
# @param name [\code{character(1)}]\cr
# Name of the mutator.
# @param description [\code{character(1)}]\cr
# Short description of how the mutator works.
# @param supported [\code{character}]\cr
# Vector of strings/names of supported parameter representations. For example
# 'permutation', 'float', 'binary'.
# @param checker [\code{function}]\cr
# Check object, which performs a sanity check in mutator strategy parameters
# passed to the control object.
# @return [\code{ecr_mutator}]
# Mutator object.
makeMutator = function(mutator, name, description,
supported = getAvailableRepresentations(),
defaults = list(),
checker = function(operator.control) TRUE) {
#' @title
#' Construct a mutation operator
#' @description
#' Helper function which constructs a mutator, i.e., a mutation operator.
#'
#' @param mutator [\code{function}]\cr
#' Actual mutation operator.
#' @param name [\code{character(1)}]\cr
#' Name of the mutator.
#' @param description [\code{character(1)}]\cr
#' Short description of how the mutator works.
#' @param supported [\code{character}]\cr
#' Vector of strings/names of supported parameter representations. Possible are
#' 'permutation', 'binary', 'float', 'custom'.
#' @param defaults [\code{list}]\cr
#' List of default values for the operators strategy parameters.
#' @param checker [\code{function}]\cr
#' Check object, which performs a sanity check in mutator strategy parameters
#' passed to the control object.
#' @return [\code{ecr_mutator}]
#' Mutator object.
#' @export
makeMutator = function(mutator, name, description
, supported = getAvailableRepresentations()
, defaults = list()
, checker = function(operator.control) TRUE
) {
# create operator
mutator = makeOperator(mutator, name, description, supported, defaults, checker)
mutator = addClasses(mutator, c("ecr_mutator"))
return(mutator)
Expand Down
13 changes: 7 additions & 6 deletions R/makeOperator.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
# List of default values for the operators strategy parameters.
# @return [\code{ecr_operator}]
# Operator object.
makeOperator = function(operator, name, description,
supported = getAvailableRepresentations(),
defaults = list(),
checker = function(operator.control) TRUE) {
makeOperator = function(operator, name, description
, supported = getAvailableRepresentations()
, defaults = list()
, checker = function(operator.control) TRUE
) {
assertFunction(operator)
assertCharacter(name, len = 1L, any.missing = FALSE)
assertCharacter(description, len = 1L, any.missing = FALSE)
assertString(name)
assertString(description)
assertSubset(supported, choices = getAvailableRepresentations(), empty.ok = FALSE)
assertList(defaults, unique = TRUE, any.missing = FALSE)
assertFunction(checker, args = "operator.control")
Expand Down
49 changes: 30 additions & 19 deletions R/makeRecombinator.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
# Helper function which constructs a recombinator, i. e., a recombination operator.
#
# @param recombinator [\code{function}]\cr
# Actual mutation operator.
# @param name [\code{character(1)}]\cr
# Name of the recombinator.
# @param supported [\code{character}]\cr
# Vector of strings/names of supported parameter representations. For example
# 'permutation', 'float', 'binary'.
# @param n.parents [\code{integer(1)}]\cr
# Number of parents supported.
# @return [\code{ecr_recombinator}]
# Recombinator object.
makeRecombinator = function(
recombinator, name, description,
supported = getAvailableRepresentations(),
n.parents = 2L,
defaults = list(),
checker = function(operator.control) TRUE) {
#' @title
#' Construct a recombination operator
#' @description
#' Helper function which constructs a recombinator, i.e., a recombination operator.
#'
#' @param recombinator [\code{function}]\cr
#' Actual mutation operator.
#' @param name [\code{character(1)}]\cr
#' Name of the recombinator.
#' @param description [\code{character(1)}]\cr
#' Short description of how the recombinator works.
#' @param supported [\code{character}]\cr
#' Vector of strings/names of supported parameter representations. For example
#' 'permutation', 'float', 'binary'.
#' @param n.parents [\code{integer(1)}]\cr
#' Number of parents supported.
#' @param defaults [\code{list}]\cr
#' List of default values for the operators strategy parameters.
#' @param checker [\code{function}]\cr
#' Check object, which performs a sanity check in mutator strategy parameters
#' passed to the control object.
#' @return [\code{ecr_recombinator}]
#' Recombinator object.
#' @export
makeRecombinator = function(recombinator, name, description
, supported = getAvailableRepresentations()
, n.parents = 2L
, defaults = list()
, checker = function(operator.control) TRUE
) {
recombinator = makeOperator(recombinator, name, description, supported, defaults)

assertInteger(n.parents, len = 1L, lower = 2L, any.missing = FALSE)
Expand Down
48 changes: 27 additions & 21 deletions R/makeSelector.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
# Helper function which defines a selector method, i. e., an operator which
# takes the population return a part of it for mating.
#
# @param selector [\code{function}]\cr
# Actual selection operator.
# @param name [\code{character(1)}]\cr
# Name of the selector.
# @param description [\code{character(1)}]\cr
# Short description of how the selector works.
# @param supported [\code{character}]\cr
# Vector of strings/names of supported parameter representations. For example
# 'permutation', 'float', 'binary'.
# @param supported.objectives [\code{character}]\cr
# At least one of \dQuote{single-objective} or \dQuote{multi-objective}.
# @return [\code{ecr_selector}]
# selector object.
makeSelector = function(
selector,
name, description,
supported = getAvailableRepresentations(),
supported.objectives) {
#' @title
#' Construct a selection operator
#' @description
#' Helper function which defines a selector method, i.e., an operator which
#' takes the population return a part of it for mating.
#'
#' @param selector [\code{function}]\cr
#' Actual selection operator.
#' @param name [\code{character(1)}]\cr
#' Name of the selector.
#' @param description [\code{character(1)}]\cr
#' Short description of how the selector works.
#' @param supported [\code{character}]\cr
#' Vector of strings/names of supported parameter representations. For example
#' 'permutation', 'float', 'binary'.
#' @param supported.objectives [\code{character}]\cr
#' At least one of \dQuote{single-objective} or \dQuote{multi-objective}.
#' @return [\code{ecr_selector}]
#' selector object.
#' @export
makeSelector = function(selector, name, description
, supported = getAvailableRepresentations()
, supported.objectives
) {
# argument check
assertFunction(selector, args = c("population", "n.select", "control"), ordered = TRUE)
assertSubset(supported.objectives, c("single-objective", "multi-objective"))

selector = makeOperator(selector, name, description, supported)
selector = setAttribute(selector, "supported.objectives", supported.objectives)
selector = addClasses(selector, c("ecr_selector"))

return(selector)
}
2 changes: 1 addition & 1 deletion inst/examples/custom_example.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ myMutator = makeMutator(
)

myRecombinator = makeRecombinator(
recombinator = function(x, control) {
recombinator = function(x, args, control) {
x[[1]]
},
name = "Convex-Combination recombinator",
Expand Down
39 changes: 39 additions & 0 deletions man/makeMutator.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/makeMutator.R
\name{makeMutator}
\alias{makeMutator}
\title{Construct a mutation operator}
\usage{
makeMutator(mutator, name, description,
supported = getAvailableRepresentations(), defaults = list(),
checker = function(operator.control) TRUE)
}
\arguments{
\item{mutator}{[\code{function}]\cr
Actual mutation operator.}

\item{name}{[\code{character(1)}]\cr
Name of the mutator.}

\item{description}{[\code{character(1)}]\cr
Short description of how the mutator works.}

\item{supported}{[\code{character}]\cr
Vector of strings/names of supported parameter representations. Possible are
'permutation', 'binary', 'float', 'custom'.}

\item{defaults}{[\code{list}]\cr
List of default values for the operators strategy parameters.}

\item{checker}{[\code{function}]\cr
Check object, which performs a sanity check in mutator strategy parameters
passed to the control object.}
}
\value{
[\code{ecr_mutator}]
Mutator object.
}
\description{
Helper function which constructs a mutator, i.e., a mutation operator.
}

42 changes: 42 additions & 0 deletions man/makeRecombinator.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/makeRecombinator.R
\name{makeRecombinator}
\alias{makeRecombinator}
\title{Construct a recombination operator}
\usage{
makeRecombinator(recombinator, name, description,
supported = getAvailableRepresentations(), n.parents = 2L,
defaults = list(), checker = function(operator.control) TRUE)
}
\arguments{
\item{recombinator}{[\code{function}]\cr
Actual mutation operator.}

\item{name}{[\code{character(1)}]\cr
Name of the recombinator.}

\item{description}{[\code{character(1)}]\cr
Short description of how the recombinator works.}

\item{supported}{[\code{character}]\cr
Vector of strings/names of supported parameter representations. For example
'permutation', 'float', 'binary'.}

\item{n.parents}{[\code{integer(1)}]\cr
Number of parents supported.}

\item{defaults}{[\code{list}]\cr
List of default values for the operators strategy parameters.}

\item{checker}{[\code{function}]\cr
Check object, which performs a sanity check in mutator strategy parameters
passed to the control object.}
}
\value{
[\code{ecr_recombinator}]
Recombinator object.
}
\description{
Helper function which constructs a recombinator, i.e., a recombination operator.
}

35 changes: 35 additions & 0 deletions man/makeSelector.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/makeSelector.R
\name{makeSelector}
\alias{makeSelector}
\title{Construct a selection operator}
\usage{
makeSelector(selector, name, description,
supported = getAvailableRepresentations(), supported.objectives)
}
\arguments{
\item{selector}{[\code{function}]\cr
Actual selection operator.}

\item{name}{[\code{character(1)}]\cr
Name of the selector.}

\item{description}{[\code{character(1)}]\cr
Short description of how the selector works.}

\item{supported}{[\code{character}]\cr
Vector of strings/names of supported parameter representations. For example
'permutation', 'float', 'binary'.}

\item{supported.objectives}{[\code{character}]\cr
At least one of \dQuote{single-objective} or \dQuote{multi-objective}.}
}
\value{
[\code{ecr_selector}]
selector object.
}
\description{
Helper function which defines a selector method, i.e., an operator which
takes the population return a part of it for mating.
}

0 comments on commit afc6441

Please sign in to comment.