Learn R Programming

gramEvol (version 2.1-4)

GrammaticalEvolution: Grammatical Evolution

Description

Evolves an expression using a context-free grammar to minimize a given cost function.

Usage

GrammaticalEvolution(grammarDef, evalFunc, 
              numExpr = 1, 
              max.depth = GrammarGetDepth(grammarDef),
              startSymb = GrammarStartSymbol(grammarDef),
              seqLen = GrammarMaxSequenceLen(grammarDef, max.depth, startSymb),
              wrappings = 3, 
              suggestions = NULL,
              optimizer = c("auto", "es", "ga"),
              popSize = "auto", newPerGen = "auto", elitism = 2,
              mutationChance = NA,
              iterations = "auto",
              terminationCost = NA,
              monitorFunc = NULL,
              disable.warnings=FALSE,
              plapply = lapply, ...)

# S3 method for GrammaticalEvolution print(x, ..., show.genome = FALSE)

Arguments

grammarDef

A grammar object.

evalFunc

The cost function, taking a string or a collection of strings containing the expression(s) as its input and returning the cost of the expression(s).

numExpr

Number of expressions generated and given to evalFunc.

max.depth

Maximum depth of search in case of a cyclic grammar. By default it is limited to the number of production rules in the grammar.

startSymb

The symbol where the generation of a new expression should start.

seqLen

Length of integer vector used to create the expression.

wrappings

Number of wrappings in case the length of chromosome is not enough for conversion to an expression.

suggestions

Suggested chromosomes to be added to the initial population pool. if optimizer parameter is set to "es", only a single chromosome (as a numeric vector) is acceptable. For "ga" mode, a list of numeric vectors.

optimizer

The evolutionary optimizer. "es" uses evolution strategy as in EvolutionStrategy.int and "ga" uses genetic algorithm as in GeneticAlg.int. "auto" chooses evolution strategy when numExpr = 1, and genetic algorithm otherwise. If "auto" is used, popSize and iterations are tweaked based on the grammar as well.

popSize

Population size in the evolutionary optimizer. By default, 8 for ES and 48 for GA.

newPerGen

Number of randomly generated individuals in evolution strategy. If ``auto", it is set to 25% of population of grammar if it is not recursive, otherwise to all of it.

elitism

Number of top ranking chromosomes that are directly transfered to the next generation without going through evolutionary operations, used in genetic algorithm optimizer.

iterations

Number of maximum iterations in the evolutionary optimizer. By default, 1000 for "es" optimizer and 200 for "ga".

terminationCost

Target cost. If a sequence with this cost or less is found, the algorithm terminates.

mutationChance

Mutation chance in the evolutionary optimizer. It must be between 0 and 1. By default it is set to 1/(1+chromosome size)) for genetic algorithm and 10/(1+chromosome size)) for evolution strategy.

monitorFunc

A function that is called at each generation. It can be used to monitor evolution of population.

disable.warnings

If TRUE, suppresses any warnings generated while evaulating evalFuncs.

plapply

lapply function used for mapping chromosomes to the cost function. See details for parallelization tips.

Additional parameters are passed to GeneticAlg.int or EvolutionStrategy.int.

x

Grammatical Evolution results.

show.genome

Prints the numeric value of genome if TRUE.

Value

The results of GeneticAlg.int or EvolutionStrategy.int with an additional item:

best$expressions

Expression(s) with the best cost.

Details

This function performs an evolutionary search over the grammar, better known as Grammatical Evolution. It evolves integer sequences and converts them to a collection containing numExpr expression. These expressions can be evaluated using eval function. The evalFunc receives these expressions and must return a numeric value. The goal of optimization would be to find a chromosome which minimizes this function.

Two evolutionary optimizers are supported: Genetic algorithm and evolution strategy, which are set by the optimizer parameter.

Only valid (i.e., terminal) expressions are passed to evalFunc, and it is guaranteed that evalFunc receives at least one expression.

If the grammar contains recursive elements, it is advisable that chromosomeLen is defined manually, as in such cases the possible search space grows explosively with the recursion. The evolutionary algorithm automatically removes the recursive chromosomes from the population by imposing a penalty for chromosomes creating expressions with non-terminal elements.

monitorFunc receives a list similar to the GrammaticalEvolution's return value.

See Also

CreateGrammar, GeneticAlg.int, EvolutionStrategy.int, EvalExpressions

Examples

Run this code
# NOT RUN {
# Grammar Definition
ruleDef <- list(expr     = gsrule("<der.expr><op><der.expr>"),
                der.expr = grule(func(var), var),
                func     = grule(log, exp, sin, cos),
                op       = gsrule("+", "-", "*"),
                var      = grule(A, B, n),
                n        = grule(1, 2, 3, 4))

# Creating the grammar object
grammarDef <- CreateGrammar(ruleDef)

# cost function
evalFunc <- function(expr) {
  # expr: a string containing a symbolic expression
  # returns: Symbolic regression Error
  A <- 1:6
  B <- c(2, 5, 8, 3, 4, 1)
  
  result <- eval(as.expression(expr))
  
  X <- log(A) * B
  err <- sum((result - X)^2)
  
  return(err)
}

# invoke grammatical evolution (with default parameters)
ge <- GrammaticalEvolution(grammarDef, evalFunc, terminationCost = 0.001)

# print results
print(ge, sequence = TRUE)
# }

Run the code above in your browser using DataLab