code wiki / (root) / nx_dr_ocm.nx

nx_dr_ocm.nx source

↩ module page · 101 lines · 3723 B

1// nx_dr_ocm.nx -- SOVEREIGN Opportunity-Cost Model ranker for research vectors (DR-6). 2// The operator's "integrate opportunity cost analysis" made a live capability: rank 3// candidate research rungs by Priority = (Value * Momentum) / Cost, report the 4// OPPORTUNITY COST of each pick (the forgone best alternative), and do budget-aware 5// greedy selection (FutureWeaver arXiv:2512.11213: score-cheap-before-spending 6// g = geomean(self-consistency, prior); only spend within the compute budget = h). 7// Composes the frontier ranking law (impact x maturity-gap x momentum) with an 8// explicit COST denominator. Integer, deterministic, bit-exact reproducible; imports 9// ONLY nx_syscalls = drift-immune. No hardware writes (Rule 26). 10// 11// module: nishi-core.research.dr_ocm 12// depends: nx_syscalls.nx 13// wired_status: LIBRARY (conductor dispatch = DR-4; cost inputs = nx_cost_decompose) 14// genealogy_id: opportunity_cost_econ + futureweaver_2026_budget_planner 15import "nx_syscalls.nx" 16 17// integer sqrt (Newton) -- for the FutureWeaver cheap pre-score g = geomean. 18func ocm_isqrt(n: i64) -> i64 { 19 if n < 2 { return n } 20 var x: i64 = n 21 var y: i64 = (x + 1) / 2 22 while y < x { x = y; y = (x + n / x) / 2 } 23 return x 24} 25 26// Priority = (value * momentum * 1000) / cost. Guard cost<1 -> 1 (a free rung is 27// maximally preferred). Higher V, higher M, LOWER C -> higher priority. 28func ocm_priority(value: i64, momentum: i64, cost: i64) -> i64 { 29 var c: i64 = cost 30 if c < 1 { c = 1 } 31 return (value * momentum * 1000) / c 32} 33 34// FutureWeaver cheap pre-score g(alpha) = geomean(self-consistency, prior). 35func ocm_gscore(selfcons: i64, prior: i64) -> i64 { 36 return ocm_isqrt(selfcons * prior) 37} 38 39// pr[i] = ocm_priority(v[i],m[i],c[i]). 40func ocm_fill_priorities(v: *i64, m: *i64, c: *i64, n: i64, pr: *i64) -> i64 { 41 var i: i64 = 0 42 while i < n { pr[i] = ocm_priority(v[i], m[i], c[i]); i = i + 1 } 43 return 0 44} 45 46// Rank ids by priority DESC (ties -> lower id). out_order[n]. 47func ocm_rank(pr: *i64, n: i64, out_order: *i64) -> i64 { 48 let used: *u8 = sys_mmap(n) 49 var i: i64 = 0 50 while i < n { used[i] = 0 as u8; i = i + 1 } 51 var r: i64 = 0 52 while r < n { 53 var best: i64 = 0 - 1 54 var j: i64 = 0 55 while j < n { 56 if used[j] == (0 as u8) { 57 if best < 0 { best = j } 58 else { if pr[j] > pr[best] { best = j } } 59 } 60 j = j + 1 61 } 62 out_order[r] = best 63 used[best] = 1 as u8 64 r = r + 1 65 } 66 return 0 67} 68 69// Opportunity cost per rank position: oc[r] = priority of the NEXT-best deferred 70// vector (the value forgone by taking order[r] now). Last position -> 0. 71func ocm_opp_cost(pr: *i64, order: *i64, n: i64, oc: *i64) -> i64 { 72 var r: i64 = 0 73 while r < n { 74 if r + 1 < n { oc[r] = pr[order[r + 1]] } else { oc[r] = 0 } 75 r = r + 1 76 } 77 return 0 78} 79 80// Budget-aware greedy selection in priority order (FutureWeaver budget-feasibility h): 81// pick a vector iff cumulative cost + its cost <= budget. sel[id]=1 if picked. 82// Returns count selected; out_spent[0] = total cost of the selected set. Greedy by 83// priority (NOT optimal knapsack -- declared, not silently optimal). 84func ocm_select(pr: *i64, c: *i64, order: *i64, n: i64, budget: i64, sel: *i64, out_spent: *i64) -> i64 { 85 var i: i64 = 0 86 while i < n { sel[i] = 0; i = i + 1 } 87 var spent: i64 = 0 88 var cnt: i64 = 0 89 var r: i64 = 0 90 while r < n { 91 let id: i64 = order[r] 92 if (spent + c[id]) <= budget { 93 sel[id] = 1 94 spent = spent + c[id] 95 cnt = cnt + 1 96 } 97 r = r + 1 98 } 99 out_spent[0] = spent 100 return cnt 101}