Documentation
¶
Overview ¶
Experimental — this package is not yet wired into the main framework.
Package gp provides tree-based genetic programming. (Stability: alpha)
Genetic programming evolves expression trees composed of primitive functions (Add, Mul, Sin, etc.) and terminals (variables, constants) to approximate a target behaviour specified by a fitness function.
The Evolve function runs a generational evolutionary loop with tournament selection, subtree crossover, and subtree mutation to search the space of programs for the best-fit individual.
Index ¶
- type Arity
- type FitnessFunc
- type GPConfig
- type Node
- type Primitive
- func AddPrimitive() Primitive
- func ConstantPrimitive(name string, value float64) Primitive
- func CosPrimitive() Primitive
- func MulPrimitive() Primitive
- func ProtectedDivPrimitive() Primitive
- func SinPrimitive() Primitive
- func SubPrimitive() Primitive
- func VariablePrimitive(name string, index int) Primitive
- type Program
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arity ¶
type Arity int
Arity indicates how many child arguments a primitive requires. Terminals have arity 0; functions have arity >= 1.
type FitnessFunc ¶
FitnessFunc evaluates how well a program solves the target task. Lower values indicate better fitness (minimisation).
type GPConfig ¶
type GPConfig struct {
// PopulationSize is the number of individuals per generation. Default: 100.
PopulationSize int
// MaxGenerations is the maximum number of evolutionary generations. Default: 50.
MaxGenerations int
// MaxDepth is the maximum allowed tree depth. Default: 5.
MaxDepth int
// CrossoverRate is the probability of applying crossover. Default: 0.9.
CrossoverRate float64
// MutationRate is the probability of applying mutation. Default: 0.1.
MutationRate float64
// TournamentSize is the number of individuals in tournament selection. Default: 3.
TournamentSize int
// Seed sets the random number generator seed. 0 means non-deterministic.
Seed int64
}
GPConfig controls the evolutionary process.
type Node ¶
Node is a single node in an expression tree.
type Primitive ¶
type Primitive struct {
// Name is a human-readable label (e.g. "Add", "x0", "const").
Name string
// Arity is the number of child arguments. Terminals have arity 0.
Arity Arity
// Func evaluates this primitive given its child values.
// For terminals, args is empty.
Func func(args []float64) float64
}
Primitive represents a function or terminal node in an expression tree.
func AddPrimitive ¶
func AddPrimitive() Primitive
AddPrimitive returns a Primitive that computes a + b.
func ConstantPrimitive ¶
ConstantPrimitive returns a terminal that always produces the given value.
func CosPrimitive ¶
func CosPrimitive() Primitive
CosPrimitive returns a Primitive that computes cos(a).
func MulPrimitive ¶
func MulPrimitive() Primitive
MulPrimitive returns a Primitive that computes a * b.
func ProtectedDivPrimitive ¶
func ProtectedDivPrimitive() Primitive
ProtectedDivPrimitive returns a Primitive that computes a / b, returning 1 when b is near zero.
func SinPrimitive ¶
func SinPrimitive() Primitive
SinPrimitive returns a Primitive that computes sin(a).
func SubPrimitive ¶
func SubPrimitive() Primitive
SubPrimitive returns a Primitive that computes a - b.
func VariablePrimitive ¶
VariablePrimitive returns a terminal that reads inputs[index].
type Program ¶
Program represents an evolved expression tree.
func Evolve ¶
func Evolve(primitives []Primitive, fitness FitnessFunc, config GPConfig) (*Program, error)
Evolve runs a generational genetic programming loop to find a program that minimises the given fitness function. It returns the best program found across all generations.