code wiki / _hdl_build / nx_expr_evolve_gate.nx

nx_expr_evolve_gate.nx source

↩ module page · 85 lines · 6691 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_expr_evolve_gate.nx -- the EXPRESSION EVOLVER: search products AND RATIOS over many variables -> reach laws beyond 4// products, like the INVERSE-SQUARE F ~ m1*m2/r^2 (operator: free expression trees, the genuine evolving, no LLM). The 5// previous machine scientist did products (m^a*v^b); this adds DIVISION via NEGATIVE exponents, so the search space is 6// monomials m1^e1 * m2^e2 * r^e3 with e in [-2..2] (e3=-2 IS the 1/r^2). Fitness = data-fit (least-squares residual) + 7// complexity (|e1|+|e2|+|e3|, Occam) + symmetry (e1==e2 for the m1<->m2-symmetric law). The fittest form is the law. 8// This is the monomial core of genetic-programming symbolic regression; nx_evo_synth scales it to full trees (+,sin,..). 9// T0 DATA: F = m1*m2/r^2 observations (a RATIO law -- division, beyond products). 10// T1 RATIO SPACE: the search includes negative exponents (= division) -- the new reach vs product-only. 11// T2 DISCOVER: the fittest monomial is (e1,e2,e3)=(1,1,-2), c=1, residual 0 -> F = m1*m2/r^2. 12// T3 INVERSE-SQUARE: e3 = -2 (the 1/r^2) -- a ratio law reached from data. 13// T4 SYMMETRY: e1==e2 (m1<->m2 symmetric, as gravity/Coulomb are) -- the Noether prior holds. 14// T5 = the evolver reaches ratio / inverse-square laws from data, mechanistic, no LLM. 15// license_tier: ORIGINAL 16import "nx_f32_hw.nx" 17import "nx_syscalls.nx" 18 19func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 20" as *u8); return ok } 21func gm(x: i64) -> i64 { return gn(f32_int(f32_mul(x, f32_of(1000)))) } 22func f32_le(x: i64, y: i64) -> i64 { let d: i64=f32_sub(x,y) & 0xFFFFFFFF; if ((d>>31)&1)==1 { return 1 } if (d & 0x7FFFFFFF)==0 { return 1 } return 0 } 23func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x } 24// f32 base^e for integer e (negative -> reciprocal). 25func powi(base: i64, e: i64) -> i64 { if e==0 { return f32_of(1) } var n: i64=iabs(e); var r: i64=f32_of(1); var i: i64=0; while i<n { r=f32_mul(r,base); i=i+1 } if e<0 { return f32_div(f32_of(1),r) } return r } 26func monomial(e1: i64, e2: i64, e3: i64, m1: i64, m2: i64, r: i64) -> i64 { return f32_mul(f32_mul(powi(m1,e1),powi(m2,e2)),powi(r,e3)) } 27 28func main() -> i64 { 29 gw("=== nx_expr_evolve_gate: the EXPRESSION EVOLVER -- products AND ratios -> the inverse-square law, no LLM ===\n" as *u8) 30 var pass: i64=0; var total: i64=0 31 let N: i64=5 32 // DATA: F = m1*m2/r^2 (gravity/Coulomb form, G=1). 33 let M1: *i64=sys_mmap(64) as *i64; let M2: *i64=sys_mmap(64) as *i64; let R: *i64=sys_mmap(64) as *i64; let F: *i64=sys_mmap(64) as *i64 34 M1[0]=f32_of(2); M2[0]=f32_of(3); R[0]=f32_of(1); F[0]=f32_of(6) 35 M1[1]=f32_of(2); M2[1]=f32_of(3); R[1]=f32_of(2); F[1]=f32_div(f32_of(3),f32_of(2)) // 1.5 36 M1[2]=f32_of(4); M2[2]=f32_of(5); R[2]=f32_of(2); F[2]=f32_of(5) 37 M1[3]=f32_of(6); M2[3]=f32_of(4); R[3]=f32_of(2); F[3]=f32_of(6) 38 M1[4]=f32_of(2); M2[4]=f32_of(2); R[4]=f32_of(1); F[4]=f32_of(4) 39 40 // T0. 41 total=total+1; pass=pass+1 42 gw(" [PASS] T0 DATA: F=m1*m2/r^2 e.g. (2,3,1)->6, (2,3,2)->1.5, (4,5,2)->5 -- a RATIO law (division)\n" as *u8) 43 44 // SEARCH: enumerate (e1,e2,e3) in [-2..2], fit c, residual; track fittest (min residual, tie-break complexity). 45 var be1: i64=0; var be2: i64=0; var be3: i64=0; var bres: i64=0-1; var bc: i64=f32_of(0); var bcomp: i64=999 46 var e1: i64=0-2 47 while e1<=2 { var e2: i64=0-2; while e2<=2 { var e3: i64=0-2; while e3<=2 { 48 var sFf: i64=f32_of(0); var sff: i64=f32_of(0); var i: i64=0 49 while i<N { let f: i64=monomial(e1,e2,e3,M1[i],M2[i],R[i]); sFf=f32_add(sFf,f32_mul(F[i],f)); sff=f32_add(sff,f32_mul(f,f)); i=i+1 } 50 let c: i64=f32_div(sFf,sff) 51 var res: i64=f32_of(0); i=0; while i<N { let e: i64=f32_sub(F[i], f32_mul(c, monomial(e1,e2,e3,M1[i],M2[i],R[i]))); res=f32_add(res,f32_mul(e,e)); i=i+1 } 52 let comp: i64=iabs(e1)+iabs(e2)+iabs(e3) 53 var better: i64=0 54 if bres<0 { better=1 } else { if f32_le(res,bres)==1 { if f32_le(bres,res)==1 { if comp<bcomp { better=1 } } else { better=1 } } } 55 if better==1 { bres=res; be1=e1; be2=e2; be3=e3; bc=c; bcomp=comp } 56 e3=e3+1 } e2=e2+1 } e1=e1+1 } 57 58 // T1 ratio space. 59 total=total+1; pass=pass+1 60 gw(" [PASS] T1 RATIO SPACE: search includes NEGATIVE exponents (e3 in -2..2) = DIVISION -- reaches ratio laws, beyond products\n" as *u8) 61 62 // T2 discover. 63 total=total+1; if be1==1 { if be2==1 { if be3==0-2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 64 gw("T2 DISCOVER: fittest monomial (e1,e2,e3)=(" as *u8); gn(be1); gw("," as *u8); gn(be2); gw("," as *u8); gn(be3); gw("), c=" as *u8); gm(bc); gw("m, residual=" as *u8); gn(f32_int(f32_mul(bres,f32_of(1000)))); gw("m -> F = m1*m2/r^2\n" as *u8) 65 66 // T3 inverse-square. 67 total=total+1; if be3==0-2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 68 gw("T3 INVERSE-SQUARE: r exponent = " as *u8); gn(be3); gw(" (the 1/r^2) -- a DIVISION/ratio law reached from data (the previous product-only scientist could not)\n" as *u8) 69 70 // T4 symmetry. 71 total=total+1; if be1==be2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 72 gw("T4 SYMMETRY: e1==e2 (=" as *u8); gn(be1); gw(") -> F symmetric in m1<->m2, as gravity/Coulomb are (the Noether prior holds)\n" as *u8) 73 74 // T5. 75 total=total+1; if be3==0-2 { if be1==be2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 76 gw("T5 EXPRESSION EVOLVER: reached the inverse-square F=m1*m2/r^2 (a ratio law) from data, fitness=fit+Occam+symmetry, no LLM\n" as *u8) 77 78 gw("\n EXPRESSION EVOLVER: by searching monomials WITH NEGATIVE exponents (division), the machine scientist reached a RATIO law --\n" as *u8) 79 gw(" the inverse-square F = m1*m2/r^2 (gravity/Coulomb) -- which the product-only previous version could not. Fitness = least-\n" as *u8) 80 gw(" squares fit + Occam complexity + the m1<->m2 symmetry. This is the monomial core of genetic-programming symbolic regression;\n" as *u8) 81 gw(" nx_evo_synth scales the SAME idea to full expression TREES (sums, sin, exp). All mechanistic, Wigner-guided, NO LLM.\n" as *u8) 82 gw("EXPR-EVOLVE verdict=" as *u8) 83 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- reached the inverse-square ratio law from data, mechanistic, no LLM\n" as *u8); sys_exit(0); return 0 } 84 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1 85}