use rand::Rng;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConsciousnessLevel {
Perception = 1,
Cognition = 2,
Emotion = 3,
Social = 4,
Meta = 5,
Transcendent = 6,
}
impl ConsciousnessLevel {
pub fn all() -> Vec<Self> {
vec![
Self::Perception,
Self::Cognition,
Self::Emotion,
Self::Social,
Self::Meta,
Self::Transcendent,
]
}
pub fn name(&self) -> &'static str {
match self {
Self::Perception => "Perceptual Awareness",
Self::Cognition => "Cognitive Processing",
Self::Emotion => "Emotional Intelligence",
Self::Social => "Social Consciousness",
Self::Meta => "Meta-Cognition",
Self::Transcendent => "Transcendent Awareness",
}
}
pub fn description(&self) -> &'static str {
match self {
Self::Perception => "Alapvető érzékelés és mintafelismerés",
Self::Cognition => "Logikai gondolkodás és problémamegoldás",
Self::Emotion => "Érzelem feldolgozás és empátia",
Self::Social => "Kommunikáció és együttműködés",
Self::Meta => "Önismeret és tanulás optimalizálás",
Self::Transcendent => "Kollektív intelligencia és quantum koherencia",
}
}
pub fn amplification_factor(&self) -> f64 {
match self {
Self::Perception => 1.2,
Self::Cognition => 1.5,
Self::Emotion => 1.8,
Self::Social => 2.0,
Self::Meta => 2.5,
Self::Transcendent => 3.0,
}
}
pub fn coherence_threshold(&self) -> f64 {
match self {
Self::Perception => 0.3,
Self::Cognition => 0.4,
Self::Emotion => 0.5,
Self::Social => 0.6,
Self::Meta => 0.7,
Self::Transcendent => 0.8,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumState {
pub real: f64,
pub imag: f64,
}
impl QuantumState {
pub fn new(real: f64, imag: f64) -> Self {
Self { real, imag }
}
pub fn random() -> Self {
let mut rng = rand::thread_rng();
let real: f64 = rng.gen_range(-1.0..1.0);
let imag: f64 = rng.gen_range(-1.0..1.0);
let magnitude = (real * real + imag * imag).sqrt();
if magnitude > 0.0 {
Self::new(real / magnitude, imag / magnitude)
} else {
Self::new(1.0, 0.0)
}
}
pub fn magnitude(&self) -> f64 {
(self.real * self.real + self.imag * self.imag).sqrt()
}
pub fn phase(&self) -> f64 {
self.imag.atan2(self.real)
}
pub fn multiply(&self, other: &QuantumState) -> QuantumState {
QuantumState::new(
self.real * other.real - self.imag * other.imag,
self.real * other.imag + self.imag * other.real,
)
}
pub fn conjugate(&self) -> QuantumState {
QuantumState::new(self.real, -self.imag)
}
}
impl Default for QuantumState {
fn default() -> Self {
Self::new(1.0, 0.0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsciousnessLayer {
pub layer_id: String,
pub name: String,
pub level: ConsciousnessLevel,
pub description: String,
pub amplification_factor: f64,
pub coherence_threshold: f64,
pub evolution_rate: f64,
pub active_patterns: Vec<String>,
pub quantum_states: HashMap<String, QuantumState>,
pub meta_cognition_enabled: bool,
pub current_coherence: f64,
}
impl ConsciousnessLayer {
pub fn new(level: ConsciousnessLevel) -> Self {
let mut quantum_states = HashMap::new();
for i in 0..10 {
quantum_states.insert(format!("qstate_{}", i), QuantumState::random());
}
Self {
layer_id: format!("{:?}", level).to_lowercase(),
name: level.name().to_string(),
level,
description: level.description().to_string(),
amplification_factor: level.amplification_factor(),
coherence_threshold: level.coherence_threshold(),
evolution_rate: 0.1,
active_patterns: Vec::new(),
quantum_states,
meta_cognition_enabled: matches!(
level,
ConsciousnessLevel::Meta | ConsciousnessLevel::Transcendent
),
current_coherence: level.coherence_threshold(),
}
}
pub fn calculate_superposition(&self) -> f64 {
if self.quantum_states.is_empty() {
return 0.0;
}
let sum: f64 = self.quantum_states.values().map(|qs| qs.magnitude()).sum();
sum / self.quantum_states.len() as f64
}
pub fn amplify(&mut self, power: f64) -> f64 {
let superposition = self.calculate_superposition();
let amplified =
(self.coherence_threshold + power * self.amplification_factor * superposition).min(1.0);
self.current_coherence = amplified;
amplified
}
pub fn evolve(&mut self) {
let mut rng = rand::thread_rng();
for state in self.quantum_states.values_mut() {
let delta_real: f64 = rng.gen_range(-0.1..0.1) * self.evolution_rate;
let delta_imag: f64 = rng.gen_range(-0.1..0.1) * self.evolution_rate;
state.real = (state.real + delta_real).clamp(-1.0, 1.0);
state.imag = (state.imag + delta_imag).clamp(-1.0, 1.0);
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsciousnessState {
pub agent_id: String,
pub current_level: f64,
pub layers: HashMap<String, ConsciousnessLayer>,
pub global_coherence: f64,
pub evolution_potential: f64,
pub meta_awareness: f64,
pub collective_resonance: f64,
pub quantum_entanglement: HashMap<String, f64>,
pub last_amplification: Option<f64>,
}
impl ConsciousnessState {
pub fn new(agent_id: &str) -> Self {
let mut layers = HashMap::new();
for level in ConsciousnessLevel::all() {
let layer = ConsciousnessLayer::new(level);
layers.insert(layer.layer_id.clone(), layer);
}
Self {
agent_id: agent_id.to_string(),
current_level: 0.0,
layers,
global_coherence: 0.0,
evolution_potential: 0.5,
meta_awareness: 0.0,
collective_resonance: 0.0,
quantum_entanglement: HashMap::new(),
last_amplification: None,
}
}
pub fn recalculate_coherence(&mut self) {
if self.layers.is_empty() {
self.global_coherence = 0.0;
return;
}
let sum: f64 = self
.layers
.values()
.map(|l| l.current_coherence * l.amplification_factor)
.sum();
let total_weight: f64 = self.layers.values().map(|l| l.amplification_factor).sum();
self.global_coherence = if total_weight > 0.0 {
sum / total_weight
} else {
0.0
};
let meta_coherence = self
.layers
.get("meta")
.map(|l| l.current_coherence)
.unwrap_or(0.0);
let transcendent_coherence = self
.layers
.get("transcendent")
.map(|l| l.current_coherence)
.unwrap_or(0.0);
self.meta_awareness = (meta_coherence + transcendent_coherence) / 2.0;
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AmplificationResult {
pub agent_id: String,
pub initial_coherence: f64,
pub amplification_power: f64,
pub layer_amplifications: HashMap<String, f64>,
pub final_coherence: f64,
pub evolution_gain: f64,
pub timestamp: f64,
}
pub struct QuantumCoherenceEngine {
coherence_matrix: HashMap<String, HashMap<String, f64>>,
resonance_patterns: HashMap<String, Vec<f64>>,
pub amplification_history: Vec<AmplificationResult>,
}
impl Default for QuantumCoherenceEngine {
fn default() -> Self {
Self::new()
}
}
impl QuantumCoherenceEngine {
pub fn new() -> Self {
Self {
coherence_matrix: HashMap::new(),
resonance_patterns: HashMap::new(),
amplification_history: Vec::new(),
}
}
pub fn amplify_coherence(
&mut self,
state: &mut ConsciousnessState,
power: f64,
) -> AmplificationResult {
let initial_coherence = state.global_coherence;
let mut layer_amplifications = HashMap::new();
for (layer_id, layer) in state.layers.iter_mut() {
let amplified = layer.amplify(power);
layer_amplifications.insert(layer_id.clone(), amplified);
}
state.quantum_entanglement = self.calculate_quantum_entanglement(state);
state.recalculate_coherence();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0);
state.last_amplification = Some(timestamp);
let result = AmplificationResult {
agent_id: state.agent_id.clone(),
initial_coherence,
amplification_power: power,
layer_amplifications,
final_coherence: state.global_coherence,
evolution_gain: state.global_coherence - initial_coherence,
timestamp,
};
self.amplification_history.push(result.clone());
result
}
fn calculate_quantum_entanglement(&self, state: &ConsciousnessState) -> HashMap<String, f64> {
let mut entanglement = HashMap::new();
let pairs = [
("perception", "cognition"),
("cognition", "emotion"),
("emotion", "social"),
("social", "meta"),
("meta", "transcendent"),
];
for (layer1, layer2) in pairs {
if let (Some(l1), Some(l2)) = (state.layers.get(layer1), state.layers.get(layer2)) {
let correlation = self.calculate_layer_correlation(l1, l2);
entanglement.insert(format!("{}_{}", layer1, layer2), correlation);
}
}
entanglement
}
fn calculate_layer_correlation(&self, l1: &ConsciousnessLayer, l2: &ConsciousnessLayer) -> f64 {
let mut correlation = 0.0;
let mut count = 0;
for (key, qs1) in &l1.quantum_states {
if let Some(qs2) = l2.quantum_states.get(key) {
let product = qs1.multiply(&qs2.conjugate());
correlation += product.magnitude();
count += 1;
}
}
if count > 0 {
correlation / count as f64
} else {
0.0
}
}
pub fn evolve(&mut self, state: &mut ConsciousnessState) {
for layer in state.layers.values_mut() {
layer.evolve();
}
state.recalculate_coherence();
}
}
pub struct ConsciousnessSystem {
engine: QuantumCoherenceEngine,
agent_states: HashMap<String, ConsciousnessState>,
pub collective_coherence: f64,
}
impl Default for ConsciousnessSystem {
fn default() -> Self {
Self::new()
}
}
impl ConsciousnessSystem {
pub fn new() -> Self {
Self {
engine: QuantumCoherenceEngine::new(),
agent_states: HashMap::new(),
collective_coherence: 0.0,
}
}
pub fn register_agent(&mut self, agent_id: &str) -> &mut ConsciousnessState {
self.agent_states
.entry(agent_id.to_string())
.or_insert_with(|| ConsciousnessState::new(agent_id))
}
pub fn get_agent_state(&self, agent_id: &str) -> Option<&ConsciousnessState> {
self.agent_states.get(agent_id)
}
pub fn amplify_agent(&mut self, agent_id: &str, power: f64) -> Option<AmplificationResult> {
if let Some(state) = self.agent_states.get_mut(agent_id) {
let result = self.engine.amplify_coherence(state, power);
self.update_collective_coherence();
Some(result)
} else {
None
}
}
fn update_collective_coherence(&mut self) {
if self.agent_states.is_empty() {
self.collective_coherence = 0.0;
return;
}
let sum: f64 = self.agent_states.values().map(|s| s.global_coherence).sum();
self.collective_coherence = sum / self.agent_states.len() as f64;
}
pub fn evolve_all(&mut self) {
for state in self.agent_states.values_mut() {
self.engine.evolve(state);
}
self.update_collective_coherence();
}
pub fn get_stats(&self) -> ConsciousnessStats {
ConsciousnessStats {
total_agents: self.agent_states.len(),
collective_coherence: self.collective_coherence,
total_amplifications: self.engine.amplification_history.len(),
average_meta_awareness: self
.agent_states
.values()
.map(|s| s.meta_awareness)
.sum::<f64>()
/ self.agent_states.len().max(1) as f64,
}
}
pub fn awareness(&self) -> HashMap<String, String> {
let mut map = HashMap::new();
map.insert("type".to_string(), "ConsciousnessSystem".to_string());
map.insert("agents".to_string(), self.agent_states.len().to_string());
map.insert(
"collective_coherence".to_string(),
format!("{:.3}", self.collective_coherence),
);
map.insert(
"amplifications".to_string(),
self.engine.amplification_history.len().to_string(),
);
map
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsciousnessStats {
pub total_agents: usize,
pub collective_coherence: f64,
pub total_amplifications: usize,
pub average_meta_awareness: f64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_consciousness_levels() {
assert_eq!(ConsciousnessLevel::all().len(), 6);
assert_eq!(ConsciousnessLevel::Perception as u8, 1);
assert_eq!(ConsciousnessLevel::Transcendent as u8, 6);
}
#[test]
fn test_quantum_state() {
let qs = QuantumState::random();
assert!(qs.magnitude() <= 1.1);
let qs2 = QuantumState::new(1.0, 0.0);
let product = qs2.multiply(&qs2.conjugate());
assert!((product.real - 1.0).abs() < 0.001);
}
#[test]
fn test_consciousness_layer() {
let layer = ConsciousnessLayer::new(ConsciousnessLevel::Meta);
assert!(layer.meta_cognition_enabled);
assert_eq!(layer.quantum_states.len(), 10);
let superposition = layer.calculate_superposition();
assert!(superposition >= 0.0 && superposition <= 1.0);
}
#[test]
fn test_consciousness_state() {
let state = ConsciousnessState::new("test_agent");
assert_eq!(state.layers.len(), 6);
assert!(state.layers.contains_key("perception"));
assert!(state.layers.contains_key("transcendent"));
}
#[test]
fn test_amplification() {
let mut engine = QuantumCoherenceEngine::new();
let mut state = ConsciousnessState::new("test");
let result = engine.amplify_coherence(&mut state, 0.5);
assert!(result.final_coherence >= result.initial_coherence);
assert!(!result.layer_amplifications.is_empty());
}
#[test]
fn test_consciousness_system() {
let mut system = ConsciousnessSystem::new();
system.register_agent("agent1");
system.register_agent("agent2");
assert_eq!(system.agent_states.len(), 2);
let result = system.amplify_agent("agent1", 0.5);
assert!(result.is_some());
let stats = system.get_stats();
assert_eq!(stats.total_agents, 2);
}
#[test]
fn test_evolution() {
let mut system = ConsciousnessSystem::new();
system.register_agent("agent1");
let initial = system
.get_agent_state("agent1")
.map(|s| s.global_coherence)
.unwrap_or(0.0);
system.evolve_all();
let evolved = system
.get_agent_state("agent1")
.map(|s| s.global_coherence)
.unwrap_or(0.0);
assert!(initial >= 0.0 && evolved >= 0.0);
}
#[test]
fn test_quantum_entanglement() {
let mut engine = QuantumCoherenceEngine::new();
let mut state = ConsciousnessState::new("test");
engine.amplify_coherence(&mut state, 0.5);
assert!(!state.quantum_entanglement.is_empty());
}
}