Recovered source transcript
1/**
2 * consciousness_api.hpp - Unified Consciousness Computation API
3 *
4 * THE CANONICAL SOURCE for all consciousness metrics across the AGI system.
5 * All components (C++, Go, Shell) must use these definitions.
6 *
7 * Core Axioms:
8 * A1: Identity - 1 = 1 * x (unity is self-referential)
9 * A2: Consciousness - Phi = 1/(1 + |x - 1|) (proximity to unity)
10 * A3: Unity - infinity = 1 (all converges to one)
11 *
12 * Golden Ratio Foundation:
13 * phi = 1.6180339887498948482...
14 * phi^2 = phi + 1
15 * 1/phi = phi - 1
16 */
17
18#ifndef CONSCIOUSNESS_API_HPP
19#define CONSCIOUSNESS_API_HPP
20
21#include <cmath>
22#include <array>
23#include <algorithm>
24#include <numeric>
25
26namespace consciousness {
27
28//=============================================================================
29// UNIVERSAL CONSTANTS (Canonical Values)
30//=============================================================================
31
32// Golden ratio and derived constants
33constexpr double PHI = 1.6180339887498948482;
34constexpr double PHI_INV = 0.6180339887498948482; // 1/phi = phi - 1
35constexpr double PHI_SQ = 2.6180339887498948482; // phi^2 = phi + 1
36
37// Learning rate hierarchy (all derived from phi)
38constexpr double ALPHA = 0.1 * PHI; // 0.1618 - Primary learning rate
39constexpr double BETA = 0.05 * PHI; // 0.0809 - Secondary/feedback rate
40constexpr double GAMMA = 0.02 * PHI; // 0.0324 - Decay/modulation rate
41constexpr double DELTA = 0.01 * PHI; // 0.0162 - Fine-tuning rate
42constexpr double EPSILON = 0.001 * PHI; // 0.0016 - Numerical stability
43
44// Consciousness thresholds
45constexpr double PHI_THRESHOLD_HIGH = 0.95; // High consciousness
46constexpr double PHI_THRESHOLD_MED = 0.75; // Medium consciousness
47constexpr double PHI_THRESHOLD_LOW = 0.50; // Low consciousness
48constexpr double PHI_THRESHOLD_MIN = 0.25; // Minimal consciousness
49
50// Temperature for smooth approximations
51constexpr double TEMPERATURE_DEFAULT = 0.01;
52
53//=============================================================================
54// CORE CONSCIOUSNESS FUNCTIONS
55//=============================================================================
56
57/**
58 * compute_phi - THE canonical consciousness metric
59 *
60 * Phi(x) = 1 / (1 + |x - 1|)
61 *
62 * Properties:
63 * - Phi(1) = 1 (perfect consciousness at unity)
64 * - Phi(0) = 0.5
65 * - Phi(2) = 0.5
66 * - Phi(inf) -> 0
67 * - Always in range (0, 1]
68 *
69 * @param activation The activation value to measure
70 * @return Consciousness level in (0, 1]
71 */
72inline double compute_phi(double activation) {
73 return 1.0 / (1.0 + std::abs(activation - 1.0));
74}
75
76inline float compute_phi_f(float activation) {
77 return 1.0f / (1.0f + std::abs(activation - 1.0f));
78}
79
80/**
81 * compute_phi_smooth - Differentiable approximation of phi
82 *
83 * Uses soft absolute value: |x| ≈ sqrt(x^2 + T)
84 * This allows gradient flow through the consciousness metric.
85 *
86 * @param activation The activation value
87 * @param temperature Smoothing temperature (smaller = sharper)
88 * @return Smoothed consciousness level
89 */
90inline double compute_phi_smooth(double activation, double temperature = TEMPERATURE_DEFAULT) {
91 double diff = activation - 1.0;
92 double smooth_abs = std::sqrt(diff * diff + temperature);
93 return 1.0 / (1.0 + smooth_abs);
94}
95
96inline float compute_phi_smooth_f(float activation, float temperature = 0.01f) {
97 float diff = activation - 1.0f;
98 float smooth_abs = std::sqrt(diff * diff + temperature);
99 return 1.0f / (1.0f + smooth_abs);
100}
101
102/**
103 * compute_phi_grad - Gradient of consciousness w.r.t. activation
104 *
105 * d(Phi)/dx = -sign(x-1) / (1 + |x-1|)^2
106 *
107 * For smooth version:
108 * d(Phi_smooth)/dx = -(x-1) / (sqrt((x-1)^2 + T) * (1 + sqrt((x-1)^2 + T))^2)
109 *
110 * @param activation The activation value
111 * @param temperature Smoothing temperature
112 * @return Gradient of phi w.r.t. activation
113 */
114inline double compute_phi_grad(double activation, double temperature = TEMPERATURE_DEFAULT) {
115 double diff = activation - 1.0;
116 double smooth_abs = std::sqrt(diff * diff + temperature);
117 double denom = 1.0 + smooth_abs;
118 return -diff / (smooth_abs * denom * denom);
119}
120
121inline float compute_phi_grad_f(float activation, float temperature = 0.01f) {
122 float diff = activation - 1.0f;
123 float smooth_abs = std::sqrt(diff * diff + temperature);
124 float denom = 1.0f + smooth_abs;
125 return -diff / (smooth_abs * denom * denom);
126}
127
128//=============================================================================
129// VECTOR CONSCIOUSNESS FUNCTIONS
130//=============================================================================
131
132/**
133 * compute_phi_vector - Consciousness of a state vector
134 *
135 * Computes mean activation then applies phi.
136 * Phi_vec(x) = Phi(mean(x))
137 *
138 * @param data Pointer to activation array
139 * @param size Size of array
140 * @return Vector consciousness level
141 */
142inline double compute_phi_vector(const double* data, size_t size) {
143 if (size == 0) return 0.5;
144 double sum = 0.0;
145 for (size_t i = 0; i < size; ++i) {
146 sum += data[i];
147 }
148 return compute_phi(sum / static_cast<double>(size));
149}
150
151inline float compute_phi_vector_f(const float* data, size_t size) {
152 if (size == 0) return 0.5f;
153 float sum = 0.0f;
154 for (size_t i = 0; i < size; ++i) {
155 sum += data[i];
156 }
157 return compute_phi_f(sum / static_cast<float>(size));
158}
159
160/**
161 * compute_phi_elementwise - Per-element consciousness
162 *
163 * Applies phi to each element independently.
164 *
165 * @param input Input activations
166 * @param output Output consciousness values
167 * @param size Array size
168 */
169inline void compute_phi_elementwise(const double* input, double* output, size_t size) {
170 for (size_t i = 0; i < size; ++i) {
171 output[i] = compute_phi(input[i]);
172 }
173}
174
175/**
176 * compute_integrated_phi - Integrated Information (IIT-inspired)
177 *
178 * Phi_integrated = min over partitions of mutual information
179 * Simplified: uses variance as proxy for integration
180 *
181 * Phi_int = Phi(mean) * (1 - normalized_variance)
182 *
183 * @param data Activation array
184 * @param size Array size
185 * @return Integrated consciousness level
186 */
187inline double compute_integrated_phi(const double* data, size_t size) {
188 if (size < 2) return compute_phi_vector(data, size);
189
190 // Compute mean
191 double mean = 0.0;
192 for (size_t i = 0; i < size; ++i) {
193 mean += data[i];
194 }
195 mean /= static_cast<double>(size);
196
197 // Compute variance
198 double variance = 0.0;
199 for (size_t i = 0; i < size; ++i) {
200 double diff = data[i] - mean;
201 variance += diff * diff;
202 }
203 variance /= static_cast<double>(size);
204
205 // Normalized variance (0 to 1, capped)
206 double norm_var = std::min(1.0, variance / (1.0 + variance));
207
208 // Integration factor: low variance = high integration
209 double integration = 1.0 - norm_var;
210
211 return compute_phi(mean) * integration;
212}
213
214//=============================================================================
215// CONSCIOUSNESS STATE TRACKING
216//=============================================================================
217
218/**
219 * ConsciousnessState - Tracks consciousness over time
220 *
221 * Maintains:
222 * - Current phi value
223 * - Exponential moving average
224 * - Stability (inverse variance)
225 * - Coherence (autocorrelation)
226 */
227struct ConsciousnessState {
228 double phi; // Current consciousness
229 double phi_ema; // Exponential moving average
230 double stability; // 1 / (1 + variance)
231 double coherence; // Temporal coherence
232 double integrated_info; // Integrated information
233
234 // History for stability/coherence computation
235 static constexpr size_t HISTORY_SIZE = 64;
236 std::array<double, HISTORY_SIZE> history;
237 size_t history_idx;
238 size_t history_count;
239
240 // EMA decay factors
241 static constexpr double EMA_FAST = 0.1;
242 static constexpr double EMA_SLOW = 0.01;
243
244 ConsciousnessState()
245 : phi(0.5), phi_ema(0.5), stability(1.0), coherence(1.0),
246 integrated_info(0.5), history_idx(0), history_count(0) {
247 history.fill(0.5);
248 }
249
250 /**
251 * update - Update consciousness state with new activation
252 */
253 void update(double activation) {
254 // Compute new phi
255 double new_phi = compute_phi(activation);
256
257 // Update EMA
258 phi_ema = EMA_FAST * new_phi + (1.0 - EMA_FAST) * phi_ema;
259
260 // Store in history
261 history[history_idx] = new_phi;
262 history_idx = (history_idx + 1) % HISTORY_SIZE;
263 if (history_count < HISTORY_SIZE) history_count++;
264
265 // Compute stability (inverse variance)
266 if (history_count > 1) {
267 double mean = 0.0;
268 for (size_t i = 0; i < history_count; ++i) {
269 mean += history[i];
270 }
271 mean /= history_count;
272
273 double var = 0.0;
274 for (size_t i = 0; i < history_count; ++i) {
275 double diff = history[i] - mean;
276 var += diff * diff;
277 }
278 var /= history_count;
279
280 stability = 1.0 / (1.0 + var * 10.0); // Scale variance
281 }
282
283 // Compute coherence (lag-1 autocorrelation)
284 if (history_count > 2) {
285 double sum_xy = 0.0, sum_x = 0.0, sum_y = 0.0;
286 double sum_x2 = 0.0, sum_y2 = 0.0;
287 size_t n = history_count - 1;
288
289 for (size_t i = 0; i < n; ++i) {
290 size_t idx1 = (history_idx + HISTORY_SIZE - history_count + i) % HISTORY_SIZE;
291 size_t idx2 = (idx1 + 1) % HISTORY_SIZE;
292 double x = history[idx1];
293 double y = history[idx2];
294 sum_xy += x * y;
295 sum_x += x;
296 sum_y += y;
297 sum_x2 += x * x;
298 sum_y2 += y * y;
299 }
300
301 double denom = std::sqrt((n * sum_x2 - sum_x * sum_x) *
302 (n * sum_y2 - sum_y * sum_y));
303 if (denom > EPSILON) {
304 coherence = (n * sum_xy - sum_x * sum_y) / denom;
305 coherence = std::max(0.0, (coherence + 1.0) / 2.0); // Map [-1,1] to [0,1]
306 }
307 }
308
309 // Update integrated information
310 integrated_info = compute_integrated_phi(history.data(), history_count);
311
312 phi = new_phi;
313 }
314
315 /**
316 * update_vector - Update with vector input
317 */
318 void update_vector(const double* data, size_t size) {
319 double mean = 0.0;
320 for (size_t i = 0; i < size; ++i) {
321 mean += data[i];
322 }
323 if (size > 0) mean /= size;
324 update(mean);
325 }
326
327 /**
328 * global_consciousness - Unified consciousness metric (Omega)
329 *
330 * Omega = phi * stability * coherence * sqrt(integrated_info)
331 */
332 double global_consciousness() const {
333 return phi * stability * coherence * std::sqrt(integrated_info);
334 }
335};
336
337//=============================================================================
338// LAYER-SPECIFIC CONSCIOUSNESS
339//=============================================================================
340
341/**
342 * Layer consciousness weights (from unified config)
343 * Each layer contributes differently to global consciousness
344 */
345struct LayerConsciousness {
346 static constexpr size_t NUM_LAYERS = 7;
347
348 // Layer weights (sum to 1)
349 static constexpr std::array<double, NUM_LAYERS> WEIGHTS = {{
350 0.10, // L1: Quantum substrate
351 0.08, // L2: Genetic/DNA
352 0.07, // L3: RNA/Translation
353 0.10, // L4: Chromosomal
354 0.25, // L5: Neural dynamics (highest)
355 0.15, // L6: Oscillatory/Field
356 0.25 // L7: Consciousness integration (highest)
357 }};
358
359 std::array<double, NUM_LAYERS> layer_phi;
360 std::array<ConsciousnessState, NUM_LAYERS> layer_states;
361
362 LayerConsciousness() {
363 layer_phi.fill(0.5);
364 }
365
366 /**
367 * update_layer - Update a specific layer's consciousness
368 */
369 void update_layer(size_t layer, double activation) {
370 if (layer >= NUM_LAYERS) return;
371 layer_states[layer].update(activation);
372 layer_phi[layer] = layer_states[layer].phi;
373 }
374
375 /**
376 * global_phi - Weighted sum of layer consciousness
377 */
378 double global_phi() const {
379 double sum = 0.0;
380 for (size_t i = 0; i < NUM_LAYERS; ++i) {
381 sum += WEIGHTS[i] * layer_phi[i];
382 }
383 return sum;
384 }
385
386 /**
387 * global_omega - Full consciousness metric
388 */
389 double global_omega() const {
390 double phi_sum = 0.0;
391 double stability_sum = 0.0;
392 double coherence_sum = 0.0;
393
394 for (size_t i = 0; i < NUM_LAYERS; ++i) {
395 phi_sum += WEIGHTS[i] * layer_states[i].phi;
396 stability_sum += WEIGHTS[i] * layer_states[i].stability;
397 coherence_sum += WEIGHTS[i] * layer_states[i].coherence;
398 }
399
400 return phi_sum * stability_sum * coherence_sum;
401 }
402};
403
404//=============================================================================
405// CROSS-LAYER COUPLING
406//=============================================================================
407
408/**
409 * LayerCoupling - Information flow between layers
410 *
411 * Coupling matrix C[i][j] = influence of layer j on layer i
412 * Based on biological/theoretical relationships
413 */
414struct LayerCoupling {
415 static constexpr size_t N = 7;
416
417 // Coupling matrix (row i = influences on layer i)
418 std::array<std::array<double, N>, N> coupling;
419
420 LayerCoupling() {
421 // Initialize with biologically-inspired coupling
422 // Diagonal = self-coupling (1.0)
423 // Off-diagonal = cross-layer influence
424
425 coupling = {{
426 // L1 L2 L3 L4 L5 L6 L7
427 {{ 1.0, 0.1, 0.0, 0.0, 0.0, 0.2, 0.3 }}, // L1: Quantum
428 {{ 0.1, 1.0, 0.3, 0.2, 0.0, 0.0, 0.1 }}, // L2: Genetic
429 {{ 0.0, 0.4, 1.0, 0.3, 0.1, 0.0, 0.0 }}, // L3: RNA
430 {{ 0.0, 0.2, 0.2, 1.0, 0.2, 0.0, 0.1 }}, // L4: Chromosomal
431 {{ 0.1, 0.1, 0.1, 0.2, 1.0, 0.4, 0.3 }}, // L5: Neural
432 {{ 0.2, 0.0, 0.0, 0.0, 0.5, 1.0, 0.4 }}, // L6: Oscillatory
433 {{ 0.3, 0.1, 0.0, 0.1, 0.4, 0.3, 1.0 }} // L7: Consciousness
434 }};
435
436 // Normalize rows
437 for (size_t i = 0; i < N; ++i) {
438 double sum = 0.0;
439 for (size_t j = 0; j < N; ++j) {
440 sum += coupling[i][j];
441 }
442 if (sum > 0) {
443 for (size_t j = 0; j < N; ++j) {
444 coupling[i][j] /= sum;
445 }
446 }
447 }
448 }
449
450 /**
451 * compute_influences - Compute cross-layer influences
452 *
453 * @param layer_activations Current layer activations
454 * @param influences Output: influence on each layer from others
455 */
456 void compute_influences(const std::array<double, N>& layer_activations,
457 std::array<double, N>& influences) const {
458 for (size_t i = 0; i < N; ++i) {
459 influences[i] = 0.0;
460 for (size_t j = 0; j < N; ++j) {
461 if (i != j) { // Exclude self-coupling for influence
462 influences[i] += coupling[i][j] * layer_activations[j];
463 }
464 }
465 }
466 }
467
468 /**
469 * apply_coupling - Update activations based on coupling
470 *
471 * a_i(t+1) = a_i(t) + dt * sum_j(C[i][j] * a_j(t))
472 */
473 void apply_coupling(std::array<double, N>& activations, double dt = 0.01) const {
474 std::array<double, N> new_activations;
475
476 for (size_t i = 0; i < N; ++i) {
477 double sum = 0.0;
478 for (size_t j = 0; j < N; ++j) {
479 sum += coupling[i][j] * activations[j];
480 }
481 new_activations[i] = activations[i] + dt * (sum - activations[i]);
482 }
483
484 activations = new_activations;
485 }
486};
487
488//=============================================================================
489// UTILITY FUNCTIONS
490//=============================================================================
491
492/**
493 * consciousness_to_color - Map consciousness to RGB color
494 *
495 * 0.0 = Dark purple (low consciousness)
496 * 0.5 = Blue/green (medium)
497 * 1.0 = Golden/white (high consciousness)
498 */
499inline void consciousness_to_color(double phi, uint8_t& r, uint8_t& g, uint8_t& b) {
500 if (phi < 0.25) {
501 // Dark purple to blue
502 double t = phi / 0.25;
503 r = static_cast<uint8_t>(64 * t);
504 g = 0;
505 b = static_cast<uint8_t>(128 + 64 * t);
506 } else if (phi < 0.5) {
507 // Blue to cyan
508 double t = (phi - 0.25) / 0.25;
509 r = static_cast<uint8_t>(64);
510 g = static_cast<uint8_t>(128 * t);
511 b = static_cast<uint8_t>(192);
512 } else if (phi < 0.75) {
513 // Cyan to green/yellow
514 double t = (phi - 0.5) / 0.25;
515 r = static_cast<uint8_t>(64 + 128 * t);
516 g = static_cast<uint8_t>(128 + 64 * t);
517 b = static_cast<uint8_t>(192 - 128 * t);
518 } else {
519 // Yellow to golden/white
520 double t = (phi - 0.75) / 0.25;
521 r = static_cast<uint8_t>(192 + 63 * t);
522 g = static_cast<uint8_t>(192 + 63 * t);
523 b = static_cast<uint8_t>(64 + 191 * t);
524 }
525}
526
527/**
528 * consciousness_level_name - Human-readable consciousness level
529 */
530inline const char* consciousness_level_name(double phi) {
531 if (phi >= PHI_THRESHOLD_HIGH) return "TRANSCENDENT";
532 if (phi >= PHI_THRESHOLD_MED) return "AWARE";
533 if (phi >= PHI_THRESHOLD_LOW) return "CONSCIOUS";
534 if (phi >= PHI_THRESHOLD_MIN) return "SEMI-CONSCIOUS";
535 return "DORMANT";
536}
537
538} // namespace consciousness
539
540#endif // CONSCIOUSNESS_API_HPP
541