numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Particle Swarm Optimization (PSO)
//!
//! PSO is a population-based stochastic optimization algorithm inspired by the social
//! behavior of bird flocking or fish schooling. Particles move through the search space
//! based on their own experience and the experience of neighboring particles.
//!
//! # Features
//!
//! - Velocity and position updates with inertia weight
//! - Cognitive and social learning parameters
//! - Multiple inertia weight strategies: Constant, Linear Decay, Adaptive
//! - Topology variants: Global best (gbest), Local best (lbest), Ring
//! - Personal and global best tracking
//!
//! # Example
//!
//! ```
//! use numrs2::optimize::{particle_swarm, PSOConfig, InertiaStrategy};
//!
//! // Minimize Rosenbrock function
//! let f = |x: &[f64]| {
//!     let (x0, x1) = (x[0], x[1]);
//!     (1.0 - x0).powi(2) + 100.0 * (x1 - x0 * x0).powi(2)
//! };
//!
//! let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];
//! let config = PSOConfig::default();
//!
//! let result = particle_swarm(f, &bounds, Some(config))
//!     .expect("PSO should succeed");
//! ```

use crate::error::{NumRs2Error, Result};
use crate::optimize::OptimizeResult;
use num_traits::Float;
use scirs2_core::random::{thread_rng, Distribution, Rng, RngExt, Uniform};

/// Inertia weight strategy
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InertiaStrategy {
    /// Constant inertia weight
    Constant(f64),
    /// Linearly decreasing inertia weight from w_max to w_min
    LinearDecay { w_max: f64, w_min: f64 },
    /// Adaptive inertia weight based on swarm performance
    Adaptive,
}

/// Topology for neighborhood structure
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Topology {
    /// Global best - all particles influenced by single global best
    Global,
    /// Local best - particles influenced by k nearest neighbors
    Local(usize),
    /// Ring topology - each particle influenced by immediate neighbors
    Ring,
}

/// Configuration for Particle Swarm Optimization
#[derive(Debug, Clone)]
pub struct PSOConfig<T: Float> {
    /// Number of particles in the swarm
    pub swarm_size: usize,
    /// Maximum number of iterations
    pub max_iter: usize,
    /// Inertia weight strategy
    pub inertia: InertiaStrategy,
    /// Cognitive parameter (personal best weight)
    pub c1: T,
    /// Social parameter (global/local best weight)
    pub c2: T,
    /// Velocity clamping factor (as fraction of search range)
    pub v_max_factor: T,
    /// Topology structure
    pub topology: Topology,
    /// Convergence tolerance
    pub ftol: T,
}

impl<T: Float> Default for PSOConfig<T> {
    fn default() -> Self {
        Self {
            swarm_size: 30,
            max_iter: 200,
            inertia: InertiaStrategy::LinearDecay {
                w_max: 0.9,
                w_min: 0.4,
            },
            c1: T::from(2.0).expect("2.0 should convert to Float"),
            c2: T::from(2.0).expect("2.0 should convert to Float"),
            v_max_factor: T::from(0.2).expect("0.2 should convert to Float"),
            topology: Topology::Global,
            ftol: T::from(1e-8).expect("1e-8 should convert to Float"),
        }
    }
}

/// Particle in the swarm
#[derive(Clone)]
struct Particle<T: Float> {
    position: Vec<T>,
    velocity: Vec<T>,
    best_position: Vec<T>,
    best_fitness: T,
    fitness: T,
}

/// Particle Swarm Optimization
///
/// # Arguments
///
/// * `f` - Objective function to minimize
/// * `bounds` - Parameter bounds as (lower, upper) tuples
/// * `config` - Optional PSO configuration
///
/// # Returns
///
/// `OptimizeResult` containing the best solution found
pub fn particle_swarm<T, F>(
    f: F,
    bounds: &[(T, T)],
    config: Option<PSOConfig<T>>,
) -> Result<OptimizeResult<T>>
where
    T: Float + std::fmt::Display + std::iter::Sum,
    F: Fn(&[T]) -> T,
{
    let config = config.unwrap_or_default();
    let dim = bounds.len();

    if dim == 0 {
        return Err(NumRs2Error::ValueError(
            "Bounds must have at least one dimension".to_string(),
        ));
    }

    if config.swarm_size < 2 {
        return Err(NumRs2Error::ValueError(
            "Swarm size must be at least 2".to_string(),
        ));
    }

    let mut rng = thread_rng();

    // Calculate velocity limits
    let v_max: Vec<T> = bounds
        .iter()
        .map(|&(lower, upper)| (upper - lower) * config.v_max_factor)
        .collect();

    // Initialize swarm
    let mut swarm = initialize_swarm::<T, F>(&f, bounds, config.swarm_size, &v_max, &mut rng)?;

    // Find global best
    let mut global_best_idx = 0;
    let mut global_best_fitness = swarm[0].fitness;
    for (i, particle) in swarm.iter().enumerate() {
        if particle.fitness < global_best_fitness {
            global_best_fitness = particle.fitness;
            global_best_idx = i;
        }
    }
    let mut global_best = swarm[global_best_idx].position.clone();

    let mut nfev = config.swarm_size;
    let njev = 0;
    let mut stagnation_count = 0;
    let max_stagnation = 20;

    for iter in 0..config.max_iter {
        let prev_best_fitness = global_best_fitness;

        // Calculate inertia weight
        let w = match config.inertia {
            InertiaStrategy::Constant(w_val) => T::from(w_val).ok_or_else(|| {
                NumRs2Error::ConversionError("Inertia weight conversion failed".to_string())
            })?,
            InertiaStrategy::LinearDecay { w_max, w_min } => {
                let progress = T::from(iter).ok_or_else(|| {
                    NumRs2Error::ConversionError("Iteration conversion failed".to_string())
                })? / T::from(config.max_iter).ok_or_else(|| {
                    NumRs2Error::ConversionError("Max iteration conversion failed".to_string())
                })?;

                let w_max_t = T::from(w_max).ok_or_else(|| {
                    NumRs2Error::ConversionError("w_max conversion failed".to_string())
                })?;
                let w_min_t = T::from(w_min).ok_or_else(|| {
                    NumRs2Error::ConversionError("w_min conversion failed".to_string())
                })?;

                w_max_t - (w_max_t - w_min_t) * progress
            }
            InertiaStrategy::Adaptive => {
                // Adaptive based on swarm diversity
                let diversity = calculate_diversity(&swarm);
                let diversity_t = T::from(diversity).ok_or_else(|| {
                    NumRs2Error::ConversionError("Diversity conversion failed".to_string())
                })?;

                // High diversity -> high inertia, low diversity -> low inertia
                let w_max = T::from(0.9).ok_or_else(|| {
                    NumRs2Error::ConversionError("w_max conversion failed".to_string())
                })?;
                let w_min = T::from(0.4).ok_or_else(|| {
                    NumRs2Error::ConversionError("w_min conversion failed".to_string())
                })?;

                w_min + (w_max - w_min) * diversity_t
            }
        };

        // Pre-compute neighborhood bests to avoid borrow checker issues
        let neighborhood_bests: Vec<Vec<T>> = (0..swarm.len())
            .map(|i| match config.topology {
                Topology::Global => global_best.clone(),
                Topology::Local(k) => get_local_best(&swarm, i, k).to_vec(),
                Topology::Ring => get_ring_best(&swarm, i).to_vec(),
            })
            .collect();

        // Update particles
        for (i, particle) in swarm.iter_mut().enumerate() {
            let neighborhood_best = &neighborhood_bests[i];

            // Update velocity and position
            for j in 0..dim {
                // Random coefficients
                let r1 = T::from(rng.random::<f64>()).ok_or_else(|| {
                    NumRs2Error::ConversionError("Random value conversion failed".to_string())
                })?;
                let r2 = T::from(rng.random::<f64>()).ok_or_else(|| {
                    NumRs2Error::ConversionError("Random value conversion failed".to_string())
                })?;

                // Velocity update: v = w*v + c1*r1*(pbest - x) + c2*r2*(gbest - x)
                let cognitive = config.c1 * r1 * (particle.best_position[j] - particle.position[j]);
                let social = config.c2 * r2 * (neighborhood_best[j] - particle.position[j]);

                particle.velocity[j] = w * particle.velocity[j] + cognitive + social;

                // Clamp velocity
                particle.velocity[j] = particle.velocity[j].max(-v_max[j]).min(v_max[j]);

                // Update position
                particle.position[j] = particle.position[j] + particle.velocity[j];

                // Enforce bounds
                let (lower, upper) = bounds[j];
                particle.position[j] = particle.position[j].max(lower).min(upper);
            }

            // Evaluate new position
            particle.fitness = f(&particle.position);
            nfev += 1;

            // Update personal best
            if particle.fitness < particle.best_fitness {
                particle.best_position = particle.position.clone();
                particle.best_fitness = particle.fitness;
            }

            // Update global best
            if particle.fitness < global_best_fitness {
                global_best = particle.position.clone();
                global_best_fitness = particle.fitness;
            }
        }

        // Check convergence
        let improvement = (prev_best_fitness - global_best_fitness).abs();
        if improvement < config.ftol && iter > 10 {
            return Ok(OptimizeResult {
                x: global_best,
                fun: global_best_fitness,
                grad: vec![T::zero(); dim],
                nit: iter + 1,
                nfev,
                njev,
                success: true,
                message: "Convergence achieved".to_string(),
            });
        }

        // Track stagnation
        if improvement < config.ftol {
            stagnation_count += 1;
        } else {
            stagnation_count = 0;
        }

        // Early stopping on stagnation
        if stagnation_count >= max_stagnation {
            return Ok(OptimizeResult {
                x: global_best,
                fun: global_best_fitness,
                grad: vec![T::zero(); dim],
                nit: iter + 1,
                nfev,
                njev,
                success: true,
                message: "Stopped due to stagnation".to_string(),
            });
        }
    }

    Ok(OptimizeResult {
        x: global_best,
        fun: global_best_fitness,
        grad: vec![T::zero(); dim],
        nit: config.max_iter,
        nfev,
        njev,
        success: true,
        message: "Maximum iterations reached".to_string(),
    })
}

/// Initialize swarm with random particles
fn initialize_swarm<T, F>(
    f: &F,
    bounds: &[(T, T)],
    swarm_size: usize,
    v_max: &[T],
    rng: &mut impl Rng,
) -> Result<Vec<Particle<T>>>
where
    T: Float + std::fmt::Display,
    F: Fn(&[T]) -> T,
{
    let dim = bounds.len();
    let mut swarm = Vec::with_capacity(swarm_size);

    for _ in 0..swarm_size {
        let mut position = Vec::with_capacity(dim);
        let mut velocity = Vec::with_capacity(dim);

        for j in 0..dim {
            let (lower, upper) = bounds[j];

            // Random position
            let lower_f64 = lower.to_f64().ok_or_else(|| {
                NumRs2Error::ConversionError("Bound conversion failed".to_string())
            })?;
            let upper_f64 = upper.to_f64().ok_or_else(|| {
                NumRs2Error::ConversionError("Bound conversion failed".to_string())
            })?;

            let pos_uniform = Uniform::new(lower_f64, upper_f64).map_err(|e| {
                NumRs2Error::ComputationError(format!("Uniform creation failed: {}", e))
            })?;

            let pos = T::from(pos_uniform.sample(rng)).ok_or_else(|| {
                NumRs2Error::ConversionError("Position sample conversion failed".to_string())
            })?;

            // Random velocity in [-v_max, v_max]
            let v_max_f64 = v_max[j].to_f64().ok_or_else(|| {
                NumRs2Error::ConversionError("Velocity max conversion failed".to_string())
            })?;

            let vel_uniform = Uniform::new(-v_max_f64, v_max_f64).map_err(|e| {
                NumRs2Error::ComputationError(format!("Uniform creation failed: {}", e))
            })?;

            let vel = T::from(vel_uniform.sample(rng)).ok_or_else(|| {
                NumRs2Error::ConversionError("Velocity sample conversion failed".to_string())
            })?;

            position.push(pos);
            velocity.push(vel);
        }

        let fitness = f(&position);

        swarm.push(Particle {
            position: position.clone(),
            velocity,
            best_position: position,
            best_fitness: fitness,
            fitness,
        });
    }

    Ok(swarm)
}

/// Calculate swarm diversity (normalized standard deviation)
fn calculate_diversity<T: Float>(swarm: &[Particle<T>]) -> f64 {
    if swarm.is_empty() {
        return 0.0;
    }

    let dim = swarm[0].position.len();
    let n = swarm.len() as f64;

    let mut total_variance = 0.0;

    for j in 0..dim {
        // Calculate mean
        let mean = swarm
            .iter()
            .map(|p| p.position[j].to_f64().unwrap_or(0.0))
            .sum::<f64>()
            / n;

        // Calculate variance
        let variance = swarm
            .iter()
            .map(|p| {
                let diff = p.position[j].to_f64().unwrap_or(0.0) - mean;
                diff * diff
            })
            .sum::<f64>()
            / n;

        total_variance += variance;
    }

    (total_variance / dim as f64).sqrt().min(1.0)
}

/// Get local best from k nearest neighbors
fn get_local_best<T: Float>(swarm: &[Particle<T>], idx: usize, k: usize) -> &Vec<T> {
    // For simplicity, use k adjacent particles in the swarm array
    let n = swarm.len();
    let half_k = k / 2;

    let start = idx.saturating_sub(half_k);
    let end = (idx + half_k + 1).min(n);

    let mut best_idx = idx;
    let mut best_fitness = swarm[idx].best_fitness;

    for i in start..end {
        if swarm[i].best_fitness < best_fitness {
            best_fitness = swarm[i].best_fitness;
            best_idx = i;
        }
    }

    &swarm[best_idx].best_position
}

/// Get ring topology best (immediate neighbors)
fn get_ring_best<T: Float>(swarm: &[Particle<T>], idx: usize) -> &Vec<T> {
    let n = swarm.len();
    let prev = if idx == 0 { n - 1 } else { idx - 1 };
    let next = (idx + 1) % n;

    let mut best_idx = idx;
    let mut best_fitness = swarm[idx].best_fitness;

    for &i in &[prev, next] {
        if swarm[i].best_fitness < best_fitness {
            best_fitness = swarm[i].best_fitness;
            best_idx = i;
        }
    }

    &swarm[best_idx].best_position
}

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

    #[test]
    fn test_pso_sphere_function() {
        // Minimize f(x,y) = x^2 + y^2
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];

        // Improved parameters for more reliable convergence
        let config = PSOConfig {
            swarm_size: 50, // Larger swarm for better exploration
            max_iter: 200,  // More iterations for convergence
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
        // Relaxed tolerance for stochastic algorithm - 0.1 is still very good
        assert!(
            result.fun < 0.1,
            "Should find good solution, got fun = {}",
            result.fun
        );
        assert_relative_eq!(result.x[0], 0.0, epsilon = 0.3);
        assert_relative_eq!(result.x[1], 0.0, epsilon = 0.3);
    }

    #[test]
    fn test_pso_rosenbrock() {
        let f = |x: &[f64]| {
            let (x0, x1) = (x[0], x[1]);
            (1.0 - x0).powi(2) + 100.0 * (x1 - x0 * x0).powi(2)
        };
        let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];

        let config = PSOConfig {
            swarm_size: 50,
            max_iter: 200,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
        assert!(result.fun < 1.0, "Should find reasonable solution");
    }

    #[test]
    fn test_pso_constant_inertia() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];

        let config = PSOConfig {
            inertia: InertiaStrategy::Constant(0.7),
            swarm_size: 30,
            max_iter: 50,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
    }

    #[test]
    fn test_pso_adaptive_inertia() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];

        let config = PSOConfig {
            inertia: InertiaStrategy::Adaptive,
            swarm_size: 30,
            max_iter: 50,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
    }

    #[test]
    fn test_pso_local_topology() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];

        let config = PSOConfig {
            topology: Topology::Local(5),
            swarm_size: 30,
            max_iter: 50,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
    }

    #[test]
    fn test_pso_ring_topology() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];

        let config = PSOConfig {
            topology: Topology::Ring,
            swarm_size: 30,
            max_iter: 50,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
    }

    #[test]
    fn test_pso_high_dimensional() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0); 10];

        let config = PSOConfig {
            swarm_size: 120,
            max_iter: 300,
            inertia: InertiaStrategy::LinearDecay {
                w_max: 0.95,
                w_min: 0.3,
            },
            c1: 2.05,
            c2: 2.05,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config)).expect("PSO should succeed");
        assert!(result.success);
        assert!(result.fun < 5.0);
    }

    #[test]
    fn test_pso_invalid_bounds() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds: Vec<(f64, f64)> = vec![];

        let result = particle_swarm(f, &bounds, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_pso_invalid_swarm_size() {
        let f = |x: &[f64]| x.iter().map(|&xi| xi * xi).sum();
        let bounds = vec![(-5.0, 5.0)];

        let config = PSOConfig {
            swarm_size: 1,
            ..Default::default()
        };

        let result = particle_swarm(f, &bounds, Some(config));
        assert!(result.is_err());
    }
}