scirs2-metrics 0.3.2

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Memory systems for neuromorphic computing
//!
//! This module provides short-term and long-term memory systems
//! with consolidation and recall mechanisms.

#![allow(clippy::too_many_arguments)]
#![allow(dead_code)]

use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

/// Neuromorphic memory system
#[derive(Debug)]
pub struct NeuromorphicMemory<F: Float> {
    /// Short-term memory (working memory)
    pub short_term_memory: ShortTermMemory<F>,
    /// Long-term memory
    pub long_term_memory: LongTermMemory<F>,
    /// Memory consolidation controller
    pub consolidation_controller: ConsolidationController<F>,
    /// Memory recall mechanisms
    pub recall_mechanisms: RecallMechanisms<F>,
}

/// Short-term memory implementation
#[derive(Debug)]
pub struct ShortTermMemory<F: Float> {
    /// Current working memory contents
    pub working_memory: VecDeque<MemoryTrace<F>>,
    /// Capacity limit
    pub capacity: usize,
    /// Decay rate
    pub decay_rate: F,
    /// Refresh mechanisms
    pub refresh_controller: RefreshController<F>,
}

/// Long-term memory implementation
#[derive(Debug)]
pub struct LongTermMemory<F: Float> {
    /// Stored memory traces
    pub memory_traces: HashMap<String, MemoryTrace<F>>,
    /// Memory strength decay
    pub decay_rate: F,
    /// Consolidation threshold
    pub consolidation_threshold: F,
}

/// Memory trace representation
#[derive(Debug, Clone)]
pub struct MemoryTrace<F: Float> {
    pub id: String,
    pub content: Vec<F>,
    pub strength: F,
    pub timestamp: Instant,
    pub access_count: usize,
    pub consolidation_level: F,
}

/// Memory consolidation controller
#[derive(Debug)]
pub struct ConsolidationController<F: Float> {
    /// Consolidation policies
    pub policies: Vec<ConsolidationPolicy>,
    /// Consolidation thresholds
    pub thresholds: HashMap<String, F>,
    /// Replay controller
    pub replay_controller: ReplayController<F>,
}

/// Memory consolidation policies
#[derive(Debug, Clone)]
pub enum ConsolidationPolicy {
    /// Time-based consolidation
    TimeBased { interval: Duration },
    /// Strength-based consolidation
    StrengthBased { threshold: f64 },
    /// Access-based consolidation
    AccessBased { min_access: usize },
    /// Combined policies
    Combined { policies: Vec<ConsolidationPolicy> },
}

/// Replay controller for memory consolidation
#[derive(Debug)]
pub struct ReplayController<F: Float> {
    /// Replay sequences
    pub replay_sequences: VecDeque<ReplaySequence<F>>,
    /// Replay patterns
    pub replay_patterns: Vec<ReplayPattern>,
    /// Replay scheduling
    pub scheduling: ReplayScheduling,
}

/// Memory replay sequence
#[derive(Debug, Clone)]
pub struct ReplaySequence<F: Float> {
    pub memory_ids: Vec<String>,
    pub replay_strength: F,
    pub temporal_compression: F,
}

/// Replay patterns
#[derive(Debug, Clone)]
pub enum ReplayPattern {
    Forward,
    Backward,
    Random,
    Priority,
}

/// Replay scheduling
#[derive(Debug, Clone)]
pub enum ReplayScheduling {
    Continuous,
    Sleep,
    Idle,
    Triggered,
}

/// Memory recall mechanisms
#[derive(Debug)]
pub struct RecallMechanisms<F: Float> {
    /// Associative recall
    pub associative_recall: AssociativeRecall<F>,
    /// Content-addressable recall
    pub content_recall: ContentAddressableRecall<F>,
    /// Context-dependent recall
    pub context_recall: ContextDependentRecall<F>,
}

/// Associative memory recall
#[derive(Debug)]
pub struct AssociativeRecall<F: Float> {
    pub association_matrix: HashMap<String, HashMap<String, F>>,
    pub recall_threshold: F,
}

/// Content-addressable memory recall
#[derive(Debug)]
pub struct ContentAddressableRecall<F: Float> {
    pub content_index: HashMap<Vec<F>, String>,
    pub similarity_threshold: F,
}

/// Context-dependent memory recall
#[derive(Debug)]
pub struct ContextDependentRecall<F: Float> {
    pub context_associations: HashMap<String, Vec<String>>,
    pub context_weights: HashMap<String, F>,
}

/// Refresh controller for short-term memory
#[derive(Debug)]
pub struct RefreshController<F: Float> {
    pub refresh_rate: Duration,
    pub last_refresh: Instant,
    pub refresh_strength: F,
}

impl<F: Float> NeuromorphicMemory<F> {
    /// Create new neuromorphic memory system
    pub fn new(capacity: usize) -> Self {
        Self {
            short_term_memory: ShortTermMemory::new(capacity),
            long_term_memory: LongTermMemory::new(),
            consolidation_controller: ConsolidationController::new(),
            recall_mechanisms: RecallMechanisms::new(),
        }
    }

    /// Store new memory
    pub fn store(&mut self, content: Vec<F>, strength: F) -> crate::error::Result<String> {
        let memory_id = format!("mem_{}", Instant::now().elapsed().as_nanos());
        let trace = MemoryTrace {
            id: memory_id.clone(),
            content,
            strength,
            timestamp: Instant::now(),
            access_count: 0,
            consolidation_level: F::zero(),
        };

        // Store in short-term memory first
        self.short_term_memory.store(trace)?;

        Ok(memory_id)
    }

    /// Recall memory by ID
    pub fn recall(&mut self, memory_id: &str) -> crate::error::Result<Option<MemoryTrace<F>>> {
        // Try short-term memory first
        if let Some(trace) = self.short_term_memory.recall(memory_id)? {
            return Ok(Some(trace));
        }

        // Try long-term memory
        if let Some(trace) = self.long_term_memory.recall(memory_id)? {
            return Ok(Some(trace));
        }

        Ok(None)
    }

    /// Update memory system
    pub fn update(&mut self, dt: Duration) -> crate::error::Result<()> {
        // Update short-term memory
        self.short_term_memory.update(dt)?;

        // Update long-term memory
        self.long_term_memory.update(dt)?;

        // Process consolidation
        self.consolidation_controller
            .update(&mut self.short_term_memory, &mut self.long_term_memory)?;

        Ok(())
    }
}

impl<F: Float> ShortTermMemory<F> {
    /// Create new short-term memory
    pub fn new(capacity: usize) -> Self {
        Self {
            working_memory: VecDeque::with_capacity(capacity),
            capacity,
            decay_rate: F::from(0.95).expect("Failed to convert constant to float"),
            refresh_controller: RefreshController::new(),
        }
    }

    /// Store memory trace
    pub fn store(&mut self, trace: MemoryTrace<F>) -> crate::error::Result<()> {
        if self.working_memory.len() >= self.capacity {
            self.working_memory.pop_front();
        }
        self.working_memory.push_back(trace);
        Ok(())
    }

    /// Recall memory by ID
    pub fn recall(&mut self, memory_id: &str) -> crate::error::Result<Option<MemoryTrace<F>>> {
        for trace in &mut self.working_memory {
            if trace.id == memory_id {
                trace.access_count += 1;
                return Ok(Some(trace.clone()));
            }
        }
        Ok(None)
    }

    /// Update short-term memory
    pub fn update(&mut self, dt: Duration) -> crate::error::Result<()> {
        // Apply decay to all traces
        for trace in &mut self.working_memory {
            trace.strength = trace.strength * self.decay_rate;
        }

        // Remove weak traces
        self.working_memory.retain(|trace| {
            trace.strength > F::from(0.01).expect("Failed to convert constant to float")
        });

        // Update refresh controller
        self.refresh_controller.update(dt)?;

        Ok(())
    }
}

impl<F: Float> LongTermMemory<F> {
    /// Create new long-term memory
    pub fn new() -> Self {
        Self {
            memory_traces: HashMap::new(),
            decay_rate: F::from(0.999).expect("Failed to convert constant to float"),
            consolidation_threshold: F::from(0.8).expect("Failed to convert constant to float"),
        }
    }

    /// Store memory trace
    pub fn store(&mut self, trace: MemoryTrace<F>) -> crate::error::Result<()> {
        self.memory_traces.insert(trace.id.clone(), trace);
        Ok(())
    }

    /// Recall memory by ID
    pub fn recall(&mut self, memory_id: &str) -> crate::error::Result<Option<MemoryTrace<F>>> {
        if let Some(trace) = self.memory_traces.get_mut(memory_id) {
            trace.access_count += 1;
            Ok(Some(trace.clone()))
        } else {
            Ok(None)
        }
    }

    /// Update long-term memory
    pub fn update(&mut self, dt: Duration) -> crate::error::Result<()> {
        // Apply slow decay to all traces
        for trace in self.memory_traces.values_mut() {
            trace.strength = trace.strength * self.decay_rate;
        }

        // Remove very weak traces
        self.memory_traces.retain(|_, trace| {
            trace.strength > F::from(0.001).expect("Failed to convert constant to float")
        });

        Ok(())
    }
}

impl<F: Float> ConsolidationController<F> {
    /// Create new consolidation controller
    pub fn new() -> Self {
        Self {
            policies: vec![ConsolidationPolicy::StrengthBased { threshold: 0.5 }],
            thresholds: HashMap::new(),
            replay_controller: ReplayController::new(),
        }
    }

    /// Update consolidation process
    pub fn update(
        &mut self,
        short_term: &mut ShortTermMemory<F>,
        long_term: &mut LongTermMemory<F>,
    ) -> crate::error::Result<()> {
        // Check for traces ready for consolidation
        let mut traces_to_consolidate = Vec::new();

        for trace in &short_term.working_memory {
            if self.should_consolidate(trace)? {
                traces_to_consolidate.push(trace.clone());
            }
        }

        // Move traces to long-term memory
        for trace in traces_to_consolidate {
            long_term.store(trace.clone())?;
            // Remove from short-term memory
            short_term.working_memory.retain(|t| t.id != trace.id);
        }

        Ok(())
    }

    /// Check if memory should be consolidated
    fn should_consolidate(&self, trace: &MemoryTrace<F>) -> crate::error::Result<bool> {
        for policy in &self.policies {
            match policy {
                ConsolidationPolicy::StrengthBased { threshold } => {
                    if trace.strength > F::from(*threshold).expect("Failed to convert to float") {
                        return Ok(true);
                    }
                }
                ConsolidationPolicy::AccessBased { min_access } => {
                    if trace.access_count >= *min_access {
                        return Ok(true);
                    }
                }
                ConsolidationPolicy::TimeBased { interval } => {
                    if trace.timestamp.elapsed() > *interval {
                        return Ok(true);
                    }
                }
                _ => {}
            }
        }
        Ok(false)
    }
}

impl<F: Float> RefreshController<F> {
    /// Create new refresh controller
    pub fn new() -> Self {
        Self {
            refresh_rate: Duration::from_millis(100),
            last_refresh: Instant::now(),
            refresh_strength: F::from(0.1).expect("Failed to convert constant to float"),
        }
    }

    /// Update refresh controller
    pub fn update(&mut self, dt: Duration) -> crate::error::Result<()> {
        self.last_refresh += dt;
        Ok(())
    }
}

impl<F: Float> ReplayController<F> {
    /// Create new replay controller
    pub fn new() -> Self {
        Self {
            replay_sequences: VecDeque::new(),
            replay_patterns: vec![ReplayPattern::Forward],
            scheduling: ReplayScheduling::Idle,
        }
    }
}

impl<F: Float> RecallMechanisms<F> {
    /// Create new recall mechanisms
    pub fn new() -> Self {
        Self {
            associative_recall: AssociativeRecall::new(),
            content_recall: ContentAddressableRecall::new(),
            context_recall: ContextDependentRecall::new(),
        }
    }
}

impl<F: Float> AssociativeRecall<F> {
    pub fn new() -> Self {
        Self {
            association_matrix: HashMap::new(),
            recall_threshold: F::from(0.7).expect("Failed to convert constant to float"),
        }
    }
}

impl<F: Float> ContentAddressableRecall<F> {
    pub fn new() -> Self {
        Self {
            content_index: HashMap::new(),
            similarity_threshold: F::from(0.8).expect("Failed to convert constant to float"),
        }
    }
}

impl<F: Float> ContextDependentRecall<F> {
    pub fn new() -> Self {
        Self {
            context_associations: HashMap::new(),
            context_weights: HashMap::new(),
        }
    }
}