code wiki / _hdl_build / nx_evo_breed.nx

nx_evo_breed.nx source

↩ module page · 170 lines · 8072 B

1// nx_evo_breed.nx -- BREEDER: one autonomous run derives a CHAIN of generations, each built 2// on the prior (kids making kids), toward self-sufficiency. GEN 1: derive gcd (2-register 3// Euclid machine). GEN 2: derive lcm using the FRESHLY-derived gcd (this run's gen-1 genome) 4// as a /gcd primitive -> lcm = a*b/gcd. ONE run, two generations, the second COMPOSED FROM 5// the first, Claude NOT in the per-generation loop (only the machine/substrate is authored). 6// Self-verifies each generation vs an independent oracle on a 40x40 grid; prints the lineage. 7// Shared GA: both genomes are 4 ops over 0-5, so one loop drives both phases (fit by phase). 8// license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 11const EVO_MAGIC_2862933555777941757: i64 = 2862933555777941757 12const EVO_MAGIC_3037000493: i64 = 3037000493 13const EVO_MAGIC_2000000: i64 = 2000000 14const EVO_MAGIC_1000000: i64 = 1000000 15const EVO_MAGIC_1000000000: i64 = 1000000000 16const EVO_MAGIC_2654435761: i64 = 2654435761 17const EVO_MAGIC_12345: i64 = 12345 18 19const GLEN: i64 = 4 20const EVO_P: i64 = 256 21const EVO_G: i64 = 2000 22const EVO_T: i64 = 5 23const NTRAIN: i64 = 6 24const GCD_CAP: i64 = 256 25 26func br_rand(state: *i64) -> i64 { state[0] = state[0] * EVO_MAGIC_2862933555777941757 + EVO_MAGIC_3037000493; return (state[0] >> 17) & 0x3fffffff } 27func br_abs(x: i64) -> i64 { if x < 0 { return 0 - x } return x } 28func br_euclid(a: i64, b: i64) -> i64 { var x: i64 = a; var y: i64 = b; while y != 0 { let t: i64 = x % y; x = y; y = t } return x } 29func br_lcm_true(a: i64, b: i64) -> i64 { let g: i64 = br_euclid(a, b); if g == 0 { return 0 } return (a * b) / g } 30func br_a(i: i64) -> i64 { if i == 0 { return 12 } if i == 1 { return 6 } if i == 2 { return 8 } if i == 3 { return 9 } if i == 4 { return 14 } return 15 } 31func br_b(i: i64) -> i64 { if i == 0 { return 8 } if i == 1 { return 4 } if i == 2 { return 12 } if i == 3 { return 6 } if i == 4 { return 21 } return 10 } 32 33// GEN 1 machine: 2-register gcd (ops {0 r0%=r1, 1 r1%=r0, 2 swap, 3 r0-=r1, 4 r1-=r0, 5 nop}). 34func gcd_eval(gen: *i64, base: i64, a: i64, b: i64) -> i64 { 35 var r0: i64 = a; var r1: i64 = b; var swp: i64 = 0; var guard: i64 = 0 36 while r1 != 0 { 37 if guard >= GCD_CAP { r1 = 0 } else { 38 var j: i64 = 0 39 while j < GLEN { 40 let op: i64 = gen[base + j] 41 if op == 0 { if r1 != 0 { r0 = r0 % r1 } } 42 if op == 1 { if r0 != 0 { r1 = r1 % r0 } } 43 if op == 2 { let t: i64 = r0; r0 = r1; r1 = t } 44 if op == 3 { r0 = r0 - r1 } 45 if op == 4 { r1 = r1 - r0 } 46 j = j + 1 47 } 48 guard = guard + 1 49 } 50 } 51 return r0 52} 53func gcd_fit(pop: *i64, base: i64) -> i64 { 54 var err: i64 = 0; var i: i64 = 0 55 while i < NTRAIN { 56 let r: i64 = gcd_eval(pop, base, br_a(i), br_b(i)) 57 var d: i64 = EVO_MAGIC_2000000 58 if r >= 0 { if r <= EVO_MAGIC_1000000 { d = br_abs(r - br_euclid(br_a(i), br_b(i))) } } 59 err = err + d; i = i + 1 60 } 61 return err 62} 63 64// GEN 2 machine: lcm acc-machine using the GEN-1 gcd genome (gcdg) as a /gcd primitive. 65func lcm_eval(pop: *i64, base: i64, a: i64, b: i64, gcdg: *i64) -> i64 { 66 var acc: i64 = 0; var j: i64 = 0 67 while j < GLEN { 68 let op: i64 = pop[base + j] 69 if op == 0 { acc = a } 70 if op == 1 { acc = b } 71 if op == 2 { acc = acc * a } 72 if op == 3 { acc = acc * b } 73 if op == 4 { let g: i64 = gcd_eval(gcdg, 0, a, b); if g != 0 { acc = acc / g } } 74 j = j + 1 75 } 76 return acc 77} 78func lcm_fit(pop: *i64, base: i64, gcdg: *i64) -> i64 { 79 var err: i64 = 0; var i: i64 = 0 80 while i < NTRAIN { 81 let r: i64 = lcm_eval(pop, base, br_a(i), br_b(i), gcdg) 82 var d: i64 = EVO_MAGIC_2000000 83 if r >= 0 { if r <= EVO_MAGIC_1000000 { d = br_abs(r - br_lcm_true(br_a(i), br_b(i))) } } 84 err = err + d; i = i + 1 85 } 86 return err 87} 88 89func br_fit(pop: *i64, base: i64, phase: i64, gcdg: *i64) -> i64 { 90 if phase == 0 { return gcd_fit(pop, base) } 91 return lcm_fit(pop, base, gcdg) 92} 93func br_tourney(fit: *i64, state: *i64) -> i64 { 94 var bi: i64 = br_rand(state) % EVO_P; var bd: i64 = fit[bi]; var k: i64 = 1 95 while k < EVO_T { let i: i64 = br_rand(state) % EVO_P; if fit[i] < bd { bd = fit[i]; bi = i } k = k + 1 } 96 return bi 97} 98 99// shared GA: derive a 4-op genome for `phase` (using gcdg if phase 1), store in `best`. 100func br_ga(phase: i64, gcdg: *i64, best: *i64, state: *i64) -> i64 { 101 let pop: *i64 = sys_mmap(EVO_P * GLEN * 8) as *i64 102 let nxt: *i64 = sys_mmap(EVO_P * GLEN * 8) as *i64 103 let fit: *i64 = sys_mmap(EVO_P * 8) as *i64 104 var p: i64 = 0 105 while p < EVO_P { var j: i64 = 0; while j < GLEN { pop[p*GLEN + j] = br_rand(state) % 6; j = j + 1 } p = p + 1 } 106 var best_err: i64 = EVO_MAGIC_1000000000 107 var g: i64 = 0 108 while g < EVO_G { 109 var gbest: i64 = EVO_MAGIC_1000000000; var gbp: i64 = 0 110 p = 0 111 while p < EVO_P { let e: i64 = br_fit(pop, p*GLEN, phase, gcdg); fit[p] = e; if e < gbest { gbest = e; gbp = p } p = p + 1 } 112 if gbest < best_err { best_err = gbest; var j: i64 = 0; while j < GLEN { best[j] = pop[gbp*GLEN + j]; j = j + 1 } } 113 if best_err == 0 { g = EVO_G } else { 114 var j2: i64 = 0; while j2 < GLEN { nxt[j2] = best[j2]; j2 = j2 + 1 } 115 p = 1 116 while p < EVO_P { 117 let pa: i64 = br_tourney(fit, state) * GLEN 118 let pb: i64 = br_tourney(fit, state) * GLEN 119 let cut: i64 = (br_rand(state) % (GLEN - 1)) + 1 120 var jj: i64 = 0 121 while jj < GLEN { if jj < cut { nxt[p*GLEN + jj] = pop[pa + jj] } else { nxt[p*GLEN + jj] = pop[pb + jj] } jj = jj + 1 } 122 let slot: i64 = br_rand(state) % GLEN 123 nxt[p*GLEN + slot] = br_rand(state) % 6 124 p = p + 1 125 } 126 var c: i64 = 0; while c < EVO_P * GLEN { pop[c] = nxt[c]; c = c + 1 } 127 g = g + 1 128 } 129 } 130 return best_err 131} 132 133func br_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 134// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 135// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 136// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 137// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 138func br_pn(v: i64) -> i64 { nxi_out(v); return 0 } 139 140func main() -> i64 { 141 let state: *i64 = sys_mmap(8) as *i64; state[0] = 7 * EVO_MAGIC_2654435761 + EVO_MAGIC_12345 142 let best_gcd: *i64 = sys_mmap(GLEN * 8) as *i64 143 let best_lcm: *i64 = sys_mmap(GLEN * 8) as *i64 144 145 let ge: i64 = br_ga(0, best_gcd, best_gcd, state) // GEN 1: derive gcd 146 let le: i64 = br_ga(1, best_gcd, best_lcm, state) // GEN 2: derive lcm USING gen-1 gcd 147 148 var gbad: i64 = 0; var lbad: i64 = 0; var total: i64 = 0 149 var a: i64 = 1 150 while a <= 40 { 151 var b: i64 = 1 152 while b <= 40 { 153 total = total + 1 154 if gcd_eval(best_gcd, 0, a, b) != br_euclid(a, b) { gbad = gbad + 1 } 155 if lcm_eval(best_lcm, 0, a, b, best_gcd) != br_lcm_true(a, b) { lbad = lbad + 1 } 156 b = b + 1 157 } 158 a = a + 1 159 } 160 161 br_p("BREED gen1=gcd train_err=" as *u8); br_pn(ge); br_p(" gen2=lcm train_err=" as *u8); br_pn(le); br_p("\n" as *u8) 162 br_p("LINEAGE: substrate -> gcd(kid) -> lcm(grandkid, composed from this run's gcd)\n" as *u8) 163 if gbad == 0 { if lbad == 0 { 164 br_p("BREED GREEN: 2 generations bred UNATTENDED in one run + verified on " as *u8); br_pn(total); br_p(" pairs each (gcd==euclid, lcm==a*b/gcd)\n" as *u8) 165 sys_exit(0); return 0 166 } } 167 br_p("BREED RED: gcd_mismatches=" as *u8); br_pn(gbad); br_p(" lcm_mismatches=" as *u8); br_pn(lbad); br_p("\n" as *u8) 168 sys_exit(1) 169 return 1 170}