Definition
Genetic Programming (GP) is an evolutionary computation technique that automatically evolves computer programs to solve specific problems. Inspired by biological evolution, GP starts with a population of random programs and uses operations like Selection, Crossover, and Mutation to iteratively improve their performance.
Why It Matters
Genetic programming offers a way to create software that ‘writes itself’; by evolving code rather than manually typing it, we can solve problems in circuit design, control systems, and data fitting that are far beyond the reach of human programmers.
Core Concepts
- Fitness Function: The “environment” that evaluates how well a program solves the problem. Programs that score higher are more likely to survive and reproduce.
- Population: A collection of candidate programs, often represented as Tree Structures (Abstract Syntax Trees) to allow for modular manipulation.
- Selection: The process of choosing the best programs from the current generation to become “parents” for the next.
- Crossover (Recombination): Swapping sub-trees (branches) between two parent programs to create offspring that inherit traits from both.
- Mutation: Randomly altering a small part of a program (e.g., changing a variable or a operator) to introduce new genetic variety and prevent stagnation.
# Conceptual representation of a GP individual (Tree: 3 * (x + 5))
gp_individual = {
'op': '*',
'left': 3,
'right': {
'op': '+',
'left': 'x',
'right': 5
}
}
def evaluate(node, x):
if isinstance(node, (int, float)): return node
if node == 'x': return x
if node['op'] == '*': return evaluate(node['left'], x) * evaluate(node['right'], x)
if node['op'] == '+': return evaluate(node['left'], x) + evaluate(node['right'], x)