optirs-core 0.3.2

OptiRS core optimization algorithms and utilities
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
// Event-Driven Optimization Algorithms
//
// This module implements event-driven optimization algorithms that process
// updates asynchronously based on neuromorphic events, designed for
// neuromorphic computing platforms with event-based architectures.

use super::{
    to_generic_or, EventPriority, MembraneDynamicsConfig, NeuromorphicEvent, NeuromorphicMetrics,
    STDPConfig,
};
use crate::error::{OptimError, Result};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use std::cmp::Reverse;
use std::collections::{BTreeMap, BinaryHeap, HashMap, HashSet, VecDeque};
use std::fmt::Debug;
use std::time::{Duration, Instant};

// --- Pure-Rust varint helpers for event (de)serialization (F57) --------
//
// LEB128 unsigned varints, with zigzag encoding for signed values. Used by
// `EventCompressionEngine` to produce real, decodable bytes for events
// instead of fixed-size placeholder buffers.

fn write_uvarint(buf: &mut Vec<u8>, mut value: u64) {
    loop {
        let mut byte = (value & 0x7f) as u8;
        value >>= 7;
        if value != 0 {
            byte |= 0x80;
        }
        buf.push(byte);
        if value == 0 {
            break;
        }
    }
}

fn read_uvarint(buf: &[u8], pos: &mut usize) -> Option<u64> {
    let mut result: u64 = 0;
    let mut shift: u32 = 0;
    loop {
        let byte = *buf.get(*pos)?;
        *pos += 1;
        result |= ((byte & 0x7f) as u64) << shift;
        if byte & 0x80 == 0 {
            break;
        }
        shift += 7;
        if shift >= 64 {
            return None;
        }
    }
    Some(result)
}

fn zigzag_encode(value: i64) -> u64 {
    ((value << 1) ^ (value >> 63)) as u64
}

fn zigzag_decode(value: u64) -> i64 {
    ((value >> 1) as i64) ^ -((value & 1) as i64)
}

fn write_ivarint(buf: &mut Vec<u8>, value: i64) {
    write_uvarint(buf, zigzag_encode(value));
}

fn read_ivarint(buf: &[u8], pos: &mut usize) -> Option<i64> {
    read_uvarint(buf, pos).map(zigzag_decode)
}

/// Fixed-point scale used to convert floating-point event fields
/// (timestamps, neuron ids treated as integers, values, energy costs) to
/// integers before varint encoding. Millisecond timestamps are kept to
/// microsecond resolution.
const EVENT_FIXED_POINT_SCALE: f64 = 1000.0;

fn decode_event_type(byte: u8) -> Result<EventType> {
    match byte {
        0 => Ok(EventType::Spike),
        1 => Ok(EventType::WeightUpdate),
        2 => Ok(EventType::ThresholdCrossing),
        3 => Ok(EventType::PlasticityEvent),
        4 => Ok(EventType::ExternalStimulus),
        5 => Ok(EventType::TimerEvent),
        6 => Ok(EventType::ErrorEvent),
        7 => Ok(EventType::HomeostaticEvent),
        8 => Ok(EventType::SynchronizationEvent),
        9 => Ok(EventType::EnergyEvent),
        other => Err(OptimError::InvalidConfig(format!(
            "unknown encoded EventType discriminant: {other}"
        ))),
    }
}

fn decode_priority(byte: u8) -> Result<EventPriority> {
    match byte {
        0 => Ok(EventPriority::Low),
        1 => Ok(EventPriority::Normal),
        2 => Ok(EventPriority::High),
        3 => Ok(EventPriority::Critical),
        4 => Ok(EventPriority::RealTime),
        other => Err(OptimError::InvalidConfig(format!(
            "unknown encoded EventPriority discriminant: {other}"
        ))),
    }
}

fn truncated_bytes_err() -> crate::error::OptimError {
    OptimError::InvalidConfig("truncated compressed event bytes".to_string())
}

/// Event types for neuromorphic computing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EventType {
    /// Spike event from a neuron
    Spike,

    /// Synaptic weight update event
    WeightUpdate,

    /// Membrane potential threshold crossing
    ThresholdCrossing,

    /// Plasticity-triggered event
    PlasticityEvent,

    /// External stimulus event
    ExternalStimulus,

    /// Timer-based event
    TimerEvent,

    /// Error backpropagation event
    ErrorEvent,

    /// Homeostatic adaptation event
    HomeostaticEvent,

    /// Population synchronization event
    SynchronizationEvent,

    /// Energy budget event
    EnergyEvent,
}

/// Event-driven optimization configuration
#[derive(Debug, Clone)]
pub struct EventDrivenConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum event queue size
    pub max_queue_size: usize,

    /// Event processing timeout (ms)
    pub processing_timeout: T,

    /// Enable event priority scheduling
    pub priority_scheduling: bool,

    /// Event filtering threshold
    pub event_threshold: T,

    /// Enable event batching
    pub event_batching: bool,

    /// Batch size for event processing
    pub batch_size: usize,

    /// Enable temporal event correlation
    pub temporal_correlation: bool,

    /// Temporal correlation window (ms)
    pub correlation_window: T,

    /// Enable adaptive event handling
    pub adaptive_handling: bool,

    /// Event rate limits (events/second)
    pub rate_limits: HashMap<EventType, T>,

    /// Enable event compression
    pub event_compression: bool,

    /// Compression algorithm
    pub compression_algorithm: EventCompressionAlgorithm,

    /// Enable distributed event processing
    pub distributed_processing: bool,

    /// Load balancing strategy
    pub load_balancing: LoadBalancingStrategy,
}

/// Event compression algorithms
#[derive(Debug, Clone, Copy)]
pub enum EventCompressionAlgorithm {
    /// No compression
    None,

    /// Delta encoding
    DeltaEncoding,

    /// Huffman encoding
    HuffmanEncoding,

    /// Run-length encoding
    RunLengthEncoding,

    /// Sparse encoding
    SparseEncoding,

    /// Predictive encoding
    PredictiveEncoding,
}

/// Load balancing strategies for distributed event processing
#[derive(Debug, Clone, Copy)]
pub enum LoadBalancingStrategy {
    /// Round-robin distribution
    RoundRobin,

    /// Event type-based partitioning
    TypeBased,

    /// Load-aware distribution
    LoadAware,

    /// Locality-aware distribution
    LocalityAware,

    /// Dynamic load balancing
    Dynamic,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for EventDrivenConfig<T> {
    fn default() -> Self {
        let mut rate_limits = HashMap::new();
        rate_limits.insert(
            EventType::Spike,
            T::from(1000.0).unwrap_or_else(|| T::zero()),
        );
        rate_limits.insert(
            EventType::WeightUpdate,
            T::from(100.0).unwrap_or_else(|| T::zero()),
        );
        rate_limits.insert(
            EventType::PlasticityEvent,
            T::from(50.0).unwrap_or_else(|| T::zero()),
        );

        Self {
            max_queue_size: 10000,
            processing_timeout: T::from(1.0).unwrap_or_else(|| T::zero()),
            priority_scheduling: true,
            event_threshold: T::from(0.001).unwrap_or_else(|| T::zero()),
            event_batching: true,
            batch_size: 32,
            temporal_correlation: true,
            correlation_window: T::from(10.0).unwrap_or_else(|| T::zero()),
            adaptive_handling: true,
            rate_limits,
            event_compression: false,
            compression_algorithm: EventCompressionAlgorithm::None,
            distributed_processing: false,
            load_balancing: LoadBalancingStrategy::RoundRobin,
        }
    }
}

/// Priority queue entry for event scheduling
#[derive(Debug, Clone)]
struct PriorityEventEntry<T: Float + Debug + Send + Sync + 'static> {
    event: NeuromorphicEvent<T>,
    insertion_time: Instant,
}

impl<T: Float + Debug + Send + Sync + 'static> PartialEq for PriorityEventEntry<T> {
    fn eq(&self, other: &Self) -> bool {
        self.event.priority == other.event.priority
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Eq for PriorityEventEntry<T> {}

impl<T: Float + Debug + Send + Sync + 'static> PartialOrd for PriorityEventEntry<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Ord for PriorityEventEntry<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // `BinaryHeap` is a max-heap: `pop()` returns the "greatest"
        // element. We want higher `EventPriority` to pop first, so
        // compare priorities directly rather than reversed (F55: the
        // previous `other.priority.cmp(&self.priority)` inverted this,
        // so pop() returned the LOWEST-priority event first). Ties break
        // FIFO: the earlier `insertion_time` must compare as "greater" so
        // it pops first, which is exactly what wrapping both sides in
        // `Reverse` gives us.
        self.event
            .priority
            .cmp(&other.event.priority)
            .then_with(|| Reverse(self.insertion_time).cmp(&Reverse(other.insertion_time)))
    }
}

/// Event-driven optimizer
pub struct EventDrivenOptimizer<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration
    config: EventDrivenConfig<T>,

    /// STDP configuration
    stdp_config: STDPConfig<T>,

    /// Membrane dynamics configuration
    membrane_config: MembraneDynamicsConfig<T>,

    /// Event queue with priority scheduling
    event_queue: BinaryHeap<PriorityEventEntry<T>>,

    /// Event processing statistics
    event_stats: HashMap<EventType, EventStatistics<T>>,

    /// Current system state
    system_state: SystemState<T>,

    /// Event handlers
    event_handlers: HashMap<EventType, Box<dyn EventHandler<T>>>,

    /// Temporal correlation tracker
    correlation_tracker: TemporalCorrelationTracker<T>,

    /// Event rate limiter
    rate_limiter: EventRateLimiter<T>,

    /// Performance metrics
    metrics: NeuromorphicMetrics<T>,

    /// Distributed processing coordinator
    distributed_coordinator: Option<DistributedEventCoordinator<T>>,

    /// Reference (uncompressed) codec, used to measure how many bytes each
    /// event would occupy without compression so the achieved compression
    /// ratio reported by [`EventDrivenOptimizer::compression_ratio`] is a
    /// real measurement rather than an estimate.
    compression_engine: EventCompressionEngine<T>,

    /// Compressed event storage, one FIFO chain per [`EventPriority`] (F57).
    /// Used instead of `event_queue` while `config.event_compression` is
    /// enabled: enqueued events are compressed to bytes immediately and only
    /// reconstructed when they are actually processed.
    compressed_chains: BTreeMap<EventPriority, CompressedEventChain<T>>,

    /// Cumulative uncompressed size of every event that entered the
    /// compressed queue.
    compression_raw_bytes: usize,

    /// Cumulative compressed size of every event that entered the compressed
    /// queue.
    compression_compressed_bytes: usize,

    /// Adaptive handler
    adaptive_handler: AdaptiveEventHandler<T>,
}

/// Event processing statistics
#[derive(Debug, Clone)]
pub struct EventStatistics<T: Float + Debug + Send + Sync + 'static> {
    /// Total events processed
    pub total_processed: usize,

    /// Average processing time (ms)
    pub avg_processing_time: T,

    /// Event rate (events/second)
    pub event_rate: T,

    /// Queue wait time (ms)
    pub avg_queue_wait_time: T,

    /// Error count
    pub error_count: usize,

    /// Last update time
    pub last_update: Instant,
}

/// System state for event-driven optimization
#[derive(Debug, Clone)]
pub struct SystemState<T: Float + Debug + Send + Sync + 'static> {
    /// Current membrane potentials
    pub membrane_potentials: Array1<T>,

    /// Synaptic weights
    pub synaptic_weights: Array2<T>,

    /// Last spike times
    pub last_spike_times: Array1<T>,

    /// Refractory states
    pub refractory_until: Array1<T>,

    /// Current simulation time
    pub current_time: T,

    /// Active neurons
    pub active_neurons: HashSet<usize>,

    /// Pending weight updates
    pub pending_updates: HashMap<(usize, usize), T>,
}

/// Event handler trait
trait EventHandler<T: Float + Debug + Send + Sync + 'static>: Send + Sync {
    fn handle_event(
        &mut self,
        event: &NeuromorphicEvent<T>,
        state: &mut SystemState<T>,
    ) -> Result<()>;
}

/// Spike event handler
struct SpikeEventHandler<T: Float + Debug + Send + Sync + 'static> {
    stdp_config: STDPConfig<T>,
    membrane_config: MembraneDynamicsConfig<T>,
}

impl<T: Float + Debug + Send + Sync + 'static> EventHandler<T> for SpikeEventHandler<T> {
    fn handle_event(
        &mut self,
        event: &NeuromorphicEvent<T>,
        state: &mut SystemState<T>,
    ) -> Result<()> {
        let neuron_id = event.source_neuron;

        // Generate spike
        if neuron_id < state.membrane_potentials.len() {
            // Reset membrane potential
            state.membrane_potentials[neuron_id] = self.membrane_config.reset_potential;

            // Set refractory period
            state.refractory_until[neuron_id] =
                state.current_time + self.membrane_config.refractory_period;

            // Update last spike time
            state.last_spike_times[neuron_id] = state.current_time;

            // Add to active neurons
            state.active_neurons.insert(neuron_id);

            // Trigger STDP updates for connected synapses
            self.trigger_stdp_updates(neuron_id, state)?;
        }

        Ok(())
    }
}

impl<T: Float + Debug + Send + Sync + 'static> SpikeEventHandler<T> {
    fn trigger_stdp_updates(&self, post_neuron: usize, state: &mut SystemState<T>) -> Result<()> {
        let long_ago = to_generic_or(-1000.0, T::zero());
        let now = state.current_time;

        for other_neuron in 0..state.last_spike_times.len() {
            if other_neuron == post_neuron {
                continue;
            }
            let other_spike_time = state.last_spike_times[other_neuron];
            if other_spike_time <= long_ago {
                continue; // no valid spike history for `other_neuron` yet
            }

            // `other_neuron` fired before `post_neuron` (now): it is PRE,
            // dt = t_post - t_pre > 0 => potentiation (LTP) on
            // other_neuron -> post_neuron.
            let dt_ltp = now - other_spike_time;
            let ltp = self.compute_stdp_weight_change(dt_ltp);
            Self::accumulate_pending(state, (other_neuron, post_neuron), ltp);

            // `post_neuron` is firing NOW, arriving after
            // `other_neuron`'s last spike: from `other_neuron`'s
            // perspective as POST, this is a PRE spike arriving late,
            // dt = t_post - t_pre = other_spike_time - now < 0 =>
            // depression (LTD) on post_neuron -> other_neuron. This is
            // the presynaptic-trace side of STDP that was previously
            // unreachable (F50): `dt` computed only from "post's own time
            // minus pre's last (necessarily past) spike time" is always
            // >= 0, so LTD never fired.
            let dt_ltd = other_spike_time - now;
            let ltd = self.compute_stdp_weight_change(dt_ltd);
            Self::accumulate_pending(state, (post_neuron, other_neuron), ltd);
        }

        Ok(())
    }

    /// Accumulate a weight delta into `pending_updates` (F56): the
    /// previous `insert(...)` overwrote any existing pending update for
    /// the same `(pre, post)` pair instead of summing contributions from
    /// multiple presynaptic partners within the same batch.
    fn accumulate_pending(state: &mut SystemState<T>, key: (usize, usize), delta: T) {
        let entry = state.pending_updates.entry(key).or_insert_with(T::zero);
        *entry = *entry + delta;
    }

    fn compute_stdp_weight_change(&self, dt: T) -> T {
        if dt > T::zero() {
            // Post-before-pre: LTP
            let exp_arg = -dt / self.stdp_config.tau_pot;
            self.stdp_config.learning_rate_pot * exp_arg.exp()
        } else {
            // Pre-before-post: LTD
            let exp_arg = dt / self.stdp_config.tau_dep;
            -self.stdp_config.learning_rate_dep * exp_arg.exp()
        }
    }
}

/// Weight update event handler
struct WeightUpdateEventHandler<T: Float + Debug + Send + Sync + 'static> {
    stdp_config: STDPConfig<T>,
}

impl<T: Float + Debug + Send + Sync + 'static> EventHandler<T> for WeightUpdateEventHandler<T> {
    fn handle_event(
        &mut self,
        event: &NeuromorphicEvent<T>,
        state: &mut SystemState<T>,
    ) -> Result<()> {
        let source = event.source_neuron;

        if let Some(target) = event.target_neuron {
            if source < state.synaptic_weights.nrows() && target < state.synaptic_weights.ncols() {
                // Apply weight update
                let current_weight = state.synaptic_weights[[source, target]];
                let new_weight = (current_weight + event.value)
                    .max(self.stdp_config.weight_min)
                    .min(self.stdp_config.weight_max);

                state.synaptic_weights[[source, target]] = new_weight;
            }
        }

        Ok(())
    }
}

/// Temporal correlation tracker
struct TemporalCorrelationTracker<T: Float + Debug + Send + Sync + 'static> {
    correlation_window: T,
    event_history: VecDeque<(T, EventType, usize)>,
    correlation_patterns: HashMap<(EventType, EventType), T>,
}

impl<T: Float + Debug + Send + Sync + 'static + std::ops::AddAssign> TemporalCorrelationTracker<T> {
    fn new(correlation_window: T) -> Self {
        Self {
            correlation_window,
            event_history: VecDeque::new(),
            correlation_patterns: HashMap::new(),
        }
    }

    fn add_event(&mut self, time: T, event_type: EventType, neuron_id: usize) {
        // Add new event
        self.event_history.push_back((time, event_type, neuron_id));

        // Remove old events outside correlation window
        while let Some(&(old_time, _, _)) = self.event_history.front() {
            if time - old_time > self.correlation_window {
                self.event_history.pop_front();
            } else {
                break;
            }
        }

        // Update correlation patterns
        self.update_correlations(time, event_type);
    }

    fn update_correlations(&mut self, current_time: T, current_event: EventType) {
        for &(event_time, event_type_, _) in &self.event_history {
            if current_time - event_time <= self.correlation_window {
                let correlation_key = (event_type_, current_event);
                let time_diff = current_time - event_time;
                let correlation_strength = (-time_diff / self.correlation_window).exp();

                *self
                    .correlation_patterns
                    .entry(correlation_key)
                    .or_insert(T::zero()) += correlation_strength;
            }
        }
    }

    /// Measured co-occurrence strength between two event types.
    pub(crate) fn get_correlation(&self, event1: EventType, event2: EventType) -> T {
        self.correlation_patterns
            .get(&(event1, event2))
            .copied()
            .unwrap_or(T::zero())
    }
}

/// Event rate limiter
struct EventRateLimiter<T: Float + Debug + Send + Sync + 'static> {
    rate_limits: HashMap<EventType, T>,
    event_counts: HashMap<EventType, usize>,
    last_reset: Instant,
    reset_interval: Duration,
}

impl<T: Float + Debug + Send + Sync + 'static> EventRateLimiter<T> {
    fn new(rate_limits: HashMap<EventType, T>) -> Self {
        Self {
            rate_limits,
            event_counts: HashMap::new(),
            last_reset: Instant::now(),
            reset_interval: Duration::from_secs(1),
        }
    }

    fn can_process(&mut self, event_type: EventType) -> bool {
        // Reset counters if interval elapsed
        if self.last_reset.elapsed() >= self.reset_interval {
            self.event_counts.clear();
            self.last_reset = Instant::now();
        }

        if let Some(&limit) = self.rate_limits.get(&event_type) {
            let current_count = self.event_counts.get(&event_type).copied().unwrap_or(0);
            if T::from(current_count).unwrap_or_else(|| T::zero()) < limit {
                *self.event_counts.entry(event_type).or_insert(0) += 1;
                true
            } else {
                false
            }
        } else {
            true
        }
    }
}

/// Event compression engine
struct EventCompressionEngine<T: Float + Debug + Send + Sync + 'static> {
    algorithm: EventCompressionAlgorithm,
    compression_buffer: Vec<u8>,
    decompression_buffer: Vec<u8>,
    /// Last event's (fixed-point-scaled) fields, used as the delta baseline
    /// by [`Self::delta_encode_event`]/[`Self::delta_decode_event`] (F57).
    /// `None` until the first event has been compressed.
    last_event_fields: Option<(i64, i64, Option<i64>, i64, i64)>,
    _phantom: std::marker::PhantomData<T>,
}

/// Convert a float event field to a fixed-point integer at
/// [`EVENT_FIXED_POINT_SCALE`] resolution, saturating instead of panicking
/// on out-of-range or non-finite values.
fn field_to_fixed<T: Float>(value: T) -> i64 {
    let scaled = value.to_f64().unwrap_or(0.0) * EVENT_FIXED_POINT_SCALE;
    if !scaled.is_finite() {
        0
    } else {
        scaled.clamp(i64::MIN as f64, i64::MAX as f64) as i64
    }
}

fn fixed_to_field<T: Float>(fixed: i64) -> T {
    to_generic_or(fixed as f64 / EVENT_FIXED_POINT_SCALE, T::zero())
}

impl<T: Float + Debug + Send + Sync + 'static> EventCompressionEngine<T> {
    fn new(algorithm: EventCompressionAlgorithm) -> Self {
        Self {
            algorithm,
            compression_buffer: Vec::new(),
            decompression_buffer: Vec::new(),
            last_event_fields: None,
            _phantom: std::marker::PhantomData,
        }
    }

    fn compress_event(&mut self, event: &NeuromorphicEvent<T>) -> Result<Vec<u8>> {
        let compressed = match self.algorithm {
            EventCompressionAlgorithm::None => {
                // No compression, serialize directly
                self.serialize_event(event)
            }
            EventCompressionAlgorithm::DeltaEncoding => self.delta_encode_event(event),
            EventCompressionAlgorithm::SparseEncoding => self.sparse_encode_event(event),
            _ => {
                // Fallback to no compression
                self.serialize_event(event)
            }
        }?;
        self.compression_buffer.clear();
        self.compression_buffer.extend_from_slice(&compressed);
        Ok(compressed)
    }

    /// Reconstruct a [`NeuromorphicEvent`] from bytes produced by
    /// [`Self::compress_event`], using the same algorithm and delta-baseline
    /// state. Must be called in the same order events were compressed when
    /// `DeltaEncoding` is in use, since each event's baseline is the
    /// previously *decoded* one.
    fn decompress_event(&mut self, bytes: &[u8]) -> Result<NeuromorphicEvent<T>> {
        self.decompression_buffer.clear();
        self.decompression_buffer.extend_from_slice(bytes);
        match self.algorithm {
            EventCompressionAlgorithm::DeltaEncoding => self.delta_decode_event(bytes),
            EventCompressionAlgorithm::SparseEncoding => self.sparse_decode_event(bytes),
            _ => self.deserialize_event(bytes),
        }
    }

    /// Full-fidelity, self-contained encoding of every event field as
    /// LEB128 varints (unsigned for the always-non-negative fields,
    /// zigzag for `value`/`energy_cost` which may be negative). This is
    /// both `EventCompressionAlgorithm::None`'s wire format and the
    /// decoding target every other algorithm falls back to.
    fn serialize_event(&self, event: &NeuromorphicEvent<T>) -> Result<Vec<u8>> {
        let mut data = Vec::new();
        data.push(event.event_type as u8);
        data.push(event.priority as u8);
        write_uvarint(&mut data, event.source_neuron as u64);
        match event.target_neuron {
            Some(target) => {
                data.push(1);
                write_uvarint(&mut data, target as u64);
            }
            None => data.push(0),
        }
        write_ivarint(&mut data, field_to_fixed(event.timestamp));
        write_ivarint(&mut data, field_to_fixed(event.value));
        write_ivarint(&mut data, field_to_fixed(event.energy_cost));
        Ok(data)
    }

    fn deserialize_event(&self, bytes: &[u8]) -> Result<NeuromorphicEvent<T>> {
        let mut pos = 0usize;
        let event_type = decode_event_type(*bytes.first().ok_or_else(truncated_bytes_err)?)?;
        pos += 1;
        let priority = decode_priority(*bytes.get(pos).ok_or_else(truncated_bytes_err)?)?;
        pos += 1;
        let source_neuron = read_uvarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)? as usize;
        let has_target = *bytes.get(pos).ok_or_else(truncated_bytes_err)?;
        pos += 1;
        let target_neuron = if has_target == 1 {
            Some(read_uvarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)? as usize)
        } else {
            None
        };
        let timestamp =
            fixed_to_field(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?);
        let value = fixed_to_field(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?);
        let energy_cost =
            fixed_to_field(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?);
        Ok(NeuromorphicEvent {
            event_type,
            timestamp,
            source_neuron,
            target_neuron,
            value,
            energy_cost,
            priority,
        })
    }

    /// Delta encoding (F57): every numeric field is written as its
    /// varint-encoded difference from the previous compressed event's
    /// corresponding field, rather than its absolute value. For a stream of
    /// similar consecutive events (the common neuromorphic case — spikes
    /// from nearby neurons close together in time) small deltas take far
    /// fewer varint bytes than the absolute fixed-point values. The first
    /// event in a stream has no baseline and is encoded as full deltas from
    /// zero, which is exactly `serialize_event`'s absolute encoding.
    fn delta_encode_event(&mut self, event: &NeuromorphicEvent<T>) -> Result<Vec<u8>> {
        let ts = field_to_fixed(event.timestamp);
        let src = event.source_neuron as i64;
        let tgt = event.target_neuron.map(|t| t as i64);
        let val = field_to_fixed(event.value);
        let energy = field_to_fixed(event.energy_cost);

        let (base_ts, base_src, base_tgt, base_val, base_energy) =
            self.last_event_fields.unwrap_or((0, 0, None, 0, 0));

        let mut data = Vec::new();
        data.push(event.event_type as u8);
        data.push(event.priority as u8);
        write_ivarint(&mut data, src - base_src);
        match (tgt, base_tgt) {
            (Some(t), Some(b)) => {
                data.push(1);
                write_ivarint(&mut data, t - b);
            }
            (Some(t), None) => {
                data.push(2); // "present, no prior baseline": encode absolute
                write_ivarint(&mut data, t);
            }
            (None, _) => data.push(0),
        }
        write_ivarint(&mut data, ts - base_ts);
        write_ivarint(&mut data, val - base_val);
        write_ivarint(&mut data, energy - base_energy);

        self.last_event_fields = Some((ts, src, tgt, val, energy));
        Ok(data)
    }

    fn delta_decode_event(&mut self, bytes: &[u8]) -> Result<NeuromorphicEvent<T>> {
        let mut pos = 0usize;
        let event_type = decode_event_type(*bytes.first().ok_or_else(truncated_bytes_err)?)?;
        pos += 1;
        let priority = decode_priority(*bytes.get(pos).ok_or_else(truncated_bytes_err)?)?;
        pos += 1;

        let (base_ts, base_src, base_tgt, base_val, base_energy) =
            self.last_event_fields.unwrap_or((0, 0, None, 0, 0));

        let src = base_src + read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?;
        let target_tag = *bytes.get(pos).ok_or_else(truncated_bytes_err)?;
        pos += 1;
        let tgt = match target_tag {
            0 => None,
            1 => {
                let base = base_tgt.ok_or_else(|| {
                    OptimError::InvalidConfig(
                        "delta-encoded target references a missing baseline".to_string(),
                    )
                })?;
                Some(base + read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?)
            }
            2 => Some(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?),
            other => {
                return Err(OptimError::InvalidConfig(format!(
                    "invalid delta-encoded target tag: {other}"
                )))
            }
        };
        let ts = base_ts + read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?;
        let val = base_val + read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?;
        let energy = base_energy + read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?;

        self.last_event_fields = Some((ts, src, tgt, val, energy));

        if src < 0 {
            return Err(OptimError::InvalidConfig(
                "delta-decoded source_neuron underflowed".to_string(),
            ));
        }
        Ok(NeuromorphicEvent {
            event_type,
            timestamp: fixed_to_field(ts),
            source_neuron: src as usize,
            target_neuron: match tgt {
                Some(t) if t >= 0 => Some(t as usize),
                Some(_) => {
                    return Err(OptimError::InvalidConfig(
                        "delta-decoded target_neuron underflowed".to_string(),
                    ))
                }
                None => None,
            },
            value: fixed_to_field(val),
            energy_cost: fixed_to_field(energy),
            priority,
        })
    }

    /// Sparse encoding (F57): most events carry a zero/default `value` and
    /// `energy_cost`, and most events have no `target_neuron` (broadcast
    /// events). A leading bitmask flags which optional fields are present,
    /// so all-zero/absent fields cost a single bit each instead of a full
    /// varint.
    fn sparse_encode_event(&mut self, event: &NeuromorphicEvent<T>) -> Result<Vec<u8>> {
        let has_target = event.target_neuron.is_some();
        let has_value = event.value != T::zero();
        let has_energy = event.energy_cost != T::zero();

        let mut mask = 0u8;
        if has_target {
            mask |= 0b001;
        }
        if has_value {
            mask |= 0b010;
        }
        if has_energy {
            mask |= 0b100;
        }

        let mut data = Vec::new();
        data.push(event.event_type as u8);
        data.push(event.priority as u8);
        data.push(mask);
        write_uvarint(&mut data, event.source_neuron as u64);
        write_ivarint(&mut data, field_to_fixed(event.timestamp));
        if let Some(target) = event.target_neuron {
            write_uvarint(&mut data, target as u64);
        }
        if has_value {
            write_ivarint(&mut data, field_to_fixed(event.value));
        }
        if has_energy {
            write_ivarint(&mut data, field_to_fixed(event.energy_cost));
        }
        Ok(data)
    }

    fn sparse_decode_event(&mut self, bytes: &[u8]) -> Result<NeuromorphicEvent<T>> {
        let mut pos = 0usize;
        let event_type = decode_event_type(*bytes.first().ok_or_else(truncated_bytes_err)?)?;
        pos += 1;
        let priority = decode_priority(*bytes.get(pos).ok_or_else(truncated_bytes_err)?)?;
        pos += 1;
        let mask = *bytes.get(pos).ok_or_else(truncated_bytes_err)?;
        pos += 1;
        let source_neuron = read_uvarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)? as usize;
        let timestamp =
            fixed_to_field(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?);
        let target_neuron = if mask & 0b001 != 0 {
            Some(read_uvarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)? as usize)
        } else {
            None
        };
        let value = if mask & 0b010 != 0 {
            fixed_to_field(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?)
        } else {
            T::zero()
        };
        let energy_cost = if mask & 0b100 != 0 {
            fixed_to_field(read_ivarint(bytes, &mut pos).ok_or_else(truncated_bytes_err)?)
        } else {
            T::zero()
        };
        Ok(NeuromorphicEvent {
            event_type,
            timestamp,
            source_neuron,
            target_neuron,
            value,
            energy_cost,
            priority,
        })
    }
}

/// A FIFO chain of *compressed* event frames for a single priority level.
///
/// F57 (wiring): compressed storage has to be decoded in the same order it
/// was encoded, because the delta/predictive codecs carry a running baseline
/// (`EventCompressionEngine::last_event_fields`) from one frame to the next.
/// The event scheduler, on the other hand, must still honour
/// [`EventPriority`]. Keeping one independent chain per priority level
/// satisfies both constraints at once: inside a chain the order is strictly
/// FIFO (so the delta baseline chain stays intact, matching the FIFO
/// tie-break the uncompressed [`BinaryHeap`] path uses), and the scheduler
/// just picks the highest-priority non-empty chain.
///
/// Two engines are held because the encoder necessarily runs ahead of the
/// decoder by `frames.len()` events, so they cannot share one baseline.
struct CompressedEventChain<T: Float + Debug + Send + Sync + 'static> {
    /// Encoder state; advanced by [`Self::push`].
    encoder: EventCompressionEngine<T>,
    /// Decoder state; advanced by [`Self::pop`], trailing the encoder.
    decoder: EventCompressionEngine<T>,
    /// Compressed frames, oldest first.
    frames: VecDeque<Vec<u8>>,
    /// Bytes currently held by `frames`.
    stored_bytes: usize,
}

impl<T: Float + Debug + Send + Sync + 'static> CompressedEventChain<T> {
    fn new(algorithm: EventCompressionAlgorithm) -> Self {
        Self {
            encoder: EventCompressionEngine::new(algorithm),
            decoder: EventCompressionEngine::new(algorithm),
            frames: VecDeque::new(),
            stored_bytes: 0,
        }
    }

    /// Compress `event` and append the resulting frame. Returns the number
    /// of bytes the frame occupies.
    fn push(&mut self, event: &NeuromorphicEvent<T>) -> Result<usize> {
        let frame = self.encoder.compress_event(event)?;
        let frame_len = frame.len();
        self.stored_bytes += frame_len;
        self.frames.push_back(frame);
        Ok(frame_len)
    }

    /// Decode and remove the oldest frame, reconstructing the event.
    fn pop(&mut self) -> Result<Option<NeuromorphicEvent<T>>> {
        match self.frames.pop_front() {
            Some(frame) => {
                self.stored_bytes = self.stored_bytes.saturating_sub(frame.len());
                Ok(Some(self.decoder.decompress_event(&frame)?))
            }
            None => Ok(None),
        }
    }

    fn len(&self) -> usize {
        self.frames.len()
    }

    fn is_empty(&self) -> bool {
        self.frames.is_empty()
    }

    /// Drop every pending frame. The codec baselines are reset as well:
    /// discarding frames breaks the delta chain, so the encoder and decoder
    /// must both restart from "no baseline" to stay consistent.
    fn clear(&mut self) {
        self.frames.clear();
        self.stored_bytes = 0;
        self.encoder.last_event_fields = None;
        self.decoder.last_event_fields = None;
    }
}

/// Adaptive event handler
struct AdaptiveEventHandler<T: Float + Debug + Send + Sync + 'static> {
    performance_history: VecDeque<T>,
    current_strategy: AdaptationStrategy,
}

#[derive(Debug, Clone, Copy)]
enum AdaptationStrategy {
    Conservative,
    Balanced,
    Aggressive,
}

impl<T: Float + Debug + Send + Sync + 'static + std::iter::Sum> AdaptiveEventHandler<T> {
    fn new() -> Self {
        Self {
            performance_history: VecDeque::new(),
            current_strategy: AdaptationStrategy::Balanced,
        }
    }

    fn adapt_processing(&mut self, current_performance: T) {
        self.performance_history.push_back(current_performance);

        if self.performance_history.len() > 100 {
            self.performance_history.pop_front();
        }

        if self.performance_history.len() >= 10 {
            let recent_avg = self
                .performance_history
                .iter()
                .rev()
                .take(10)
                .cloned()
                .sum::<T>()
                / T::from(10).unwrap_or_else(|| T::zero());
            let older_avg = if self.performance_history.len() >= 20 {
                self.performance_history
                    .iter()
                    .rev()
                    .skip(10)
                    .take(10)
                    .cloned()
                    .sum::<T>()
                    / T::from(10).unwrap_or_else(|| T::zero())
            } else {
                recent_avg
            };

            let performance_change = recent_avg - older_avg;

            self.current_strategy =
                if performance_change > T::from(0.1).unwrap_or_else(|| T::zero()) {
                    AdaptationStrategy::Aggressive
                } else if performance_change < T::from(-0.1).unwrap_or_else(|| T::zero()) {
                    AdaptationStrategy::Conservative
                } else {
                    AdaptationStrategy::Balanced
                };
        }
    }

    fn get_adaptation_factor(&self) -> T {
        match self.current_strategy {
            AdaptationStrategy::Conservative => T::from(0.5).unwrap_or_else(|| T::zero()),
            AdaptationStrategy::Balanced => T::one(),
            AdaptationStrategy::Aggressive => T::from(1.5).unwrap_or_else(|| T::zero()),
        }
    }
}

/// Distributed event coordinator
struct DistributedEventCoordinator<T: Float + Debug + Send + Sync + 'static> {
    load_balancing: LoadBalancingStrategy,
    worker_loads: HashMap<usize, T>,
    current_worker: usize,
    total_workers: usize,
}

impl<T: Float + Debug + Send + Sync + 'static> DistributedEventCoordinator<T> {
    fn new(strategy: LoadBalancingStrategy, num_workers: usize) -> Self {
        Self {
            load_balancing: strategy,
            worker_loads: HashMap::new(),
            current_worker: 0,
            total_workers: num_workers,
        }
    }

    fn assign_worker(&mut self, event: &NeuromorphicEvent<T>) -> usize {
        match self.load_balancing {
            LoadBalancingStrategy::RoundRobin => {
                let worker = self.current_worker;
                self.current_worker = (self.current_worker + 1) % self.total_workers;
                worker
            }
            LoadBalancingStrategy::TypeBased => {
                // Hash event type to worker
                (event.event_type as usize) % self.total_workers
            }
            LoadBalancingStrategy::LoadAware => {
                // Find worker with minimum load
                self.worker_loads
                    .iter()
                    .min_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
                    .map(|(&worker_id, _)| worker_id)
                    .unwrap_or(0)
            }
            _ => 0,
        }
    }

    fn update_worker_load(&mut self, worker_id: usize, load: T) {
        self.worker_loads.insert(worker_id, load);
    }

    /// Load currently recorded for `worker_id`.
    fn worker_load(&self, worker_id: usize) -> Option<T> {
        self.worker_loads.get(&worker_id).copied()
    }

    /// All recorded worker loads, ascending by worker id.
    fn loads(&self) -> Vec<(usize, T)> {
        let mut loads: Vec<(usize, T)> = self
            .worker_loads
            .iter()
            .map(|(&worker, &load)| (worker, load))
            .collect();
        loads.sort_by_key(|(worker, _)| *worker);
        loads
    }
}

impl<
        T: Float
            + Debug
            + Send
            + Sync
            + 'static
            + std::iter::Sum
            + scirs2_core::ndarray::ScalarOperand
            + std::ops::AddAssign,
    > EventDrivenOptimizer<T>
{
    /// Create a new event-driven optimizer
    pub fn new(
        config: EventDrivenConfig<T>,
        stdp_config: STDPConfig<T>,
        membrane_config: MembraneDynamicsConfig<T>,
        num_neurons: usize,
    ) -> Self {
        let mut optimizer = Self {
            config: config.clone(),
            stdp_config: stdp_config.clone(),
            membrane_config: membrane_config.clone(),
            event_queue: BinaryHeap::new(),
            event_stats: HashMap::new(),
            system_state: SystemState {
                membrane_potentials: Array1::from_elem(
                    num_neurons,
                    membrane_config.resting_potential,
                ),
                synaptic_weights: Array2::ones((num_neurons, num_neurons))
                    * T::from(0.1).unwrap_or_else(|| T::zero()),
                last_spike_times: Array1::from_elem(
                    num_neurons,
                    T::from(-1000.0).unwrap_or_else(|| T::zero()),
                ),
                refractory_until: Array1::zeros(num_neurons),
                current_time: T::zero(),
                active_neurons: HashSet::new(),
                pending_updates: HashMap::new(),
            },
            event_handlers: HashMap::new(),
            correlation_tracker: TemporalCorrelationTracker::new(config.correlation_window),
            rate_limiter: EventRateLimiter::new(config.rate_limits.clone()),
            metrics: NeuromorphicMetrics::default(),
            distributed_coordinator: if config.distributed_processing {
                Some(DistributedEventCoordinator::new(config.load_balancing, 4))
            } else {
                None
            },
            compression_engine: EventCompressionEngine::new(config.compression_algorithm),
            compressed_chains: BTreeMap::new(),
            compression_raw_bytes: 0,
            compression_compressed_bytes: 0,
            adaptive_handler: AdaptiveEventHandler::new(),
        };

        // Register default event handlers
        optimizer.register_default_handlers();

        optimizer
    }

    /// Register default event handlers
    fn register_default_handlers(&mut self) {
        let spike_handler = Box::new(SpikeEventHandler {
            stdp_config: self.stdp_config.clone(),
            membrane_config: self.membrane_config.clone(),
        });

        let weight_handler = Box::new(WeightUpdateEventHandler {
            stdp_config: self.stdp_config.clone(),
        });

        self.event_handlers.insert(EventType::Spike, spike_handler);
        self.event_handlers
            .insert(EventType::WeightUpdate, weight_handler);
    }

    /// Add event to the processing queue.
    ///
    /// With `config.event_compression` enabled the event is compressed to
    /// bytes *here* and only those bytes are retained (F57): the live queue
    /// really does store compressed frames instead of whole events, and the
    /// event is reconstructed lazily in `Self::pop_next_event` when it is
    /// about to be processed.
    pub fn enqueue_event(&mut self, event: NeuromorphicEvent<T>) -> Result<()> {
        // Check rate limits
        if !self.rate_limiter.can_process(event.event_type) {
            return Err(OptimError::InvalidConfig("Rate limit exceeded".to_string()));
        }

        // Check queue capacity (both stores count towards the budget, so the
        // capacity check keeps working when compression is enabled).
        if self.get_queue_size() >= self.config.max_queue_size {
            return Err(OptimError::InvalidConfig("Event queue full".to_string()));
        }

        // Extract event fields before moving
        let timestamp = event.timestamp;
        let event_type = event.event_type;
        let source_neuron = event.source_neuron;

        if self.config.event_compression {
            let raw_bytes = self.compression_engine.serialize_event(&event)?.len();
            let algorithm = self.config.compression_algorithm;
            let chain = self
                .compressed_chains
                .entry(event.priority)
                .or_insert_with(|| CompressedEventChain::new(algorithm));
            let compressed_bytes = chain.push(&event)?;
            self.compression_raw_bytes += raw_bytes;
            self.compression_compressed_bytes += compressed_bytes;
        } else {
            self.event_queue.push(PriorityEventEntry {
                event,
                insertion_time: Instant::now(),
            });
        }

        // Update correlation tracking
        if self.config.temporal_correlation {
            self.correlation_tracker
                .add_event(timestamp, event_type, source_neuron);
        }

        Ok(())
    }

    /// Take the next event to process, highest priority first.
    ///
    /// Compressed frames are drained before the in-memory heap. The two
    /// stores only ever hold events simultaneously if
    /// `config.event_compression` was toggled while events were queued; in
    /// that case the compressed frames (which were necessarily enqueued
    /// while compression was on) are the older ones, so draining them first
    /// preserves arrival order across the switch.
    fn pop_next_event(&mut self) -> Result<Option<NeuromorphicEvent<T>>> {
        // `BTreeMap` iterates in ascending key order and `EventPriority`
        // derives `Ord` lowest-first, so scan in reverse for the
        // highest-priority non-empty chain.
        let highest = self
            .compressed_chains
            .iter()
            .rev()
            .find(|(_, chain)| !chain.is_empty())
            .map(|(&priority, _)| priority);
        if let Some(priority) = highest {
            if let Some(chain) = self.compressed_chains.get_mut(&priority) {
                return chain.pop();
            }
        }
        Ok(self.event_queue.pop().map(|entry| entry.event))
    }

    /// Whether any event (compressed or not) is still waiting.
    fn has_pending_events(&self) -> bool {
        !self.event_queue.is_empty()
            || self
                .compressed_chains
                .values()
                .any(|chain| !chain.is_empty())
    }

    /// Process events from the queue
    pub fn process_events(&mut self) -> Result<usize> {
        let mut processed_count = 0;
        let start_time = Instant::now();
        let timeout =
            Duration::from_millis(self.config.processing_timeout.to_u64().unwrap_or(1000));

        while self.has_pending_events() && start_time.elapsed() < timeout {
            if self.config.event_batching {
                let batch_size = self.adaptive_batch_size().min(self.get_queue_size());
                let batch_processed = self.process_event_batch(batch_size)?;
                if batch_processed == 0 {
                    break;
                }
                processed_count += batch_processed;
            } else if let Some(event) = self.pop_next_event()? {
                self.assign_event_to_worker(&event);
                self.process_single_event(&event)?;
                processed_count += 1;
            } else {
                break;
            }
        }

        // Apply pending weight updates
        self.apply_pending_updates()?;

        // Update adaptive processing. An interval the monotonic clock reports
        // as exactly zero carries no rate information at all, so no sample is
        // recorded for it: the previous code divided by that zero (and
        // `expect`ed the conversion), poisoning `performance_history` with
        // `inf` whenever a batch completed inside one clock tick.
        let elapsed_secs = start_time.elapsed().as_secs_f64();
        if elapsed_secs > 0.0 {
            let processing_rate = to_generic_or(processed_count as f64 / elapsed_secs, T::zero());
            self.adaptive_handler.adapt_processing(processing_rate);
        }

        Ok(processed_count)
    }

    /// Process a batch of events
    fn process_event_batch(&mut self, batch_size: usize) -> Result<usize> {
        let mut batch_events = Vec::with_capacity(batch_size);

        // Collect batch events
        for _ in 0..batch_size {
            match self.pop_next_event()? {
                Some(event) => batch_events.push(event),
                None => break,
            }
        }

        // Process batch
        for event in &batch_events {
            self.assign_event_to_worker(event);
            self.process_single_event(event)?;
        }

        Ok(batch_events.len())
    }

    /// Process a single event
    fn process_single_event(&mut self, event: &NeuromorphicEvent<T>) -> Result<()> {
        let start_time = Instant::now();

        // Find appropriate handler
        if let Some(handler) = self.event_handlers.get_mut(&event.event_type) {
            handler.handle_event(event, &mut self.system_state)?;
        } else {
            // Default handling
            self.default_event_handling(event)?;
        }

        // Update statistics
        let processing_time = start_time.elapsed().as_nanos() as f64 / 1_000_000.0;
        self.update_event_statistics(
            event.event_type,
            T::from(processing_time).unwrap_or_else(|| T::zero()),
        );

        // Update energy consumption
        self.metrics.energy_consumption += event.energy_cost;

        Ok(())
    }

    /// Default event handling
    fn default_event_handling(&mut self, event: &NeuromorphicEvent<T>) -> Result<()> {
        match event.event_type {
            EventType::ExternalStimulus
                // Apply external stimulus to neuron
                if event.source_neuron < self.system_state.membrane_potentials.len() => {
                    self.system_state.membrane_potentials[event.source_neuron] += event.value;
                }
            EventType::TimerEvent => {
                // Update system time
                self.system_state.current_time = event.timestamp;
            }
            _ => {
                // Ignore unknown events
            }
        }

        Ok(())
    }

    /// Apply pending weight updates
    fn apply_pending_updates(&mut self) -> Result<()> {
        for ((pre, post), weight_change) in self.system_state.pending_updates.drain() {
            if pre < self.system_state.synaptic_weights.nrows()
                && post < self.system_state.synaptic_weights.ncols()
            {
                let current_weight = self.system_state.synaptic_weights[[pre, post]];
                let new_weight = (current_weight + weight_change)
                    .max(self.stdp_config.weight_min)
                    .min(self.stdp_config.weight_max);

                self.system_state.synaptic_weights[[pre, post]] = new_weight;
            }
        }

        Ok(())
    }

    /// Update event processing statistics
    fn update_event_statistics(&mut self, event_type: EventType, processing_time: T) {
        let stats = self
            .event_stats
            .entry(event_type)
            .or_insert_with(|| EventStatistics {
                total_processed: 0,
                avg_processing_time: T::zero(),
                event_rate: T::zero(),
                avg_queue_wait_time: T::zero(),
                error_count: 0,
                last_update: Instant::now(),
            });

        stats.total_processed += 1;

        // Update average processing _time using exponential moving average
        let alpha = T::from(0.1).unwrap_or_else(|| T::zero());
        stats.avg_processing_time =
            stats.avg_processing_time * (T::one() - alpha) + processing_time * alpha;

        // Update event rate
        let time_since_last = stats.last_update.elapsed().as_secs_f64();
        if time_since_last > 0.0 {
            let current_rate = T::one() / T::from(time_since_last).unwrap_or_else(|| T::zero());
            stats.event_rate = stats.event_rate * (T::one() - alpha) + current_rate * alpha;
        }

        stats.last_update = Instant::now();
    }

    /// Get event processing statistics
    pub fn get_event_statistics(&self) -> &HashMap<EventType, EventStatistics<T>> {
        &self.event_stats
    }

    /// Get current system state
    pub fn get_system_state(&self) -> &SystemState<T> {
        &self.system_state
    }

    /// Get current metrics
    pub fn get_metrics(&self) -> &NeuromorphicMetrics<T> {
        &self.metrics
    }

    /// Clear event queue, including any compressed frames still pending.
    pub fn clear_event_queue(&mut self) {
        self.event_queue.clear();
        for chain in self.compressed_chains.values_mut() {
            chain.clear();
        }
    }

    /// Get queue size: events waiting in the in-memory priority queue plus
    /// events waiting as compressed frames.
    pub fn get_queue_size(&self) -> usize {
        self.event_queue.len()
            + self
                .compressed_chains
                .values()
                .map(|chain| chain.len())
                .sum::<usize>()
    }

    /// Bytes currently occupied by the compressed event queue (F57). Zero
    /// while `config.event_compression` is disabled, since nothing is stored
    /// in compressed form then.
    pub fn compressed_queue_bytes(&self) -> usize {
        self.compressed_chains
            .values()
            .map(|chain| chain.stored_bytes)
            .sum()
    }

    /// Cumulative `(uncompressed_bytes, compressed_bytes)` measured across
    /// every event that has entered the compressed queue since construction.
    /// The uncompressed figure is the size the same event occupies under the
    /// reference (`EventCompressionAlgorithm::None`) codec, so the pair is a
    /// direct measurement of what compression actually achieved.
    pub fn compression_statistics(&self) -> (usize, usize) {
        (
            self.compression_raw_bytes,
            self.compression_compressed_bytes,
        )
    }

    /// Achieved compression ratio (`compressed / uncompressed`); `None` until
    /// at least one event has been compressed.
    pub fn compression_ratio(&self) -> Option<f64> {
        if self.compression_raw_bytes == 0 {
            None
        } else {
            Some(self.compression_compressed_bytes as f64 / self.compression_raw_bytes as f64)
        }
    }

    /// The most recent *measured* event-processing rate (events per second)
    /// recorded by [`Self::process_events`], or `None` if no interval long
    /// enough to be measurable has been observed yet. Always finite: the
    /// previous implementation divided the processed count by an integer
    /// millisecond count, which is zero for any batch finishing inside one
    /// clock tick, feeding `inf`/`NaN` into the adaptive handler.
    pub fn last_measured_event_rate(&self) -> Option<T> {
        self.adaptive_handler.performance_history.back().copied()
    }

    /// The adaptation factor currently chosen by the adaptive event handler
    /// from the measured event-processing rate history (0.5 while throughput
    /// is degrading, 1.0 while it is stable, 1.5 while it is improving).
    pub fn current_adaptation_factor(&self) -> T {
        self.adaptive_handler.get_adaptation_factor()
    }

    /// Effective per-iteration batch size: the configured `batch_size` scaled
    /// by [`Self::current_adaptation_factor`], so a system whose measured
    /// throughput is improving takes larger bites and one that is degrading
    /// takes smaller ones. Never zero.
    pub fn adaptive_batch_size(&self) -> usize {
        let factor = self.current_adaptation_factor().to_f64().unwrap_or(1.0);
        let scaled = (self.config.batch_size as f64 * factor).round();
        if scaled.is_finite() && scaled >= 1.0 {
            scaled as usize
        } else {
            self.config.batch_size.max(1)
        }
    }

    /// Route an event to a worker under the configured load-balancing policy
    /// and record the resulting load, when distributed processing is enabled.
    ///
    /// The coordinator used to be constructed from `distributed_processing` and
    /// then never consulted, so `LoadBalancingStrategy` selected nothing and no
    /// worker load was ever tracked.
    fn assign_event_to_worker(&mut self, event: &NeuromorphicEvent<T>) {
        let Some(coordinator) = self.distributed_coordinator.as_mut() else {
            return;
        };
        let worker = coordinator.assign_worker(event);
        let current = coordinator.worker_load(worker).unwrap_or_else(T::zero);
        coordinator.update_worker_load(worker, current + T::one());
    }

    /// Measured temporal correlation between two event types, as accumulated by
    /// the correlation tracker over the configured correlation window.
    ///
    /// The tracker genuinely computes and stores these strengths; before this
    /// accessor existed there was no way to read any of them back.
    pub fn event_correlation(&self, first: EventType, second: EventType) -> T {
        self.correlation_tracker.get_correlation(first, second)
    }

    /// Per-worker event counts recorded by the distributed coordinator, or
    /// `None` when distributed processing is disabled.
    pub fn worker_loads(&self) -> Option<Vec<(usize, T)>> {
        self.distributed_coordinator
            .as_ref()
            .map(|coordinator| coordinator.loads())
    }

    /// Enable distributed processing
    pub fn enable_distributed_processing(&mut self, num_workers: usize) {
        self.distributed_coordinator = Some(DistributedEventCoordinator::new(
            self.config.load_balancing,
            num_workers,
        ));
        self.config.distributed_processing = true;
    }

    /// Disable distributed processing
    pub fn disable_distributed_processing(&mut self) {
        self.distributed_coordinator = None;
        self.config.distributed_processing = false;
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for EventStatistics<T> {
    fn default() -> Self {
        Self {
            total_processed: 0,
            avg_processing_time: T::zero(),
            event_rate: T::zero(),
            avg_queue_wait_time: T::zero(),
            error_count: 0,
            last_update: Instant::now(),
        }
    }
}

/// Regression tests for the F57 *wiring* (compressed live queue), kept in
/// their own file so `event_driven.rs` stays under the 2000-line policy.
#[cfg(test)]
#[path = "event_driven_compression_tests.rs"]
mod compression_wiring_tests;

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

    fn sample_events() -> Vec<NeuromorphicEvent<f64>> {
        vec![
            NeuromorphicEvent {
                event_type: EventType::Spike,
                timestamp: 12.5,
                source_neuron: 3,
                target_neuron: Some(7),
                value: 1.0,
                energy_cost: 0.05,
                priority: EventPriority::High,
            },
            NeuromorphicEvent {
                event_type: EventType::WeightUpdate,
                timestamp: 12.6,
                source_neuron: 4,
                target_neuron: Some(8),
                value: -0.25,
                energy_cost: 0.02,
                priority: EventPriority::Normal,
            },
            NeuromorphicEvent {
                event_type: EventType::TimerEvent,
                timestamp: 100.0,
                source_neuron: 0,
                target_neuron: None,
                value: 0.0,
                energy_cost: 0.0,
                priority: EventPriority::Low,
            },
            NeuromorphicEvent {
                event_type: EventType::EnergyEvent,
                timestamp: 0.0,
                source_neuron: 42,
                target_neuron: Some(1),
                value: 999.75,
                energy_cost: -3.5,
                priority: EventPriority::RealTime,
            },
        ]
    }

    fn assert_events_close(a: &NeuromorphicEvent<f64>, b: &NeuromorphicEvent<f64>) {
        assert_eq!(a.event_type, b.event_type);
        assert_eq!(a.priority, b.priority);
        assert_eq!(a.source_neuron, b.source_neuron);
        assert_eq!(a.target_neuron, b.target_neuron);
        assert!(
            (a.timestamp - b.timestamp).abs() < 1e-3,
            "timestamp mismatch: {} vs {}",
            a.timestamp,
            b.timestamp
        );
        assert!(
            (a.value - b.value).abs() < 1e-3,
            "value mismatch: {} vs {}",
            a.value,
            b.value
        );
        assert!(
            (a.energy_cost - b.energy_cost).abs() < 1e-3,
            "energy_cost mismatch: {} vs {}",
            a.energy_cost,
            b.energy_cost
        );
    }

    /// F57: `EventCompressionAlgorithm::None` must round-trip every field,
    /// and must not be the fixed-size all-zero buffer the previous
    /// delta/sparse stubs returned regardless of algorithm choice.
    #[test]
    fn none_algorithm_round_trips_all_fields() {
        let mut engine = EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::None);
        for event in sample_events() {
            let bytes = engine.compress_event(&event).expect("compress");
            let decoded = engine.decompress_event(&bytes).expect("decompress");
            assert_events_close(&event, &decoded);
        }
    }

    /// F57: delta encoding must round-trip a whole stream of events in
    /// order (each event's baseline is the previously *decoded* event),
    /// including the target-neuron presence/absence transitions.
    #[test]
    fn delta_encoding_round_trips_event_stream() {
        let mut encoder =
            EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::DeltaEncoding);
        let mut decoder =
            EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::DeltaEncoding);

        for event in sample_events() {
            let bytes = encoder.compress_event(&event).expect("compress");
            let decoded = decoder.decompress_event(&bytes).expect("decompress");
            assert_events_close(&event, &decoded);
        }
    }

    /// F57: delta-encoded bytes for a stream of *similar* consecutive
    /// events (the case delta encoding exists to exploit) must be smaller
    /// than the same events' absolute (`None`) encoding — otherwise the
    /// "compression" is not actually compressing anything.
    #[test]
    fn delta_encoding_is_smaller_for_similar_events() {
        let mut none_engine = EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::None);
        let mut delta_engine =
            EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::DeltaEncoding);

        let mut none_total = 0usize;
        let mut delta_total = 0usize;
        for i in 0..20 {
            let event = NeuromorphicEvent {
                event_type: EventType::Spike,
                timestamp: 1000.0 + i as f64 * 0.1,
                source_neuron: 50 + (i % 3),
                target_neuron: Some(100 + (i % 2)),
                value: 0.5,
                energy_cost: 0.01,
                priority: EventPriority::Normal,
            };
            none_total += none_engine.compress_event(&event).expect("compress").len();
            delta_total += delta_engine.compress_event(&event).expect("compress").len();
        }
        assert!(
            delta_total < none_total,
            "delta encoding was not smaller for a similar-event stream: \
             delta={delta_total} bytes, none={none_total} bytes"
        );
    }

    /// F57: sparse encoding must round-trip events with all-default
    /// optional fields (no target, zero value/energy) as well as events
    /// that set every optional field.
    #[test]
    fn sparse_encoding_round_trips_default_and_full_events() {
        let mut engine =
            EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::SparseEncoding);
        for event in sample_events() {
            let bytes = engine.compress_event(&event).expect("compress");
            let decoded = engine.decompress_event(&bytes).expect("decompress");
            assert_events_close(&event, &decoded);
        }
    }

    /// F57: decoding truncated/corrupt bytes must return an honest `Err`,
    /// never panic (the varint readers use `?`/`ok_or_else` throughout,
    /// never indexing or unwrapping directly).
    #[test]
    fn decoding_truncated_bytes_is_an_error_not_a_panic() {
        let mut engine = EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::None);
        let event = sample_events().remove(0);
        let bytes = engine.compress_event(&event).expect("compress");

        for len in 0..bytes.len() {
            let mut fresh_engine =
                EventCompressionEngine::<f64>::new(EventCompressionAlgorithm::None);
            let result = fresh_engine.decompress_event(&bytes[..len]);
            assert!(
                result.is_err(),
                "truncating to {len} bytes should be a decode error, got {result:?}"
            );
        }
    }
}