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.
- Initialization: random trees are generated, commonly with the ramped half-and-half method to obtain varied shapes and depths.
- Evaluation: each program is executed on training cases and receives a fitness score, for example the mean squared error.
- Selection: fitter programs are chosen as parents, usually by tournament selection with tournaments of 2 to 7 individuals.
- Variation: subtree crossover swaps branches between parents, and subtree mutation replaces a random branch with a new one.
- 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.
| Parameter | Typical range | Effect |
|---|---|---|
| Population size | 500 to 10,000 programs | Larger populations improve diversity but cost more evaluations |
| Generations | 50 to 500 | More generations allow refinement but increase bloat |
| Crossover rate | About 0.8 to 0.9 | Main source of structural recombination |
| Mutation rate | About 0.05 to 0.2 | Introduces new material and preserves diversity |
| Maximum tree depth | Often 17 | Limits uncontrolled program growth |
| Tournament size | 2 to 7 | Larger tournaments raise selection pressure |
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.
| Variant | Program encoding | Typical strength |
|---|---|---|
| Tree-based GP | Syntax trees of functions and terminals | Readable formulas, symbolic regression |
| Linear GP | Sequences of register machine instructions | Fast execution, reuse of intermediate results |
| Cartesian genetic programming | Graph on a fixed grid of nodes | Little bloat, circuit design |
| Grammatical evolution | Integer strings mapped through a grammar | Output in any language defined by a grammar |
| Stack-based GP | Programs for a stack machine, such as Push | Rich data types and control flow |
Genetic programming compared with related methods
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.
| Approach | Search object | Output interpretability |
|---|---|---|
| Genetic programming | Variable-size programs or expressions | High when solutions stay small |
| Genetic algorithm | Fixed-length bit or value strings | Depends on the encoding |
| Neuroevolution | Neural network weights or topologies | Low |
| Gradient-based deep learning | Weights of a fixed architecture | Low |
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.
| Problem | Cause | Remedy |
|---|---|---|
| Bloat | Crossover favors larger neutral subtrees | Depth limits, parsimony pressure, lexicographic selection |
| Premature convergence | Loss of diversity in the population | Larger populations, island models, niching |
| Overfitting | Programs memorize training cases | Validation sets, simpler function sets, size penalties |
| High compute cost | Every program runs on every case | Sampling of cases, caching, parallel evaluation |
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.