Skip to content

Repository files navigation

Genetic Image Evolution in Rust

A multithreaded genetic algorithm written in Rust that reconstructs target images using overlapping semi-transparent polygons and ellipses. Over successive generations, individuals mutate and cross over to approximate the target image pixel-by-pixel.


Examples

Target (images/) Reconstructed Output (Root)
Original: Charles Darwin

Target Darwin
Evolved (~96.7% fitness)

Evolved Darwin
Original: 9c

Target 9c
Evolved (~95.0% fitness)

Evolved 9c

Features

  • Parallel Evaluation: Scales across CPU threads with rayon for concurrent rasterization and fitness scoring.
  • Vector Rasterization: Uses tiny-skia for rendering semi-transparent paths and shapes directly into memory buffers.
  • Dynamic Hyperparameters: Automatically scales mutation steps, shape bounds, and survival thresholds as overall population fitness increases.
  • Shape Variety: Combines variable-vertex polygons with bounding ellipses to capture both broad gradients and sharp edges.

Getting Started

Prerequisites

Ensure you have a recent Rust toolchain installed:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Running

Execute with native CPU vectorization enabled in release mode:

RUSTFLAGS="-C target-cpu=native" cargo run --release -- <image_file> <generations> <population_size>

Examples:

# Evolve darwin.png with 9000 generations and 300 individuals
RUSTFLAGS="-C target-cpu=native" cargo run --release -- darwin.png 9000 300

# Evolve 9c.png with custom generation limits
RUSTFLAGS="-C target-cpu=native" cargo run --release -- 9c.png 10000 500

Arguments

Parameter Default Description
image_file 9c.png Name of the file inside images/
generations 1000 Total generations to run
population_size 350 Number of candidate individuals per generation

Outputs

Final renders are written to the out/ directory with run parameters and the final fitness score in the filename:

out/{file_name}_{generations}_{pop_size}_{chromosome_count}_{fitness}.png

About

A multithreaded genetic algorithm in Rust that evolves overlapping semi-transparent polygons and ellipses to reconstruct target images pixel-by-pixel.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages