From eb8faa7c995688e9747abdb141f69311df88ce8f Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Mon, 24 Aug 2026 08:14:32 +0200 Subject: [PATCH] prototype: GA evolution spike -- sin(x) prediction validates pipeline (#66) Co-Authored-By: Claude Opus 4.6 --- prototypes/ga_gun_spike/ga_spike.nim | 111 +++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 prototypes/ga_gun_spike/ga_spike.nim diff --git a/prototypes/ga_gun_spike/ga_spike.nim b/prototypes/ga_gun_spike/ga_spike.nim new file mode 100644 index 0000000..bdb5e6d --- /dev/null +++ b/prototypes/ga_gun_spike/ga_spike.nim @@ -0,0 +1,111 @@ +## ga_spike.nim — throwaway GA neuroevolution spike: evolve ANN to predict sin(x) +## Validates: population init, fitness eval, truncation selection, gaussian +## mutation on all weights, single-elite preservation, champion tracking. +## Issue #66. + +import std/[math, random, algorithm, strformat] + +const + InputDim = 10 # sliding window of 10 past sin values + HiddenDim = 4 + OutputDim = 1 + NumWeights = (InputDim * HiddenDim + HiddenDim) + # W1 + b1 + (HiddenDim * OutputDim + OutputDim) # W2 + b2 = 49 + PopSize = 200 + Generations = 200 + TopFrac = 0.2 # keep top 20% + Sigma = 0.01 # gaussian mutation sigma on ALL weights + EvalPoints = 50 # fitness eval sample size + +type Individual = object + weights: seq[float64] + fitness: float64 + +proc forward(w: seq[float64]; input: array[InputDim, float64]): float64 = + ## 10->4->1 tanh ANN, flat weight layout: W1[40], b1[4], W2[4], b2[1] + var hidden: array[HiddenDim, float64] + for h in 0..= 0.1 — evolution did not converge" + echo "PASS: assert avgErr < 0.1"