code wiki / _hdl_build / nx_param_joint.nx
nx_param_joint.nx source
↩ module page · 96 lines · 4672 B
1// nx_param_joint.nx -- X-PRM-004 (PRMJOINT-pairs-swept): the JOINT pair-sweep rung for COUPLED
2// knobs. The terrace lesson (params.tsv procgen_coupled_knobs): greedy single-variable tuning
3// ZEROES peaks that are only reachable by moving two coupled knobs TOGETHER -- coordinate ascent
4// stalls on the terrace because every single-knob step from the start goes DOWN into the coupling
5// valley. This organ is the generic pair-sweep harness + the proof:
6// POS (coupled): a terrace fitness whose peak (a>=H AND b>=H)=100 sits behind a valley (single
7// moves =5 < start=10). JOINT grid-sweep finds 100; GREEDY coordinate-ascent stalls at 10.
8// joint_wins = (coupled_joint > coupled_greedy).
9// NEG (separable): f=a+b has NO coupling, so greedy is already optimal -- JOINT must NOT beat it
10// (no_false_win = (sep_joint == sep_greedy)). Proves the harness only claims a win on REAL
11// coupling, never a spurious one.
12// Honest scope: the fitness here is a synthetic terrace MODEL of coupled knobs (capability proof);
13// wiring the live grader WIN floor as the fitness is the follow-on (cf. nx_param_tune rung2 note).
14// Sovereign (nx_cc->nxasm_x86, no gcc). license_tier: ORIGINAL
15import "nx_syscalls.nx"
16const K_MAGIC_1000000000: i64 = 1000000000
17
18func pj_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
19func pj_n(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {sys_write(fd,"-" as *u8,1); m=0-m}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 }
20
21// COUPLED terrace: peak only when BOTH knobs are high; single moves fall into a valley below start.
22func pj_fit_coupled(a: i64, b: i64) -> i64 {
23 let h: i64 = 8
24 if a >= h { if b >= h { return 100 } } // the peak -- needs BOTH
25 if a >= h { return 5 } // moved a only -> valley
26 if b >= h { return 5 } // moved b only -> valley
27 return 10 // start terrace (both low)
28}
29// SEPARABLE: no coupling -- each knob contributes independently (greedy is already optimal here).
30func pj_fit_sep(a: i64, b: i64) -> i64 { return a + b }
31
32func pj_eval(coupled: i64, a: i64, b: i64) -> i64 {
33 if coupled == 1 { return pj_fit_coupled(a, b) }
34 return pj_fit_sep(a, b)
35}
36
37// JOINT pair-sweep: evaluate the FULL (a,b) grid, return the global best.
38func pj_joint(coupled: i64, lo: i64, hi: i64) -> i64 {
39 var best: i64 = 0 - K_MAGIC_1000000000
40 var a: i64 = lo
41 while a <= hi {
42 var b: i64 = lo
43 while b <= hi {
44 let f: i64 = pj_eval(coupled, a, b)
45 if f > best { best = f }
46 b = b + 1
47 }
48 a = a + 1
49 }
50 return best
51}
52
53// GREEDY coordinate ascent from (lo,lo): optimize a (hold b=lo), then b (hold best a). The single-
54// variable strategy -- this is what X-PRM-003 does per-knob; on coupled knobs it stalls on the terrace.
55func pj_greedy(coupled: i64, lo: i64, hi: i64) -> i64 {
56 var besta: i64 = lo
57 var bestfa: i64 = pj_eval(coupled, lo, lo)
58 var a: i64 = lo
59 while a <= hi { let f: i64 = pj_eval(coupled, a, lo); if f > bestfa { bestfa = f; besta = a } a = a + 1 }
60 var bestb: i64 = lo
61 var bestfb: i64 = pj_eval(coupled, besta, lo)
62 var b: i64 = lo
63 while b <= hi { let f: i64 = pj_eval(coupled, besta, b); if f > bestfb { bestfb = f; bestb = b } b = b + 1 }
64 return pj_eval(coupled, besta, bestb)
65}
66
67func main() -> i64 {
68 let lo: i64 = 0
69 let hi: i64 = 10
70
71 let cj: i64 = pj_joint(1, lo, hi) // coupled, joint sweep
72 let cg: i64 = pj_greedy(1, lo, hi) // coupled, greedy
73 let sj: i64 = pj_joint(0, lo, hi) // separable, joint sweep
74 let sg: i64 = pj_greedy(0, lo, hi) // separable, greedy
75
76 var joint_wins: i64 = 0
77 if cj > cg { joint_wins = 1 }
78 var no_false_win: i64 = 0
79 if sj == sg { no_false_win = 1 }
80
81 pj_w(1, "PRMJOINT param=procgen_coupled_knobs coupled_joint=" as *u8); pj_n(1, cj)
82 pj_w(1, " coupled_greedy=" as *u8); pj_n(1, cg)
83 pj_w(1, " joint_wins=" as *u8); pj_n(1, joint_wins)
84 pj_w(1, " sep_joint=" as *u8); pj_n(1, sj)
85 pj_w(1, " sep_greedy=" as *u8); pj_n(1, sg)
86 pj_w(1, " no_false_win=" as *u8); pj_n(1, no_false_win)
87
88 var ok: i64 = 1
89 if joint_wins != 1 { ok = 0 }
90 if no_false_win != 1 { ok = 0 }
91 if cj != 100 { ok = 0 }
92 if cg != 10 { ok = 0 }
93 if ok == 1 { pj_w(1, " verdict=GREEN\n" as *u8); return 0 }
94 pj_w(1, " verdict=RED\n" as *u8)
95 return 1
96}