# expressionMutation: Random mutation of functions and expressions In rgp: R genetic programming framework

## Description

RGP implements two sets of mutation operators. The first set is inspired by classical GP systems. Mutation strength is controlled by giving mutation probabilities: `mutateFunc` mutates a function f by recursively replacing inner function labels in f with probability `mutatefuncprob`. `mutateSubtree` mutates a function by recursively replacing inner nodes with newly grown subtrees of maximum depth `maxsubtreedepth`. `mutateNumericConst` mutates a function by perturbing each numeric (double) constant c with probability `mutateconstprob` by setting c := c + rnorm(1, mean = mu, sd = sigma). Note that constants of other typed than `double` (e.g `integer`s) are not affected.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49``` ```mutateFunc(func, funcset, mutatefuncprob = 0.1, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateSubtree(func, funcset, inset, conset, mutatesubtreeprob = 0.1, maxsubtreedepth = 5, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateNumericConst(func, mutateconstprob = 0.1, breedingFitness = function(individual) TRUE, breedingTries = 50, mu = 0, sigma = 1) mutateFuncTyped(func, funcset, mutatefuncprob = 0.1, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateSubtreeTyped(func, funcset, inset, conset, mutatesubtreeprob = 0.1, maxsubtreedepth = 5, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateNumericConstTyped(func, mutateconstprob = 0.1, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateChangeLabel(func, funcset, inset, conset, strength = 1, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateInsertSubtree(func, funcset, inset, conset, strength = 1, subtreeDepth = 2, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateDeleteSubtree(func, funcset, inset, conset, strength = 1, subtreeDepth = 2, constprob = 0.2, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateChangeDeleteInsert(func, funcset, inset, conset, strength = 1, subtreeDepth = 2, constprob = 0.2, iterations = 1, changeProbability = 1/3, deleteProbability = 1/3, insertProbability = 1/3, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateDeleteInsert(func, funcset, inset, conset, strength = 1, subtreeDepth = 2, constprob = 0.2, iterations = 1, deleteProbability = 0.5, insertProbability = 0.5, breedingFitness = function(individual) TRUE, breedingTries = 50) mutateFuncFast(funcbody, funcset, mutatefuncprob = 0.1) mutateSubtreeFast(funcbody, funcset, inset, constmin, constmax, insertprob, deleteprob, subtreeprob, constprob, maxsubtreedepth) mutateNumericConstFast(funcbody, mutateconstprob = 0.1, mu = 0, sigma = 1) ```

## Arguments

 `func` The function to mutate randomly. `funcbody` The function body to mutate randomly, obtain it via `body(func)`. `funcset` The function set. `inset` The set of input variables. `conset` The set of constant factories. `mutatefuncprob` The probability of trying to replace an inner function at each node. `mutatesubtreeprob` The probability of replacing a subtree with a newly grown subtree at each node. `maxsubtreedepth` The maximum depth of newly grown subtrees. `mutateconstprob` The probability of mutating a constant by adding `rnorm(1)` to it. `strength` The number of individual point mutations (changes, insertions, deletions) to perform. `subtreeDepth` The depth of the subtrees to insert or delete. `constprob` The probability of creating a constant versus an input variable. `insertprob` The probability to insert a subtree. `deleteprob` The probability to insert a subtree. `constmin` The lower limit for numeric constants. `constmax` The upper limit for numeric onstants. `mu` The normal distribution mean for random numeric constant mutation. `sigma` The normal distribution standard deviation for random numeric constant mutation. `subtreeprob` The probability of creating a subtree instead of a leaf in the random subtree generator function. `iterations` The number of times to apply a mutation operator to a GP individual. This can be used as a generic way of controling the strength of the genotypic effect of mutation. `changeProbability` The probability for selecting the `mutateChangeLabel` operator. `deleteProbability` The probability for selecting the `mutateDeleteSubtree` operator. `insertProbability` The probability for selecting the `mutateInsertSubtree` operator. `breedingFitness` A breeding function. See the documentation for `geneticProgramming` for details. `breedingTries` The number of breeding steps.

## Details

`mutateFuncTyped`, `mutateSubtreeTyped`, and `mutateNumericConstTyped` are variants of the above functions that only create well-typed result expressions.

`mutateFuncFast`, `mutateSubtreeFast`, `mutateNumericConstFast` are variants of the above untyped mutation function implemented in C. They offer a considerably faster execution speed for the price of limited flexibility. These variants take function bodies as arguments (obtain these via R's `body` function) and return function bodies as results. To turn a function body into a function, use RGP's `makeClosure` tool function.

The second set of mutation operators features a more orthogonal design, with each individual operator having a only a small effect on the genotype. Mutation strength is controlled by the integral `strength` parameter. `mutateChangeLabel` Selects a node (inner node or leaf) by uniform random sampling and replaces the label of this node by a new label of matching type. `mutateInsertSubtree` Selects a leaf by uniform random sampling and replaces it with a matching subtree of the exact depth of `subtreeDepth`. `mutateDeleteSubtree` Selects a subree of the exact depth of `subtreeDepth` by uniform random sampling and replaces it with a matching leaf. `mutateChangeDeleteInsert` Either applies `mutateChangeLabel`, `mutateInsertSubtree`, or `mutateDeleteSubtree`. The probability weights for selecting an operator can be supplied via the ...Probability arguments (probability weights are normalized to a sum of 1). `mutateDeleteInsert` Either applies `mutateDeleteSubtree` or `mutateInsertSubtree`. The probability weights for selecting an operator can be supplied via the ...Probability arguments (probability weights are normalized to a sum of 1). The above functions automatically create well-typed result expressions when used in a strongly typed GP run.

All RGP mutation operators have the S3 class `c("mutationOperator", "function")`.

## Value

The randomly mutated function.

rgp documentation built on May 30, 2017, 12:45 a.m.