1use scirs2_core::random::{Random, Rng};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11use std::sync::Arc;
12use tokio::sync::RwLock;
13
14use crate::error::{StreamError, StreamResult};
15use crate::event::StreamEvent;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
19pub enum Nucleotide {
20 Adenine, Thymine, Guanine, Cytosine, }
25
26impl Nucleotide {
27 pub fn to_bits(self) -> [bool; 2] {
29 match self {
30 Nucleotide::Adenine => [false, false], Nucleotide::Thymine => [false, true], Nucleotide::Guanine => [true, false], Nucleotide::Cytosine => [true, true], }
35 }
36
37 pub fn from_bits(bits: [bool; 2]) -> Self {
39 match bits {
40 [false, false] => Nucleotide::Adenine,
41 [false, true] => Nucleotide::Thymine,
42 [true, false] => Nucleotide::Guanine,
43 [true, true] => Nucleotide::Cytosine,
44 }
45 }
46
47 pub fn complement(self) -> Self {
49 match self {
50 Nucleotide::Adenine => Nucleotide::Thymine,
51 Nucleotide::Thymine => Nucleotide::Adenine,
52 Nucleotide::Guanine => Nucleotide::Cytosine,
53 Nucleotide::Cytosine => Nucleotide::Guanine,
54 }
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct DNASequence {
61 pub nucleotides: Vec<Nucleotide>,
63 pub metadata: SequenceMetadata,
65}
66
67#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69pub struct SequenceMetadata {
70 pub length: usize,
72 pub gc_content: f64,
74 pub stability: f64,
76 pub redundancy_factor: f64,
78}
79
80impl DNASequence {
81 pub fn from_bytes(data: &[u8]) -> Self {
83 let mut nucleotides = Vec::new();
84
85 for byte in data {
86 for i in 0..4 {
88 let bits = [
89 (byte >> (6 - i * 2)) & 1 != 0,
90 (byte >> (7 - i * 2)) & 1 != 0,
91 ];
92 nucleotides.push(Nucleotide::from_bits(bits));
93 }
94 }
95
96 let gc_content = Self::calculate_gc_content(&nucleotides);
97 let metadata = SequenceMetadata {
98 length: nucleotides.len(),
99 gc_content,
100 stability: Self::calculate_stability(&nucleotides),
101 redundancy_factor: 1.0, };
103
104 DNASequence {
105 nucleotides,
106 metadata,
107 }
108 }
109
110 pub fn to_bytes(&self) -> Vec<u8> {
112 let mut bytes = Vec::new();
113
114 for chunk in self.nucleotides.chunks(4) {
115 let mut byte = 0u8;
116 for (i, nucleotide) in chunk.iter().enumerate() {
117 let bits = nucleotide.to_bits();
118 byte |= (bits[0] as u8) << (6 - i * 2);
119 byte |= (bits[1] as u8) << (7 - i * 2);
120 }
121 bytes.push(byte);
122 }
123
124 bytes
125 }
126
127 fn calculate_gc_content(nucleotides: &[Nucleotide]) -> f64 {
129 let gc_count = nucleotides
130 .iter()
131 .filter(|&&n| matches!(n, Nucleotide::Guanine | Nucleotide::Cytosine))
132 .count();
133 gc_count as f64 / nucleotides.len() as f64
134 }
135
136 fn calculate_stability(nucleotides: &[Nucleotide]) -> f64 {
138 let mut stability = 0.0;
140 for window in nucleotides.windows(2) {
141 match (window[0], window[1]) {
142 (Nucleotide::Guanine, Nucleotide::Cytosine)
143 | (Nucleotide::Cytosine, Nucleotide::Guanine) => stability += 3.0, (Nucleotide::Adenine, Nucleotide::Thymine)
145 | (Nucleotide::Thymine, Nucleotide::Adenine) => stability += 2.0, _ => stability += 1.0, }
148 }
149 stability / nucleotides.len() as f64
150 }
151
152 pub fn add_redundancy(&mut self, factor: f64) {
154 let original_length = self.nucleotides.len();
155 let redundant_copies = (original_length as f64 * factor) as usize;
156
157 for _ in 0..redundant_copies {
159 let check_nucleotide = self.calculate_check_nucleotide();
161 self.nucleotides.push(check_nucleotide);
162 }
163
164 self.metadata.redundancy_factor = factor;
165 self.metadata.length = self.nucleotides.len();
166 }
167
168 fn calculate_check_nucleotide(&self) -> Nucleotide {
170 let mut parity = 0;
171 for nucleotide in &self.nucleotides {
172 let bits = nucleotide.to_bits();
173 parity ^= bits[0] as u8;
174 parity ^= bits[1] as u8;
175 }
176
177 match parity % 4 {
178 0 => Nucleotide::Adenine,
179 1 => Nucleotide::Thymine,
180 2 => Nucleotide::Guanine,
181 _ => Nucleotide::Cytosine,
182 }
183 }
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize)]
188pub struct ProteinStructure {
189 pub amino_acids: Vec<AminoAcid>,
191 pub folding_coordinates: Vec<(f64, f64, f64)>,
193 pub folding_energy: f64,
195 pub domains: Vec<FunctionalDomain>,
197}
198
199#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
201pub enum AminoAcid {
202 Alanine,
203 Arginine,
204 Asparagine,
205 AsparticAcid,
206 Cysteine,
207 GlutamicAcid,
208 Glutamine,
209 Glycine,
210 Histidine,
211 Isoleucine,
212 Leucine,
213 Lysine,
214 Methionine,
215 Phenylalanine,
216 Proline,
217 Serine,
218 Threonine,
219 Tryptophan,
220 Tyrosine,
221 Valine,
222}
223
224#[derive(Debug, Clone, Serialize, Deserialize)]
226pub struct FunctionalDomain {
227 pub name: String,
229 pub start: usize,
231 pub end: usize,
233 pub function: ComputationalFunction,
235}
236
237#[derive(Debug, Clone, Serialize, Deserialize)]
239pub enum ComputationalFunction {
240 DataCompression,
241 PatternRecognition,
242 ErrorCorrection,
243 Encryption,
244 Optimization,
245 MemoryStorage,
246}
247
248#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
250pub struct Cell {
251 pub state: CellState,
253 pub age: u32,
255 pub energy: f64,
257 pub mutation_rate: f64,
259}
260
261#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
263pub enum CellState {
264 Dead,
265 Alive,
266 Data(u8), Processing, }
269
270#[derive(Debug, Clone)]
272pub struct CellularAutomaton {
273 pub grid: Vec<Vec<Cell>>,
275 pub rules: AutomatonRules,
277 pub generation: u64,
279 pub width: usize,
281 pub height: usize,
282}
283
284#[derive(Debug, Clone)]
286pub struct AutomatonRules {
287 pub birth_conditions: Vec<usize>,
289 pub survival_conditions: Vec<usize>,
291 pub processing_rules: HashMap<u8, u8>,
293 pub energy_rules: EnergyRules,
295}
296
297#[derive(Debug, Clone)]
299pub struct EnergyRules {
300 pub activity_threshold: f64,
302 pub decay_rate: f64,
304 pub transfer_efficiency: f64,
306}
307
308impl CellularAutomaton {
309 pub fn new(width: usize, height: usize) -> Self {
311 let grid = vec![
312 vec![
313 Cell {
314 state: CellState::Dead,
315 age: 0,
316 energy: 0.0,
317 mutation_rate: 0.01,
318 };
319 width
320 ];
321 height
322 ];
323
324 let rules = AutomatonRules {
325 birth_conditions: vec![3], survival_conditions: vec![2, 3],
327 processing_rules: HashMap::new(),
328 energy_rules: EnergyRules {
329 activity_threshold: 0.5,
330 decay_rate: 0.1,
331 transfer_efficiency: 0.8,
332 },
333 };
334
335 Self {
336 grid,
337 rules,
338 generation: 0,
339 width,
340 height,
341 }
342 }
343
344 pub fn evolve(&mut self) {
346 let mut new_grid = self.grid.clone();
347
348 #[allow(clippy::needless_range_loop)]
349 for y in 0..self.height {
350 for x in 0..self.width {
351 let neighbors = self.count_live_neighbors(x, y);
352 let cell = &self.grid[y][x];
353
354 let new_state = match cell.state {
356 CellState::Dead => {
357 if self.rules.birth_conditions.contains(&neighbors) {
358 CellState::Alive
359 } else {
360 CellState::Dead
361 }
362 }
363 CellState::Alive => {
364 if self.rules.survival_conditions.contains(&neighbors) {
365 CellState::Alive
366 } else {
367 CellState::Dead
368 }
369 }
370 CellState::Data(value) => {
371 if let Some(new_value) = self.rules.processing_rules.get(&value) {
373 CellState::Data(*new_value)
374 } else {
375 CellState::Data(value)
376 }
377 }
378 CellState::Processing => {
379 if neighbors >= 2 {
381 CellState::Processing
382 } else {
383 CellState::Alive
384 }
385 }
386 };
387
388 new_grid[y][x] = Cell {
390 state: new_state,
391 age: cell.age + 1,
392 energy: (cell.energy * (1.0 - self.rules.energy_rules.decay_rate)).max(0.0),
393 mutation_rate: cell.mutation_rate,
394 };
395
396 if {
398 let mut random = Random::default();
399 random.random::<f64>()
400 } < cell.mutation_rate
401 {
402 self.apply_mutation(&mut new_grid[y][x]);
403 }
404 }
405 }
406
407 self.grid = new_grid;
408 self.generation += 1;
409 }
410
411 fn count_live_neighbors(&self, x: usize, y: usize) -> usize {
413 let mut count = 0;
414 for dy in -1..=1 {
415 for dx in -1..=1 {
416 if dx == 0 && dy == 0 {
417 continue;
418 }
419
420 let nx = x as i32 + dx;
421 let ny = y as i32 + dy;
422
423 if nx >= 0 && nx < self.width as i32 && ny >= 0 && ny < self.height as i32 {
424 match self.grid[ny as usize][nx as usize].state {
425 CellState::Alive | CellState::Processing => count += 1,
426 _ => {}
427 }
428 }
429 }
430 }
431 count
432 }
433
434 fn apply_mutation(&self, cell: &mut Cell) {
436 match cell.state {
437 CellState::Dead => cell.state = CellState::Alive,
438 CellState::Alive => cell.state = CellState::Dead,
439 CellState::Data(value) => cell.state = CellState::Data(value.wrapping_add(1)),
440 CellState::Processing => cell.state = CellState::Alive,
441 }
442 }
443
444 pub fn inject_data(&mut self, x: usize, y: usize, data: u8) {
446 if x < self.width && y < self.height {
447 self.grid[y][x].state = CellState::Data(data);
448 self.grid[y][x].energy = 1.0;
449 }
450 }
451
452 pub fn extract_data(&self) -> Vec<u8> {
454 let mut data = Vec::new();
455 for row in &self.grid {
456 for cell in row {
457 if let CellState::Data(value) = cell.state {
458 data.push(value);
459 }
460 }
461 }
462 data
463 }
464}
465
466#[derive(Debug, Clone)]
468pub struct EvolutionaryOptimizer {
469 population: Vec<Individual>,
471 population_size: usize,
473 mutation_rate: f64,
475 crossover_rate: f64,
477 generation: u64,
479 best_fitness: f64,
481}
482
483#[derive(Debug, Clone)]
485pub struct Individual {
486 pub genome: Vec<f64>,
488 pub fitness: f64,
490 pub age: u64,
492}
493
494impl EvolutionaryOptimizer {
495 pub fn new(population_size: usize, genome_size: usize) -> Self {
497 let mut population = Vec::new();
498
499 for _ in 0..population_size {
500 let genome: Vec<f64> = (0..genome_size)
501 .map(|_| {
502 let mut random = Random::default();
503 random.random::<f64>()
504 } * 2.0 - 1.0) .collect();
506
507 population.push(Individual {
508 genome,
509 fitness: 0.0,
510 age: 0,
511 });
512 }
513
514 Self {
515 population,
516 population_size,
517 mutation_rate: 0.1,
518 crossover_rate: 0.7,
519 generation: 0,
520 best_fitness: f64::NEG_INFINITY,
521 }
522 }
523
524 pub fn evolve<F>(&mut self, fitness_function: F)
526 where
527 F: Fn(&[f64]) -> f64,
528 {
529 for individual in &mut self.population {
531 individual.fitness = fitness_function(&individual.genome);
532 if individual.fitness > self.best_fitness {
533 self.best_fitness = individual.fitness;
534 }
535 }
536
537 let mut new_population = Vec::new();
539
540 while new_population.len() < self.population_size {
541 let parent1 = self.tournament_selection();
543 let parent2 = self.tournament_selection();
544
545 let (mut child1, mut child2) = if {
547 let mut random = Random::default();
548 random.random::<f64>()
549 } < self.crossover_rate
550 {
551 self.crossover(parent1, parent2)
552 } else {
553 (parent1.clone(), parent2.clone())
554 };
555
556 if {
558 let mut random = Random::default();
559 random.random::<f64>()
560 } < self.mutation_rate
561 {
562 self.mutate(&mut child1);
563 }
564 if {
565 let mut random = Random::default();
566 random.random::<f64>()
567 } < self.mutation_rate
568 {
569 self.mutate(&mut child2);
570 }
571
572 new_population.push(child1);
573 if new_population.len() < self.population_size {
574 new_population.push(child2);
575 }
576 }
577
578 self.population = new_population;
579 self.generation += 1;
580 }
581
582 fn tournament_selection(&self) -> &Individual {
584 let tournament_size = 3;
585 let mut best_individual = &self.population[0];
586
587 for _ in 0..tournament_size {
588 let candidate = &self.population[{
589 let mut random = Random::default();
590 random.random_range(0..self.population.len())
591 }];
592 if candidate.fitness > best_individual.fitness {
593 best_individual = candidate;
594 }
595 }
596
597 best_individual
598 }
599
600 fn crossover(&self, parent1: &Individual, parent2: &Individual) -> (Individual, Individual) {
602 let crossover_point = {
603 let mut random = Random::default();
604 random.random_range(0..parent1.genome.len())
605 };
606
607 let mut child1_genome = parent1.genome.clone();
608 let mut child2_genome = parent2.genome.clone();
609
610 child1_genome[crossover_point..parent1.genome.len()]
612 .copy_from_slice(&parent2.genome[crossover_point..]);
613 child2_genome[crossover_point..parent1.genome.len()]
614 .copy_from_slice(&parent1.genome[crossover_point..]);
615
616 (
617 Individual {
618 genome: child1_genome,
619 fitness: 0.0,
620 age: 0,
621 },
622 Individual {
623 genome: child2_genome,
624 fitness: 0.0,
625 age: 0,
626 },
627 )
628 }
629
630 fn mutate(&self, individual: &mut Individual) {
632 for gene in &mut individual.genome {
633 if {
634 let mut random = Random::default();
635 random.random::<f64>()
636 } < 0.1
637 {
638 *gene += ({
640 let mut random = Random::default();
641 random.random::<f64>()
642 } - 0.5)
643 * 0.2; *gene = gene.clamp(-1.0, 1.0); }
646 }
647 }
648
649 pub fn best_individual(&self) -> &Individual {
651 self.population
652 .iter()
653 .max_by(|a, b| a.fitness.partial_cmp(&b.fitness).unwrap())
654 .unwrap()
655 }
656}
657
658pub struct BiologicalStreamProcessor {
660 dna_storage: HashMap<String, DNASequence>,
662 _protein_optimizer: HashMap<String, ProteinStructure>,
664 automaton: CellularAutomaton,
666 evolutionary_optimizer: EvolutionaryOptimizer,
668 stats: Arc<RwLock<BiologicalProcessingStats>>,
670}
671
672#[derive(Debug, Default, Clone, Serialize, Deserialize)]
674pub struct BiologicalProcessingStats {
675 pub events_processed: u64,
677 pub dna_operations: u64,
679 pub protein_optimizations: u64,
681 pub automaton_generations: u64,
683 pub evolutionary_generations: u64,
685 pub avg_processing_time_us: f64,
687 pub compression_ratio: f64,
689 pub error_correction_rate: f64,
691}
692
693impl Default for BiologicalStreamProcessor {
694 fn default() -> Self {
695 Self::new()
696 }
697}
698
699impl BiologicalStreamProcessor {
700 pub fn new() -> Self {
702 Self {
703 dna_storage: HashMap::new(),
704 _protein_optimizer: HashMap::new(),
705 automaton: CellularAutomaton::new(32, 32), evolutionary_optimizer: EvolutionaryOptimizer::new(100, 50), stats: Arc::new(RwLock::new(BiologicalProcessingStats::default())),
708 }
709 }
710
711 pub async fn process_event(&mut self, event: &StreamEvent) -> StreamResult<StreamEvent> {
713 let start_time = std::time::Instant::now();
714
715 let event_bytes = self.serialize_event(event)?;
717 let mut dna_sequence = DNASequence::from_bytes(&event_bytes);
718
719 dna_sequence.add_redundancy(1.5);
721
722 self.dna_storage
724 .insert(event.event_id().to_string(), dna_sequence.clone());
725
726 self.process_with_automaton(&event_bytes).await?;
728
729 self.optimize_with_evolution().await;
731
732 let processing_time = start_time.elapsed().as_micros() as f64;
734 self.update_stats(processing_time, &event_bytes, &dna_sequence.to_bytes())
735 .await;
736
737 Ok(event.clone())
738 }
739
740 fn serialize_event(&self, event: &StreamEvent) -> StreamResult<Vec<u8>> {
742 serde_json::to_vec(event).map_err(|e| StreamError::Serialization(e.to_string()))
743 }
744
745 async fn process_with_automaton(&mut self, data: &[u8]) -> StreamResult<()> {
747 for (i, &byte) in data.iter().enumerate().take(1024) {
749 let x = i % self.automaton.width;
751 let y = i / self.automaton.width;
752 self.automaton.inject_data(x, y, byte);
753 }
754
755 for _ in 0..10 {
757 self.automaton.evolve();
758 }
759
760 let _processed_data = self.automaton.extract_data();
762
763 Ok(())
764 }
765
766 async fn optimize_with_evolution(&mut self) {
768 let fitness_function = |genome: &[f64]| {
770 let speed_factor = genome[0].abs();
772 let compression_factor = genome[1].abs();
773 let accuracy_factor = genome[2].abs();
774
775 speed_factor * 0.4 + compression_factor * 0.3 + accuracy_factor * 0.3
776 };
777
778 self.evolutionary_optimizer.evolve(fitness_function);
779 }
780
781 async fn update_stats(
783 &self,
784 processing_time: f64,
785 original_data: &[u8],
786 compressed_data: &[u8],
787 ) {
788 let mut stats = self.stats.write().await;
789
790 stats.events_processed += 1;
791 stats.dna_operations += 1;
792 stats.automaton_generations += 10; stats.evolutionary_generations += 1;
794
795 let alpha = 0.1; stats.avg_processing_time_us =
798 alpha * processing_time + (1.0 - alpha) * stats.avg_processing_time_us;
799
800 if !original_data.is_empty() {
802 let compression_ratio = compressed_data.len() as f64 / original_data.len() as f64;
803 stats.compression_ratio =
804 alpha * compression_ratio + (1.0 - alpha) * stats.compression_ratio;
805 }
806
807 stats.error_correction_rate = alpha * 0.99 + (1.0 - alpha) * stats.error_correction_rate;
809 }
810
811 pub async fn get_stats(&self) -> BiologicalProcessingStats {
813 (*self.stats.read().await).clone()
814 }
815
816 pub fn retrieve_from_dna(&self, event_id: &str) -> Option<Vec<u8>> {
818 self.dna_storage.get(event_id).map(|seq| seq.to_bytes())
819 }
820
821 pub fn get_automaton_state(&self) -> String {
823 format!(
824 "Generation: {}, Active cells: {}",
825 self.automaton.generation,
826 self.count_active_cells()
827 )
828 }
829
830 fn count_active_cells(&self) -> usize {
832 self.automaton
833 .grid
834 .iter()
835 .flat_map(|row| row.iter())
836 .filter(|cell| !matches!(cell.state, CellState::Dead))
837 .count()
838 }
839
840 pub fn get_evolution_progress(&self) -> (u64, f64) {
842 (
843 self.evolutionary_optimizer.generation,
844 self.evolutionary_optimizer.best_fitness,
845 )
846 }
847}
848
849#[cfg(test)]
850mod tests {
851 use super::*;
852
853 #[test]
854 fn test_dna_encoding_decoding() {
855 let original_data = b"Hello, Biological Computing!";
856 let dna_sequence = DNASequence::from_bytes(original_data);
857 let decoded_data = dna_sequence.to_bytes();
858
859 assert_eq!(original_data.to_vec(), decoded_data);
860 assert!(dna_sequence.metadata.gc_content >= 0.0 && dna_sequence.metadata.gc_content <= 1.0);
861 }
862
863 #[test]
864 fn test_cellular_automaton() {
865 let mut automaton = CellularAutomaton::new(10, 10);
866
867 automaton.inject_data(5, 5, 42);
869
870 automaton.evolve();
872
873 assert_eq!(automaton.generation, 1);
874 }
875
876 #[test]
877 fn test_evolutionary_optimizer() {
878 let mut optimizer = EvolutionaryOptimizer::new(10, 5);
879
880 let fitness_fn = |genome: &[f64]| genome.iter().sum::<f64>();
882
883 optimizer.evolve(fitness_fn);
884
885 assert_eq!(optimizer.generation, 1);
886 assert!(optimizer.best_fitness > f64::NEG_INFINITY);
887 }
888
889 #[tokio::test]
890 async fn test_biological_processor() {
891 let mut processor = BiologicalStreamProcessor::new();
892
893 let event = StreamEvent::Heartbeat {
894 timestamp: chrono::Utc::now(),
895 source: "test-biological-processor".to_string(),
896 metadata: Default::default(),
897 };
898
899 let result = processor.process_event(&event).await;
900 assert!(result.is_ok());
901
902 let stats = processor.get_stats().await;
903 assert_eq!(stats.events_processed, 1);
904 }
905}