# Psyche Calculator ($\psi$): Emotional State Engine > **Superseded (2026-09-01):** replaced by §5 of the general spec. ψ is now two axes (valence, arousal) derived from the error stream; the chess features become an optional domain adapter. See `AILED_General_Specification.md`. ## Technical Specification for Dynamic Affective Modulation **Project Context:** AILED Organism **Module Type:** State Controller / Affective Feedback Loop **Mathematical Focus:** Non-linear compression and temporal reactivity. --- ## 1. Concept Overview The **Psyche Calculator** is responsible for translating the raw, discrete state of a chess board into a continuous, emotional scalar $\psi \in [-100, +100]$. Instead of treating chess pieces as mere values, this module treats them as sources of **tension** and **stability**. The resulting $\psi$ value represents the "psychological health" of the engine: - **$\psi \to +100$ (Euphoria/Overconfidence):** High material advantage, safe king, dominant center. - **$\psi \to 0$ (Neutrality/Equilibrium):** Balanced position. - **$\psi \to -100$ (Stress/Panic):** Material deficit, king under attack, positional collapse. --- ## 2. The Five Pillars of Tension The $\psi$ value is derived from five distinct factors ($F_i$), each with an assigned weight ($w_i$) representing its impact on the engine's "psyche". | Factor ($F_i$) | Weight ($w_i$) | Description | | :--- | :--- | :--- | | **Material** | 10 | Difference in piece values (P=1, N/B=3, R=5, Q=9). | | **King Safety** | 5 | Proximity of enemy attackers to the king and pawn shield integrity. | | **Attack Pressure** | 3 | Number of own pieces currently under direct attack. | | **Center Control** | 2 | Presence/Control of the classical central squares (d4, d5, e4, e5). | | **Mobility** | 1 | Relative delta in available legal moves. | --- ## 3. Mathematical Formalism The calculation is a two-stage process: **Aggregation** and **Temporal Integration**. ### 3.1. Aggregation (The Instantaneous State) First, we calculate the raw affective sum $\Psi_{raw}$ by normalizing factors into a $[-1, 1]$ range and applying weights. $$\Psi_{raw} = \sum_{i=1}^{5} w_i \cdot \text{norm}(F_i)$$ To ensure the value stays within a manageable range for the subsequent tanh-compression, we apply a scaling factor. ### 3.2. Temporal Integration (Reactivity & Decay) The psyche is not instantaneous; it has **inertia**. We use an Exponential Moving Average (EMA) to simulate the "emotional momentum". A sudden checkmate doesn't instantly move $\psi$ from 0 to -100 in one frame; it follows a rapid but continuous trajectory. $$\psi_t = (1 - \lambda) \cdot \psi_{t-1} + \lambda \cdot \text{Compress}(\Psi_{raw})$$ Where: - $\lambda \in [0, 1]$ is the **Reactivity** (high $\lambda$ = volatile/reactive; low $\lambda$ = stoic/stable). - $\text{Compress}(x) = 100 \cdot \tanh(x / 100)$ maps the sum to the $[-100, 100]$ range. --- ## 4. Minimal Implementation (Python + NumPy) This implementation uses `numpy` for efficient vectorization of the factor calculations. ```python import numpy as np class PsycheCalculator: """ A minimal implementation of the Psyche Calculator. Simulates the calculation of the affective state psi. """ def __init__(self, reactivity: float = 0.3): # Weights for Material, King Safety, Pressure, Center, Mobility self.weights = np.array([10.0, 5.0, 3.0, 2.0, 1.0]) self.reactivity = reactivity # Lambda (decay/momentum) self.psyche = 0.0 # Initial state (neutral) def _calculate_factors(self, board_state: dict) -> np.ndarray: """ Simulates extracting the 5 factors from the board. In a real engine, this would interface with the chess engine. """ # Returns normalized factors in range [-1, 1] # Order: [Material, KingSafety, Pressure, Center, Mobility] factors = np.array([ board_state.get('material', 0.0), board_state.get('king_safety', 0.0), board_state.get('pressure', 0.0), board_state.get('center', 0.0), board_state.get('mobility', 0.0) ]) return factors def update_psyche(self, board_state: dict) -> float: """ Updates the internal psyche state based on the new board state. """ # 1. Extract factors factors = self._calculate_factors(board_state) # 2. Aggregate weighted sum raw_sum = np.dot(self.weights, factors) # 3. Compress to [-100, 100] compressed = 100.0 * np.tanh(raw_sum / 100.0) # 4. Temporal Integration (EMA) self.psyche = (1 - self.reactivity) * self.psyche + (self.reactivity * compressed) return self.psyche # --- DEMO --- if __name__ == "__main__": calc = PsycheCalculator(reactivity=0.5) # Scenario 1: Stable/Winning Position winning_board = { 'material': 0.8, # Significant material advantage 'king_safety': 0.9, # Very safe 'pressure': -0.2, # Low pressure on self 'center': 0.7, # Controlling center 'mobility': 0.5 # Good mobility } # Scenario 2: Sudden Crisis (Check/Attack) crisis_board = { 'material': -0.5, # Lost material 'king_safety': -0.9, # King in danger 'pressure': 0.8, # High attack pressure 'center': -0.4, # Lost center 'mobility': -0.6 # Cramped } print("--- Psyche Evolution Simulation ---") print("\nStarting in Winning Position...") for _ in range(3): print(f"ψ = {calc.update_psyche(winning_board):.2f}") print("\nCRISIS DETECTED!") for _ in range(3): print(f"ψ = {calc.update_psyche(crisis_board):.2f}") ``` --- ## 5. Implementation Considerations * **Phase Awareness:** The weights $w_i$ should ideally be dynamic. In the **Opening**, material weight should be lower to allow for gambits. In the **Endgame**, king safety weight should decrease as the king becomes an active piece. * **Numerical Stability:** The use of `tanh` prevents the Psyche from exploding if a massive material advantage is suddenly captured. * **Complexity:** The calculation is $O(1)$ relative to board size (since it operates on a fixed number of factors), making it extremely lightweight. ```