gp

package
v1.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

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

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

type FitnessFunc func(p *Program) float64

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

type Node struct {
	Prim     Primitive
	Children []*Node
}

Node is a single node in an expression tree.

func (*Node) Evaluate

func (n *Node) Evaluate(inputs []float64) float64

Evaluate recursively computes the value of the expression tree rooted at n, binding variable terminals to the provided inputs.

func (*Node) String

func (n *Node) String() string

String returns a human-readable S-expression for the 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

func ConstantPrimitive(name string, value float64) Primitive

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

func VariablePrimitive(name string, index int) Primitive

VariablePrimitive returns a terminal that reads inputs[index].

type Program

type Program struct {
	Root    *Node
	Fitness float64
}

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.

func (*Program) Evaluate

func (p *Program) Evaluate(inputs []float64) float64

Evaluate computes the program output for the given input vector.

func (*Program) String

func (p *Program) String() string

String returns the S-expression representation of the program.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL