Genetic programming

Also known as: tree-based genetic programming

On this page 7

Genetic programming (GP) is a branch of evolutionary computation that automatically evolves computer programs rather than fixed-length parameter vectors. A population of candidate programs is repeatedly evaluated against a fitness function, and the better programs are recombined and mutated to produce the next generation, so that useful behavior emerges without a human writing the solution.

The approach was popularized by John Koza in the early 1990s and is closely related to the genetic algorithm, but its individuals are variable-size executable structures. In its classic form, called tree-based genetic programming, each program is a syntax tree of functions and terminals, which makes GP a core technique of artificial intelligence for symbolic regression, automatic design and program synthesis.

How genetic programming evolves programs

Genetic programming follows the generational loop common to evolutionary algorithms, adapted to program structures. The designer defines a function set (such as +, -, ×, sin or if-then-else) and a terminal set (input variables and constants), and the algorithm combines them into trees of growing complexity.

  1. Initialization: random trees are generated, commonly with the ramped half-and-half method to obtain varied shapes and depths.
  2. Evaluation: each program is executed on training cases and receives a fitness score, for example the mean squared error.
  3. Selection: fitter programs are chosen as parents, usually by tournament selection with tournaments of 2 to 7 individuals.
  4. Variation: subtree crossover swaps branches between parents, and subtree mutation replaces a random branch with a new one.
  5. Termination: the loop stops when a target fitness is reached or a generation budget is exhausted.

Because trees can grow and shrink freely, the size and shape of the solution are discovered by evolution instead of being fixed in advance. This flexibility is the defining advantage of GP and also the source of its most serious problems.

Key parameters and typical values

Run settings strongly influence both solution quality and computational cost. The values below are common starting points in the literature, but suitable settings depend on the problem, the size of the function set and the available compute.

ParameterTypical rangeEffect
Population size500 to 10,000 programsLarger populations improve diversity but cost more evaluations
Generations50 to 500More generations allow refinement but increase bloat
Crossover rateAbout 0.8 to 0.9Main source of structural recombination
Mutation rateAbout 0.05 to 0.2Introduces new material and preserves diversity
Maximum tree depthOften 17Limits uncontrolled program growth
Tournament size2 to 7Larger tournaments raise selection pressure
Common genetic programming parameters

Practitioners also use elitism to copy the best programs unchanged into the next generation, and parsimony pressure to penalize unnecessary size. Both choices trade exploration against exploitation and should be tuned with repeated independent runs, since GP results vary considerably from seed to seed.

Representations and variants

Tree-based GP is the best known form, but several alternative representations exist. Each changes how programs are encoded and therefore how crossover and mutation behave.

VariantProgram encodingTypical strength
Tree-based GPSyntax trees of functions and terminalsReadable formulas, symbolic regression
Linear GPSequences of register machine instructionsFast execution, reuse of intermediate results
Cartesian genetic programmingGraph on a fixed grid of nodesLittle bloat, circuit design
Grammatical evolutionInteger strings mapped through a grammarOutput in any language defined by a grammar
Stack-based GPPrograms for a stack machine, such as PushRich data types and control flow
Main genetic programming variants

Genetic programming differs from neighboring methods mainly in what is being evolved. A genetic algorithm tunes a fixed-length string, an evolution strategy tunes real-valued vectors, while GP searches the space of programs, which is open-ended and highly irregular.

ApproachSearch objectOutput interpretability
Genetic programmingVariable-size programs or expressionsHigh when solutions stay small
Genetic algorithmFixed-length bit or value stringsDepends on the encoding
NeuroevolutionNeural network weights or topologiesLow
Gradient-based deep learningWeights of a fixed architectureLow
Genetic programming versus related approaches

The comparison explains why GP remains attractive where interpretable models matter. A short evolved formula can be inspected, simplified and verified, whereas a neural network of similar accuracy is usually a black box.

Limitations and common pitfalls

The best known weakness of GP is bloat, the growth of program size without matching gains in fitness. Bloated programs contain introns, code that has no effect on output, which slows evaluation and hides the useful logic.

ProblemCauseRemedy
BloatCrossover favors larger neutral subtreesDepth limits, parsimony pressure, lexicographic selection
Premature convergenceLoss of diversity in the populationLarger populations, island models, niching
OverfittingPrograms memorize training casesValidation sets, simpler function sets, size penalties
High compute costEvery program runs on every caseSampling of cases, caching, parallel evaluation
Typical problems and remedies

Another pitfall is a poorly designed fitness function. GP exploits any loophole in the evaluation, so a fitness measure that rewards the wrong behavior will reliably produce programs that game the metric rather than solve the real task.

Stripped to the essentials

Genetic programming lets a computer breed programs. It starts with many random little programs. Each one is tested on a task. The better ones get to have offspring.

Offspring are made by swapping parts between two parent programs or by changing a small piece at random. After many rounds, the programs get better at the task. The main point is simple: the solution is grown, not written by hand. The risk is that programs become long and messy, so people keep them in check.

Where it is applied

An engineering team at a materials laboratory wants a formula that predicts the strength of a new alloy from five measured properties. They run tree-based genetic programming as symbolic regression with a population of 2,000 programs, 200 generations, a function set of basic arithmetic plus logarithm, and a penalty on tree size.

After about 30 independent runs, the team keeps the solutions on the Pareto front of accuracy and size. The chosen expression uses only three variables and fits the validation data with an error close to that of a gradient-boosted model, yet it is short enough to read and check against physical intuition.

The engineers then use the formula to screen candidate compositions before costly laboratory tests. The result is a transparent model that domain experts trust, which a black-box model would not have provided.