v2026.6.1
All Bundles
Bundle Classic AI algorithms: graph search, adversarial game search, optimization and tabular reinforcement learning (-lib ai)

GeneticAlgorithm

Generational genetic algorithm over boolean chromosomes: tournament selection, single-point crossover, per-bit mutation and single-member elitism. Seeded for reproducible runs.

Example

ga := GeneticAlgorithm->New(40, 24, 0.02, 0.9, 7);
best := ga->Run(fitness, 60);
"best fitness: {$ga->GetBestFitness()}"->PrintLine();

Operations

GetBestFitness #

Gets the best fitness seen during the last run.

method : public : GetBestFitness() ~ Float

Return

TypeDescription
Floatbest fitness

New # constructor

Constructor

New(population_size:Int, gene_count:Int, mutation_rate:Float, crossover_rate:Float, seed:Int)

Parameters

NameTypeDescription
population_sizeIntchromosomes per generation (>= 2)
gene_countIntbits per chromosome
mutation_rateFloatper-bit flip probability
crossover_rateFloatprobability a pair is crossed rather than copied
seedIntPRNG seed for reproducible runs

Run #

Evolves a population and returns the best chromosome found.

method : public : Run(fitness:FitnessFunction, generations:Int) ~ Bool[]

Parameters

NameTypeDescription
fitnessFitnessFunctionfitness function (higher is better)
generationsIntnumber of generations

Return

TypeDescription
Boolbest chromosome, or Nil on invalid settings