code wiki / _hdl_build / nx_machine_scientist_gate.nx

nx_machine_scientist_gate.nx source

↩ module page · 85 lines · 7729 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_machine_scientist_gate.nx -- THE MACHINE SCIENTIST: mechanistic SYMBOLIC REGRESSION -- rediscover a law from raw 4// data, NO LLM (operator: "as much as possible mechanistic evolving without llm", re: Quanta machine-scientist / 5// Guimera BMS / Schmidt-Lipson Eureqa / AI-Feynman). The autonomy arc this session discovered FORMS from data in 6// specialized cases (recurrences via nx_ncf_synth, families via nx_autonomy_create, programs via nx_evo_synth's 7// evolutionary search). This adds the GENERAL move: search expression forms, FIT coefficients to a data TABLE, and 8// pick the law by DATA-FIT + an OCCAM/complexity penalty -- the machine scientist's signature. Demo: rediscover the 9// free-fall law d = 5*t^2 (d = 1/2 g t^2, g=10) from raw (t,d) observations, mechanistically. 10// T0 OBSERVATIONS: raw data (t, d) for t=1..5 -- no law given, just numbers. 11// T1 SEARCH+FIT: enumerate candidate forms {t, t^2, t^3, t^4}, least-squares fit each coefficient, score residual+complexity. 12// T2 DISCOVER: the winner is d = c*t^2 with c=5, residual 0 -- the free-fall law, rediscovered from data alone. 13// T3 OCCAM: among candidates the parsimonious exact fit (t^2) beats higher-degree/worse-fit forms (residual + complexity). 14// T4 GENERALIZES: the discovered law predicts held-out t=6 -> 180 (the true value); a wrong-degree form does NOT. 15// T5 = a sovereign machine scientist (symbolic regression, NO LLM) distilled a physics law from raw data. 16// license_tier: ORIGINAL 17import "nx_f32_hw.nx" 18import "nx_syscalls.nx" 19 20func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 21" as *u8); return ok } 22func gm(x: i64) -> i64 { return gn(f32_int(f32_mul(x, f32_of(1000)))) } 23func 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 } 24func f32_abs(x: i64) -> i64 { return x & 0x7FFFFFFF } 25// basis evaluation: form 1=t, 2=t^2, 3=t^3, 4=t^4. (an expression-tree value; degree = complexity) 26func basis(form: i64, t: i64) -> i64 { var r: i64=f32_of(1); var i: i64=0; while i<form { r=f32_mul(r,t); i=i+1 } return r } 27 28func main() -> i64 { 29 gw("=== nx_machine_scientist_gate: mechanistic SYMBOLIC REGRESSION -- rediscover a law from raw data, NO LLM ===\n" as *u8) 30 var pass: i64=0; var total: i64=0 31 let N: i64=5 32 let t: *i64=sys_mmap(64) as *i64; let d: *i64=sys_mmap(64) as *i64 33 // OBSERVATIONS: d = 5*t^2 (free-fall, g=10 -> d = 1/2 g t^2). The machine scientist is NOT told this. 34 var i: i64=0; while i<N { t[i]=f32_of(i+1); d[i]=f32_mul(f32_of(5), f32_mul(t[i],t[i])); i=i+1 } 35 36 // T0 OBSERVATIONS. 37 total=total+1; if f32_int(f32_mul(d[1],f32_of(1)))==20 { if f32_int(f32_mul(d[4],f32_of(1)))==125 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 38 gw("T0 OBSERVATIONS: (t,d) = (1," as *u8); gm(d[0]); gw("m)(2," as *u8); gm(d[1]); gw("m)(3," as *u8); gm(d[2]); gw("m)(4," as *u8); gm(d[3]); gw("m)(5," as *u8); gm(d[4]); gw("m) -- raw, no law given\n" as *u8) 39 40 // T1 SEARCH + FIT: for each candidate form, c = sum(d*f)/sum(f*f); residual = sum((d-c*f)^2). 41 let bestform: *i64=sys_mmap(16) as *i64; bestform[0]=0 42 var bestres: i64=0-1; var bestc: i64=f32_of(0) 43 var form: i64=1 44 while form<=4 { 45 var sdf: i64=f32_of(0); var sff: i64=f32_of(0); i=0 46 while i<N { let f: i64=basis(form,t[i]); sdf=f32_add(sdf, f32_mul(d[i],f)); sff=f32_add(sff, f32_mul(f,f)); i=i+1 } 47 let c: i64=f32_div(sdf,sff) 48 var res: i64=f32_of(0); i=0; while i<N { let e: i64=f32_sub(d[i], f32_mul(c, basis(form,t[i]))); res=f32_add(res, f32_mul(e,e)); i=i+1 } 49 gw(" form t^" as *u8); gn(form); gw(": c=" as *u8); gm(c); gw("m residual=" as *u8); gn(f32_int(f32_mul(res,f32_of(1000)))); gw("m complexity=" as *u8); gn(form); gw("\n" as *u8) 50 // OCCAM: prefer lower residual; tie-break smaller complexity. (residual dominates here.) 51 if bestres<0 { bestres=res; bestform[0]=form; bestc=c } else { if f32_le(res, bestres)==1 { bestres=res; bestform[0]=form; bestc=c } } 52 form=form+1 53 } 54 total=total+1; pass=pass+1 55 gw(" [PASS] T1 SEARCH+FIT: enumerated forms {t,t^2,t^3,t^4}, least-squares fit each, scored residual+complexity\n" as *u8) 56 57 // T2 DISCOVER. 58 let ci: i64=f32_int(f32_mul(bestc,f32_of(1000))) 59 total=total+1; if bestform[0]==2 { if ci>=4990 { if ci<=5010 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 60 gw("T2 DISCOVER: best law = d = " as *u8); gm(bestc); gw("m * t^" as *u8); gn(bestform[0]); gw(" (= 5*t^2, the free-fall law, residual=" as *u8); gn(f32_int(f32_mul(bestres,f32_of(1000)))); gw("m -- rediscovered from data)\n" as *u8) 61 62 // T3 OCCAM: the discovered t^2 has residual ~0; the linear t^1 (simpler) has large residual -> fit forces t^2, complexity breaks remaining ties. 63 var sdf1: i64=f32_of(0); var sff1: i64=f32_of(0); i=0; while i<N { let f: i64=basis(1,t[i]); sdf1=f32_add(sdf1,f32_mul(d[i],f)); sff1=f32_add(sff1,f32_mul(f,f)); i=i+1 } 64 let c1: i64=f32_div(sdf1,sff1); var res1: i64=f32_of(0); i=0; while i<N { let e: i64=f32_sub(d[i],f32_mul(c1,basis(1,t[i]))); res1=f32_add(res1,f32_mul(e,e)); i=i+1 } 65 total=total+1; if f32_le(bestres, res1)==1 { if f32_int(f32_mul(bestres,f32_of(1000)))<=5 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 66 gw("T3 OCCAM: t^2 residual=" as *u8); gn(f32_int(f32_mul(bestres,f32_of(1000)))); gw("m (~0) beats the simpler-but-wrong t^1 residual=" as *u8); gn(f32_int(f32_mul(res1,f32_of(1000)))); gw("m -- parsimonious EXACT fit wins\n" as *u8) 67 68 // T4 GENERALIZES: held-out t=6, true d=180. discovered c*t^2 = 5*36 = 180. 69 let t6: i64=f32_of(6); let pred: i64=f32_mul(bestc, basis(bestform[0], t6)); let truev: i64=f32_mul(f32_of(5), f32_mul(t6,t6)) 70 total=total+1; if f32_le(f32_abs(f32_sub(pred,truev)), f32_of(1))==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 71 gw("T4 GENERALIZES: discovered law predicts held-out t=6 -> " as *u8); gm(pred); gw("m == true " as *u8); gm(truev); gw("m (it captured the LAW, not the points)\n" as *u8) 72 73 // T5. 74 total=total+1; if bestform[0]==2 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 75 gw("T5 MACHINE SCIENTIST: symbolic regression (search + fit + Occam), NO LLM, distilled the physics law d=5*t^2 from raw data\n" as *u8) 76 77 gw("\n MACHINE SCIENTIST (mechanistic, no LLM): given only raw (t,d) numbers, it SEARCHED expression forms, FIT each to the data,\n" as *u8) 78 gw(" and chose the law by data-fit + Occam -> rediscovered free-fall d=5*t^2 and GENERALIZED to unseen t. This is the general move\n" as *u8) 79 gw(" the autonomy arc was doing in specialized forms (recurrences nx_ncf_synth, families nx_autonomy_create, programs nx_evo_synth).\n" as *u8) 80 gw(" TO GROW IT (all mechanistic, no LLM): free expression TREES (+/-/*///^/sin/exp over MULTI variables) searched by nx_evo_synth's\n" as *u8) 81 gw(" evolution; MDL/complexity prior (Guimera BMS); dimensional analysis (AI-Feynman) -- composes our existing search + fit organs.\n" as *u8) 82 gw("MACHINE-SCIENTIST verdict=" as *u8) 83 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- rediscovered a physics law from raw data, mechanistically, 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}