code wiki / (root) / nx_caplab_market.nx

nx_caplab_market.nx source

↩ module page · 67 lines · 2658 B

1// nx_caplab_market.nx -- Capitalism Lab R0: the single-firm market atom. 2// 3// The economic "atom" of the capitalism sim: ONE firm sells ONE product 4// into ONE market. Demand falls linearly with price; profit per unit is 5// (price - unit_cost). Everything above this rung -- multi-firm 6// competition, supply chains, the tick loop, multiplayer -- composes from 7// this one tick. All integer math (currency + quantity in whole units) so 8// the gate is bit-exact and reproducible; fixed-point elasticity is a later 9// rung, not an R0 dependency. 10// 11// MODEL: 12// demand(p) = max(0, a - b*p) a = max units demanded at price 0 13// b = units lost per unit of price 14// revenue(p) = p * demand(p) 15// profit(p) = (p - unit_cost) * demand(p) 16// optimal = argmax_p profit(p) (monopoly price; deterministic scan) 17// 18// nx_safety_envelope: 19// intended_use: capitalism-lab economic core (pure functions, no I/O) 20// sil_target: SIL1 21// verdict: NOT_YET_EVALUATED 22// 23// genealogy_id: capitalism_lab_market_canon 24// lineage_id: nx_caplab_market_v1 25 26import "nx_syscalls.nx" 27import "nx_tier.nx" 28 29// Linear demand curve, clamped at zero (no negative quantities sold). 30func nx_clab_demand(a: nx_int, b: nx_int, price: nx_int) -> nx_int { 31 let q: nx_int = a - (b * price) 32 if q < 0 { return 0 } 33 return q 34} 35 36func nx_clab_revenue(a: nx_int, b: nx_int, price: nx_int) -> nx_int { 37 return price * nx_clab_demand(a, b, price) 38} 39 40func nx_clab_cost(a: nx_int, b: nx_int, price: nx_int, unit_cost: nx_int) -> nx_int { 41 return unit_cost * nx_clab_demand(a, b, price) 42} 43 44// Profit = (price - unit_cost) * quantity sold. Goes negative when the 45// firm prices below its unit cost -- the lab must let players lose money. 46func nx_clab_profit(a: nx_int, b: nx_int, price: nx_int, unit_cost: nx_int) -> nx_int { 47 return (price - unit_cost) * nx_clab_demand(a, b, price) 48} 49 50// Monopoly profit-maximizing price: scan integer prices from 0 until demand 51// chokes to zero, returning the price with the greatest profit. Pure and 52// deterministic, no floating point. This is the seed of the lab's "what 53// price should I set?" advisor that later rungs grow into an AI opponent. 54func nx_clab_optimal_price(a: nx_int, b: nx_int, unit_cost: nx_int) -> nx_int { 55 var best_p: nx_int = 0 56 var best_profit: nx_int = nx_clab_profit(a, b, 0, unit_cost) 57 var p: nx_int = 1 58 while nx_clab_demand(a, b, p) > 0 { 59 let pr: nx_int = nx_clab_profit(a, b, p, unit_cost) 60 if pr > best_profit { 61 best_profit = pr 62 best_p = p 63 } 64 p = p + 1 65 } 66 return best_p 67}