optirs-core 0.3.1

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
// Neuromorphic Computing Optimization
//
// This module implements optimization algorithms specifically designed for neuromorphic
// computing platforms, including spike-based optimization, event-driven parameter updates,
// and energy-efficient optimization strategies for neuromorphic chips.

use crate::error::Result;
use crate::optimizers::Optimizer;
use scirs2_core::ndarray::{Array1, Array2, ArrayBase, Data, DataMut, Dimension};
use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;
use std::time::{Duration, Instant};

pub mod energy_efficient;
pub mod event_driven;
pub mod spike_based;

// Re-export key types
pub use energy_efficient::{EnergyBudget, EnergyEfficientOptimizer, EnergyOptimizationStrategy};
pub use event_driven::{EventDrivenConfig, EventDrivenOptimizer, EventType};
pub use spike_based::{SpikeTrainOptimizer, SpikingConfig, SpikingOptimizer};

/// Neuromorphic computing platform types
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum NeuromorphicPlatform {
    /// Intel Loihi neuromorphic chip
    IntelLoihi,

    /// SpiNNaker platform
    SpiNNaker,

    /// IBM TrueNorth
    IBMTrueNorth,

    /// BrainChip Akida
    BrainChipAkida,

    /// University research platforms
    Research,

    /// Custom neuromorphic hardware
    Custom(String),
}

/// Neuromorphic optimization configuration
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct NeuromorphicConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Target neuromorphic platform
    pub platform: NeuromorphicPlatform,

    /// Spike-based optimization settings
    pub spike_config: SpikingConfig<T>,

    /// Event-driven optimization settings
    pub event_config: EventDrivenConfig<T>,

    /// Energy optimization settings
    pub energy_config: EnergyOptimizationConfig<T>,

    /// Enable temporal coding
    pub temporal_coding: bool,

    /// Enable rate coding
    pub rate_coding: bool,

    /// Spike timing dependent plasticity (STDP) parameters
    pub stdp_config: STDPConfig<T>,

    /// Membrane potential dynamics
    pub membrane_dynamics: MembraneDynamicsConfig<T>,

    /// Synaptic plasticity model
    pub plasticity_model: PlasticityModel,

    /// Enable homeostatic mechanisms
    pub homeostatic_plasticity: bool,

    /// Enable metaplasticity
    pub metaplasticity: bool,

    /// Population dynamics configuration
    pub population_config: PopulationConfig,
}

/// Spike Timing Dependent Plasticity configuration
#[derive(Debug, Clone)]
pub struct STDPConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Learning rate for potentiation
    pub learning_rate_pot: T,

    /// Learning rate for depression
    pub learning_rate_dep: T,

    /// Time constant for potentiation (ms)
    pub tau_pot: T,

    /// Time constant for depression (ms)
    pub tau_dep: T,

    /// Maximum weight value
    pub weight_max: T,

    /// Minimum weight value
    pub weight_min: T,

    /// Enable triplet STDP
    pub enable_triplet: bool,

    /// Triplet learning rate
    pub triplet_learning_rate: T,
}

/// Membrane potential dynamics configuration
#[derive(Debug, Clone)]
pub struct MembraneDynamicsConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Membrane time constant (ms)
    pub tau_membrane: T,

    /// Resting potential (mV)
    pub resting_potential: T,

    /// Threshold potential (mV)
    pub threshold_potential: T,

    /// Reset potential (mV)
    pub reset_potential: T,

    /// Refractory period (ms)
    pub refractory_period: T,

    /// Capacitance (pF)
    pub capacitance: T,

    /// Leak conductance (nS)
    pub leak_conductance: T,

    /// Enable adaptive threshold
    pub adaptive_threshold: bool,

    /// Threshold adaptation time constant
    pub threshold_adaptation_tau: T,
}

/// Synaptic plasticity models
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum PlasticityModel {
    /// Hebbian plasticity
    Hebbian,

    /// Anti-Hebbian plasticity
    AntiHebbian,

    /// Spike Timing Dependent Plasticity
    STDP,

    /// Triplet STDP
    TripletSTDP,

    /// Voltage-dependent plasticity
    VoltageDependentSTDP,

    /// Calcium-based plasticity
    CalciumBased,

    /// BCM (Bienenstock-Cooper-Munro) rule
    BCM,

    /// Oja's rule
    Oja,
}

/// Population-level configuration
#[derive(Debug, Clone)]
pub struct PopulationConfig {
    /// Population size
    pub population_size: usize,

    /// Enable lateral inhibition
    pub lateral_inhibition: bool,

    /// Inhibition strength
    pub inhibition_strength: f64,

    /// Enable winner-take-all dynamics
    pub winner_take_all: bool,

    /// Population coding strategy
    pub coding_strategy: PopulationCodingStrategy,

    /// Enable population bursting
    pub enable_bursting: bool,

    /// Synchronization mechanisms
    pub synchronization: SynchronizationMechanism,
}

/// Population coding strategies
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum PopulationCodingStrategy {
    /// Distributed coding
    Distributed,

    /// Sparse coding
    Sparse,

    /// Local coding
    Local,

    /// Vector coding
    Vector,

    /// Rank order coding
    RankOrder,
}

/// Synchronization mechanisms
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum SynchronizationMechanism {
    /// No synchronization
    None,

    /// Global clock
    GlobalClock,

    /// Phase-locked loops
    PhaseLocked,

    /// Adaptive synchronization
    Adaptive,

    /// Network oscillations
    NetworkOscillations,
}

/// Energy optimization configuration
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct EnergyOptimizationConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Energy budget (nJ per operation)
    pub energy_budget: T,

    /// Energy optimization strategy
    pub strategy: EnergyOptimizationStrategy,

    /// Enable dynamic voltage scaling
    pub dynamic_voltage_scaling: bool,

    /// Enable clock gating
    pub clock_gating: bool,

    /// Enable power gating
    pub power_gating: bool,

    /// Sleep mode configuration
    pub sleep_mode_config: SleepModeConfig<T>,

    /// Energy monitoring frequency
    pub monitoring_frequency: Duration,

    /// Thermal management
    pub thermal_management: ThermalManagementConfig<T>,
}

/// Sleep mode configuration for energy efficiency
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct SleepModeConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Enable sleep mode
    pub enable_sleep_mode: bool,

    /// Sleep threshold (inactivity time)
    pub sleep_threshold: Duration,

    /// Wake-up time (ms)
    pub wakeup_time: T,

    /// Sleep energy consumption (nW)
    pub sleep_power: T,

    /// Wake-up energy cost (nJ)
    pub wakeup_energy: T,
}

/// Thermal management configuration
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ThermalManagementConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Enable thermal management
    pub enable_thermal_management: bool,

    /// Target temperature (°C)
    pub target_temperature: T,

    /// Maximum temperature (°C)
    pub max_temperature: T,

    /// Thermal time constant (s)
    pub thermal_time_constant: T,

    /// Thermal throttling strategy
    pub throttling_strategy: ThermalThrottlingStrategy,
}

/// Thermal throttling strategies
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
pub enum ThermalThrottlingStrategy {
    /// Frequency scaling
    FrequencyScaling,

    /// Voltage scaling
    VoltageScaling,

    /// Activity reduction
    ActivityReduction,

    /// Selective shutdown
    SelectiveShutdown,

    /// Dynamic load balancing
    DynamicLoadBalancing,
}

/// Spike representation for neuromorphic optimization
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Spike<T: Float + Debug + Send + Sync + 'static> {
    /// Neuron ID
    pub neuron_id: usize,

    /// Spike time (ms)
    pub time: T,

    /// Spike amplitude (mV)
    pub amplitude: T,

    /// Spike width (ms)
    pub width: Option<T>,

    /// Associated synapse weight
    pub weight: T,

    /// Presynaptic neuron ID
    pub presynaptic_id: Option<usize>,

    /// Postsynaptic neuron ID
    pub postsynaptic_id: Option<usize>,
}

/// Spike train representation
#[derive(Debug, Clone)]
pub struct SpikeTrain<T: Float + Debug + Send + Sync + 'static> {
    /// Neuron ID
    pub neuron_id: usize,

    /// Spike times
    pub spike_times: Vec<T>,

    /// Inter-spike intervals
    pub inter_spike_intervals: Vec<T>,

    /// Firing rate (Hz)
    pub firing_rate: T,

    /// Spike train duration (ms)
    pub duration: T,

    /// Spike count
    pub spike_count: usize,
}

impl<T: Float + Debug + Send + Sync + 'static + std::iter::Sum> SpikeTrain<T> {
    /// Create a new spike train from spike times
    pub fn new(neuron_id: usize, spike_times: Vec<T>) -> Self {
        let spike_count = spike_times.len();
        let duration = if spike_count > 0 {
            spike_times[spike_count - 1] - spike_times[0]
        } else {
            T::zero()
        };

        let firing_rate = if duration > T::zero() {
            T::from(spike_count).unwrap_or_else(|| T::zero())
                / (duration / T::from(1000.0).unwrap_or_else(|| T::zero()))
        } else {
            T::zero()
        };

        let inter_spike_intervals = if spike_count > 1 {
            spike_times.windows(2).map(|w| w[1] - w[0]).collect()
        } else {
            Vec::new()
        };

        Self {
            neuron_id,
            spike_times,
            inter_spike_intervals,
            firing_rate,
            duration,
            spike_count,
        }
    }

    /// Calculate coefficient of variation of inter-spike intervals
    pub fn coefficient_of_variation(&self) -> T {
        if self.inter_spike_intervals.len() < 2 {
            return T::zero();
        }

        let mean = self.inter_spike_intervals.iter().cloned().sum::<T>()
            / T::from(self.inter_spike_intervals.len()).expect("unwrap failed");

        let variance = self
            .inter_spike_intervals
            .iter()
            .map(|&isi| (isi - mean) * (isi - mean))
            .sum::<T>()
            / T::from(self.inter_spike_intervals.len()).expect("unwrap failed");

        variance.sqrt() / mean
    }

    /// Calculate local variation measure
    pub fn local_variation(&self) -> T {
        if self.inter_spike_intervals.len() < 2 {
            return T::zero();
        }

        let mut lv_sum = T::zero();
        for window in self.inter_spike_intervals.windows(2) {
            let isi1 = window[0];
            let isi2 = window[1];
            let diff = isi1 - isi2;
            let sum = isi1 + isi2;

            if sum > T::zero() {
                lv_sum = lv_sum + (diff * diff) / (sum * sum);
            }
        }

        let three = T::from(3.0).unwrap_or_else(|| T::zero());
        three * lv_sum / T::from(self.inter_spike_intervals.len() - 1).expect("unwrap failed")
    }
}

/// Event-driven update representation
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct NeuromorphicEvent<T: Float + Debug + Send + Sync + 'static> {
    /// Event type
    pub event_type: EventType,

    /// Event timestamp
    pub timestamp: T,

    /// Source neuron
    pub source_neuron: usize,

    /// Target neuron
    pub target_neuron: Option<usize>,

    /// Event value/weight
    pub value: T,

    /// Energy cost of processing this event
    pub energy_cost: T,

    /// Priority level
    pub priority: EventPriority,
}

/// Event priority levels for neuromorphic processing
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[allow(dead_code)]
pub enum EventPriority {
    Low,
    Normal,
    High,
    Critical,
    RealTime,
}

/// Neuromorphic optimization metrics
#[derive(Debug, Clone)]
pub struct NeuromorphicMetrics<T: Float + Debug + Send + Sync + 'static> {
    /// Total spikes processed
    pub total_spikes: usize,

    /// Average firing rate (Hz)
    pub average_firing_rate: T,

    /// Energy consumption (nJ)
    pub energy_consumption: T,

    /// Power consumption (nW)
    pub power_consumption: T,

    /// Spike timing precision (ms)
    pub timing_precision: T,

    /// Synaptic operations per second
    pub synaptic_ops_per_sec: T,

    /// Plasticity events per second
    pub plasticity_events_per_sec: T,

    /// Memory bandwidth utilization
    pub memory_bandwidth_utilization: T,

    /// Thermal efficiency score
    pub thermal_efficiency: T,

    /// Network synchronization measure
    pub network_synchronization: T,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for NeuromorphicMetrics<T> {
    fn default() -> Self {
        Self {
            total_spikes: 0,
            average_firing_rate: T::zero(),
            energy_consumption: T::zero(),
            power_consumption: T::zero(),
            timing_precision: T::from(0.1).unwrap_or_else(|| T::zero()), // 0.1ms default
            synaptic_ops_per_sec: T::zero(),
            plasticity_events_per_sec: T::zero(),
            memory_bandwidth_utilization: T::zero(),
            thermal_efficiency: T::one(),
            network_synchronization: T::zero(),
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for NeuromorphicConfig<T> {
    fn default() -> Self {
        Self {
            platform: NeuromorphicPlatform::IntelLoihi,
            spike_config: SpikingConfig::default(),
            event_config: EventDrivenConfig::default(),
            energy_config: EnergyOptimizationConfig::default(),
            temporal_coding: true,
            rate_coding: false,
            stdp_config: STDPConfig::default(),
            membrane_dynamics: MembraneDynamicsConfig::default(),
            plasticity_model: PlasticityModel::STDP,
            homeostatic_plasticity: false,
            metaplasticity: false,
            population_config: PopulationConfig::default(),
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for STDPConfig<T> {
    fn default() -> Self {
        Self {
            learning_rate_pot: T::from(0.01).unwrap_or_else(|| T::zero()),
            learning_rate_dep: T::from(0.01).unwrap_or_else(|| T::zero()),
            tau_pot: T::from(20.0).unwrap_or_else(|| T::zero()),
            tau_dep: T::from(20.0).unwrap_or_else(|| T::zero()),
            weight_max: T::one(),
            weight_min: T::zero(),
            enable_triplet: false,
            triplet_learning_rate: T::from(0.001).unwrap_or_else(|| T::zero()),
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for MembraneDynamicsConfig<T> {
    fn default() -> Self {
        Self {
            tau_membrane: T::from(20.0).unwrap_or_else(|| T::zero()),
            resting_potential: T::from(-70.0).unwrap_or_else(|| T::zero()),
            threshold_potential: T::from(-55.0).unwrap_or_else(|| T::zero()),
            reset_potential: T::from(-80.0).unwrap_or_else(|| T::zero()),
            refractory_period: T::from(2.0).unwrap_or_else(|| T::zero()),
            capacitance: T::from(100.0).unwrap_or_else(|| T::zero()),
            leak_conductance: T::from(10.0).unwrap_or_else(|| T::zero()),
            adaptive_threshold: false,
            threshold_adaptation_tau: T::from(100.0).unwrap_or_else(|| T::zero()),
        }
    }
}

impl Default for PopulationConfig {
    fn default() -> Self {
        Self {
            population_size: 1000,
            lateral_inhibition: false,
            inhibition_strength: 0.1,
            winner_take_all: false,
            coding_strategy: PopulationCodingStrategy::Distributed,
            enable_bursting: false,
            synchronization: SynchronizationMechanism::None,
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for EnergyOptimizationConfig<T> {
    fn default() -> Self {
        Self {
            energy_budget: T::from(10.0).unwrap_or_else(|| T::zero()), // 10 nJ per operation
            strategy: EnergyOptimizationStrategy::DynamicVoltageScaling,
            dynamic_voltage_scaling: true,
            clock_gating: true,
            power_gating: false,
            sleep_mode_config: SleepModeConfig::default(),
            monitoring_frequency: Duration::from_millis(100),
            thermal_management: ThermalManagementConfig::default(),
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for SleepModeConfig<T> {
    fn default() -> Self {
        Self {
            enable_sleep_mode: true,
            sleep_threshold: Duration::from_millis(100),
            wakeup_time: T::from(1.0).unwrap_or_else(|| T::zero()),
            sleep_power: T::from(0.1).unwrap_or_else(|| T::zero()),
            wakeup_energy: T::from(0.01).unwrap_or_else(|| T::zero()),
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> Default for ThermalManagementConfig<T> {
    fn default() -> Self {
        Self {
            enable_thermal_management: true,
            target_temperature: T::from(65.0).unwrap_or_else(|| T::zero()),
            max_temperature: T::from(85.0).unwrap_or_else(|| T::zero()),
            thermal_time_constant: T::from(10.0).unwrap_or_else(|| T::zero()),
            throttling_strategy: ThermalThrottlingStrategy::FrequencyScaling,
        }
    }
}