agent_system / main.py
janggi3's picture
Upload folder using huggingface_hub
d96a847 verified
Raw History Blame Contribute Delete
2.86 kB
#main.py Created by gihoon jang on 1/8/26.
# file: agent_system/main.py
import numpy as np
from core import Force, AssetAllocation, Portfolio
from regime import RegimeDetector
from brain import AgentBrain # MPCBoltzmannBrain
from execution import ExecutionAgent
def generate_synthetic_forces(num=10):
"""Factory to create fake strategies for the PoC"""
forces = []
t = np.linspace(0, 10, 60)
for i in range(num):
# Create either Momentum or Mean Reversion strategies
is_mom = np.random.random() > 0.5
if is_mom:
name = f"Trend_Strat_{i}"
tags = {"momentum", "aggressive"}
# Sine wave (Trend)
ret = np.sin(t) + np.random.normal(0, 0.1, 60)
alloc = AssetAllocation({"BTC": 1.0})
else:
name = f"Reversion_Strat_{i}"
tags = {"mean_reversion", "chop_friendly"}
# Noise (Mean Rev)
ret = np.random.normal(0, 1, 60)
alloc = AssetAllocation({"ETH": 0.5, "USDT": 0.5})
forces.append(Force(str(i), name, tags, alloc, ret))
return forces
def run_system():
# 1. Init System
prices = {"BTC": 65000.0, "ETH": 3500.0, "USDT": 1.0}
# Simulate a Price History for Regime Detection (e.g., a massive crash)
price_history = np.linspace(50000, 30000, 100) # Downtrend
portfolio = Portfolio(100000, {"BTC": 0, "ETH": 0, "USDT": 100000})
portfolio.update_valuation(prices)
regime_det = RegimeDetector()
brain = AgentBrain()
executor = ExecutionAgent(portfolio)
# 2. Observe Regime
# (Since price_history is a crash, we expect 'bear_trend' or 'high_vol')
ctx = regime_det.detect(price_history)
print(f"🔍 REGIME DETECTED: {ctx.active_tags}")
# 3. Generate Strategy Space
forces = generate_synthetic_forces(10)
# 4. Formulate Intent (Query)
# The Agent perceives the crash and wants safety/hedges
# But let's say the Agent *wants* momentum.
# The Regime Mask should punish momentum strategies because of the crash context.
query = "Find me strong momentum strategies"
print(f"🗣️ AGENT QUERY: '{query}'")
# 5. Reasoning (Brain)
weights = brain.compute_attention(query, forces, ctx.active_tags)
# Debug: Show top pick
top_idx = np.argmax(weights)
print(f"🏆 TOP PICK: {forces[top_idx].name} (Weight: {weights[top_idx]:.2f})")
print(f" (Note: If this weight is low, the Regime Mask successfully suppressed the bad idea)")
#print(f"Force {f.name} tags: {f.tags}")
#print(f"{f.name}: dot={np.dot(q_vec, k_vec):.4f}, mask={mask_multiplier}, final={np.dot(q_vec, k_vec) * mask_multiplier:.4f}")
# 6. Act (Execution)
executor.execute_rebalance(forces, weights, prices)
if __name__ == "__main__":
run_system()