GPR Manual
Table of Contents
 1 The mglgpr ASDF System
 2 Links
 3 Background
 4 Evolutionary Algorithms
 5 Genetic Programming
 6 Differential Evolution
[in package MGLGPR]
1 The mglgpr ASDF System
 Version: 0.0.1
 Description:
mglgpr
is a library of evolutionary algorithms such as Genetic Programming (evolving typed expressions from a set of operators and constants) and Differential Evolution.  Licence: MIT, see COPYING.
 Author: GĂˇbor Melis mailto:mega@retes.hu
 Mailto: mega@retes.hu
 Homepage: http://melisgl.github.io/mglgpr
 Bug tracker: https://github.com/melisgl/mglgpr/issues
 Source control: GIT
2 Links
Here is the official repository and the HTML documentation for the latest version.
3 Background
Evolutionary algorithms are optimization tools that assume little of the task at hand. Often they are population based, that is, there is a set of individuals that each represent a candidate solution. Individuals are combined and changed with crossover and mutationlike operators to produce the next generation. Individuals with lower fitness have a lower probability to survive than those with higher fitness. In this way, the fitness function defines the optimization task.
Typically, EAs are quick to get up and running, can produce reasonable results across a wild variety of domains, but they may need a bit of fiddling to perform well and domain specific approaches will almost always have better results. All in all, EA can be very useful to cut down on the tedium of human trial and error. However, they have serious problems scaling to higher number of variables.
This library grew from the Genetic Programming implementation I wrote while working for Ravenpack who agreed to release it under an MIT licence. Several years later I cleaned it up, and documented it. Enjoy.
4 Evolutionary Algorithms
Evolutionary algorithm is an umbrella term. In this section we first discuss the concepts common to conrete evolutionary algorithms Genetic Programming and Differential Evolution.
[class] evolutionaryalgorithm
The
evolutionaryalgorithm
is an abstract base class for generational, population based optimization algorithms.
4.1 Populations
The currenly implemented EAs are generational. That is, they maintain a population of candidate solutions (also known as individuals) which they replace with the next generation of individuals.
[accessor] populationsize evolutionaryalgorithm (:populationsize)
The number of individuals in a generation. This is a very important parameter. Too low and there won't be enough diversity in the population, too high and convergence will be slow.
[accessor] population evolutionaryalgorithm (= (makearray 0 :adjustable 0 :fillpointer t))
An adjustable array with a fillpointer that holds the individuals that make up the population.
[reader] generationcounter evolutionaryalgorithm (= 0)
A counter that starts from 0 and is incremented by
advance
. All accessors ofevolutionaryalgorithm
are allowed to be specialized on a subclass which allows them to be functions ofgenerationcounter
.
[function] addindividual ea individual
Adds
individual
topopulation
ofea
. Usually called when initializing theea
.
4.2 Evaluation
[reader] evaluator evolutionaryalgorithm (:evaluator)
A function of two arguments: the
evolutionaryalgorithm
object and an individual. It must return the fitness of the individual. For Genetic Programming, the evaluator often simply callseval
, orcompile
+funcall
, and compares the result to some gold standard. It is also typical to slightly penalize solutions with too many nodes to control complexity and evaluation cost (seecountnodes
). For Differential Evolution, individuals are conceptually (and often implemented as) vectors of numbers so the fitness function may include an L1 or L2 penalty term.Alternatively, one can specify
massevaluator
instead.
[reader] massevaluator evolutionaryalgorithm (:massevaluator = nil)
nil
or a function of three arguments: theevolutionaryalgorithm
object, the population vector and the fitness vector into which the fitnesses of the individuals in the population vector shall be written. By specifyingmassevaluator
instead of anevaluator
, one can, for example, distribute costly evaluations over multiple threads.massevaluator
has precedence overevaluator
.
[reader] fitnesskey evolutionaryalgorithm (:fitnesskey = #'identity)
A function that returns a real number for an object returned by
evaluator
. It is called when two fitness are to be compared. The default value is #'identity
which is sufficient whenevaluator
returns real numbers. However, sometimes the evaluator returns more information about the solution (such as fitness in various situations) andfitnesskey
key be used to select the fitness value.
4.3 Training
Training is easy: one creates an object of a subclass of
evolutionaryalgorithm
such as geneticprogramming
or
differentialevolution
, creates the initial population by adding
individuals to it (see addindividual
) and calls advance
in a loop
to move on to the next generation until a certain number of
generations or until the fittest
individual is good enough.

Create the next generation and place it in
population
ofea
.
[reader] fittest evolutionaryalgorithm (= nil)
The fittest individual ever to be seen and its fittness as a cons cell.
[accessor] fittestchangedfn evolutionaryalgorithm (:fittestchangedfn = nil)
If non
nil
, a function that's called whenfittest
is updated with three arguments: theevolutionaryalgorithm
object, the fittest individual and its fitness. Useful for tracking progress.
5 Genetic Programming
5.1 Background
What is Genetic Programming? This is what Wikipedia has to say:
In artificial intelligence, genetic programming (GP) is an evolutionary algorithmbased methodology inspired by biological evolution to find computer programs that perform a userdefined task. Essentially GP is a set of instructions and a fitness function to measure how well a computer has performed a task. It is a specialization of genetic algorithms (GA) where each individual is a computer program. It is a machine learning technique used to optimize a population of computer programs according to a fitness landscape determined by a program's ability to perform a given computational task.
Lisp has a long history of Genetic Programming because GP involves manipulation of expressions which is of course particularly easy with sexps.
5.2 Tutorial
GPR works with typed expressions. Mutation and crossover never produce expressions that fail with a type error. Let's define a couple of operators that work with real numbers and also return a real:
(defparameter *operators* (list (operator (+ real real) real)
(operator ( real real) real)
(operator (* real real) real)
(operator (sin real) real)))
One cannot build an expression out of these operators because they
all have at least one argument. Let's define some literal classes
too. The first is produces random numbers, the second always returns
the symbol *x*
:
(defparameter *literals* (list (literal (real)
( (random 32.0) 16.0))
(literal (real)
'*x*)))
Armed with *operators*
and *literals*
, one can already build
random expressions with randomexpression
, but we also need to
define how good a certain expression is which is called fitness.
In this example, we are going to perform symbolic regression, that is, try to find an expression that approximates some target expression well:
(defparameter *targetexpr* '(+ 7 (sin (expt (* *x* 2 pi) 2))))
Think of *targetexpr*
as a function of *x*
. The evaluator
function will bind the special *x*
to the input and simply eval
the expression to be evaluated.
(defvar *x*)
The evaluator function calculates the average difference between
expr
and targetexpr
, penalizes large expressions and returns
the fitness of expr
. Expressions with higher fitness have higher
chance to produce offsprings.
(defun evaluate (gp expr targetexpr)
(declare (ignore gp))
(/ 1
(1+
;; Calculate average difference from target.
(/ (loop for x from 0d0 to 10d0 by 0.5d0
summing (let ((*x* x))
(abs ( (eval expr)
(eval targetexpr)))))
21))
;; Penalize large expressions.
(let ((minpenalizedsize 40)
(size (countnodes expr)))
(if (< size minpenalizedsize)
1
(exp (min 120 (/ ( size minpenalizedsize) 10d0)))))))
When an expression is to undergo mutation, a randomizer function is
called. Here we change literal numbers slightly, or produce an
entirely new random expression that will be substituted for expr
:
(defun randomize (gp type expr)
(if (and (numberp expr)
(< (random 1.0) 0.5))
(+ expr (random 1.0) 0.5)
(randomgpexpression gp (lambda (level)
(<= 3 level))
:type type)))
That's about it. Now we create a GP instance hooking everything up,
set up the initial population and just call advance
a couple of
times to create new generations of expressions.
(defun run ()
(let ((*printlength* nil)
(*printlevel* nil)
(gp (makeinstance
'gp
:topleveltype 'real
:operators *operators*
:literals *literals*
:populationsize 1000
:copychance 0.0
:mutationchance 0.5
:evaluator (lambda (gp expr)
(evaluate gp expr *targetexpr*))
:randomizer 'randomize
:selector (lambda (gp fitnesses)
(declare (ignore gp))
(holdtournament fitnesses :ncontestants 2))
:fittestchangedfn
(lambda (gp fittest fitness)
(format t "Best fitness until generation ~S: ~S for~% ~S~%"
(generationcounter gp) fitness fittest)))))
(loop repeat (populationsize gp) do
(addindividual gp (randomgpexpression gp (lambda (level)
(<= 5 level)))))
(loop repeat 1000 do
(when (zerop (mod (generationcounter gp) 20))
(format t "Generation ~S~%" (generationcounter gp)))
(advance gp))
(destructuringbind (fittest . fitness) (fittest gp)
(format t "Best fitness: ~S for~% ~S~%" fitness fittest))))
Note that this example can be found in example/symbolicregression.lisp.
5.3 Expressions
Genetic programming works with a population of individuals. The
individuals are sexps that may be evaluated directly by eval
or by
other means. The internal nodes and the leafs of the sexp as a tree
represent the application of operators and literal objects,
respectively. Note that currently there is no way to represent
literal lists.

An object of
expressionclass
defines two things: how to build a random expression that belongs to that expression class and what lisp type those expressions evaluate to.
[reader] resulttype expressionclass (:resulttype)
Expressions belonging to this expression class must evaluate to a value of this lisp type.
[reader] weight expressionclass (:weight = 1)
The probability of an expression class to be selected from a set of candidates is proportional to its weight.
[class] operator expressionclass
Defines how the symbol
name
in the function position of a list can be combined arguments: how many and of what types. The following defines+
as an operator that adds twofloat
(0
1
)s:(makeinstance 'operator :name '+ :resulttype float :argumenttypes '(float float))
See the macro
operator
for a shorthand for the above.Currently no lambda list keywords are supported and there is no way to define how an expression with a particular operator is to be built. See
randomexpression
.
[reader] argumenttypes operator (:argumenttypes)
A list of lisp types. One for each argument of this operator.
[macro] operator (name &rest argtypes) resulttype &key (weight 1)
Syntactic sugar for instantiating operators. The example given for
operator
could be written as:(operator (+ float float) float)
See
weight
for whatweight
means.
[class] literal expressionclass
This is slightly misnamed. An object belonging to the
literal
class is not a literal itself, it's a factory for literals via itsbuilder
function. For example, the following literal builds bytes:(makeinstance 'literal :resulttype '(unsignedbyte 8) :builder (lambda () (random 256)))
In practice, one rarely writes it out like that, because the
literal
macro provides a more convenient shorthand.
[reader] builder literal (:builder)
A function of no arguments that returns a random literal that belongs to its literal class.
[macro] literal (resulttype &key (weight 1)) &body body
Syntactic sugar for defining literal classes. The example given for
literal
could be written as:(literal ((unsignedbyte 8)) (random 256))
See
weight
for whatweight
means.
[function] randomexpression operators literals type terminatefn
Return an expression built from
operators
andliterals
that evaluates to values oftype
.terminatefn
is a function of one argument: the level of the root of the subexpression to be generated in the context of the entire expression. If it returnst
then aliteral
will be inserted (by calling itsbuilder
function), else anoperator
with all its necessary arguments.The algorithm recursively generates the expression starting from level 0 where only operators and literals with a
resulttype
that's a subtype oftype
are considered and one is selected with the unnormalized probability given by itsweight
. On lower levels, theargumenttypes
specification of operators is similarly satisfied and the resulting expression should evaluate without without a type error.The building of expressions cannot backtrack. If it finds itself in a situation where no literals or operators of the right type are available then it will fail with an error.
5.4 Basics
To start the evolutionary process one creates a GP object,
adds to it the individuals (see addindividual
) that make up the
initial population and calls advance
in a loop to move on to the
next generation.
[class] geneticprogramming evolutionaryalgorithm
The
geneticprogramming
class defines the search space, how mutation and recombination occur, and hold various parameters of the evolutionary process and the individuals themselves.
[function] randomgpexpression gp terminatefn &key (type (topleveltype gp))
Creating the initial population by hand is tedious. This convenience function calls
randomexpression
to create a random individual that producesgp
'stopleveltype
. By passing in anothertype
one can create expressions that fit somewhere else in a larger expression which is useful in arandomizer
function.
5.5 Search Space
The search space of the GP is defined by the available operators, literals and the type of the final result produced. The evaluator function acts as the guiding light.
[reader] operators geneticprogramming (:operators)
The set of
operator
s from which (together withliteral
s) individuals are built.
[reader] literals geneticprogramming (:literals)
The set of
literal
s from which (together withoperator
s) individuals are built.
[reader] topleveltype geneticprogramming (:topleveltype = t)
The type of the results produced by individuals. If the problem is to find the minimum a 1d real function then this may be the symbol
real
. If the problem is to find the shortest route, then this may be a vector. It all depends on the representation of the problem, the operators and the literals.
[function] countnodes tree &key internal
Count the nodes in the sexp
tree
. Ifinternal
then don't count the leaves.
5.6 Reproduction
The randomizer
and selector
functions define how mutation and
recombination occur.
[reader] randomizer geneticprogramming (:randomizer)
Used for mutations, this is a function of three arguments: the GP object, the type the expression must produce and current expression to be replaced with the returned value. It is called with subexpressions of individuals.
[reader] selector geneticprogramming (:selector)
A function of two arguments: the GP object and a vector of fitnesses. It must return the and index into the fitness vector. The individual whose fitness was thus selected will be selected for reproduction be it copying, mutation or crossover. Typically, this defers to
holdtournament
.
[function] holdtournament fitnesses &key selectcontestantfn ncontestants key
Select
ncontestants
(all different) for the tournament randomly, represented by indices intofitnesses
and return the one with the highest fitness. Ifselectcontestantfn
isnil
then contestants are selected randomly with uniform probability. Ifselectcontestantfn
is a function, then it's called withfitnesses
to return an index (that may or may not be already selected for the tournament). Specifyingselectcontestantfn
allows one to conduct 'local' tournaments biased towards a particular region of the index range.key
isnil
or a function that select the real fitness value from elements offitnesses
.
5.7 Environment
The new generation is created by applying a reproduction operator
until populationsize
is reached in the new generation. At each
step, a reproduction operator is randomly chosen.
[accessor] copychance geneticprogramming (:copychance = 0)
The probability of the copying reproduction operator being chosen. Copying simply creates an exact copy of a single individual.
[accessor] mutationchance geneticprogramming (:mutationchance = 0)
The probability of the mutation reproduction operator being chosen. Mutation creates a randomly altered copy of an individual. See
randomizer
.
If neither copying nor mutation were chosen, then a crossover will take place.
[accessor] keepfittestp geneticprogramming (:keepfittestp = t)
If true, then the fittest individual is always copied without mutation to the next generation. Of course, it may also have other offsprings.
6 Differential Evolution
The concepts in this section are covered by Differential Evolution: A Survey of the StateoftheArt.
[class] differentialevolution evolutionaryalgorithm
Differential evolution (DE) is an evolutionary algorithm in which individuals are represented by vectors of numbers. New individuals are created by taking linear combinations or by randomly swapping some of these numbers between two individuals.
[reader] mapweightsintofn differentialevolution (:mapweightsintofn = #'mapinto)
The vector of numbers (the 'weights') are most often stored in some kind of array. All individuals must have the same number of weights, but the actual representation can be anything as long as the function in this slot mimics the semantics of
mapinto
that's the default.
[reader] createindividualfn differentialevolution (:createindividualfn)
Holds a function of one argument, the DE, that returns a new individual that needs not be initialized in any way. Typically this just calls
makearray
.
[reader] mutatefn differentialevolution (:mutatefn)
One of the supplied mutation functions:
mutate/rand/1
mutate/best/1
mutate/currenttobest/2
.
[reader] crossoverfn differentialevolution (:crossoverfn = #'crossover/binary)
A function of three arguments, the DE and two individuals, that destructively modifies the second individual by using some parts of the first one. Currently, the implemented crossover function is
crossover/binary
.
 [function] mutate/rand/1 de current best population nursery &key (f 0.5)
 [function] mutate/best/1 de current best population nursery &key (f 0.5)
 [function] mutate/currenttobest/2 de current best population nursery &key (f 0.5)
[function] crossover/binary de individual1 individual2 &key (crossoverrate 0.5)
Destructively modify
individual2
by replacement each element with a probability of 1 crossoverrate
with the corresponding element inindividual1
. At least one, element is changed. Returnindividual2
.
 [function] selectdistinctrandomnumbers taboos n limit
6.1 SANSDE
[class] sansde differentialevolution
SaNSDE is a special DE that dynamically adjust the crossover and mutation are performed. The only parameters are the generic EA ones:
populationsize
,evaluator
, etc. One also has to specifymapweightsintofn
andcreateindividualfn
.