---
title: "Evolving a string with a genetic algorithm"
url: https://daily.dev/posts/evolving-a-string-with-a-genetic-algorithm-t6z4sdnox
source_url: https://sylvesterroos.com/blog/evolving-a-string-using-a-genetic-algorithm
type: article
source: "ElixirStatus"
published: 2026-08-14T11:55:01.229Z
updated: 2026-08-14T11:55:22.388Z
tags: ["data-science", "algorithms", "elixir"]
reading_time: 10
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Evolving a string with a genetic algorithm

**[ElixirStatus](https://daily.dev/sources/elixirstatus)** · 10 min read · 0 upvotes · 0 comments

## Summary

A demonstration of using Petri, an Elixir genetic algorithm library, to evolve random bytes into the One Ring inscription from Lord of the Rings. The post walks through encoding a string as an integer chromosome, designing a fitness function combining character and bigram matches, choosing tournament selection, two-point crossover, and uniform mutation operators, and configuring the run with Elixir's BEAM concurrency. The full 107-character string converges from random noise to the exact target in about 2030 generations and 30 seconds.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://sylvesterroos.com/blog/evolving-a-string-using-a-genetic-algorithm>

## Questions this post answers

### How can I design a fitness function for a genetic algorithm that evolves a target string?

Combine per-character scoring with bigram scoring so the fitness function rewards adjacent correct characters, not just isolated matches. Pure character matching treats positions independently, causing slow, uncoordinated drift toward the target. Adding overlapping character-pair (bigram) matches, weighted at roughly twice the value of single characters, gives selection a reason to preserve correct fragments together, speeding convergence substantially.

_daily.dev surfaces practical breakdowns like this for developers tuning genetic algorithm fitness functions._

### What crossover and mutation settings work well for a genetic algorithm evolving a 107-character string?

Two-point crossover (cutting parents at two positions and swapping the middle segment) combined with uniform mutation at a 2% per-gene rate works well for a 107-character, 256-value-per-gene search space. This produces roughly two mutations per chromosome per generation, enough to explore without destroying fragments already assembled. Tournament selection with a size of 5 in a population of 200 balances selection pressure against diversity, and keeping one elite chromosome per generation anchors progress without letting elites dominate early.

_Developers tuning genetic algorithm parameters can find similar hands-on breakdowns on daily.dev._

---

Tags: [#data-science](https://daily.dev/tags/data-science), [#algorithms](https://daily.dev/tags/algorithms), [#elixir](https://daily.dev/tags/elixir)

[View this post on daily.dev](https://daily.dev/posts/evolving-a-string-with-a-genetic-algorithm-t6z4sdnox)
