basin 0.8.0

Numerical optimization in pure Rust, with pluggable linear-algebra backends and WASM support.
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
//! N-dimensional Levy function.
//!
//! ```text
//! wᵢ = 1 + (xᵢ − 1)/4
//! f(x) = sin²(π·w₁)
//!      + Σᵢ₌₁ⁿ⁻¹ (wᵢ − 1)²·[1 + 10·sin²(π·wᵢ + 1)]
//!      + (wₙ − 1)²·[1 + sin²(2π·wₙ)]
//! ```
//!
//! Multimodal but smooth: the affine `wᵢ` reparameterization places the global
//! minimum at `x = (1, …, 1)` with `f = 0`, surrounded by many local minima
//! from the `sin²` ripple terms. Unlike [`Ackley`](super::Ackley), Levy is
//! differentiable everywhere, so an analytic gradient is provided (a local
//! method started near the optimum converges; from far away it stalls in a
//! ripple). Standard search domain is `[−10, 10]^n`.

use core::marker::PhantomData;

use super::spec::{Dimensionality, HasSpec, ProblemSpec, Properties, Reference};
use crate::{BoxConstraints, CostFunction, Gradient};

/// Standard lower bound on each coordinate.
pub const STANDARD_LOWER: f64 = -10.0;
/// Standard upper bound on each coordinate.
pub const STANDARD_UPPER: f64 = 10.0;

/// Evaluates the Levy function at `x`. Works for any `n >= 1`.
pub fn levy(x: &[f64]) -> f64 {
    debug_assert!(!x.is_empty());
    let pi = core::f64::consts::PI;
    let n = x.len();
    let w = |i: usize| 1.0 + (x[i] - 1.0) / 4.0;

    let mut s = (pi * w(0)).sin().powi(2);
    for i in 0..n - 1 {
        let wi = w(i);
        let t = (pi * wi + 1.0).sin();
        s += (wi - 1.0).powi(2) * (1.0 + 10.0 * t * t);
    }
    let wl = w(n - 1);
    let tl = (2.0 * pi * wl).sin();
    s += (wl - 1.0).powi(2) * (1.0 + tl * tl);
    s
}

/// Writes the Levy gradient at `x` into `out`. Both slices must have the same
/// length `n >= 1`.
pub fn levy_gradient(x: &[f64], out: &mut [f64]) {
    debug_assert_eq!(x.len(), out.len());
    debug_assert!(!x.is_empty());
    let pi = core::f64::consts::PI;
    let n = x.len();
    let w = |i: usize| 1.0 + (x[i] - 1.0) / 4.0;
    // dwᵢ/dxᵢ = 1/4 throughout.
    for g in out.iter_mut() {
        *g = 0.0;
    }

    // Term 1: sin²(π·w₁), depends on w₀.
    out[0] += 0.25 * pi * (2.0 * pi * w(0)).sin();

    // Middle terms i = 1..n−1 (zero-indexed 0..n−1):
    //   Mᵢ = (wᵢ − 1)²·[1 + 10·sin²(θ)], θ = π·wᵢ + 1.
    //   dMᵢ/dwᵢ = 2(wᵢ−1)(1 + 10·sin²θ) + (wᵢ−1)²·10π·sin(2θ).
    for (g, &xi) in out.iter_mut().zip(x.iter()).take(n - 1) {
        let wi = 1.0 + (xi - 1.0) / 4.0;
        let theta = pi * wi + 1.0;
        let s = theta.sin();
        let dm = 2.0 * (wi - 1.0) * (1.0 + 10.0 * s * s)
            + (wi - 1.0).powi(2) * 10.0 * pi * (2.0 * theta).sin();
        *g += 0.25 * dm;
    }

    // Last term: (wₙ − 1)²·[1 + sin²(φ)], φ = 2π·wₙ.
    //   dL/dwₙ = 2(wₙ−1)(1 + sin²φ) + (wₙ−1)²·2π·sin(2φ).
    let wl = w(n - 1);
    let phi = 2.0 * pi * wl;
    let sl = phi.sin();
    let dl = 2.0 * (wl - 1.0) * (1.0 + sl * sl) + (wl - 1.0).powi(2) * 2.0 * pi * (2.0 * phi).sin();
    out[n - 1] += 0.25 * dl;
}

/// Pre-wrapped Levy problem. Generic over the parameter backend `P`; the
/// default `P = Vec<f64>` lets you write `Levy::default()` for the common case.
/// Backend impls (`nalgebra::DVector<f64>`, `ndarray::Array1<f64>`,
/// `faer::Col<f64>`) are gated behind their respective features.
///
/// Carries no constraint metadata. For solvers that need explicit box bounds,
/// use [`LevyBoxed`].
pub struct Levy<P = Vec<f64>>(PhantomData<fn() -> P>);

impl<P> Levy<P> {
    /// Build a freshly typed problem instance. Pair with one of the
    /// backend-specific impl blocks (Vec, nalgebra, ndarray, faer).
    pub const fn new() -> Self {
        Self(PhantomData)
    }
}

impl<P> Default for Levy<P> {
    fn default() -> Self {
        Self::new()
    }
}

/// Catalogue entry for this problem.
pub static LEVY_SPEC: ProblemSpec = ProblemSpec {
    name: "Levy",
    dim: Dimensionality::NDimensional { min: 1 },
    properties: Properties {
        smooth: true,
        differentiable: true,
        convex: false,
        unimodal: false,
        separable: false,
        scalable: true,
    },
    references: &[Reference {
        citation: "Laguna & Martí (2005)",
        title: "Experimental testing of advanced scatter search designs for global optimization of multimodal functions",
        source: "Journal of Global Optimization, 33(2), 235–255",
        doi: Some("10.1007/s10898-004-1936-z"),
        url: None,
    }],
    description: "Smooth but multimodal: an affine reparameterization wᵢ = 1 + \
                  (xᵢ−1)/4 places the global minimum at x = (1, …, 1), value 0, \
                  surrounded by sin² ripple minima. Standard search domain is \
                  [−10, 10]ⁿ. Differentiable everywhere (analytic gradient).",
};

impl<P> HasSpec for Levy<P> {
    const SPEC: &'static ProblemSpec = &LEVY_SPEC;
}

impl CostFunction for Levy<Vec<f64>> {
    type Param = Vec<f64>;
    type Output = f64;
    type Error = std::convert::Infallible;
    fn cost(&self, x: &Vec<f64>) -> Result<f64, std::convert::Infallible> {
        Ok(levy(x))
    }
}

impl Gradient for Levy<Vec<f64>> {
    type Gradient = Vec<f64>;
    fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, std::convert::Infallible> {
        let mut out = vec![0.0; x.len()];
        levy_gradient(x, &mut out);
        Ok(out)
    }
}

#[cfg(feature = "nalgebra")]
mod nalgebra_impl {
    use super::{levy, levy_gradient, Levy};
    use crate::{CostFunction, Gradient};
    use nalgebra::DVector;

    impl CostFunction for Levy<DVector<f64>> {
        type Param = DVector<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &DVector<f64>) -> Result<f64, std::convert::Infallible> {
            Ok(levy(x.as_slice()))
        }
    }

    impl Gradient for Levy<DVector<f64>> {
        type Gradient = DVector<f64>;
        fn gradient(&self, x: &DVector<f64>) -> Result<DVector<f64>, std::convert::Infallible> {
            let mut out = DVector::zeros(x.len());
            levy_gradient(x.as_slice(), out.as_mut_slice());
            Ok(out)
        }
    }
}

#[cfg(feature = "ndarray")]
mod ndarray_impl {
    use super::{levy, levy_gradient, Levy};
    use crate::{CostFunction, Gradient};
    use ndarray::Array1;

    impl CostFunction for Levy<Array1<f64>> {
        type Param = Array1<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Array1<f64>) -> Result<f64, std::convert::Infallible> {
            Ok(levy(x.as_slice().expect("Array1 is contiguous")))
        }
    }

    impl Gradient for Levy<Array1<f64>> {
        type Gradient = Array1<f64>;
        fn gradient(&self, x: &Array1<f64>) -> Result<Array1<f64>, std::convert::Infallible> {
            let mut out = Array1::zeros(x.len());
            levy_gradient(
                x.as_slice().expect("Array1 is contiguous"),
                out.as_slice_mut().expect("Array1 is contiguous"),
            );
            Ok(out)
        }
    }
}

#[cfg(feature = "faer")]
mod faer_impl {
    use super::{levy, levy_gradient, Levy};
    use crate::{CostFunction, Gradient};
    use faer::Col;

    // The Levy math is index-coupled (first/middle/last terms differ), so we
    // collect into a `Vec` and route through the slice primitives rather than
    // duplicating the recurrence elementwise.
    impl CostFunction for Levy<Col<f64>> {
        type Param = Col<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Col<f64>) -> Result<f64, std::convert::Infallible> {
            let v: Vec<f64> = (0..x.nrows()).map(|i| x[i]).collect();
            Ok(levy(&v))
        }
    }

    impl Gradient for Levy<Col<f64>> {
        type Gradient = Col<f64>;
        fn gradient(&self, x: &Col<f64>) -> Result<Col<f64>, std::convert::Infallible> {
            let v: Vec<f64> = (0..x.nrows()).map(|i| x[i]).collect();
            let mut g = vec![0.0; v.len()];
            levy_gradient(&v, &mut g);
            Ok(Col::<f64>::from_fn(g.len(), |i| g[i]))
        }
    }
}

// ----------------------------------------------------------------------
// Boxed (constrained) form
// ----------------------------------------------------------------------

/// Levy function with explicit element-wise box bounds, suitable for
/// box-constrained solvers (L-BFGS-B, projected gradient, CMA-ES variants).
/// Implements both [`CostFunction`] and [`Gradient`] plus [`BoxConstraints`],
/// routing through the same raw [`levy`] / [`levy_gradient`] free functions as
/// the unconstrained [`Levy`]. The standard search domain `[−10, 10]ⁿ` is the
/// common case; build it with [`LevyBoxed::with_standard_bounds`].
pub struct LevyBoxed<P> {
    lower: P,
    upper: P,
}

impl<P> LevyBoxed<P> {
    /// Build a Levy problem with arbitrary element-wise bounds. Caller must
    /// ensure `lower[i] ≤ upper[i]` per component.
    pub fn new(lower: P, upper: P) -> Self {
        Self { lower, upper }
    }
}

impl<P> HasSpec for LevyBoxed<P> {
    const SPEC: &'static ProblemSpec = &LEVY_SPEC;
}

impl LevyBoxed<Vec<f64>> {
    /// Build the canonical Levy instance on `[−10, 10]ⁿ` for the requested
    /// dimension `n`.
    pub fn with_standard_bounds(n: usize) -> Self {
        Self {
            lower: vec![STANDARD_LOWER; n],
            upper: vec![STANDARD_UPPER; n],
        }
    }
}

impl CostFunction for LevyBoxed<Vec<f64>> {
    type Param = Vec<f64>;
    type Output = f64;
    type Error = std::convert::Infallible;
    fn cost(&self, x: &Vec<f64>) -> Result<f64, std::convert::Infallible> {
        Ok(levy(x))
    }
}

impl Gradient for LevyBoxed<Vec<f64>> {
    type Gradient = Vec<f64>;
    fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, std::convert::Infallible> {
        let mut out = vec![0.0; x.len()];
        levy_gradient(x, &mut out);
        Ok(out)
    }
}

impl BoxConstraints for LevyBoxed<Vec<f64>> {
    fn lower(&self) -> &Vec<f64> {
        &self.lower
    }
    fn upper(&self) -> &Vec<f64> {
        &self.upper
    }
}

#[cfg(feature = "nalgebra")]
mod nalgebra_boxed_impl {
    use super::{levy, levy_gradient, LevyBoxed, STANDARD_LOWER, STANDARD_UPPER};
    use crate::{BoxConstraints, CostFunction, Gradient};
    use nalgebra::DVector;

    impl LevyBoxed<DVector<f64>> {
        /// Build the canonical Levy instance on `[−10, 10]ⁿ` for the requested
        /// dimension `n`.
        pub fn with_standard_bounds(n: usize) -> Self {
            Self {
                lower: DVector::from_element(n, STANDARD_LOWER),
                upper: DVector::from_element(n, STANDARD_UPPER),
            }
        }
    }

    impl CostFunction for LevyBoxed<DVector<f64>> {
        type Param = DVector<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &DVector<f64>) -> Result<f64, std::convert::Infallible> {
            Ok(levy(x.as_slice()))
        }
    }

    impl Gradient for LevyBoxed<DVector<f64>> {
        type Gradient = DVector<f64>;
        fn gradient(&self, x: &DVector<f64>) -> Result<DVector<f64>, std::convert::Infallible> {
            let mut out = DVector::zeros(x.len());
            levy_gradient(x.as_slice(), out.as_mut_slice());
            Ok(out)
        }
    }

    impl BoxConstraints for LevyBoxed<DVector<f64>> {
        fn lower(&self) -> &DVector<f64> {
            &self.lower
        }
        fn upper(&self) -> &DVector<f64> {
            &self.upper
        }
    }
}

#[cfg(feature = "ndarray")]
mod ndarray_boxed_impl {
    use super::{levy, levy_gradient, LevyBoxed, STANDARD_LOWER, STANDARD_UPPER};
    use crate::{BoxConstraints, CostFunction, Gradient};
    use ndarray::Array1;

    impl LevyBoxed<Array1<f64>> {
        /// Build the canonical Levy instance on `[−10, 10]ⁿ` for the requested
        /// dimension `n`.
        pub fn with_standard_bounds(n: usize) -> Self {
            Self {
                lower: Array1::from_elem(n, STANDARD_LOWER),
                upper: Array1::from_elem(n, STANDARD_UPPER),
            }
        }
    }

    impl CostFunction for LevyBoxed<Array1<f64>> {
        type Param = Array1<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Array1<f64>) -> Result<f64, std::convert::Infallible> {
            Ok(levy(x.as_slice().expect("Array1 is contiguous")))
        }
    }

    impl Gradient for LevyBoxed<Array1<f64>> {
        type Gradient = Array1<f64>;
        fn gradient(&self, x: &Array1<f64>) -> Result<Array1<f64>, std::convert::Infallible> {
            let mut out = Array1::zeros(x.len());
            levy_gradient(
                x.as_slice().expect("Array1 is contiguous"),
                out.as_slice_mut().expect("Array1 is contiguous"),
            );
            Ok(out)
        }
    }

    impl BoxConstraints for LevyBoxed<Array1<f64>> {
        fn lower(&self) -> &Array1<f64> {
            &self.lower
        }
        fn upper(&self) -> &Array1<f64> {
            &self.upper
        }
    }
}

#[cfg(feature = "faer")]
mod faer_boxed_impl {
    use super::{levy, levy_gradient, LevyBoxed, STANDARD_LOWER, STANDARD_UPPER};
    use crate::{BoxConstraints, CostFunction, Gradient};
    use faer::Col;

    impl LevyBoxed<Col<f64>> {
        /// Build the canonical Levy instance on `[−10, 10]ⁿ` for the requested
        /// dimension `n`.
        pub fn with_standard_bounds(n: usize) -> Self {
            Self {
                lower: Col::<f64>::from_fn(n, |_| STANDARD_LOWER),
                upper: Col::<f64>::from_fn(n, |_| STANDARD_UPPER),
            }
        }
    }

    impl CostFunction for LevyBoxed<Col<f64>> {
        type Param = Col<f64>;
        type Output = f64;
        type Error = std::convert::Infallible;
        fn cost(&self, x: &Col<f64>) -> Result<f64, std::convert::Infallible> {
            let v: Vec<f64> = (0..x.nrows()).map(|i| x[i]).collect();
            Ok(levy(&v))
        }
    }

    impl Gradient for LevyBoxed<Col<f64>> {
        type Gradient = Col<f64>;
        fn gradient(&self, x: &Col<f64>) -> Result<Col<f64>, std::convert::Infallible> {
            let v: Vec<f64> = (0..x.nrows()).map(|i| x[i]).collect();
            let mut g = vec![0.0; v.len()];
            levy_gradient(&v, &mut g);
            Ok(Col::<f64>::from_fn(g.len(), |i| g[i]))
        }
    }

    impl BoxConstraints for LevyBoxed<Col<f64>> {
        fn lower(&self) -> &Col<f64> {
            &self.lower
        }
        fn upper(&self) -> &Col<f64> {
            &self.upper
        }
    }
}

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

    #[test]
    fn minimum_is_zero_at_ones() {
        assert!(levy(&[1.0]).abs() < 1e-12);
        assert!(levy(&[1.0, 1.0]).abs() < 1e-12);
        assert!(levy(&[1.0; 7]).abs() < 1e-12);
    }

    #[test]
    fn known_value_at_fives() {
        // x = (5, 5) ⇒ w = (2, 2). Term1 = sin²(2π) = 0.
        // Middle (i = 1): (2−1)²·[1 + 10·sin²(2π+1)] = 1 + 10·sin²(1).
        // Last: (2−1)²·[1 + sin²(4π)] = 1.
        // f = 0 + (1 + 10·sin²1) + 1 = 2 + 10·sin²1 ≈ 9.0807342.
        let expected = 2.0 + 10.0 * 1.0_f64.sin().powi(2);
        assert!((levy(&[5.0, 5.0]) - expected).abs() < 1e-9);
    }

    #[test]
    fn gradient_zero_at_minimum() {
        let mut g = vec![0.0; 4];
        levy_gradient(&[1.0; 4], &mut g);
        for v in g {
            assert!(v.abs() < 1e-12);
        }
    }

    #[test]
    fn gradient_matches_finite_difference() {
        let x = [-1.2, 0.7, 2.3];
        let mut g = vec![0.0; x.len()];
        levy_gradient(&x, &mut g);
        let h = 1e-6;
        for i in 0..x.len() {
            let mut xp = x;
            let mut xm = x;
            xp[i] += h;
            xm[i] -= h;
            let fd = (levy(&xp) - levy(&xm)) / (2.0 * h);
            assert!((g[i] - fd).abs() < 1e-5, "i={i}, g={}, fd={fd}", g[i]);
        }
    }

    #[test]
    fn spec_is_wired_up_via_has_spec_trait() {
        let spec = <Levy<Vec<f64>> as HasSpec>::SPEC;
        assert_eq!(spec.name, "Levy");
        assert!(spec.properties.smooth);
        assert!(spec.properties.differentiable);
        assert!(spec.properties.scalable);
        assert!(matches!(spec.dim, Dimensionality::NDimensional { min: 1 }));
        assert!(!spec.references.is_empty());
    }

    #[test]
    fn boxed_form_exposes_standard_bounds_and_shares_math() {
        let p = LevyBoxed::<Vec<f64>>::with_standard_bounds(4);
        let lo = <LevyBoxed<Vec<f64>> as BoxConstraints>::lower(&p);
        let hi = <LevyBoxed<Vec<f64>> as BoxConstraints>::upper(&p);
        assert!(lo.iter().all(|&v| v == STANDARD_LOWER));
        assert!(hi.iter().all(|&v| v == STANDARD_UPPER));

        let x = vec![0.3, -0.7, 1.2, 2.1];
        assert!((p.cost(&x).unwrap() - levy(&x)).abs() < 1e-12);
        let mut g = vec![0.0; x.len()];
        levy_gradient(&x, &mut g);
        assert_eq!(p.gradient(&x).unwrap(), g);
    }

    #[test]
    fn boxed_form_reuses_levy_spec() {
        let spec = <LevyBoxed<Vec<f64>> as HasSpec>::SPEC;
        assert!(core::ptr::eq(spec, &LEVY_SPEC));
    }
}