File size: 1,438 Bytes
d96a847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# file: agent_system/brain_optimized.py
import numpy as np
from scipy.optimize import minimize

def objective_function(weights, scores):
    """Minimize negative reward (Maximize Attention Score)"""
    return -np.dot(weights, scores)

def constraint_cost(weights, forces, regime_tags):
    """The 'Field' as a Cost Function"""
    penalty = 0
    for i, f in enumerate(forces):
        # Penalty for 'Momentum' during 'Bear' (Falling Knife Cost)
        if "momentum" in f.tags and "bear_trend" in regime_tags:
            penalty += weights[i] * 5.0  # Heavy penalty
            
        # Penalty for 'Aggressive' during 'High Vol'
        if "aggressive" in f.tags and "high_vol" in regime_tags:
            penalty += weights[i] * 10.0
            
    return penalty

def solve_allocation(query_scores, forces, regime_tags):
    n = len(forces)
    
    # 1. Total objective: Maximize Attention - Minimize Constraint Cost
    def total_cost(w):
        reward = np.dot(w, query_scores)
        penalty = constraint_cost(w, forces, regime_tags)
        return -(reward - penalty) # Minimize negative net utility

    # 2. Constraints: Weights must sum to 1, and be between 0 and 1
    cons = ({'type': 'eq', 'fun': lambda w: np.sum(w) - 1.0})
    bounds = [(0, 1) for _ in range(n)]
    
    # 3. Initial guess
    init_w = np.ones(n) / n
    
    res = minimize(total_cost, init_w, bounds=bounds, constraints=cons)
    return res.x