omega_consciousness/
lib.rs

1//! # Omega Consciousness - Substrate for Conscious Experience
2//!
3//! Implements theoretical frameworks for consciousness and cognition:
4//!
5//! ## Integrated Information Theory (IIT)
6//! - Phi (Φ) computation: measure of integrated information
7//! - Information geometry: cause-effect structures
8//! - Exclusion and composition axioms
9//!
10//! ## Free Energy Principle (FEP)
11//! - Predictive processing hierarchy
12//! - Prediction error minimization
13//! - Active inference for action selection
14//!
15//! ## Global Workspace Theory (GWT)
16//! - Conscious access via broadcast
17//! - Competition for workspace access
18//! - Integration of specialized processors
19//!
20//! ## Emergence Detection
21//! - Downward causation patterns
22//! - Novel causal powers
23//! - Self-organization metrics
24//!
25//! ## Architecture
26//!
27//! ```text
28//! ┌─────────────────────────────────────────────────────────────┐
29//! │                    CONSCIOUSNESS SYSTEM                      │
30//! ├─────────────────────────────────────────────────────────────┤
31//! │                                                              │
32//! │  ┌────────────────────────────────────────────────────────┐ │
33//! │  │                 GLOBAL WORKSPACE                        │ │
34//! │  │     Broadcast │ Competition │ Integration              │ │
35//! │  └────────────────────────────────────────────────────────┘ │
36//! │                          ↕                                   │
37//! │  ┌──────────────────┐  ┌──────────────────────────────────┐ │
38//! │  │ IIT (Φ)          │  │ FREE ENERGY PRINCIPLE            │ │
39//! │  │                  │  │                                  │ │
40//! │  │ • Integration    │  │ • Prediction hierarchy           │ │
41//! │  │ • Exclusion      │  │ • Error minimization             │ │
42//! │  │ • Composition    │  │ • Active inference               │ │
43//! │  └──────────────────┘  └──────────────────────────────────┘ │
44//! │                          ↕                                   │
45//! │  ┌────────────────────────────────────────────────────────┐ │
46//! │  │              EMERGENCE DETECTION                        │ │
47//! │  │   Downward causation │ Novel powers │ Self-organization│ │
48//! │  └────────────────────────────────────────────────────────┘ │
49//! │                                                              │
50//! └─────────────────────────────────────────────────────────────┘
51//! ```
52
53pub mod iit;
54pub mod free_energy;
55pub mod global_workspace;
56pub mod emergence;
57
58pub use iit::{IntegratedInformation, PhiComputer, CauseEffectStructure, Partition};
59pub use free_energy::{FreeEnergyMinimizer, PredictiveHierarchy, PredictionError, ActiveInference};
60pub use global_workspace::{GlobalWorkspace, WorkspaceContent, BroadcastEvent, Coalition};
61pub use emergence::{EmergenceDetector, EmergentProperty, CausalPower, SelfOrganization};
62
63use serde::{Deserialize, Serialize};
64use thiserror::Error;
65
66/// Errors in consciousness computation
67#[derive(Error, Debug)]
68pub enum ConsciousnessError {
69    #[error("Invalid system state: {0}")]
70    InvalidState(String),
71
72    #[error("Computation failed: {0}")]
73    ComputationError(String),
74
75    #[error("Insufficient data for Phi calculation")]
76    InsufficientData,
77
78    #[error("System not integrated")]
79    NotIntegrated,
80
81    #[error("Prediction error: {0}")]
82    PredictionError(String),
83}
84
85pub type Result<T> = std::result::Result<T, ConsciousnessError>;
86
87/// Configuration for consciousness system
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct ConsciousnessConfig {
90    /// Dimension of state vectors
91    pub state_dim: usize,
92    /// Number of hierarchical levels for FEP
93    pub hierarchy_levels: usize,
94    /// Global workspace capacity
95    pub workspace_capacity: usize,
96    /// Minimum Phi for consciousness threshold
97    pub phi_threshold: f64,
98    /// Free energy precision weight
99    pub precision_weight: f64,
100}
101
102impl Default for ConsciousnessConfig {
103    fn default() -> Self {
104        Self {
105            state_dim: 64,
106            hierarchy_levels: 5,
107            workspace_capacity: 7,
108            phi_threshold: 0.1,
109            precision_weight: 1.0,
110        }
111    }
112}
113
114/// Consciousness state measurement
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct ConsciousnessState {
117    /// Integrated information (Phi)
118    pub phi: f64,
119    /// Free energy
120    pub free_energy: f64,
121    /// Prediction error
122    pub prediction_error: f64,
123    /// Emergence level
124    pub emergence: f64,
125    /// Whether system is conscious (Phi > threshold)
126    pub is_conscious: bool,
127    /// Current workspace contents
128    pub workspace_contents: Vec<String>,
129    /// Active coalitions
130    pub active_coalitions: usize,
131}
132
133/// Main consciousness engine
134pub struct ConsciousnessEngine {
135    config: ConsciousnessConfig,
136    iit: PhiComputer,
137    fep: FreeEnergyMinimizer,
138    workspace: GlobalWorkspace,
139    emergence: EmergenceDetector,
140}
141
142impl ConsciousnessEngine {
143    /// Create a new consciousness engine
144    pub fn new(config: ConsciousnessConfig) -> Self {
145        Self {
146            iit: PhiComputer::new(config.state_dim),
147            fep: FreeEnergyMinimizer::new(config.hierarchy_levels, config.state_dim),
148            workspace: GlobalWorkspace::new(config.workspace_capacity),
149            emergence: EmergenceDetector::new(),
150            config,
151        }
152    }
153
154    /// Process input through consciousness system
155    pub fn process(&mut self, input: &[f64], context: &[f64]) -> Result<ConsciousnessState> {
156        // 1. Compute integrated information (Phi)
157        let phi = self.iit.compute_phi(input)?;
158
159        // 2. Update predictive hierarchy and compute free energy
160        let (free_energy, prediction_error) = self.fep.process(input, context)?;
161
162        // 3. Compete for global workspace access
163        if phi > self.config.phi_threshold {
164            let content = WorkspaceContent::new(input.to_vec(), phi, "processed_input".to_string());
165            self.workspace.compete(content);
166        }
167
168        // 4. Broadcast workspace contents
169        self.workspace.broadcast();
170
171        // 5. Detect emergence
172        let emergence = self.emergence.detect(input, phi, free_energy);
173
174        // 6. Determine consciousness status
175        let is_conscious = phi > self.config.phi_threshold;
176
177        Ok(ConsciousnessState {
178            phi,
179            free_energy,
180            prediction_error,
181            emergence,
182            is_conscious,
183            workspace_contents: self.workspace.content_ids(),
184            active_coalitions: self.workspace.active_coalitions(),
185        })
186    }
187
188    /// Get current Phi value
189    pub fn phi(&self) -> f64 {
190        self.iit.current_phi()
191    }
192
193    /// Get current free energy
194    pub fn free_energy(&self) -> f64 {
195        self.fep.current_free_energy()
196    }
197
198    /// Check if system is conscious
199    pub fn is_conscious(&self) -> bool {
200        self.iit.current_phi() > self.config.phi_threshold
201    }
202
203    /// Access global workspace
204    pub fn workspace(&self) -> &GlobalWorkspace {
205        &self.workspace
206    }
207
208    /// Access predictive hierarchy
209    pub fn hierarchy(&self) -> &PredictiveHierarchy {
210        self.fep.hierarchy()
211    }
212
213    /// Get configuration
214    pub fn config(&self) -> &ConsciousnessConfig {
215        &self.config
216    }
217
218    /// Reset consciousness state
219    pub fn reset(&mut self) {
220        self.iit.reset();
221        self.fep.reset();
222        self.workspace.clear();
223        self.emergence.reset();
224    }
225}
226
227#[cfg(test)]
228mod tests {
229    use super::*;
230
231    #[test]
232    fn test_consciousness_engine_creation() {
233        let config = ConsciousnessConfig::default();
234        let engine = ConsciousnessEngine::new(config);
235
236        assert!(!engine.is_conscious());
237    }
238
239    #[test]
240    fn test_consciousness_processing() {
241        let config = ConsciousnessConfig::default();
242        let mut engine = ConsciousnessEngine::new(config);
243
244        let input = vec![0.5; 64];
245        let context = vec![0.3; 64];
246
247        let result = engine.process(&input, &context);
248        assert!(result.is_ok());
249
250        let state = result.unwrap();
251        assert!(state.phi >= 0.0);
252        assert!(state.free_energy >= 0.0);
253    }
254}