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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | best fitness |
New # constructor
Constructor
New(population_size:Int, gene_count:Int, mutation_rate:Float, crossover_rate:Float, seed:Int)Parameters
| Name | Type | Description |
|---|---|---|
| population_size | Int | chromosomes per generation (>= 2) |
| gene_count | Int | bits per chromosome |
| mutation_rate | Float | per-bit flip probability |
| crossover_rate | Float | probability a pair is crossed rather than copied |
| seed | Int | PRNG seed for reproducible runs |
Run #
Evolves a population and returns the best chromosome found.
method : public : Run(fitness:FitnessFunction, generations:Int) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| fitness | FitnessFunction | fitness function (higher is better) |
| generations | Int | number of generations |
Return
| Type | Description |
|---|---|
| Bool | best chromosome, or Nil on invalid settings |