Chronoequalibrium v5.8
#GødEngine
import numpy as np
import matplotlib.pyplot as plt
# -----------------------------
# GodEngine v5.9 Hybrid Benchmark
class GodEngine:
def __init__(self, N_agents, N_goods, seed=None, creative=False):
self.N = N_agents
self.M = N_goods
self.seed = seed
self.creative = creative
self.rng = np.random.default_rng(seed)
self.reset_state()
def reset_state(self):
self.preferences = self.rng.uniform(0.1, 1.0, size=(self.N, self.M))
self.budgets = np.ones(self.N)
self.prices = np.ones(self.M)
self.coherence_weights = np.ones(self.N) / self.N
self.welfare_history = []
self.price_history = []
def coherence_update(self):
"""UNL★ inspired coherence weighting"""
# Add minor noise for robustness
coherence_noise = self.rng.normal(0, 0.01, size=self.N)
self.coherence_weights = np.clip(self.coherence_weights 0.05 * coherence_noise, 0, None)
self.coherence_weights /= np.sum(self.coherence_weights)
def demand_allocation(self):
"""Vectorized proportional allocation with price sensitivity"""
demand = (self.budgets[:, None] * self.preferences) / self.prices[None, :]
# Scale if total demand exceeds supply
total_demand = np.sum(demand, axis=0)
oversupply = total_demand > self.M
if np.any(oversupply):
scaling = self.M / total_demand[oversupply]
demand[:, oversupply] *= scaling
return demand
def step(self, eta=0.05):
self.coherence_update()
demand = self.demand_allocation()
excess = np.sum(demand, axis=0) - self.M
self.prices *= np.exp(eta * excess)
self.prices = np.clip(self.prices, 0.01, None)
welfare = np.sum(self.coherence_weights[:, None] * demand)
self.welfare_history.append(welfare)
self.price_history.append(self.prices.copy())
if self.creative:
# Dr. Snuggles narrative boost (cosmetic/log)
narrative = f"[Dr. Snuggles] Step welfare: {welfare:.3f}, price avg: {np.mean(self.prices):.3f}"
print(narrative)
def run(self, steps=50, eta=0.05):
for _ in range(steps):
self.step(eta=eta)
# -----------------------------
# Benchmarking over multiple seeds
def multi_seed_benchmark(N_agents=10, N_goods=5, seeds=5, steps=50):
all_welfares = []
all_prices = []
for s in range(seeds):
ge = GodEngine(N_agents, N_goods, seed=s, creative=False)
ge.run(steps=steps)
all_welfares.append(ge.welfare_history)
all_prices.append(ge.price_history)
all_welfares = np.array(all_welfares)
all_prices = np.array(all_prices)
# Mean and std
welfare_mean = np.mean(all_welfares, axis=0)
welfare_std = np.std(all_welfares, axis=0)
price_mean = np.mean(all_prices, axis=0)
price_std = np.std(all_prices, axis=0)
# Plot results
plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(welfare_mean, label='GodEngine mean welfare')
plt.fill_between(np.arange(steps), welfare_mean-welfare_std, welfare_mean welfare_std, alpha=0.3)
plt.title("Multi-seed Welfare over Time")
plt.xlabel("Step")
plt.ylabel("Welfare")
plt.legend()
plt.subplot(1,2,2)
plt.plot(price_mean, label='Mean Prices')
plt.fill_between(np.arange(steps), price_mean-price_std, price_mean price_std, alpha=0.3)
plt.title("Multi-seed Prices over Time")
plt.xlabel("Step")
plt.ylabel("Price")
plt.legend()
plt.tight_layout()
plt.show()
print(f"Final mean welfare: {welfare_mean[-1]:.3f} ± {welfare_std[-1]:.3f}")
print(f"Final mean prices: {price_mean[-1]} ± {price_std[-1]}")
# -----------------------------
# Execute benchmark
if __name__ == "__main__":
multi_seed_benchmark(N_agents=10, N_goods=5, seeds=10, steps=50)