🧬 Genetic Algorithm Framework

Evolutionary computation with functors, selection, crossover, and mutation

Overview

A full-featured genetic algorithm framework using OCaml's module system. Defines a PROBLEM signature and a Make functor that builds a complete GA solver for any conforming problem. Includes tournament/roulette/rank selection, multiple crossover and mutation operators, island model parallelism, fitness sharing, and adaptive parameter control.

Concepts Demonstrated

Key Components

ComponentDescription
PROBLEM signatureDefine gene type, fitness, init, crossover, mutate
Make(P) functorBuild a GA solver from any PROBLEM implementation
evolveRun the GA for N generations with given config
island_evolveMulti-island GA with periodic migration
OneMax / TSP / KnapsackThree complete example problem implementations

Usage Pattern

(* 1. Define a problem module *)
module MyProblem : PROBLEM = struct
  type gene = ...
  let fitness individual = ...
  let random_individual () = ...
  let crossover a b = ...
  let mutate rate individual = ...
end

(* 2. Build a solver via the functor *)
module Solver = Make(MyProblem)

(* 3. Configure and run *)
let config = {
  pop_size = 100;
  generations = 500;
  mutation_rate = 0.01;
  crossover_rate = 0.8;
  selection = Tournament 3;
  elitism = 2;
}
let best = Solver.evolve config

Example Problems

ProblemDescriptionGene Type
OneMaxMaximize 1-bits in a bitstringbool array
TSPTravelling salesman — minimize route distanceint array (permutation)
KnapsackMaximize value under weight constraintbool array (inclusion mask)