scirs2-optimize 0.4.2

Optimization module for SciRS2 (scirs2-optimize)
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
//! Classical stochastic approximation algorithms.
//!
//! This submodule implements the three canonical stochastic approximation (SA)
//! methods from the 1950s–1960s together with a modern SPSA variant:
//!
//! | Algorithm | Reference | Use case |
//! |-----------|-----------|----------|
//! | Robbins-Monro | Robbins & Monro (1951) | Root-finding under noise |
//! | Kiefer-Wolfowitz | Kiefer & Wolfowitz (1952) | Gradient-free stochastic minimisation |
//! | SPSA | Spall (1992) | High-dimensional gradient-free SA |
//!
//! # Notation
//!
//! - xₖ  : current iterate
//! - aₖ  : gain sequence for update step (must satisfy Σ aₖ = ∞, Σ aₖ² < ∞)
//! - cₖ  : gain sequence for finite-difference width (must → 0)

use crate::error::{OptimizeError, OptimizeResult};
use scirs2_core::ndarray::{Array1, ArrayView1};

// ─── Robbins-Monro ───────────────────────────────────────────────────────────

/// Result from Robbins-Monro root finding.
#[derive(Debug, Clone)]
pub struct RobbinsMonroResult {
    /// Approximate root: θ* such that M(θ*) ≈ 0.
    pub x: Array1<f64>,
    /// Final residual ‖M(xₖ)‖.
    pub residual: f64,
    /// Number of iterations.
    pub n_iter: usize,
    /// Whether tolerance was met.
    pub converged: bool,
}

/// Options for the Robbins-Monro algorithm.
#[derive(Debug, Clone)]
pub struct RobbinsMonroOptions {
    /// Maximum number of iterations.
    pub max_iter: usize,
    /// Convergence tolerance on ‖update step‖.
    pub tol: f64,
    /// Exponent α in the gain aₖ = a / kᵅ.  Standard choice: α = 1.0.
    pub alpha: f64,
    /// Scale a in aₖ = a / kᵅ.
    pub a: f64,
}

impl Default for RobbinsMonroOptions {
    fn default() -> Self {
        Self {
            max_iter: 10_000,
            tol: 1e-6,
            alpha: 1.0,
            a: 1.0,
        }
    }
}

/// Robbins-Monro stochastic root-finding algorithm.
///
/// Finds θ* such that M(θ*) = 0, where M is a noisy mapping (possibly
/// the gradient of an expected loss).  The update rule is:
///
/// θₖ₊₁ = θₖ - aₖ · M(θₖ)
///
/// with gain aₖ = a / k^α.
///
/// # Arguments
///
/// * `m`    – noisy mapping M: θ → ℝⁿ (should satisfy E[M(θ)] ≈ ∇L(θ))
/// * `x0`   – initial point
/// * `opts` – algorithm options
pub fn robbins_monro<M>(
    m: &mut M,
    x0: &ArrayView1<f64>,
    opts: &RobbinsMonroOptions,
) -> OptimizeResult<RobbinsMonroResult>
where
    M: FnMut(&ArrayView1<f64>) -> Array1<f64>,
{
    let n = x0.len();
    if n == 0 {
        return Err(OptimizeError::ValueError(
            "x0 must be non-empty".to_string(),
        ));
    }

    let mut x = x0.to_owned();
    let mut converged = false;
    let mut residual = f64::INFINITY;

    for k in 1..=opts.max_iter {
        let mk = m(&x.view());
        if mk.len() != n {
            return Err(OptimizeError::ValueError(format!(
                "M returned length {} but x has length {}",
                mk.len(),
                n
            )));
        }
        let ak = opts.a / (k as f64).powf(opts.alpha);
        let mut step_norm = 0.0_f64;
        for i in 0..n {
            let step = ak * mk[i];
            x[i] -= step;
            step_norm += step * step;
        }
        residual = step_norm.sqrt();
        if residual < opts.tol {
            converged = true;
            residual = mk.iter().map(|v| v * v).sum::<f64>().sqrt();
            return Ok(RobbinsMonroResult {
                x,
                residual,
                n_iter: k,
                converged,
            });
        }
    }

    // Final residual
    let mk_final = m(&x.view());
    residual = mk_final.iter().map(|v| v * v).sum::<f64>().sqrt();

    Ok(RobbinsMonroResult {
        x,
        residual,
        n_iter: opts.max_iter,
        converged,
    })
}

// ─── Kiefer-Wolfowitz ────────────────────────────────────────────────────────

/// Result from the Kiefer-Wolfowitz algorithm.
#[derive(Debug, Clone)]
pub struct KieferWolfowitzResult {
    /// Approximate minimiser.
    pub x: Array1<f64>,
    /// Function value at x.
    pub fun: f64,
    /// Number of iterations.
    pub n_iter: usize,
    /// Whether tolerance was met.
    pub converged: bool,
}

/// Options for the Kiefer-Wolfowitz algorithm.
#[derive(Debug, Clone)]
pub struct KieferWolfowitzOptions {
    /// Maximum iterations.
    pub max_iter: usize,
    /// Convergence tolerance (step norm).
    pub tol: f64,
    /// Exponent α in aₖ = a / kᵅ.  Must satisfy α ∈ (1/2, 1].
    pub alpha: f64,
    /// Exponent γ in cₖ = c / kᵞ.  Must satisfy γ ∈ (0, 1/6] for unbiased gradients.
    pub gamma: f64,
    /// Scale constant a.
    pub a: f64,
    /// Scale constant c (initial finite-difference width).
    pub c: f64,
}

impl Default for KieferWolfowitzOptions {
    fn default() -> Self {
        Self {
            max_iter: 10_000,
            tol: 1e-6,
            alpha: 0.602,
            gamma: 0.101,
            a: 0.1,
            c: 0.1,
        }
    }
}

/// Kiefer-Wolfowitz gradient-free stochastic approximation.
///
/// Minimises E[L(x, ξ)] using only noisy function evaluations (no gradients).
/// The finite-difference gradient estimate in dimension i is:
///
/// ĝᵢ = (L(x + cₖ eᵢ) - L(x - cₖ eᵢ)) / (2 cₖ)
///
/// followed by the update x ← x - aₖ ĝ.
///
/// # Arguments
///
/// * `loss` – noisy loss function (x) → f64 (internally uses two evaluations per dimension per step)
/// * `x0`   – initial point
/// * `opts` – algorithm options
pub fn kiefer_wolfowitz<L>(
    loss: &mut L,
    x0: &ArrayView1<f64>,
    opts: &KieferWolfowitzOptions,
) -> OptimizeResult<KieferWolfowitzResult>
where
    L: FnMut(&ArrayView1<f64>) -> f64,
{
    let n = x0.len();
    if n == 0 {
        return Err(OptimizeError::ValueError(
            "x0 must be non-empty".to_string(),
        ));
    }

    let mut x = x0.to_owned();
    let mut converged = false;

    for k in 1..=opts.max_iter {
        let ak = opts.a / (k as f64).powf(opts.alpha);
        let ck = opts.c / (k as f64).powf(opts.gamma);

        // Finite-difference gradient
        let mut grad = Array1::<f64>::zeros(n);
        for i in 0..n {
            let mut x_fwd = x.clone();
            let mut x_bwd = x.clone();
            x_fwd[i] += ck;
            x_bwd[i] -= ck;
            grad[i] = (loss(&x_fwd.view()) - loss(&x_bwd.view())) / (2.0 * ck);
        }

        let mut step_norm = 0.0_f64;
        for i in 0..n {
            let step = ak * grad[i];
            x[i] -= step;
            step_norm += step * step;
        }

        if step_norm.sqrt() < opts.tol {
            converged = true;
            let fun = loss(&x.view());
            return Ok(KieferWolfowitzResult {
                x,
                fun,
                n_iter: k,
                converged,
            });
        }
    }

    let fun = loss(&x.view());
    Ok(KieferWolfowitzResult {
        x,
        fun,
        n_iter: opts.max_iter,
        converged,
    })
}

// ─── SPSA ────────────────────────────────────────────────────────────────────

/// Options for the SPSA optimizer.
#[derive(Debug, Clone)]
pub struct SpsaOptions {
    /// Maximum number of iterations.
    pub max_iter: usize,
    /// Convergence tolerance on ‖step‖.
    pub tol: f64,
    /// Exponent α in aₖ = a / (A + k)^α.
    pub alpha: f64,
    /// Exponent γ in cₖ = c / k^γ.
    pub gamma: f64,
    /// Scale a.
    pub a: f64,
    /// Stability constant A (typically ≈ 0.1 * max_iter).
    pub big_a: f64,
    /// Finite-difference constant c.
    pub c: f64,
}

impl Default for SpsaOptions {
    fn default() -> Self {
        Self {
            max_iter: 5_000,
            tol: 1e-6,
            alpha: 0.602,
            gamma: 0.101,
            a: 0.1,
            big_a: 100.0,
            c: 0.1,
        }
    }
}

/// Result from the SPSA algorithm.
#[derive(Debug, Clone)]
pub struct SpsaResult {
    /// Approximate minimiser.
    pub x: Array1<f64>,
    /// Function value at x.
    pub fun: f64,
    /// Number of iterations.
    pub n_iter: usize,
    /// Whether tolerance was met.
    pub converged: bool,
}

/// Compute one SPSA gradient-estimate step.
///
/// The simultaneous perturbation direction Δ is sampled from {±1}ⁿ (Rademacher).
/// The gradient estimate is:
///
/// ĝ(x) = [f(x + cₖ Δ) - f(x - cₖ Δ)] / (2 cₖ) * (1/Δᵢ)  component-wise.
///
/// Returns the updated x after one SPSA step.
///
/// # Arguments
///
/// * `f`     – noisy objective function
/// * `x`     – current point (modified in place)
/// * `k`     – current iteration number (1-indexed)
/// * `opts`  – SPSA options
/// * `rng`   – mutable u64 state for the Rademacher perturbation (LCG)
pub fn spsa_step<F>(
    f: &mut F,
    x: &mut Array1<f64>,
    k: usize,
    opts: &SpsaOptions,
    rng_state: &mut u64,
) -> f64
where
    F: FnMut(&ArrayView1<f64>) -> f64,
{
    let n = x.len();
    let ak = opts.a / (opts.big_a + k as f64).powf(opts.alpha);
    let ck = opts.c / (k as f64).powf(opts.gamma);

    // Draw Rademacher perturbation Δ ∈ {-1, +1}ⁿ using LCG
    let mut delta = Array1::<f64>::zeros(n);
    for i in 0..n {
        *rng_state = rng_state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        delta[i] = if (*rng_state >> 63) == 0 { 1.0 } else { -1.0 };
    }

    // Two-sided function evaluations
    let x_fwd: Array1<f64> = x.iter().zip(delta.iter()).map(|(&xi, &di)| xi + ck * di).collect();
    let x_bwd: Array1<f64> = x.iter().zip(delta.iter()).map(|(&xi, &di)| xi - ck * di).collect();
    let f_fwd = f(&x_fwd.view());
    let f_bwd = f(&x_bwd.view());

    let diff = (f_fwd - f_bwd) / (2.0 * ck);

    // Update: x ← x - aₖ * ĝ  where ĝᵢ = diff / Δᵢ
    let mut step_sq = 0.0_f64;
    for i in 0..n {
        let gi = diff / delta[i]; // Δᵢ ∈ {±1} so 1/Δᵢ = Δᵢ
        let step = ak * gi;
        x[i] -= step;
        step_sq += step * step;
    }
    step_sq.sqrt()
}

/// Simultaneous Perturbation Stochastic Approximation (SPSA) optimizer.
///
/// SPSA uses only two function evaluations per iteration (regardless of dimension n),
/// making it very efficient for high-dimensional black-box minimisation.
///
/// # Arguments
///
/// * `f`    – noisy objective (minimised)
/// * `x0`   – starting point
/// * `opts` – SPSA options
pub fn spsa_minimize<F>(
    f: &mut F,
    x0: &ArrayView1<f64>,
    opts: &SpsaOptions,
) -> OptimizeResult<SpsaResult>
where
    F: FnMut(&ArrayView1<f64>) -> f64,
{
    if x0.is_empty() {
        return Err(OptimizeError::ValueError(
            "x0 must be non-empty".to_string(),
        ));
    }

    let mut x = x0.to_owned();
    let mut rng_state: u64 = 12345678901234567;
    let mut converged = false;

    for k in 1..=opts.max_iter {
        let step_norm = spsa_step(f, &mut x, k, opts, &mut rng_state);
        if step_norm < opts.tol {
            converged = true;
            let fun = f(&x.view());
            return Ok(SpsaResult {
                x,
                fun,
                n_iter: k,
                converged,
            });
        }
    }

    let fun = f(&x.view());
    Ok(SpsaResult {
        x,
        fun,
        n_iter: opts.max_iter,
        converged,
    })
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_robbins_monro_linear() {
        // M(x) = x - 2 (root at x=2)
        let mut m = |x: &ArrayView1<f64>| array![x[0] - 2.0];
        let x0 = array![0.0];
        let opts = RobbinsMonroOptions {
            max_iter: 50_000,
            tol: 1e-4,
            a: 1.0,
            alpha: 1.0,
        };
        let res = robbins_monro(&mut m, &x0.view(), &opts).expect("failed to create res");
        assert!(
            (res.x[0] - 2.0).abs() < 0.1,
            "expected x* ≈ 2.0, got {}",
            res.x[0]
        );
    }

    #[test]
    fn test_kiefer_wolfowitz_quadratic() {
        // L(x) = (x-3)²; minimiser at x=3
        let mut loss = |x: &ArrayView1<f64>| (x[0] - 3.0).powi(2);
        let x0 = array![0.0];
        let opts = KieferWolfowitzOptions {
            max_iter: 20_000,
            tol: 1e-5,
            ..Default::default()
        };
        let res = kiefer_wolfowitz(&mut loss, &x0.view(), &opts).expect("failed to create res");
        assert!(
            (res.x[0] - 3.0).abs() < 0.2,
            "expected x* ≈ 3.0, got {}",
            res.x[0]
        );
    }

    #[test]
    fn test_spsa_quadratic() {
        // f(x) = (x₀-1)² + (x₁-2)²; minimiser at (1, 2)
        let mut f = |x: &ArrayView1<f64>| (x[0] - 1.0).powi(2) + (x[1] - 2.0).powi(2);
        let x0 = array![0.0, 0.0];
        let opts = SpsaOptions {
            max_iter: 10_000,
            tol: 1e-5,
            a: 0.5,
            big_a: 50.0,
            c: 0.2,
            ..Default::default()
        };
        let res = spsa_minimize(&mut f, &x0.view(), &opts).expect("failed to create res");
        assert!(
            (res.x[0] - 1.0).abs() < 0.3,
            "expected x[0] ≈ 1.0, got {}",
            res.x[0]
        );
        assert!(
            (res.x[1] - 2.0).abs() < 0.3,
            "expected x[1] ≈ 2.0, got {}",
            res.x[1]
        );
    }
}