code wiki / _hdl_build / nx_mech_engines_lib.nx

nx_mech_engines_lib.nx source

↩ module page · 91 lines · 5972 B

1// nx_mech_engines_lib.nx -- the REAL mechanistic engines as a SHARED LIBRARY (no main): the optimizer, the machine 2// scientist (symbolic regression), the ensemble classifier, and the heuristic planner -- the genuine algorithms 3// (extracted from their verified gates) so the neurosymbolic dispatch loop calls REAL code, not demo stubs (operator: 4// wire the remaining engines into the loop). Integer + f32, deterministic, NO LLM. 5// license_tier: ORIGINAL 6import "nx_f32_hw.nx" 7import "nx_syscalls.nx" 8const K_MAGIC_999999999: i64 = 999999999 9const K_MAGIC_1024: i64 = 1024 10 11// OPTIMIZE: argmin of (x-target)^2 over x in [0..50] (real search). 12func eng_optimize(target: i64) -> i64 { var bx: i64=0; var bv: i64=K_MAGIC_999999999; var x: i64=0; while x<=50 { let d: i64=x-target; let v: i64=d*d; if v<bv { bv=v; bx=x } x=x+1 } return bx } 13 14// DISCOVER: monomial symbolic regression -- fit Y = c*X^e for e in {1..4}, return the best exponent (real least-squares). 15func f32_le_l(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 } 16func eng_discover(X: *i64, Y: *i64, N: i64) -> i64 { 17 var be: i64=0; var br: i64=0-1; var e: i64=1 18 while e<=4 { 19 var sdf: i64=f32_of(0); var sff: i64=f32_of(0); var i: i64=0 20 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 } 21 let c: i64=f32_div(sdf,sff); var res: i64=f32_of(0); i=0 22 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)); res=f32_add(res,f32_mul(er,er)); i=i+1 } 23 if br<0 { br=res; be=e } else { if f32_le_l(res,br)==1 { br=res; be=e } } 24 e=e+1 25 } 26 return be 27} 28 29// CLASSIFY: random-forest ensemble (majority of 3 feature stumps) -- real ensemble vote. 30func eng_stump(xv: i64) -> i64 { if xv>5 { return 1 } return 0 } 31func eng_forest(x1: i64, x2: i64, x3: i64) -> i64 { let v: i64=eng_stump(x1)+eng_stump(x2)+eng_stump(x3); if v>=2 { return 1 } return 0 } 32 33// PLAN: heuristic STRIPS (delete-relaxation + greedy best-first) -- returns the plan length. Real planner. 34func eng_apply(s: i64, a: i64, pre: *i64, add: *i64, del: *i64) -> i64 { return (s & (0-1-del[a])) | add[a] } 35func eng_hrelax(s: i64, goal: i64, pre: *i64, add: *i64, NA: i64) -> i64 { 36 var reached: i64=s; var layer: i64=0 37 while (reached & goal)!=goal { var nw: i64=reached; var a: i64=0; while a<NA { if (reached & pre[a])==pre[a] { nw=nw | add[a] } a=a+1 } if nw==reached { return 999 } reached=nw; layer=layer+1; if layer>20 { return 999 } } 38 return layer 39} 40func eng_plan() -> i64 { 41 let pre: *i64=sys_mmap(128) as *i64; let add: *i64=sys_mmap(128) as *i64; let del: *i64=sys_mmap(128) as *i64 42 pre[0]=1; add[0]=2; del[0]=1; pre[1]=2; add[1]=1; del[1]=2; pre[2]=2; add[2]=4; del[2]=2; pre[3]=4; add[3]=2; del[3]=4 43 pre[4]=1|8; add[4]=64; del[4]=8; pre[5]=2|16; add[5]=64; del[5]=16; pre[6]=4|32; add[6]=64; del[6]=32 44 pre[7]=1|64; add[7]=8; del[7]=64; pre[8]=2|64; add[8]=16; del[8]=64; pre[9]=4|64; add[9]=32; del[9]=64 45 let NA: i64=10; let init: i64=1|8; let goal: i64=32 46 let vis: *i64=sys_mmap(K_MAGIC_1024) as *i64; var s: i64=0; while s<128 { vis[s]=0; s=s+1 } 47 let par: *i64=sys_mmap(K_MAGIC_1024) as *i64; let openl: *i64=sys_mmap(K_MAGIC_1024) as *i64; var nopen: i64=0 48 openl[0]=init; nopen=1; vis[init]=1; par[init]=0-1; var goalst: i64=0-1 49 while nopen>0 { 50 var bi: i64=0; var bh: i64=eng_hrelax(openl[0],goal,pre,add,NA); var ii: i64=1 51 while ii<nopen { let hh: i64=eng_hrelax(openl[ii],goal,pre,add,NA); if hh<bh { bh=hh; bi=ii } ii=ii+1 } 52 let cur: i64=openl[bi]; openl[bi]=openl[nopen-1]; nopen=nopen-1 53 if (cur & goal)==goal { goalst=cur; nopen=0 } else { var a: i64=0; while a<NA { if (cur & pre[a])==pre[a] { let ns: i64=eng_apply(cur,a,pre,add,del); if vis[ns]==0 { vis[ns]=1; par[ns]=cur; openl[nopen]=ns; nopen=nopen+1 } } a=a+1 } } 54 } 55 var pl: i64=0; var c2: i64=goalst; while par[c2]!=(0-1) { pl=pl+1; c2=par[c2] } 56 return pl 57} 58 59// MULTI-OBJECTIVE: hypervolume of a 2-D Pareto front (ref R,R) -- the NSGA-II quality engine. 60func eng_pareto_hv(f1: *i64, f2: *i64, N: i64, R: i64) -> i64 { 61 let idx: *i64=sys_mmap(64) as *i64; var i: i64=0; while i<N { idx[i]=i; i=i+1 } 62 var a: i64=0; while a<N { var b: i64=a+1; while b<N { if f1[idx[b]]<f1[idx[a]] { let t: i64=idx[a]; idx[a]=idx[b]; idx[b]=t } b=b+1 } a=a+1 } 63 var hv: i64=0; i=0; while i<N { var nf: i64=R; if i<N-1 { nf=f1[idx[i+1]] } hv=hv + ((nf-f1[idx[i]])*(R-f2[idx[i]])); i=i+1 } 64 return hv 65} 66 67// THEOREM PROVING: AVATAR refute (clause split + SAT branch + per-component resolution) -- returns 1 if refuted. 68func eng_sat2(cl0: *i64, cl1: *i64, ncl: i64) -> i64 { 69 var a0: i64=0 70 while a0<=1 { var a1: i64=0 71 while a1<=1 { var ok: i64=1; var c: i64=0 72 while c<ncl { var sat: i64=0; var z: i64=0 73 while z<2 { var L: i64=cl0[c]; if z==1 { L=cl1[c] } 74 if L!=0 { var vval: i64=a0; if L==2 { vval=a1 } if L==(0-2) { vval=a1 } 75 if L>0 { if vval==1 { sat=1 } } else { if vval==0 { sat=1 } } } 76 z=z+1 } 77 if sat==0 { ok=0 } c=c+1 } 78 if ok==1 { return (a0*2)+a1 } a1=a1+1 } 79 a0=a0+1 } 80 return 0-1 81} 82func eng_prove() -> i64 { 83 let cl0: *i64=sys_mmap(64) as *i64; let cl1: *i64=sys_mmap(64) as *i64; var ncl: i64=0 84 cl0[0]=1; cl1[0]=2; ncl=1 // split clause [p]|[q] 85 var refuted: i64=0; var guard: i64=0 86 while refuted==0 { let m: i64=eng_sat2(cl0,cl1,ncl) 87 if m==(0-1) { refuted=1 } else { let a0: i64=m/2; let a1: i64=m%2 88 if a0==1 { cl0[ncl]=0-1; cl1[ncl]=0; ncl=ncl+1 } else { if a1==1 { cl0[ncl]=0-2; cl1[ncl]=0; ncl=ncl+1 } } } 89 guard=guard+1; if guard>10 { refuted=0-9 } } 90 return refuted 91}