A genetic programming (GP) individual is a program tree, stored as a preorder sequence in which every subtree is a contiguous block. Its primitive set needs closure, so that every tree runs (protected division, which returns 1 at a zero divisor, supplies it at the price of hidden poles), and sufficiency: $\{+, -, \times, \div\}$ with $x$ and constants can express $x^2 + x + 1$ but, up to finitely many points, only rational functions, never $e^x$. A tree of depth $d$ with largest arity $a \ge 2$ has at most $(a^{d+1} - 1)/(a - 1)$ nodes and, with smallest arity $m$, at least $1 + md$. Over $\{+, -, \times, \%\}$ and $\{x, 1\}$ there are 6,739,218 trees of depth at most 3 and about $1.8 \times 10^{14}$ of depth at most 4, which is why every run limits size.
The expression $x^2 + x + 1$ can be written as a tree. The root is an addition. Its left child multiplies $x$ by $x$, and its right child is another addition, of $x$ and 1. In the bracketed prefix notation of Lisp the same tree reads
(+ (* x x) (+ x 1))
and it is a small computer program: given a value of $x$, evaluating the tree from the leaves up returns a number. Suppose a computer had to find this program on its own, knowing only what outputs it should produce. Two things have to be settled before it can search: what the candidate programs are made of, and what the pieces must satisfy so that any combination of them still runs. The pieces are a chosen set of functions and terminals, and the property they need is called closure.
Genetic programming (GP) is an evolutionary algorithm whose individuals are programs like this one. It creates a population of random trees and runs each one on a set of test inputs. It scores how close each tree's outputs come to what is wanted, and it breeds the better trees by exchanging and replacing their subtrees. Over many generations the population can arrive at a program, such as the tree above, that nobody wrote.
This article describes the representation. First we see what GP evolves, and how a tree is written down and run. Then we choose the building blocks, the function and terminal sets, and the two properties they need: closure, so that every tree runs, and sufficiency, so that some tree solves the problem. Next we score programs on fitness cases and list the decisions that set up a run. Finally we count how many trees there are, and compare GP with a genetic algorithm on fixed-length strings. How the first trees are created and how they are varied is the subject of the next article, initialization and variation in genetic programming.
The main modern source is the free A Field Guide to Genetic Programming by Poli, Langdon and McPhee (2008), cited here by section. The textbook treatment is Eiben and Smith (2015, §4.6 and §6.4). Every number below is printed by tools/experiments/evolution/gp_trees.py in the site repository, which uses the small GP library tools/experiments/evolution/gp.py.
What genetic programming evolves
The idea of evolving programs is older than GP. Cramer (1985) presented two small languages for evolving programs at the first International Conference on Genetic Algorithms. In the second, programs were trees and crossover exchanged subtrees. Koza developed the tree representation into a general method and demonstrated it on a wide range of problems in Genetic Programming: On the Programming of Computers by Means of Natural Selection (Koza, 1992). The history of evolutionary computation places this work among the other branches of the field.
In practice GP rarely evolves programs in a general-purpose language. It evolves expressions in a small, problem-specific language whose words the user chooses (Poli, Langdon and McPhee, 2008, §3.1):
- arithmetic, for fitting a formula to data;
- Boolean operators, for a logic circuit;
- movement commands, for a simulated robot.
Fitting a formula to data, one of the earliest and most widely used applications, is called symbolic regression. It is where this unit's running example, $x^2 + x + 1$, comes from.
Program trees and prefix notation
What is a program tree made of? It has two kinds of node.
- Functions are internal nodes: operations such as $+$, $\times$, $\sin$ or IF. Each takes a fixed number of arguments, its arity.
- Terminals are leaves: input variables such as $x$, constants such as 1, and functions of no arguments, such as a sensor reading.
The functions and terminals together make up the primitive set (Poli, Langdon and McPhee, 2008, §2.1).
Two measures of a tree recur throughout GP. The depth of a node is the number of edges between it and the root, so the root has depth 0. The depth of a tree is the depth of its deepest node. The size of a tree is its number of nodes. For the example tree:
| Notation | Form |
|---|---|
| Prefix (S-expression) | (+ (* x x) (+ x 1)) |
| Linear prefix | + * x x + x 1 |
| Infix | ((x * x) + (x + 1)) |
| Nodes in preorder, with depths | +@0, *@1, x@2, x@2, +@1, x@2, 1@2 |
The tree has size 7 and depth 2. Prefix notation mirrors the tree directly: each pair of brackets is a subtree, and its first symbol is that subtree's root. When every function has a fixed arity, the brackets are redundant. The arity of each symbol says how many of the following subexpressions belong to it, so a tree can be stored as the flat sequence + * x x + x 1 (Poli, Langdon and McPhee, 2008, §2.1). Many GP systems store trees this way, as a flat array in preorder, because it is compact and fast to copy. The same idea, a tree serialized so that operators precede their operands, appears in the query trees that a search engine builds from a Boolean query.
A useful habit when reading GP output is to translate between the three forms. The bracketed prefix form is the tree read top-down, and the infix form is what a person would write. The preorder index of a node is its position in the flat sequence. It is how the operators described in the next article address crossover and mutation points.
Example (a subtree in the flat sequence). In the flat sequence + * x x + x 1, the subtree rooted at index 4 occupies indices 4 to 6, the block + x 1, which computes $x + 1$. The subtree rooted at index 1 occupies indices 1 to 3, the block * x x, and computes $x^2$. Every subtree is contiguous in preorder, because preorder lists a node and then everything below it before moving on to any node outside that subtree. That is why a subtree can be cut out of, or spliced into, a flat array with one slice.
Running a tree
How does a tree become a number? An interpreter evaluates each node after its arguments, usually by a recursive traversal from the root (Poli, Langdon and McPhee, 2008, §3.3, Algorithm 3.1). The call on the root asks for the values of its two children, and each child asks for its own children's values. The recursion bottoms out at the leaves, and values then flow back up. At $x = 2$, nodes finish in this order (printed by Part 8 of the script):
| Finishes | Node (preorder index) | Arguments | Returns |
|---|---|---|---|
| 1 | x [2] |
2 | |
| 2 | x [3] |
2 | |
| 3 | * [1] |
2, 2 | 4 |
| 4 | x [5] |
2 | |
| 5 | 1 [6] |
1 | |
| 6 | + [4] |
2, 1 | 3 |
| 7 | + [0] |
4, 3 | 7 |
The output is $7 = 2^2 + 2 + 1$. The finishing order is a postorder traversal: every node appears after all of its descendants.
The flat preorder array can also be run without recursion and without brackets. Scan it from right to left with a stack. A terminal pushes its value. A function of arity $k$ pops $k$ values (the first popped is its first argument), applies itself, and pushes the result.
| Read | Stack afterwards (top at right) |
|---|---|
1 |
1 |
x |
1, 2 |
+ |
3 |
x |
3, 2 |
x |
3, 2, 2 |
* |
3, 4 |
+ |
7 |
Each function finds exactly its arguments on top of the stack, because, read backward, a subtree's arguments are complete before its root is reached. The precise claim is that scanning the block of any subtree from right to left leaves the stack as it was, with the subtree's value added on top. The proof is by induction on the size of the subtree. A single terminal pushes its value, which is the claim for size 1. A larger subtree is a function $f$ of arity $k$ followed by the blocks of its arguments $A_1, \dots, A_k$, each smaller than the subtree. The scan meets $A_k$ first, and by the induction hypothesis it adds the value $v_k$. It then adds $v_{k-1}$, and so on down to $v_1$, which ends on top. Then $f$ pops $k$ values, receiving $v_1$ first, and pushes $f(v_1, \dots, v_k)$, the value of the subtree. Applied to the whole tree, the claim says the scan ends with one value on the stack, the program's output. This is the reason fixed arities make the brackets redundant. It is also the evaluation scheme that stack-based GP adopts as its representation, with the program written in postfix order and read left to right (see genetic programming beyond trees).
For functions without side effects, the order of evaluation among siblings does not matter. For functions with side effects, such as a robot's MOVE, it does. The interpreter can then treat some primitives as macros that receive their arguments unevaluated, so that a conditional, for example, runs only the branch it chooses.
Function and terminal sets
We now know what a tree is and how it runs. The next choice is which functions and terminals it is built from. The first two decisions in any GP application are the terminal set and the function set, because together they define the language in which every candidate program is written. Typical choices (after Poli, Langdon and McPhee, 2008, Table 3.1):
| Kind | Examples |
|---|---|
| Arithmetic functions | $+$, $-$, $\times$, protected $\div$ |
| Mathematical functions | $\sin$, $\cos$, $\exp$ |
| Boolean functions | AND, OR, NOT |
| Conditionals and loops | IF-THEN-ELSE, REPEAT |
| Terminals | input variables, constants, zero-argument functions such as rand or a sensor |
Constants need special handling, because a fixed list of constants rarely contains the value a problem needs. The usual device is the ephemeral random constant. It is a terminal symbol that, each time it is chosen while building a tree, is replaced by a freshly drawn random number. That number then stays fixed in that tree for the rest of the run (Poli, Langdon and McPhee, 2008, §3.1). Evolution can build other values by arithmetic on these.
Two properties of the primitive set decide whether GP can work with it at all. Closure asks whether every tree runs; sufficiency asks whether some tree solves the problem.
Closure
GP's operators cut and splice trees without regard to meaning. Subtree crossover can move any subtree into any argument position of any function. For every resulting tree to be a valid program, the primitive set needs closure (Koza, 1992). Poli, Langdon and McPhee (2008, §3.2.1) divide it into two parts.
Type consistency requires that every function accept, in every argument, any value that any function or terminal can return. The simplest way to achieve it is to use a single type throughout, such as floating-point numbers.
Evaluation safety requires that every function return a usable value for every input. Division fails when the divisor is 0, and the logarithm fails for non-positive arguments. GP systems therefore use protected versions that check their inputs and return a default value instead of failing. Protected division, often written %, typically returns 1 when the divisor is 0 (Poli, Langdon and McPhee, 2008, §3.2.1):
| Expression | Value |
|---|---|
(% x (- x x)) at $x = -2$, 0 and 3.5 |
1, 1, 1 |
x / (x - x) in plain Python at $x = 3.5$ |
ZeroDivisionError |
Example (the constant 1 from protected division). Returning 1 at a zero divisor, rather than 0 or some large number, has a useful consequence: (% x x) equals 1 at every $x$, including 0. GP can therefore build the constant 1 from the variable alone, just as (- x x) builds 0. Poli, Langdon and McPhee (2008, §3.2.1, footnote 1) give this as the reason for returning 1. In the fitness-case table below, the tree (+ (% x x) (* x (+ x 1))) matches the target exactly at $x = 0$ only because the protected division returns 1 there.
Protection has a cost. It hides the singularity only at exactly 0; next to it the function behaves as ordinary division. The script evaluates (% 1 (- x 0.3)) near $x = 0.3$:
| $x$ | 0.29 | 0.2999 | 0.3 | 0.3001 | 0.31 |
|---|---|---|---|---|---|
| value | $-100$ | $-10{,}000$ | 1 | 10,000 | 100 |
An evolved formula can therefore contain a pole that happens to fall between the fitness cases. It fits the data and returns enormous values on new inputs. This is one reason symbolic regression checks evolved models on data not used for fitness.
The alternative to protection is to let errors occur and give any program that causes one a very poor fitness. When invalid programs are common, though, this leaves many individuals with nearly the same poor fitness and gives selection little to work with (Poli, Langdon and McPhee, 2008, §3.2.1).
Types and strongly typed GP
A single type is not always natural. A conditional needs a Boolean test, and a numeric program may still want comparisons. Consider the absolute value written with a three-argument IF:
P1 = (if (> x 0) x (- 0 x))
Here > returns a Boolean, if needs a Boolean first argument and numbers for the other two, and everything else is numeric. What happens when crossover ignores the types? The script performs every possible subtree crossover between P1 and P2 = (+ x 1), in both directions: 48 pairs of crossover points, since P1 has 8 nodes and P2 has 3, and $8 \times 3 = 24$ pairs in each direction. Six of the children are ill-typed, and the script lists them:
(if (+ x 1) x (- 0 x)),(if x x (- 0 x))and(if 1 x (- 0 x)), each with a number where the test should be. These are the three subtrees of P2 placed at the>of P1;(+ (> x 0) 1)and(+ x (> x 0)), with a Boolean added to a number;- the bare
(> x 0), a Boolean where the program should return a number. These last three are the subtree(> x 0)placed at each of the three points of P2.
No other pair can go wrong. (> x 0) is the only subtree that returns a Boolean, and the test of the if is the only place that needs one.
There are three standard remedies (Poli, Langdon and McPhee, 2008, §3.2.1 and §6.2.2).
- Convert automatically between types, for example by treating negative numbers as false. This risks introducing biases.
- Restructure the function set so that one type suffices. The four-argument IFLTE, "if $a < b$ then $c$ else $d$", does the work of IF and $<$ with numbers only, and
(iflte x 0 (- 0 x) x)computes $|x|$. - Strongly typed genetic programming (STGP), introduced by Montana (1995). Every terminal has a type, and every function has types for its arguments and its return value. Tree creation and every operator respect them: crossover may only swap subtrees whose roots return the type expected at the insertion point.
Of the 48 pairs above, STGP allows 42, and all 42 yield well-typed children. Montana noted that this basic form is close to the "constrained syntactic structures" of Koza (1992, ch. 19), which specify directly which children each function may have. His main additions were generic functions and generic data types. A generic function is, for example, a single MATRIX-ADD that adds any two matrices of the same shape. His motivation was that, without type constraints, programs that manipulate several data types can need unnecessarily long searches and generalize unnecessarily poorly. Grammar-based GP extends the same idea from types to grammars (Poli, Langdon and McPhee, 2008, §6.2.3).
Sufficiency
Closure guarantees that every tree runs. Sufficiency asks whether some tree solves the problem: the primitive set is sufficient if a solution can be expressed by composing its primitives (Poli, Langdon and McPhee, 2008, §3.2.2).
Example (sufficient for the quadratic, not for the exponential). The set $\{+, -, \times, \div\}$ with $x$ and a few integer constants is sufficient for $x^2 + x + 1$, as the tree at the top of this article shows. It is not sufficient for $e^x$. Every tree over that set computes a rational function of $x$, a ratio of two polynomials, and $e^x$ is not one. GP with that set can only approximate $e^x$, although the approximation may be good enough.
Both halves of that argument can be written out. Every tree computes a rational function, by induction on the tree. A terminal is $x$ or a constant, which is a polynomial. A function node combines the rational functions of its two arguments by $+$, $-$, $\times$ or $\div$, and each of these gives a rational function again. With protected division the claim needs one adjustment: every tree agrees with some rational function except at finitely many points. The induction carries this weaker statement. A terminal agrees everywhere. At a node, the two arguments agree with rational functions $R_1$ and $R_2$, each outside a finite set. For $+$, $-$ and $\times$, the node then agrees with $R_1 + R_2$, $R_1 - R_2$ or $R_1 R_2$ outside the union of the two sets. For protected division there are two cases. If $R_2$ is not the zero function, it has finitely many roots, and outside those and the two sets the node returns $R_1/R_2$. If $R_2$ is the zero function, the divisor is 0 outside a finite set, and there the node returns 1, so the node agrees with the constant 1. Two such divisors are (- x x), which is 0 everywhere, and (- (% (* x x) x) x), which is 1 at $x = 0$ and 0 elsewhere. The exponential is not rational. Suppose $e^x = P(x)/Q(x)$ for polynomials $P$ and $Q$ at all but finitely many $x$. For large $x$ the ratio $P(x)/Q(x)$ behaves like $c\,x^k$, with $c$ a constant and $k$ the degree of $P$ minus the degree of $Q$. So for any whole number $n > k$, $P(x)/(Q(x)\,x^n)$ tends to 0 as $x \to \infty$. But $e^x / x^n$ tends to infinity for every $n$, a contradiction.
Sometimes theory settles the question in the other direction: {AND, OR, NOT} with the input variables can express every Boolean function, so it is sufficient for any Boolean problem. (Both examples, this one and the exponential, are those of Poli, Langdon and McPhee, 2008, §3.2.2; the proofs are written out here.) The construction is the disjunctive normal form. For each row of the truth table where the function is true, write the AND of every variable or its negation, as the row dictates. The OR of those terms is true on exactly those rows, because each AND term is true on its own row and on no other: any other row differs from it in some variable, and the term contains that variable or its negation with the opposite value. A function that is never true is $x_1$ AND (NOT $x_1$).
For most real problems sufficiency cannot be guaranteed, and the user's choice of primitives encodes a guess about the form of the answer. Adding primitives makes sufficiency more likely but enlarges the search space. Poli, Langdon and McPhee (2008, §3.2.2) report that a few unnecessary primitives do not tend to slow GP down much, although they can bias it in unexpected ways.
Fitness cases and the preparatory steps
With a language that is closed and, we hope, sufficient, the next question is how to tell a good program from a poor one. A program's fitness usually depends on its behavior over many inputs or situations, called fitness cases. Take the target $x^2 + x + 1$, five fitness cases at $x = -1, -0.5, 0, 0.5, 1$, and fitness defined as the sum of absolute errors.
Example (the crudest program scores best). Of the three candidate programs (+ x 1), (* x (+ x 1)) and (+ (* x x) 1), the crudest one, (+ x 1), has the smallest error: 2.50 against 5.00 and 3.00. The second and third each contain a piece of the answer, $x(x+1)$ and $x^2 + 1$, and still score worse. The table gives every output:
| Program | $-1$ | $-0.5$ | 0 | 0.5 | 1 | Error |
|---|---|---|---|---|---|---|
| target $x^2 + x + 1$ | 1.00 | 0.75 | 1.00 | 1.75 | 3.00 | |
(+ x 1) |
0.00 | 0.50 | 1.00 | 1.50 | 2.00 | 2.50 |
(* x (+ x 1)) |
0.00 | −0.25 | 0.00 | 0.75 | 2.00 | 5.00 |
(+ (* x x) 1) |
2.00 | 1.25 | 1.00 | 1.25 | 2.00 | 3.00 |
(+ (% x x) (* x (+ x 1))) |
1.00 | 0.75 | 1.00 | 1.75 | 3.00 | 0.00 |
The figure shows why the pieces score badly.
(* x (+ x 1))has exactly the right shape and is wrong by the same amount, 1, at every case, so its five errors add up to 5.(+ (* x x) 1)is exact at $x = 0$ but misses the linear term, which costs $\lvert x \rvert$ at each case, a total of 3.(+ x 1)misses the quadratic term instead. That costs $x^2$, which is small near the middle of the interval: $1 + 0.25 + 0 + 0.25 + 1 = 2.5$.
Fitness rewards behavior, not the presence of useful parts. That is why the way GP recombines parts matters. A crossover that replaces the x of a parent (+ 1 x) with the (* x (+ x 1)) of another produces the target, but nothing in either parent's fitness says which parts to keep.
Setting up a GP run is conventionally described as a short list of preparatory steps, which Poli, Langdon and McPhee (2008, ch. 3) give as five decisions:
- the terminal set;
- the function set;
- the fitness measure, including the fitness cases;
- the parameters that control the run, such as population size, operator probabilities and limits on tree size;
- the termination criterion and the method of designating the result, typically the best program found.
The first two define the space of programs, and the third says which programs in it are good. The last two control the search. The fourth step includes a limit on tree size, and the next section shows why every practical system needs one.
How big trees can get
How large can a tree of a given depth be? Its size is bounded by its depth and by the arities of its functions. If the largest arity is $a \geq 2$, a tree of depth $d$ has at most $a^i$ nodes at depth $i$, so its size is at most
$$\sum_{i=0}^{d} a^i = \frac{a^{d+1} - 1}{a - 1}.$$
The familiar bound $2^{d+1} - 1$ is the case $a = 2$, and it holds only when no function takes more than two arguments. A function set containing a three-argument IF raises the bound to $(3^{d+1} - 1)/2$.
In the other direction, a tree of depth exactly $d$ contains a path of $d$ function nodes from the root to its deepest leaf. The smallest such tree fills every other argument of those nodes with a terminal. If the smallest arity is $m$, its size is at least $1 + md$: each of the $d$ function nodes on the path has at least $m$ children, no node is a child of two parents, and the root is nobody's child. The bound is reached when every function on the path has arity $m$:
| Depth $d$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Maximum size, largest arity 2 | 1 | 3 | 7 | 15 | 31 | 63 | 127 |
| Maximum size, largest arity 3 | 1 | 4 | 13 | 40 | 121 | 364 | 1093 |
| Minimum size, smallest arity 2 | 1 | 3 | 5 | 7 | 9 | 11 | 13 |
| Minimum size, smallest arity 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
The running example sits at the top of its range: with size 7 at depth 2 and only binary functions, $x^2 + x + 1$ is a full tree, one whose leaves all lie at the same depth.
With mixed arities even a full tree has no fixed size. The script draws 10,000 full trees of depth 4 over {+, −, ×, %, sin, cos}, four binary functions and two unary ones. Their sizes range from 5 to 31, the two bounds, with median 18 and mean 17.83. Each function node has $b = (4 \cdot 2 + 2 \cdot 1)/6 = 5/3$ children on average. In a full tree every node above depth 4 is a function, drawn uniformly and independently of the others. Given $n_i$ nodes at a depth $i < 4$, the expected number of their children is therefore $b\,n_i$. So $E[n_{i+1}] = b\,E[n_i]$, and by induction from $n_0 = 1$ the expected number of nodes at depth $i$ is $b^i$. The expected size is $\sum_{i=0}^{4} b^i = 1 + 1.667 + 2.778 + 4.630 + 7.716 = 17.79$, in line with the sample.
Counting distinct trees
The number of distinct trees grows far faster than their size. Here is the idea in one sentence: a tree of depth at most $d$ is either a single terminal, or a function whose every argument is a tree of depth at most $d - 1$. The two cases do not overlap, and in the second the arguments can be chosen independently, so their counts multiply. With $|T|$ terminals and the function set $F$, the number $N_d$ of trees of depth at most $d$ therefore satisfies
$$N_0 = |T|, \qquad N_d = |T| + \sum_{f \in F} N_{d-1}^{\,\mathrm{arity}(f)}.$$
For {+, −, ×, %} and {$x$, 1}, $|T| = 2$ and there are four binary functions, so $N_d = 2 + 4 N_{d-1}^2$. Step by step:
- $N_1 = 2 + 4 \cdot 2^2 = 18$: the two terminals and the $4 \times 2 \times 2 = 16$ trees such as
(+ x 1); - $N_2 = 2 + 4 \cdot 18^2 = 1{,}298$;
- $N_3 = 2 + 4 \cdot 1{,}298^2 = 6{,}739{,}218$.
Because the previous count is squared at every step, $\log N_d$ roughly doubles with each extra level of depth:
| Depth at most | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Distinct trees | 2 | 18 | 1,298 | 6,739,218 | about $1.8 \times 10^{14}$ |
| Distinct functions they compute | 2 | 10 | 131 | 14,494 |
Many of these trees compute the same function. The last row, from Part 9 of the script, counts the different functions. Two trees are counted once if their values at eight fixed values of $x$, computed exactly in rational arithmetic, are equal. Of the 18 trees of depth at most 1:
(+ x 1)and(+ 1 x)compute the same function;(- x x)and(- 1 1)both compute 0;(* x 1),(% x 1)andxall compute $x$.
After all such merges, 10 functions remain. At depth 3, about 465 trees on average compute each distinct function. (The count at depth 4 is beyond this simple method: it would combine every pair of the 14,494 depth-3 functions under four operators.) This redundancy is part of what makes GP's search space easier than its raw size suggests. A search that reaches a function can reach it through many trees.
Even so, the count shows why GP cannot enumerate even shallow programs, and why limits on depth or size are part of every practical system. Without them, trees tend to grow during a run without improving fitness, the problem of bloat.
How GP differs from a genetic algorithm
GP uses the same loop as every evolutionary algorithm: a population, selection by fitness, variation, repetition. Is it then just a genetic algorithm with a different encoding? It is not: the representation changes almost everything else.
- Variable size and shape. A genetic algorithm's genome is a string of fixed length, and position $i$ always encodes the same variable. A program tree has no fixed length or shape, and its size typically changes during a run. The search is over structures as well as values.
- Operators on subtrees. Crossover in a GA exchanges the values at corresponding positions, as the one-point, two-point and uniform crossovers of crossover and mutation operators do. GP's subtree crossover removes a subtree from one parent and inserts a subtree from the other, from any position, at the chosen point. The operators are described in initialization and variation in genetic programming.
- No fixed positions. Because a subtree can move to a different place in the tree, the meaning of a piece of code depends on its context. The example below shows what this does to crossover.
- Genotype and phenotype coincide. The tree is both what is varied and what is run. There is no decoding step, unlike the binary encodings of a GA.
- Theory. The schema theorem counts patterns at fixed string positions, which tree representations do not have. GP schema theories exist, but they define schemata over tree shapes and positions and are considerably more complicated (Poli, Langdon and McPhee, 2008, §11.1).
Example (crossing a tree with itself). Subtree crossover of (+ (* x x) (+ x 1)) with itself mostly returns something new. A GA crossover of a string with itself, such as 11010011 with 11010011, returns the parent at every cut point. Over all 49 pairs of crossover points in the tree, the script finds 29 different children, of sizes from 1 to 13, among them (+ (+ x 1) (+ x 1)) and the single node x. Only 13 of the 49 pairs return the parent. Crossover in GP can therefore keep creating new programs even in a population of identical trees. Whether it does more than a large random mutation has been debated, and the next article summarizes the evidence.
The same variable-structure problem returns in NEAT, which evolves neural networks whose topology grows. It returns too in compositional pattern-producing networks, which generate patterns by composing simple functions, much as a GP tree does.
What we have learned
A GP individual is a program tree: functions at the internal nodes, terminals at the leaves, stored as a flat preorder sequence in which every subtree is a contiguous block. The primitive set is the language of the search. It needs closure, so that every tree that crossover can make still runs, and protected division supplies it at the price of hidden poles. It also needs sufficiency, so that some tree solves the problem, which is a guess on most real problems. Fitness cases score behavior rather than parts, so (+ x 1) beats two trees that each hold a piece of $x^2 + x + 1$. The number of trees roughly squares with each level of depth, which is why every run limits size.
A run starts from random trees and changes them with crossover and mutation. The next article, initialization and variation in genetic programming, measures which trees the standard methods put in the first generation, and what one application of each operator does to a tree's size and shape.
References
- Cramer, N. L. (1985). A representation for the adaptive generation of simple sequential programs. In J. J. Grefenstette (ed.), Proceedings of the First International Conference on Genetic Algorithms and Their Applications, 183–187. Lawrence Erlbaum. record · 2 pages
- Eiben, A. E., and Smith, J. E. (2015). Introduction to Evolutionary Computing, 2nd ed., sections 4.6 and 6.4. Springer. record · 35 pages
- Koza, J. R. (1992). Genetic Programming: On the Programming of Computers by Means of Natural Selection. MIT Press. record · 6 pages
- Montana, D. J. (1995). Strongly typed genetic programming. Evolutionary Computation, 3(2), 199–230. record
- Poli, R., Langdon, W. B., and McPhee, N. F. (2008). A Field Guide to Genetic Programming. Published via lulu.com, freely available at gp-field-guide.org.uk (with contributions by J. R. Koza). record · 5 pages
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 7 revisions