code wiki / _hdl_build / nx_pareto_moea_gate.nx

nx_pareto_moea_gate.nx source

↩ module page · 92 lines · 5980 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_pareto_moea_gate.nx -- MULTI-OBJECTIVE optimization / PARETO front (the MOEA / NSGA-II core): when objectives trade 4// off there is no single best -- there is a PARETO FRONT of non-dominated solutions (operator: build the mechanistic-AI 5// foundation, MOEA Framework was a primary link). Solution A DOMINATES B iff A is no worse in every objective and 6// strictly better in at least one. The non-dominated set (front 0) is the trade-off frontier; non-dominated SORTING 7// ranks the rest into successive fronts (NSGA-II's key step). Pure integer comparison, NO LLM. Demo: 9 candidate 8// solutions over 2 objectives (minimize cost f1 AND weight f2) -> extract the 6-point Pareto front, exclude 3 dominated. 9// T0 CANDIDATES: 9 (f1,f2) solutions, minimize both -- a genuine trade-off (can't minimize both fully). 10// T1 DOMINANCE: (2,6) dominates (5,8) [<= in both, < in one]; (1,9) does NOT dominate (2,6) [worse in f2]. 11// T2 NON-DOMINATED SORT: front 0 = the 6 Pareto-optimal solutions; the 3 dominated ones fall to front 1. 12// T3 PARETO FRONT: the 6 non-dominated trade-offs span min-f1 (1,9) ... min-f2 (9,1). 13// T4 FRONTIER PROPERTY: no front-0 solution dominates another (each is a real, incomparable trade-off). 14// T5 = MOEA/Pareto extracts the trade-off frontier by dominance ranking, mechanistic, no LLM. 15// license_tier: ORIGINAL 16import "nx_syscalls.nx" 17 18// minimize both: i dominates j iff f1[i]<=f1[j] && f2[i]<=f2[j] && (f1[i]<f1[j] || f2[i]<f2[j]) 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 dominates(i: i64, j: i64, f1: *i64, f2: *i64) -> i64 { 22 if f1[i]>f1[j] { return 0 } 23 if f2[i]>f2[j] { return 0 } 24 if f1[i]<f1[j] { return 1 } 25 if f2[i]<f2[j] { return 1 } 26 return 0 27} 28 29func main() -> i64 { 30 gw("=== nx_pareto_moea_gate: multi-objective / Pareto front (MOEA/NSGA core) -- the trade-off frontier, no LLM ===\n" as *u8) 31 var pass: i64=0; var total: i64=0 32 let N: i64=9 33 let f1: *i64=sys_mmap(128) as *i64; let f2: *i64=sys_mmap(128) as *i64 34 f1[0]=1; f2[0]=9 35 f1[1]=2; f2[1]=6 36 f1[2]=3; f2[2]=4 37 f1[3]=4; f2[3]=3 38 f1[4]=6; f2[4]=2 39 f1[5]=9; f2[5]=1 40 f1[6]=5; f2[6]=8 41 f1[7]=7; f2[7]=7 42 f1[8]=8; f2[8]=5 43 44 // T0. 45 total=total+1; pass=pass+1 46 gw(" [PASS] T0 CANDIDATES: 9 solutions (minimize f1=cost, f2=weight): (1,9)(2,6)(3,4)(4,3)(6,2)(9,1)(5,8)(7,7)(8,5)\n" as *u8) 47 48 // T1 dominance. 49 let d_2_6: i64=dominates(1,6,f1,f2) // (2,6) vs (5,8) -> dominates 50 let d_1_1: i64=dominates(0,1,f1,f2) // (1,9) vs (2,6) -> does NOT (worse in f2) 51 total=total+1; if d_2_6==1 { if d_1_1==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 52 gw("T1 DOMINANCE: (2,6) dominates (5,8)? " as *u8); gn(d_2_6); gw(" ; (1,9) dominates (2,6)? " as *u8); gn(d_1_1); gw(" (0 -- incomparable trade-off)\n" as *u8) 53 54 // T2 non-dominated sort: front 0 = solutions dominated by NONE. 55 let front: *i64=sys_mmap(128) as *i64; var nf: i64=0 56 var i: i64=0 57 while i<N { 58 var dominated: i64=0; var j: i64=0 59 while j<N { if j!=i { if dominates(j,i,f1,f2)==1 { dominated=1 } } j=j+1 } 60 if dominated==0 { front[nf]=i; nf=nf+1 } 61 i=i+1 62 } 63 total=total+1; if nf==6 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 64 gw("T2 NON-DOMINATED SORT: front 0 has " as *u8); gn(nf); gw(" solutions (the 3 dominated -- (5,8),(7,7),(8,5) -- fall to front 1)\n" as *u8) 65 66 // T3 Pareto front contents. 67 gw(" Pareto front (front 0): " as *u8) 68 i=0; while i<nf { gw("(" as *u8); gn(f1[front[i]]); gw("," as *u8); gn(f2[front[i]]); gw(") " as *u8); i=i+1 } 69 gw("\n" as *u8) 70 // verify min-f1 (1,9) and min-f2 (9,1) are both on the front. 71 var has_minf1: i64=0; var has_minf2: i64=0 72 i=0; while i<nf { if front[i]==0 { has_minf1=1 } if front[i]==5 { has_minf2=1 } i=i+1 } 73 total=total+1; if has_minf1==1 { if has_minf2==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 74 gw("T3 PARETO FRONT: spans min-cost (1,9) ... min-weight (9,1) -- the full trade-off curve\n" as *u8) 75 76 // T4 frontier property: no front-0 solution dominates another. 77 var bad: i64=0; i=0; while i<nf { var j: i64=0; while j<nf { if i!=j { if dominates(front[i],front[j],f1,f2)==1 { bad=1 } } j=j+1 } i=i+1 } 78 total=total+1; if bad==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 79 gw("T4 FRONTIER PROPERTY: no front-0 solution dominates another (bad=" as *u8); gn(bad); gw(") -- every point is an incomparable, valid trade-off\n" as *u8) 80 81 // T5. 82 total=total+1; if nf==6 { if bad==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 83 gw("T5 MOEA/PARETO: dominance ranking extracted the 6-point trade-off frontier from 9 candidates, mechanistic, no LLM\n" as *u8) 84 85 gw("\n MULTI-OBJECTIVE / PARETO (MOEA/NSGA core): with competing objectives there is no single optimum -- the answer is the PARETO\n" as *u8) 86 gw(" FRONT of non-dominated solutions. Dominance (no worse in all, better in one) ranked 9 candidates -> a 6-point trade-off curve\n" as *u8) 87 gw(" from min-cost to min-weight, the 3 dominated solutions demoted to front 1 (NSGA-II's non-dominated sorting). Pure integer\n" as *u8) 88 gw(" comparison, NO LLM. Composes with nx_genetic_algorithm (evolve the population toward the front) = a real MOEA. Foundation rung.\n" as *u8) 89 gw("PARETO-MOEA verdict=" as *u8) 90 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- Pareto front extracted by dominance ranking, no LLM\n" as *u8); sys_exit(0); return 0 } 91 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1 92}