code wiki / _hdl_build / nx_builder_synth.nx

nx_builder_synth.nx source

↩ module page · 54 lines · 2988 B

1// nx_builder_synth.nx -- the BUILDER's SYNTHESIS engine: the team AUTHORS solutions to a spec by 2// SEARCH, instead of Claude hand-coding each answer (operator: "build the team to build the 3// capabilities, not just you doing it"; the test is "who wrote the answer?"). Given only a SPEC -- 4// an objective to minimize over a bit-allocation -- the Builder runs a GENERIC local search 5// (hill-climb over bit-transfer moves) and DISCOVERS the optimal allocation itself. It is handed 6// the objective, never the answer. Because the search is objective-agnostic, the team can author 7// allocations for ANY spec (importance-weighted today; perception-weighted, energy-weighted next) -- 8// that is a capability-to-build, not one artifact. The ENGINEER then verifies the searched solution 9// beats the naive baseline; the CRITIC evaluates; the Librarian banks. RACI: Builder authors-by- 10// search, Engineer verifies. license_tier: ORIGINAL Refs: mechanizable-invention doctrine; superopt search. 11 12import "nx_imatrix.nx" // imat_weighted_error (the SPEC/objective), imat_uniform, imat_total_bits 13 14// the Builder AUTHORS a bit allocation by hill-climbing the objective: from uniform, repeatedly make 15// the single bit-transfer (move one bit from group i to group j) that most reduces the objective, 16// until no transfer improves it. The team discovers the optimum; nobody hands it the formula. 17func bsy_author_alloc(n: i64, imp: *i64, budget: i64, out: *i64) -> i64 { 18 imat_uniform(n, budget, out) 19 var steps: i64 = 0 20 var improving: i64 = 1 21 while improving == 1 { 22 improving = 0 23 let cur: i64 = imat_weighted_error(n, imp, out) 24 var besti: i64 = 0 - 1; var bestj: i64 = 0 - 1; var bestobj: i64 = cur 25 var i: i64 = 0 26 while i < n { 27 if out[i] > 0 { 28 var j: i64 = 0 29 while j < n { 30 if j != i { 31 out[i] = out[i] - 1; out[j] = out[j] + 1 32 let o: i64 = imat_weighted_error(n, imp, out) 33 out[i] = out[i] + 1; out[j] = out[j] - 1 // revert the trial 34 if o < bestobj { bestobj = o; besti = i; bestj = j } 35 } 36 j = j + 1 37 } 38 } 39 i = i + 1 40 } 41 if besti >= 0 { out[besti] = out[besti] - 1; out[bestj] = out[bestj] + 1; improving = 1; steps = steps + 1 } 42 } 43 return steps 44} 45 46// the ENGINEER's verification: did the team's searched solution (a) keep the budget and (b) strictly 47// beat the naive uniform baseline on the objective? Returns 1 if the authored solution is valid+better. 48func bsy_verify(n: i64, imp: *i64, budget: i64, authored: *i64) -> i64 { 49 if imat_total_bits(n, authored) != budget { return 0 } 50 let ub: *i64 = sys_mmap(8 * (n + 4)) as *i64 51 imat_uniform(n, budget, ub) 52 if imat_weighted_error(n, imp, authored) < imat_weighted_error(n, imp, ub) { return 1 } 53 return 0 54}