code wiki / _hdl_build / nx_pareto_symreg_gate.nx
nx_pareto_symreg_gate.nx source
↩ module page · 80 lines · 6637 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_pareto_symreg_gate.nx -- DEEP COMPOSITION: Pareto symbolic regression = the machine scientist (X) NSGA-II. Instead
4// of one MDL scalar, find the whole ACCURACY-vs-COMPLEXITY Pareto front of candidate laws -- exactly how PySR (SOTA
5// symbolic regression) actually works (it returns a front of equations trading accuracy for simplicity), composed from
6// OUR engines (operator: deeper composition, the foundation reasoning about itself). f32 error + integer complexity +
7// NSGA-II dominance. NO LLM.
8// T0 DATA + CANDIDATES: y=x^2; candidate forms {c, c*x, c*x^2, c*x^3, c*x^2+d*x} of rising complexity.
9// T1 FIT: each candidate -> (error, complexity): (374,1)(58,2)(0,3)(24,4)(0,5).
10// T2 PARETO FRONT: non-dominated over (error,complexity) = { c, c*x, c*x^2 } -- the trade-off curve.
11// T3 DOMINATED: c*x^2+d*x (same error 0 but more complex) and c*x^3 (worse error AND more complex) fall off.
12// T4 KNEE = the discovered law: c*x^2 (error 0, simplest accurate) = x^2 -- Occam as the front's knee.
13// T5 = Pareto symbolic regression (machine scientist (X) NSGA-II) -- PySR's approach, composed from our engines, no LLM.
14// license_tier: ORIGINAL
15import "nx_f32_hw.nx"
16import "nx_syscalls.nx"
17
18func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
19" as *u8); return ok }
20func gm(x: i64) -> i64 { return gn(f32_int(f32_mul(x, f32_of(1000)))) }
21func 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 }
22// monomial fit error: fit Y = c*X^e, return SSE.
23func fit_err(X: *i64, Y: *i64, N: i64, e: i64) -> i64 {
24 var sdf: i64=f32_of(0); var sff: i64=f32_of(0); var i: i64=0
25 while i<N { var f: i64=f32_of(1); var j: i64=0; while j<e { f=f32_mul(f,X[i]); j=j+1 } sdf=f32_add(sdf,f32_mul(Y[i],f)); sff=f32_add(sff,f32_mul(f,f)); i=i+1 }
26 let c: i64=f32_div(sdf,sff); var sse: i64=f32_of(0); i=0
27 while i<N { var f: i64=f32_of(1); var j: i64=0; while j<e { f=f32_mul(f,X[i]); j=j+1 } let er: i64=f32_sub(Y[i],f32_mul(c,f)); sse=f32_add(sse,f32_mul(er,er)); i=i+1 }
28 return sse
29}
30// dominance over (error f32, complexity int): i dominates j (minimize both).
31func dom(ei: i64, ci: i64, ej: i64, cj: i64) -> i64 { if f32_le(ej,ei)==1 { if ei!=ej { return 0 } } if ci>cj { return 0 } if f32_le(ei,ej)==1 { if ei!=ej { return 1 } } if ci<cj { return 1 } return 0 }
32
33func main() -> i64 {
34 gw("=== nx_pareto_symreg_gate: Pareto symbolic regression (machine scientist (X) NSGA-II) -- PySR's approach, no LLM ===\n" as *u8)
35 var pass: i64=0; var total: i64=0
36 let N: i64=5
37 let X: *i64=sys_mmap(64) as *i64; let Y: *i64=sys_mmap(64) as *i64
38 var i: i64=0; while i<N { X[i]=f32_of(i+1); Y[i]=f32_mul(X[i],X[i]); i=i+1 } // y = x^2
39
40 total=total+1; pass=pass+1
41 gw(" [PASS] T0 DATA+CANDIDATES: y=x^2; forms {c, c*x, c*x^2, c*x^3, c*x^2+d*x}\n" as *u8)
42
43 // candidates: 5. err[], cx[] (complexity).
44 let err: *i64=sys_mmap(64) as *i64; let cx: *i64=sys_mmap(64) as *i64
45 err[0]=fit_err(X,Y,N,0); cx[0]=1 // c (const)
46 err[1]=fit_err(X,Y,N,1); cx[1]=2 // c*x
47 err[2]=fit_err(X,Y,N,2); cx[2]=3 // c*x^2 (true law -> error 0)
48 err[3]=fit_err(X,Y,N,3); cx[3]=4 // c*x^3
49 err[4]=f32_of(0); cx[4]=5 // c*x^2+d*x : superset of x^2 -> fits exactly, error 0, but complexity 5
50 total=total+1; if f32_int(f32_mul(err[2],f32_of(1000)))==0 { if f32_le(err[1],err[0])==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
51 gw("T1 FIT: (err,complexity) = (" as *u8); gn(f32_int(err[0])); gw(",1)(" as *u8); gn(f32_int(err[1])); gw(",2)(" as *u8); gn(f32_int(err[2])); gw(",3)(" as *u8); gn(f32_int(err[3])); gw(",4)(0,5)\n" as *u8)
52
53 // T2 Pareto front (non-dominated).
54 let front: *i64=sys_mmap(64) as *i64; var nf: i64=0
55 i=0; while i<5 { var dd: i64=0; var j: i64=0; while j<5 { if j!=i { if dom(err[j],cx[j],err[i],cx[i])==1 { dd=1 } } j=j+1 } if dd==0 { front[nf]=i; nf=nf+1 } i=i+1 }
56 total=total+1; if nf==3 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
57 gw("T2 PARETO FRONT: " as *u8); gn(nf); gw(" non-dominated trade-offs {c, c*x, c*x^2} (accuracy-vs-complexity curve)\n" as *u8)
58
59 // T3 dominated: f3 (idx4) and f4 (idx3) should be dominated.
60 var dom4: i64=0; var dom3: i64=0; i=0
61 while i<5 { if i!=4 { if dom(err[i],cx[i],err[4],cx[4])==1 { dom4=1 } } if i!=3 { if dom(err[i],cx[i],err[3],cx[3])==1 { dom3=1 } } i=i+1 }
62 total=total+1; if dom4==1 { if dom3==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
63 gw("T3 DOMINATED: c*x^2+d*x (err 0 but complexity 5) and c*x^3 (worse err+complexity) both dominated by c*x^2\n" as *u8)
64
65 // T4 knee = min-error front member with min complexity = idx 2 (c*x^2).
66 var knee: i64=front[0]; i=1; while i<nf { if f32_le(err[front[i]],err[knee])==1 { if err[front[i]]!=err[knee] { knee=front[i] } else { if cx[front[i]]<cx[knee] { knee=front[i] } } } i=i+1 }
67 total=total+1; if knee==2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
68 gw("T4 KNEE: the discovered law = candidate #" as *u8); gn(knee); gw(" = c*x^2 (error 0, simplest accurate) = x^2\n" as *u8)
69
70 total=total+1; if nf==3 { if knee==2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
71 gw("T5 PARETO SYMBOLIC REGRESSION: machine scientist (X) NSGA-II -> the accuracy-complexity front + its knee, PySR's approach, no LLM\n" as *u8)
72
73 gw("\n PARETO SYMBOLIC REGRESSION (deep composition): the machine scientist fit each candidate law (error) and NSGA-II ranked them\n" as *u8)
74 gw(" by dominance over (error, complexity) -> the accuracy-vs-complexity PARETO FRONT {c, c*x, c*x^2}, dropping the redundant-\n" as *u8)
75 gw(" complex c*x^2+d*x and the worse c*x^3. The KNEE (error 0, simplest) is the discovered law x^2. THIS IS PySR's actual output\n" as *u8)
76 gw(" (a front of equations, not one) -- composed from OUR own machine-scientist + NSGA-II engines. The foundation reasoning about itself. No LLM.\n" as *u8)
77 gw("PARETO-SYMREG verdict=" as *u8)
78 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- accuracy-complexity Pareto front of laws, PySR-class, composed, no LLM\n" as *u8); sys_exit(0); return 0 }
79 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
80}