AI · zero-dependency · MIT

larzgenetic

Evolve a population toward whatever you reward.

Tournament selection, single-point crossover, per-gene mutation and elitism - a real genetic algorithm. Genes are opaque to the package, so it optimizes over any search space a gene_fn() can describe, not just the classic string-matching demo (though that's the easiest way to watch it converge).

larzscript pkg install genetic
View on GitHub →Learn the stack

What you get

Try it

import "genetic" as ga
import "random" as rng
rng.seed(1)
let target = "LARZ"
fn random_letter() { return chr(65 + rng.randint(0, 25)) }
fn fitness(genes) {
  let score = 0
  let i = 0
  while i < len(genes) { if genes[i] == target[i:i+1] { score = score + 1 } i = i + 1 }
  return score
}
let pop = ga.population_random(60, len(target), random_letter)
let final = ga.evolve(pop, fitness, random_letter, {"generations": 60})
let best = ga.best_of(final)
print(join(best["genes"], ""), best["fitness"])
Output
LARZ 4

Why it's in the Larz Stack

larzgenetic is a real Larzscript package (larzscript pkg install genetic) - money-native, zero dependencies, the same language as the rest of the estate, not a separate Python runtime. It composes with the rest of the Larz Stack and the Larz framework.