GrammaticalEvolution {gramEvol} | R Documentation |
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 class 'GrammaticalEvolution'
print(x, ..., show.genome = FALSE)
Arguments
grammarDef |
A |
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 |
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 |
The evolutionary optimizer. |
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 |
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 |
monitorFunc |
A function that is called at each generation. It can be used to monitor evolution of population. |
disable.warnings |
If |
plapply |
|
... |
Additional parameters are passed to |
x |
Grammatical Evolution results. |
show.genome |
Prints the numeric value of genome if TRUE. |
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.
Value
The results of GeneticAlg.int
or EvolutionStrategy.int
with an additional item:
best$expressions |
Expression(s) with the best cost. |
See Also
CreateGrammar
,
GeneticAlg.int
,
EvolutionStrategy.int
,
EvalExpressions
Examples
# 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)