engine

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config[T any] struct {
	PopulationSize   int
	Generations      int
	MutationRate     float64
	CrossoverRate    float64
	ElitismCount     int
	ConcurrencyLevel int       // Number of goroutines for fitness evaluation (default: runtime.NumCPU())
	Seed             *[32]byte // Optional seed for reproducible deterministic runs (ChaCha8)

	InitFunc      InitFunc[T]
	FitnessFunc   FitnessFunc[T]
	SelectionFunc SelectionFunc[T]
	CrossoverFunc CrossoverFunc[T]
	MutationFunc  MutationFunc[T]

	// Optional callback executed at the end of each generation
	OnGeneration func(generation int, best T, bestFitness float64, avgFitness float64)
}

Config holds the configuration for the Engine.

type CrossoverFunc

type CrossoverFunc[T any] func(rng *rand.Rand, p1, p2 T) (T, T)

CrossoverFunc performs crossover between two parents to produce two offspring.

type Engine

type Engine[T any] struct {
	// contains filtered or unexported fields
}

Engine is the core struct that drives the evolutionary process.

func New

func New[T any](cfg Config[T]) (*Engine[T], error)

New returns a new Engine configured with the given Config.

func (*Engine[T]) EvaluatePopulation

func (e *Engine[T]) EvaluatePopulation(pop []T) []float64

EvaluatePopulation evaluates all individuals in the population concurrently.

func (*Engine[T]) Evolve

func (e *Engine[T]) Evolve() (best T, bestFitness float64)

Evolve runs the genetic algorithm for the specified number of generations.

type FitnessFunc

type FitnessFunc[T any] func(individual T) float64

FitnessFunc evaluates the fitness of a single individual. Higher values should represent better fitness (maximization).

type InitFunc

type InitFunc[T any] func(rng *rand.Rand) T

InitFunc generates a single random individual for the initial population.

type MutationFunc

type MutationFunc[T any] func(rng *rand.Rand, individual T, rate float64) T

MutationFunc mutates an individual based on the given mutation rate.

type SelectionFunc

type SelectionFunc[T any] func(rng *rand.Rand, population []T, fitnesses []float64, num int) []T

SelectionFunc selects 'num' individuals from the population based on their fitnesses.

Jump to

Keyboard shortcuts

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