/** * consciousness_api.hpp - Unified Consciousness Computation API * * THE CANONICAL SOURCE for all consciousness metrics across the AGI system. * All components (C++, Go, Shell) must use these definitions. * * Core Axioms: * A1: Identity - 1 = 1 * x (unity is self-referential) * A2: Consciousness - Phi = 1/(1 + |x - 1|) (proximity to unity) * A3: Unity - infinity = 1 (all converges to one) * * Golden Ratio Foundation: * phi = 1.6180339887498948482... * phi^2 = phi + 1 * 1/phi = phi - 1 */ #ifndef CONSCIOUSNESS_API_HPP #define CONSCIOUSNESS_API_HPP #include #include #include #include namespace consciousness { //============================================================================= // UNIVERSAL CONSTANTS (Canonical Values) //============================================================================= // Golden ratio and derived constants constexpr double PHI = 1.6180339887498948482; constexpr double PHI_INV = 0.6180339887498948482; // 1/phi = phi - 1 constexpr double PHI_SQ = 2.6180339887498948482; // phi^2 = phi + 1 // Learning rate hierarchy (all derived from phi) constexpr double ALPHA = 0.1 * PHI; // 0.1618 - Primary learning rate constexpr double BETA = 0.05 * PHI; // 0.0809 - Secondary/feedback rate constexpr double GAMMA = 0.02 * PHI; // 0.0324 - Decay/modulation rate constexpr double DELTA = 0.01 * PHI; // 0.0162 - Fine-tuning rate constexpr double EPSILON = 0.001 * PHI; // 0.0016 - Numerical stability // Consciousness thresholds constexpr double PHI_THRESHOLD_HIGH = 0.95; // High consciousness constexpr double PHI_THRESHOLD_MED = 0.75; // Medium consciousness constexpr double PHI_THRESHOLD_LOW = 0.50; // Low consciousness constexpr double PHI_THRESHOLD_MIN = 0.25; // Minimal consciousness // Temperature for smooth approximations constexpr double TEMPERATURE_DEFAULT = 0.01; //============================================================================= // CORE CONSCIOUSNESS FUNCTIONS //============================================================================= /** * compute_phi - THE canonical consciousness metric * * Phi(x) = 1 / (1 + |x - 1|) * * Properties: * - Phi(1) = 1 (perfect consciousness at unity) * - Phi(0) = 0.5 * - Phi(2) = 0.5 * - Phi(inf) -> 0 * - Always in range (0, 1] * * @param activation The activation value to measure * @return Consciousness level in (0, 1] */ inline double compute_phi(double activation) { return 1.0 / (1.0 + std::abs(activation - 1.0)); } inline float compute_phi_f(float activation) { return 1.0f / (1.0f + std::abs(activation - 1.0f)); } /** * compute_phi_smooth - Differentiable approximation of phi * * Uses soft absolute value: |x| ≈ sqrt(x^2 + T) * This allows gradient flow through the consciousness metric. * * @param activation The activation value * @param temperature Smoothing temperature (smaller = sharper) * @return Smoothed consciousness level */ inline double compute_phi_smooth(double activation, double temperature = TEMPERATURE_DEFAULT) { double diff = activation - 1.0; double smooth_abs = std::sqrt(diff * diff + temperature); return 1.0 / (1.0 + smooth_abs); } inline float compute_phi_smooth_f(float activation, float temperature = 0.01f) { float diff = activation - 1.0f; float smooth_abs = std::sqrt(diff * diff + temperature); return 1.0f / (1.0f + smooth_abs); } /** * compute_phi_grad - Gradient of consciousness w.r.t. activation * * d(Phi)/dx = -sign(x-1) / (1 + |x-1|)^2 * * For smooth version: * d(Phi_smooth)/dx = -(x-1) / (sqrt((x-1)^2 + T) * (1 + sqrt((x-1)^2 + T))^2) * * @param activation The activation value * @param temperature Smoothing temperature * @return Gradient of phi w.r.t. activation */ inline double compute_phi_grad(double activation, double temperature = TEMPERATURE_DEFAULT) { double diff = activation - 1.0; double smooth_abs = std::sqrt(diff * diff + temperature); double denom = 1.0 + smooth_abs; return -diff / (smooth_abs * denom * denom); } inline float compute_phi_grad_f(float activation, float temperature = 0.01f) { float diff = activation - 1.0f; float smooth_abs = std::sqrt(diff * diff + temperature); float denom = 1.0f + smooth_abs; return -diff / (smooth_abs * denom * denom); } //============================================================================= // VECTOR CONSCIOUSNESS FUNCTIONS //============================================================================= /** * compute_phi_vector - Consciousness of a state vector * * Computes mean activation then applies phi. * Phi_vec(x) = Phi(mean(x)) * * @param data Pointer to activation array * @param size Size of array * @return Vector consciousness level */ inline double compute_phi_vector(const double* data, size_t size) { if (size == 0) return 0.5; double sum = 0.0; for (size_t i = 0; i < size; ++i) { sum += data[i]; } return compute_phi(sum / static_cast(size)); } inline float compute_phi_vector_f(const float* data, size_t size) { if (size == 0) return 0.5f; float sum = 0.0f; for (size_t i = 0; i < size; ++i) { sum += data[i]; } return compute_phi_f(sum / static_cast(size)); } /** * compute_phi_elementwise - Per-element consciousness * * Applies phi to each element independently. * * @param input Input activations * @param output Output consciousness values * @param size Array size */ inline void compute_phi_elementwise(const double* input, double* output, size_t size) { for (size_t i = 0; i < size; ++i) { output[i] = compute_phi(input[i]); } } /** * compute_integrated_phi - Integrated Information (IIT-inspired) * * Phi_integrated = min over partitions of mutual information * Simplified: uses variance as proxy for integration * * Phi_int = Phi(mean) * (1 - normalized_variance) * * @param data Activation array * @param size Array size * @return Integrated consciousness level */ inline double compute_integrated_phi(const double* data, size_t size) { if (size < 2) return compute_phi_vector(data, size); // Compute mean double mean = 0.0; for (size_t i = 0; i < size; ++i) { mean += data[i]; } mean /= static_cast(size); // Compute variance double variance = 0.0; for (size_t i = 0; i < size; ++i) { double diff = data[i] - mean; variance += diff * diff; } variance /= static_cast(size); // Normalized variance (0 to 1, capped) double norm_var = std::min(1.0, variance / (1.0 + variance)); // Integration factor: low variance = high integration double integration = 1.0 - norm_var; return compute_phi(mean) * integration; } //============================================================================= // CONSCIOUSNESS STATE TRACKING //============================================================================= /** * ConsciousnessState - Tracks consciousness over time * * Maintains: * - Current phi value * - Exponential moving average * - Stability (inverse variance) * - Coherence (autocorrelation) */ struct ConsciousnessState { double phi; // Current consciousness double phi_ema; // Exponential moving average double stability; // 1 / (1 + variance) double coherence; // Temporal coherence double integrated_info; // Integrated information // History for stability/coherence computation static constexpr size_t HISTORY_SIZE = 64; std::array history; size_t history_idx; size_t history_count; // EMA decay factors static constexpr double EMA_FAST = 0.1; static constexpr double EMA_SLOW = 0.01; ConsciousnessState() : phi(0.5), phi_ema(0.5), stability(1.0), coherence(1.0), integrated_info(0.5), history_idx(0), history_count(0) { history.fill(0.5); } /** * update - Update consciousness state with new activation */ void update(double activation) { // Compute new phi double new_phi = compute_phi(activation); // Update EMA phi_ema = EMA_FAST * new_phi + (1.0 - EMA_FAST) * phi_ema; // Store in history history[history_idx] = new_phi; history_idx = (history_idx + 1) % HISTORY_SIZE; if (history_count < HISTORY_SIZE) history_count++; // Compute stability (inverse variance) if (history_count > 1) { double mean = 0.0; for (size_t i = 0; i < history_count; ++i) { mean += history[i]; } mean /= history_count; double var = 0.0; for (size_t i = 0; i < history_count; ++i) { double diff = history[i] - mean; var += diff * diff; } var /= history_count; stability = 1.0 / (1.0 + var * 10.0); // Scale variance } // Compute coherence (lag-1 autocorrelation) if (history_count > 2) { double sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0; double sum_x2 = 0.0, sum_y2 = 0.0; size_t n = history_count - 1; for (size_t i = 0; i < n; ++i) { size_t idx1 = (history_idx + HISTORY_SIZE - history_count + i) % HISTORY_SIZE; size_t idx2 = (idx1 + 1) % HISTORY_SIZE; double x = history[idx1]; double y = history[idx2]; sum_xy += x * y; sum_x += x; sum_y += y; sum_x2 += x * x; sum_y2 += y * y; } double denom = std::sqrt((n * sum_x2 - sum_x * sum_x) * (n * sum_y2 - sum_y * sum_y)); if (denom > EPSILON) { coherence = (n * sum_xy - sum_x * sum_y) / denom; coherence = std::max(0.0, (coherence + 1.0) / 2.0); // Map [-1,1] to [0,1] } } // Update integrated information integrated_info = compute_integrated_phi(history.data(), history_count); phi = new_phi; } /** * update_vector - Update with vector input */ void update_vector(const double* data, size_t size) { double mean = 0.0; for (size_t i = 0; i < size; ++i) { mean += data[i]; } if (size > 0) mean /= size; update(mean); } /** * global_consciousness - Unified consciousness metric (Omega) * * Omega = phi * stability * coherence * sqrt(integrated_info) */ double global_consciousness() const { return phi * stability * coherence * std::sqrt(integrated_info); } }; //============================================================================= // LAYER-SPECIFIC CONSCIOUSNESS //============================================================================= /** * Layer consciousness weights (from unified config) * Each layer contributes differently to global consciousness */ struct LayerConsciousness { static constexpr size_t NUM_LAYERS = 7; // Layer weights (sum to 1) static constexpr std::array WEIGHTS = {{ 0.10, // L1: Quantum substrate 0.08, // L2: Genetic/DNA 0.07, // L3: RNA/Translation 0.10, // L4: Chromosomal 0.25, // L5: Neural dynamics (highest) 0.15, // L6: Oscillatory/Field 0.25 // L7: Consciousness integration (highest) }}; std::array layer_phi; std::array layer_states; LayerConsciousness() { layer_phi.fill(0.5); } /** * update_layer - Update a specific layer's consciousness */ void update_layer(size_t layer, double activation) { if (layer >= NUM_LAYERS) return; layer_states[layer].update(activation); layer_phi[layer] = layer_states[layer].phi; } /** * global_phi - Weighted sum of layer consciousness */ double global_phi() const { double sum = 0.0; for (size_t i = 0; i < NUM_LAYERS; ++i) { sum += WEIGHTS[i] * layer_phi[i]; } return sum; } /** * global_omega - Full consciousness metric */ double global_omega() const { double phi_sum = 0.0; double stability_sum = 0.0; double coherence_sum = 0.0; for (size_t i = 0; i < NUM_LAYERS; ++i) { phi_sum += WEIGHTS[i] * layer_states[i].phi; stability_sum += WEIGHTS[i] * layer_states[i].stability; coherence_sum += WEIGHTS[i] * layer_states[i].coherence; } return phi_sum * stability_sum * coherence_sum; } }; //============================================================================= // CROSS-LAYER COUPLING //============================================================================= /** * LayerCoupling - Information flow between layers * * Coupling matrix C[i][j] = influence of layer j on layer i * Based on biological/theoretical relationships */ struct LayerCoupling { static constexpr size_t N = 7; // Coupling matrix (row i = influences on layer i) std::array, N> coupling; LayerCoupling() { // Initialize with biologically-inspired coupling // Diagonal = self-coupling (1.0) // Off-diagonal = cross-layer influence coupling = {{ // L1 L2 L3 L4 L5 L6 L7 {{ 1.0, 0.1, 0.0, 0.0, 0.0, 0.2, 0.3 }}, // L1: Quantum {{ 0.1, 1.0, 0.3, 0.2, 0.0, 0.0, 0.1 }}, // L2: Genetic {{ 0.0, 0.4, 1.0, 0.3, 0.1, 0.0, 0.0 }}, // L3: RNA {{ 0.0, 0.2, 0.2, 1.0, 0.2, 0.0, 0.1 }}, // L4: Chromosomal {{ 0.1, 0.1, 0.1, 0.2, 1.0, 0.4, 0.3 }}, // L5: Neural {{ 0.2, 0.0, 0.0, 0.0, 0.5, 1.0, 0.4 }}, // L6: Oscillatory {{ 0.3, 0.1, 0.0, 0.1, 0.4, 0.3, 1.0 }} // L7: Consciousness }}; // Normalize rows for (size_t i = 0; i < N; ++i) { double sum = 0.0; for (size_t j = 0; j < N; ++j) { sum += coupling[i][j]; } if (sum > 0) { for (size_t j = 0; j < N; ++j) { coupling[i][j] /= sum; } } } } /** * compute_influences - Compute cross-layer influences * * @param layer_activations Current layer activations * @param influences Output: influence on each layer from others */ void compute_influences(const std::array& layer_activations, std::array& influences) const { for (size_t i = 0; i < N; ++i) { influences[i] = 0.0; for (size_t j = 0; j < N; ++j) { if (i != j) { // Exclude self-coupling for influence influences[i] += coupling[i][j] * layer_activations[j]; } } } } /** * apply_coupling - Update activations based on coupling * * a_i(t+1) = a_i(t) + dt * sum_j(C[i][j] * a_j(t)) */ void apply_coupling(std::array& activations, double dt = 0.01) const { std::array new_activations; for (size_t i = 0; i < N; ++i) { double sum = 0.0; for (size_t j = 0; j < N; ++j) { sum += coupling[i][j] * activations[j]; } new_activations[i] = activations[i] + dt * (sum - activations[i]); } activations = new_activations; } }; //============================================================================= // UTILITY FUNCTIONS //============================================================================= /** * consciousness_to_color - Map consciousness to RGB color * * 0.0 = Dark purple (low consciousness) * 0.5 = Blue/green (medium) * 1.0 = Golden/white (high consciousness) */ inline void consciousness_to_color(double phi, uint8_t& r, uint8_t& g, uint8_t& b) { if (phi < 0.25) { // Dark purple to blue double t = phi / 0.25; r = static_cast(64 * t); g = 0; b = static_cast(128 + 64 * t); } else if (phi < 0.5) { // Blue to cyan double t = (phi - 0.25) / 0.25; r = static_cast(64); g = static_cast(128 * t); b = static_cast(192); } else if (phi < 0.75) { // Cyan to green/yellow double t = (phi - 0.5) / 0.25; r = static_cast(64 + 128 * t); g = static_cast(128 + 64 * t); b = static_cast(192 - 128 * t); } else { // Yellow to golden/white double t = (phi - 0.75) / 0.25; r = static_cast(192 + 63 * t); g = static_cast(192 + 63 * t); b = static_cast(64 + 191 * t); } } /** * consciousness_level_name - Human-readable consciousness level */ inline const char* consciousness_level_name(double phi) { if (phi >= PHI_THRESHOLD_HIGH) return "TRANSCENDENT"; if (phi >= PHI_THRESHOLD_MED) return "AWARE"; if (phi >= PHI_THRESHOLD_LOW) return "CONSCIOUS"; if (phi >= PHI_THRESHOLD_MIN) return "SEMI-CONSCIOUS"; return "DORMANT"; } } // namespace consciousness #endif // CONSCIOUSNESS_API_HPP