omega_consciousness/
lib.rs1pub 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#[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#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct ConsciousnessConfig {
90 pub state_dim: usize,
92 pub hierarchy_levels: usize,
94 pub workspace_capacity: usize,
96 pub phi_threshold: f64,
98 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#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct ConsciousnessState {
117 pub phi: f64,
119 pub free_energy: f64,
121 pub prediction_error: f64,
123 pub emergence: f64,
125 pub is_conscious: bool,
127 pub workspace_contents: Vec<String>,
129 pub active_coalitions: usize,
131}
132
133pub struct ConsciousnessEngine {
135 config: ConsciousnessConfig,
136 iit: PhiComputer,
137 fep: FreeEnergyMinimizer,
138 workspace: GlobalWorkspace,
139 emergence: EmergenceDetector,
140}
141
142impl ConsciousnessEngine {
143 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 pub fn process(&mut self, input: &[f64], context: &[f64]) -> Result<ConsciousnessState> {
156 let phi = self.iit.compute_phi(input)?;
158
159 let (free_energy, prediction_error) = self.fep.process(input, context)?;
161
162 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 self.workspace.broadcast();
170
171 let emergence = self.emergence.detect(input, phi, free_energy);
173
174 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 pub fn phi(&self) -> f64 {
190 self.iit.current_phi()
191 }
192
193 pub fn free_energy(&self) -> f64 {
195 self.fep.current_free_energy()
196 }
197
198 pub fn is_conscious(&self) -> bool {
200 self.iit.current_phi() > self.config.phi_threshold
201 }
202
203 pub fn workspace(&self) -> &GlobalWorkspace {
205 &self.workspace
206 }
207
208 pub fn hierarchy(&self) -> &PredictiveHierarchy {
210 self.fep.hierarchy()
211 }
212
213 pub fn config(&self) -> &ConsciousnessConfig {
215 &self.config
216 }
217
218 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}