EvolutionStrategy.int {gramEvol} | R Documentation |
Evolution Strategy with Integer Chromosomes
Description
Uses evolution strategy to find the minima of a given cost function. It evolves chromosomes with limited-range integers as codons.
Usage
EvolutionStrategy.int(genomeLen, codonMin, codonMax,
genomeMin = rep.int(codonMin, genomeLen),
genomeMax = rep.int(codonMax, genomeLen),
suggestion = NULL, popSize=4, newPerGen = 4,
iterations = 500, terminationCost = NA,
mutationChance = 1/(genomeLen+1),
monitorFunc = NULL, evalFunc, allowrepeat = TRUE,
showSettings = FALSE, verbose = FALSE, plapply = lapply)
Arguments
genomeLen |
Number of integers (i.e, codons) in chromosome. |
codonMin |
Minimum integer value range for all codons. |
codonMax |
Maximum integer value range for all codons. |
genomeMin |
A vector of length |
genomeMax |
A vector of length |
suggestion |
A list of suggested chromosomes to be used in the initial population. |
popSize |
Size of the population generated by mutating the parent. |
newPerGen |
Number of the new randomly generated chromosome in each generation. |
iterations |
Number of generations to evolve the population. |
terminationCost |
Target cost. If the best chromosome's cost reaches this value, the algorithm terminates. |
mutationChance |
The chance of a codon being mutated. It must be between 0 and 1. |
monitorFunc |
A function that is called at each generation. Can be used to monitor evolution of population. |
evalFunc |
The cost function. |
allowrepeat |
Allows or forbids repeated integers in the chromosome. |
showSettings |
Enables printing GA settings. |
verbose |
Enables verbose debugging info. |
plapply |
|
Details
EvolutionStrategy.int
implements evolutionary strategy search algorithm with
chromosomes created from integer values in the range of codonMin
to
codonMax
. genomeMin
and genomeMax
allow fine-grained
control of range for individual codons.
It first creates an initial population, using suggested input
suggestion
or a randomly generated chromosome.
Score of each chromosome is evaluated using the cost function
costFunc
. If the best chromosome reaches
terminationCost
, the algorithm terminates;
otherwise only the best candidate is selected and mutated to create a new generation,
and the cycle is repeated.
This iteration continues until the required cost is reached
or the number of generations exceeds iterations
.
At each generation, the supplied monitorFunc
is called with a
list similar to EvolutionStrategy.int
returning value as its argument.
The evalFunc
receives integer sequences and must return a numeric value.
The goal of optimization would be to find a chromosome which minimizes this value.
To parallelize cost function evaluation, set plapply
to a parallelized
lapply
, such as mclapply
from package parallel
.
In functions that do not handle data dependencies such as parLapply
,
variables and functions required for correct execution of evalFunc
must be exported to worker nodes before invoking EvolutionStrategy.int
.
Value
A list containing information about
settings
, population
, and the best
chromosome.
settings$genomeMin |
Minimum of each codon. |
Settings$genomeMax |
Maximum of each codon. |
settings$popSize |
Size of the population created using mutation. |
settings$newPerGen |
Number of the new randomly generated chromosome in each generation. |
settings$totalPopulation |
Size of the total population. |
settings$iterations |
Number of maximum generations. |
settings$suggestion |
Suggested chromosomes. |
settings$mutationChance |
Mutation chance. |
population$population |
The genomic data of the current population. |
population$evaluations |
Cost of the latest generation. |
population$best |
Historical cost of the best chromosomes. |
population$mean |
Historical mean cost of population. |
population$currentIteration |
Number of generations evolved until now. |
best$genome |
The best chromosome in integer sequence format. |
best$cost |
The cost of the best chromosome. |
See Also
GrammaticalEvolution
,
GeneticAlg.int
Examples
# define the evaluate function
evalfunc <- function(l) {
# maximize the odd indices and minimize the even indices
# no repeated values are allowed
odd <- seq(1, 20, 2)
even <- seq(2, 20, 2)
err <- sum(l[even]) - sum(l[odd]);
stopifnot(!any(duplicated(l))) # no duplication allowed
return (err)
}
monitorFunc <- function(result) {
cat("Best of gen: ", min(result$best$cost), "\n")
}
x <- EvolutionStrategy.int(genomeLen = 20, codonMin = 0, codonMax = 20,
allowrepeat = FALSE, terminationCost = -110,
monitorFunc = monitorFunc, evalFunc = evalfunc)
print(x)
best.result <- x$best$genome
print("Odds:")
print(sort(best.result[seq(1, 20, 2)]))
print("Evens:")
print(sort(best.result[seq(2, 20, 2)]))