thermogram 1.0.0

Plastic memory capsule with 4-temperature tensor states (hot/warm/cool/cold), bidirectional transitions, and hash-chained auditability
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
//! Plasticity Rules - STDP-like policies for memory updates
//!
//! Inspired by Spike-Timing Dependent Plasticity (STDP) in neuroscience,
//! plasticity rules determine when to:
//! - Update existing memory (strengthen/weaken)
//! - Create new memory (novelty threshold exceeded)
//! - Merge memories (similar patterns detected)
//! - Prune memories (decay below threshold)
//!
//! ## Signal-Native Plasticity
//!
//! All thresholds and rates use `Signal` (2-byte polarity + magnitude).
//! Strength updates operate on Signal magnitudes using integer arithmetic.
//!
//! ## Ternary Plasticity
//!
//! For ternary weights (+1, 0, -1), plasticity works via discrete state transitions:
//! - **Strengthen**: Neg→Zero→Pos
//! - **Weaken**: Pos→Zero→Neg
//! - **Prune**: Only Zero weights are prunable
//!
//! This enables BitNet-style quantized neural networks with STDP learning.

use serde::{Deserialize, Serialize};
use ternary_signal::Signal;

use crate::ternary::TernaryWeight;

/// Plasticity rule governing memory updates
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlasticityRule {
    /// Update policy
    pub policy: UpdatePolicy,

    /// Novelty threshold — magnitude above which a new memory is created
    pub novelty_threshold: Signal,

    /// Merge threshold — similarity above (1.0 - threshold) triggers merge
    pub merge_threshold: Signal,

    /// Decay rate per consolidation cycle
    pub decay_rate: Signal,

    /// Minimum strength to keep (prune below this)
    pub prune_threshold: Signal,

    /// Learning rate for updates
    pub learning_rate: Signal,
}

/// Policy for how updates are applied
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UpdatePolicy {
    /// STDP-like: Strengthen recent, weaken old
    STDP,

    /// Always replace (no plasticity)
    Replace,

    /// Exponential moving average
    EMA,

    /// Bayesian update (weighted by confidence)
    Bayesian,

    /// Winner-take-all (strongest wins)
    WTA,
}

impl PlasticityRule {
    /// STDP-like plasticity (default for neural memory)
    pub fn stdp_like() -> Self {
        Self {
            policy: UpdatePolicy::STDP,
            novelty_threshold: Signal::positive(153),  // ~0.6
            merge_threshold: Signal::positive(77),     // ~0.3
            decay_rate: Signal::positive(3),           // ~0.01
            prune_threshold: Signal::positive(26),     // ~0.1
            learning_rate: Signal::positive(26),       // ~0.1
        }
    }

    /// Conservative plasticity (high novelty threshold, slow updates)
    pub fn conservative() -> Self {
        Self {
            policy: UpdatePolicy::EMA,
            novelty_threshold: Signal::positive(204),  // ~0.8
            merge_threshold: Signal::positive(102),    // ~0.4
            decay_rate: Signal::positive(1),           // ~0.005
            prune_threshold: Signal::positive(13),     // ~0.05
            learning_rate: Signal::positive(13),       // ~0.05
        }
    }

    /// Aggressive plasticity (low novelty threshold, fast updates)
    pub fn aggressive() -> Self {
        Self {
            policy: UpdatePolicy::STDP,
            novelty_threshold: Signal::positive(102),  // ~0.4
            merge_threshold: Signal::positive(51),     // ~0.2
            decay_rate: Signal::positive(5),           // ~0.02
            prune_threshold: Signal::positive(51),     // ~0.2
            learning_rate: Signal::positive(51),       // ~0.2
        }
    }

    /// No plasticity (simple replacement)
    pub fn replace_only() -> Self {
        Self {
            policy: UpdatePolicy::Replace,
            novelty_threshold: Signal::positive(255),  // 1.0 - never create new
            merge_threshold: Signal::positive(0),      // 0.0 - never merge
            decay_rate: Signal::positive(0),           // 0.0 - no decay
            prune_threshold: Signal::positive(0),      // 0.0 - never prune
            learning_rate: Signal::positive(255),      // 1.0 - full replacement
        }
    }

    /// Bayesian update (confidence-weighted)
    pub fn bayesian() -> Self {
        Self {
            policy: UpdatePolicy::Bayesian,
            novelty_threshold: Signal::positive(179),  // ~0.7
            merge_threshold: Signal::positive(77),     // ~0.3
            decay_rate: Signal::positive(3),           // ~0.01
            prune_threshold: Signal::positive(26),     // ~0.1
            learning_rate: Signal::positive(26),       // ~0.1
        }
    }

    /// Apply this rule to decide how to update
    ///
    /// Returns the new strength for the memory based on:
    /// - Current strength (Signal)
    /// - New observation strength (Signal)
    /// - Time since last update (seconds)
    ///
    /// All arithmetic uses Signal magnitudes. The result preserves
    /// the polarity of the stronger contributor.
    pub fn apply_update(
        &self,
        current_strength: Signal,
        new_strength: Signal,
        time_delta_seconds: f64,
    ) -> Signal {
        let cur_f = current_strength.magnitude_f32();
        let new_f = new_strength.magnitude_f32();
        let decay_f = self.decay_rate.magnitude_f32();
        let lr_f = self.learning_rate.magnitude_f32();

        let result_f = match self.policy {
            UpdatePolicy::STDP => {
                // Decay old, strengthen with new
                let decayed = cur_f * (1.0 - decay_f * time_delta_seconds as f32 / 86400.0);
                let updated = decayed + lr_f * new_f;
                updated.clamp(0.0, 1.0)
            }

            UpdatePolicy::Replace => new_f,

            UpdatePolicy::EMA => {
                // Exponential moving average
                lr_f * new_f + (1.0 - lr_f) * cur_f
            }

            UpdatePolicy::Bayesian => {
                // Weighted average by confidence
                let total_weight = cur_f + new_f;
                if total_weight > 0.0 {
                    (cur_f * cur_f + new_f * new_f) / total_weight
                } else {
                    0.0
                }
            }

            UpdatePolicy::WTA => {
                // Winner takes all
                cur_f.max(new_f)
            }
        };

        // Preserve polarity: use new_strength polarity if it's stronger,
        // otherwise keep current polarity
        let polarity = if new_f >= cur_f {
            new_strength.polarity
        } else {
            current_strength.polarity
        };

        Signal::new(polarity, (result_f * 255.0) as u8)
    }

    /// Should this observation create a new memory?
    pub fn should_create_new(&self, novelty_score: Signal) -> bool {
        novelty_score.magnitude > self.novelty_threshold.magnitude
    }

    /// Should this observation merge with existing?
    /// Takes similarity as a Signal magnitude (higher = more similar)
    pub fn should_merge(&self, similarity: Signal) -> bool {
        // Merge when similarity > (1.0 - threshold)
        let anti_threshold = 255 - self.merge_threshold.magnitude;
        similarity.magnitude > anti_threshold
    }

    /// Should this memory be pruned?
    pub fn should_prune_signal(&self, strength: Signal) -> bool {
        strength.magnitude < self.prune_threshold.magnitude
    }

    // =========================================================================
    // TERNARY PLASTICITY - Discrete state transitions for quantized weights
    // =========================================================================

    /// Apply ternary update based on policy
    ///
    /// For ternary weights, updates happen via discrete state transitions:
    /// - **Strengthen**: Neg→Zero→Pos (when confident observation agrees)
    /// - **Weaken**: Pos→Zero→Neg (when confident observation disagrees)
    /// - **No change**: When confidence is too low
    ///
    /// The `confidence` parameter (0.0-1.0) determines transition probability.
    pub fn apply_ternary_update(
        &self,
        current: TernaryWeight,
        observed: TernaryWeight,
        confidence: f32,
    ) -> TernaryWeight {
        match self.policy {
            UpdatePolicy::STDP => {
                if current == observed {
                    if confidence > 0.7 {
                        current.strengthen()
                    } else {
                        current
                    }
                } else if confidence > 0.5 {
                    self.move_toward(current, observed)
                } else {
                    self.decay_toward_zero(current)
                }
            }

            UpdatePolicy::Replace => {
                if confidence > 0.5 {
                    observed
                } else {
                    current
                }
            }

            UpdatePolicy::EMA => {
                if current == observed {
                    current
                } else if confidence > self.learning_rate.magnitude_f32() {
                    observed
                } else {
                    current
                }
            }

            UpdatePolicy::Bayesian => {
                if confidence > 0.7 {
                    observed
                } else if confidence > 0.3 {
                    self.decay_toward_zero(current)
                } else {
                    current
                }
            }

            UpdatePolicy::WTA => {
                if confidence > 0.5 {
                    observed
                } else {
                    current
                }
            }
        }
    }

    /// Move current weight one step toward target
    fn move_toward(&self, current: TernaryWeight, target: TernaryWeight) -> TernaryWeight {
        use TernaryWeight::*;
        match (current, target) {
            (Pos, Pos) | (Zero, Zero) | (Neg, Neg) => current,
            (Zero, Pos) | (Neg, Pos) => current.strengthen(),
            (Zero, Neg) | (Pos, Neg) => current.weaken(),
            (Pos, Zero) => current.weaken(),
            (Neg, Zero) => current.strengthen(),
        }
    }

    /// Decay weight toward zero (for low-confidence or aging)
    fn decay_toward_zero(&self, current: TernaryWeight) -> TernaryWeight {
        match current {
            TernaryWeight::Pos => TernaryWeight::Zero,
            TernaryWeight::Neg => TernaryWeight::Zero,
            TernaryWeight::Zero => TernaryWeight::Zero,
        }
    }

    /// Public method to decay weight toward zero
    /// Used by embedded_snn for ternary weight aging
    pub fn decay_toward_zero_public(&self, current: TernaryWeight) -> TernaryWeight {
        self.decay_toward_zero(current)
    }

    /// Should this ternary weight be pruned?
    ///
    /// For ternary weights, only Zero weights are prunable (inactive connections).
    pub fn should_prune_ternary(&self, weight: TernaryWeight) -> bool {
        weight == TernaryWeight::Zero
    }

    /// Apply STDP-style ternary update for co-firing neurons
    ///
    /// Pre→Post firing (causal): strengthen connection
    /// Post→Pre firing (anti-causal): weaken connection
    pub fn apply_ternary_stdp(
        &self,
        current: TernaryWeight,
        pre_fired: bool,
        post_fired: bool,
        time_delta_ms: i64,
    ) -> TernaryWeight {
        if pre_fired && post_fired {
            if time_delta_ms > 0 {
                current.strengthen()
            } else if time_delta_ms < 0 {
                current.weaken()
            } else {
                if current == TernaryWeight::Neg {
                    TernaryWeight::Zero
                } else {
                    current
                }
            }
        } else {
            current
        }
    }

    /// Ternary majority voting for merge decisions
    ///
    /// Given multiple ternary weights, return the majority value.
    pub fn ternary_majority_vote(weights: &[TernaryWeight]) -> TernaryWeight {
        let mut pos_count = 0i32;
        let mut neg_count = 0i32;

        for &w in weights {
            match w {
                TernaryWeight::Pos => pos_count += 1,
                TernaryWeight::Neg => neg_count += 1,
                TernaryWeight::Zero => {}
            }
        }

        if pos_count > neg_count {
            TernaryWeight::Pos
        } else if neg_count > pos_count {
            TernaryWeight::Neg
        } else {
            TernaryWeight::Zero
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stdp_update() {
        let rule = PlasticityRule::stdp_like();

        let current = Signal::positive(128); // ~0.5
        let new = Signal::positive(204);     // ~0.8
        let time_delta = 0.0; // Immediate

        let updated = rule.apply_update(current, new, time_delta);

        // Should strengthen (learning_rate * new adds to current)
        assert!(updated.magnitude > current.magnitude);
        assert!(updated.magnitude <= 255);
    }

    #[test]
    fn test_novelty_threshold() {
        let rule = PlasticityRule::stdp_like();

        assert!(rule.should_create_new(Signal::positive(179)));  // ~0.7, above 0.6 threshold
        assert!(!rule.should_create_new(Signal::positive(128))); // ~0.5, below 0.6 threshold
    }

    #[test]
    fn test_merge_decision() {
        let rule = PlasticityRule::stdp_like();

        // merge_threshold is ~0.3, so merge when similarity > 0.7 (255 - 77 = 178)
        assert!(rule.should_merge(Signal::positive(204)));  // High similarity
        assert!(!rule.should_merge(Signal::positive(153))); // Low similarity
    }

    #[test]
    fn test_prune_decision() {
        let rule = PlasticityRule::stdp_like();

        assert!(rule.should_prune_signal(Signal::positive(13)));  // Below threshold (~0.1 = 26)
        assert!(!rule.should_prune_signal(Signal::positive(128))); // Above threshold
    }

    #[test]
    fn test_ema_update() {
        let rule = PlasticityRule {
            policy: UpdatePolicy::EMA,
            learning_rate: Signal::positive(26), // ~0.1
            ..PlasticityRule::stdp_like()
        };

        let current = Signal::positive(128); // ~0.5
        let new = Signal::positive(255);     // ~1.0

        let updated = rule.apply_update(current, new, 0.0);

        // Should be weighted average: 0.1 * 1.0 + 0.9 * 0.5 = 0.55
        // magnitude ≈ 140
        let expected_f = 0.1 * 1.0 + 0.9 * (128.0 / 255.0);
        let expected_mag = (expected_f * 255.0) as u8;
        // Allow some rounding tolerance
        assert!((updated.magnitude as i16 - expected_mag as i16).unsigned_abs() <= 3);
    }
}