|
| 1 | +"""Vibrational metabolism functions for THOL (Self-organization) operator. |
| 2 | +
|
| 3 | +Implements canonical pattern digestion: capturing external network signals |
| 4 | +and transforming them into internal structural reorganization (ΔNFR and sub-EPIs). |
| 5 | +
|
| 6 | +TNFR Canonical Principle |
| 7 | +------------------------- |
| 8 | +From "El pulso que nos atraviesa" (TNFR Manual, §2.2.10): |
| 9 | +
|
| 10 | + "THOL es el glifo de la autoorganización activa. No necesita intervención |
| 11 | + externa, ni programación, ni control — su función es reorganizar la forma |
| 12 | + desde dentro, en respuesta a la coherencia vibracional del campo." |
| 13 | +
|
| 14 | + "THOL no es una propiedad, es una dinámica. No es un atributo de lo vivo, |
| 15 | + es lo que hace que algo esté vivo. La autoorganización no es espontaneidad |
| 16 | + aleatoria, es resonancia estructurada desde el interior del nodo." |
| 17 | +
|
| 18 | +This module operationalizes vibrational metabolism: |
| 19 | +1. **Capture**: Sample network environment (EPI gradient, phase variance, coupling) |
| 20 | +2. **Metabolize**: Transform external patterns into internal structure (sub-EPIs) |
| 21 | +3. **Integrate**: Sub-EPIs reflect both internal acceleration AND network context |
| 22 | +
|
| 23 | +Metabolic Formula |
| 24 | +----------------- |
| 25 | +sub-EPI = base_internal + network_contribution + complexity_bonus |
| 26 | +
|
| 27 | +Where: |
| 28 | +- base_internal: parent_epi * scaling_factor (internal bifurcation) |
| 29 | +- network_contribution: epi_gradient * weight (external pressure) |
| 30 | +- complexity_bonus: phase_variance * weight (field complexity) |
| 31 | +""" |
| 32 | + |
| 33 | +from __future__ import annotations |
| 34 | + |
| 35 | +import math |
| 36 | +from typing import TYPE_CHECKING, Any |
| 37 | + |
| 38 | +if TYPE_CHECKING: |
| 39 | + from ..types import NodeId, TNFRGraph |
| 40 | + |
| 41 | +from ..alias import get_attr |
| 42 | +from ..constants.aliases import ALIAS_DNFR, ALIAS_EPI, ALIAS_THETA, ALIAS_VF |
| 43 | +from ..utils import get_numpy |
| 44 | + |
| 45 | +__all__ = [ |
| 46 | + "capture_network_signals", |
| 47 | + "metabolize_signals_into_subepi", |
| 48 | +] |
| 49 | + |
| 50 | + |
| 51 | +def capture_network_signals(G: TNFRGraph, node: NodeId) -> dict[str, Any] | None: |
| 52 | + """Capture external vibrational patterns from coupled neighbors. |
| 53 | +
|
| 54 | + This function implements the "perception" phase of THOL's vibrational metabolism. |
| 55 | + It samples the network environment around the target node, computing structural |
| 56 | + gradients, phase variance, and coupling strength. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + G : TNFRGraph |
| 61 | + Graph containing the node and its network context |
| 62 | + node : NodeId |
| 63 | + Node performing metabolic capture |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + dict | None |
| 68 | + Network signal structure containing: |
| 69 | + - epi_gradient: Difference between mean neighbor EPI and node EPI |
| 70 | + - phase_variance: Variance of neighbor phases (instability indicator) |
| 71 | + - neighbor_count: Number of coupled neighbors |
| 72 | + - coupling_strength_mean: Average phase alignment with neighbors |
| 73 | + - mean_neighbor_epi: Mean EPI value of neighbors |
| 74 | + Returns None if node has no neighbors (isolated metabolism). |
| 75 | +
|
| 76 | + Notes |
| 77 | + ----- |
| 78 | + TNFR Principle: THOL doesn't operate in vacuum—it metabolizes the network's |
| 79 | + vibrational field. EPI gradient represents "structural pressure" from environment. |
| 80 | + Phase variance indicates "complexity" of external patterns to digest. |
| 81 | +
|
| 82 | + Examples |
| 83 | + -------- |
| 84 | + >>> # Node with coherent neighbors (low variance) |
| 85 | + >>> signals = capture_network_signals(G, node) |
| 86 | + >>> signals["phase_variance"] # Low = stable field |
| 87 | + 0.02 |
| 88 | +
|
| 89 | + >>> # Node in dissonant field (high variance) |
| 90 | + >>> signals = capture_network_signals(G_dissonant, node) |
| 91 | + >>> signals["phase_variance"] # High = complex field |
| 92 | + 0.45 |
| 93 | + """ |
| 94 | + np = get_numpy() |
| 95 | + |
| 96 | + neighbors = list(G.neighbors(node)) |
| 97 | + if not neighbors: |
| 98 | + return None |
| 99 | + |
| 100 | + node_epi = float(get_attr(G.nodes[node], ALIAS_EPI, 0.0)) |
| 101 | + node_theta = float(get_attr(G.nodes[node], ALIAS_THETA, 0.0)) |
| 102 | + |
| 103 | + # Aggregate neighbor states |
| 104 | + neighbor_epis = [] |
| 105 | + neighbor_thetas = [] |
| 106 | + coupling_strengths = [] |
| 107 | + |
| 108 | + for n in neighbors: |
| 109 | + n_epi = float(get_attr(G.nodes[n], ALIAS_EPI, 0.0)) |
| 110 | + n_theta = float(get_attr(G.nodes[n], ALIAS_THETA, 0.0)) |
| 111 | + |
| 112 | + neighbor_epis.append(n_epi) |
| 113 | + neighbor_thetas.append(n_theta) |
| 114 | + |
| 115 | + # Coupling strength based on phase alignment |
| 116 | + phase_diff = abs(n_theta - node_theta) |
| 117 | + # Normalize to [0, π] |
| 118 | + if phase_diff > math.pi: |
| 119 | + phase_diff = 2 * math.pi - phase_diff |
| 120 | + coupling_strength = 1.0 - (phase_diff / math.pi) |
| 121 | + coupling_strengths.append(coupling_strength) |
| 122 | + |
| 123 | + # Compute structural gradients |
| 124 | + mean_neighbor_epi = float(np.mean(neighbor_epis)) |
| 125 | + epi_gradient = mean_neighbor_epi - node_epi |
| 126 | + |
| 127 | + # Phase variance (complexity/dissonance indicator) |
| 128 | + phase_variance = float(np.var(neighbor_thetas)) |
| 129 | + |
| 130 | + # Mean coupling strength |
| 131 | + coupling_strength_mean = float(np.mean(coupling_strengths)) |
| 132 | + |
| 133 | + return { |
| 134 | + "epi_gradient": epi_gradient, |
| 135 | + "phase_variance": phase_variance, |
| 136 | + "neighbor_count": len(neighbors), |
| 137 | + "coupling_strength_mean": coupling_strength_mean, |
| 138 | + "mean_neighbor_epi": mean_neighbor_epi, |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | +def metabolize_signals_into_subepi( |
| 143 | + parent_epi: float, |
| 144 | + signals: dict[str, Any] | None, |
| 145 | + d2_epi: float, |
| 146 | + scaling_factor: float = 0.25, |
| 147 | + gradient_weight: float = 0.15, |
| 148 | + complexity_weight: float = 0.10, |
| 149 | +) -> float: |
| 150 | + """Transform external signals into sub-EPI structure through metabolism. |
| 151 | +
|
| 152 | + This function implements the "digestion" phase of THOL's vibrational metabolism. |
| 153 | + It combines internal acceleration (d²EPI/dt²) with external network pressure |
| 154 | + to compute the magnitude of emergent sub-EPI. |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + parent_epi : float |
| 159 | + Current EPI magnitude of parent node |
| 160 | + signals : dict | None |
| 161 | + Network signals captured from environment (from capture_network_signals). |
| 162 | + If None, falls back to internal bifurcation only. |
| 163 | + d2_epi : float |
| 164 | + Internal structural acceleration (∂²EPI/∂t²) |
| 165 | + scaling_factor : float, default 0.25 |
| 166 | + Canonical THOL sub-EPI scaling (0.25 = 25% of parent) |
| 167 | + gradient_weight : float, default 0.15 |
| 168 | + Weight for external EPI gradient contribution |
| 169 | + complexity_weight : float, default 0.10 |
| 170 | + Weight for phase variance complexity bonus |
| 171 | +
|
| 172 | + Returns |
| 173 | + ------- |
| 174 | + float |
| 175 | + Metabolized sub-EPI magnitude, bounded to [0, 1.0] |
| 176 | +
|
| 177 | + Notes |
| 178 | + ----- |
| 179 | + TNFR Metabolic Formula: |
| 180 | +
|
| 181 | + sub-EPI = base_internal + network_contribution + complexity_bonus |
| 182 | +
|
| 183 | + Where: |
| 184 | + - base_internal: parent_epi * scaling_factor (internal bifurcation) |
| 185 | + - network_contribution: epi_gradient * weight (external pressure) |
| 186 | + - complexity_bonus: phase_variance * weight (field complexity) |
| 187 | +
|
| 188 | + This reflects canonical principle: "THOL reorganizes external experience |
| 189 | + into internal structure without external instruction" (Manual TNFR, p. 112). |
| 190 | +
|
| 191 | + Examples |
| 192 | + -------- |
| 193 | + >>> # Internal bifurcation only (isolated node) |
| 194 | + >>> metabolize_signals_into_subepi(0.60, None, d2_epi=0.15) |
| 195 | + 0.15 |
| 196 | +
|
| 197 | + >>> # Metabolizing network pressure |
| 198 | + >>> signals = {"epi_gradient": 0.20, "phase_variance": 0.10, ...} |
| 199 | + >>> metabolize_signals_into_subepi(0.60, signals, d2_epi=0.15) |
| 200 | + 0.21 # Enhanced by network context |
| 201 | + """ |
| 202 | + np = get_numpy() |
| 203 | + |
| 204 | + # Base: Internal bifurcation (existing behavior) |
| 205 | + base_sub_epi = parent_epi * scaling_factor |
| 206 | + |
| 207 | + # If isolated, return internal bifurcation only |
| 208 | + if signals is None: |
| 209 | + return float(np.clip(base_sub_epi, 0.0, 1.0)) |
| 210 | + |
| 211 | + # Network contribution: EPI gradient pressure |
| 212 | + network_contribution = signals["epi_gradient"] * gradient_weight |
| 213 | + |
| 214 | + # Complexity bonus: Phase variance indicates rich field to metabolize |
| 215 | + complexity_bonus = signals["phase_variance"] * complexity_weight |
| 216 | + |
| 217 | + # Combine internal + external |
| 218 | + metabolized_epi = base_sub_epi + network_contribution + complexity_bonus |
| 219 | + |
| 220 | + # Structural bounds [0, 1] |
| 221 | + return float(np.clip(metabolized_epi, 0.0, 1.0)) |
0 commit comments