basin 3.0.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
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
//! Solver-owned models for the state API experiment.

use super::state::{FirstOrderState, PointState, SimplexState};
use basin::core::math::{
    Dot, MatVec, MatrixIdentity, NegInPlace, NormSquared, Scalar, ScaleInPlace,
    ScaledAdd, VectorLen,
};
use basin::core::rng::{ChaCha8Rng, RngExt, SeedableRng};
use basin::line_search::{LineSearch, LineSearchOutcome, MoreThuente, Wolfe};
use basin::solver::simulated_annealing::{Neighbor, TemperatureSchedule};
use basin::{CostFunction, Gradient, Problem, Solver, State};
use std::marker::PhantomData;

// Basin's rank-update trait and DenseMatrix mutation are private. Keeping this
// adapter local lets the external prototype exercise the same operation without
// expanding the stable math API or rebuilding the matrix on every update.
pub trait RankUpdate<V, F> {
    fn general_rank_one_update(&mut self, alpha: F, u: &V, v: &V);
}

pub struct Dense<F: Scalar> {
    data: Vec<F>,
    n: usize,
}

impl<F: Scalar> Dense<F> {
    pub fn nrows(&self) -> usize {
        self.n
    }
    pub fn data(&self) -> &[F] {
        &self.data
    }
}

impl<F: Scalar> MatrixIdentity for Dense<F> {
    fn identity(n: usize) -> Self {
        let mut data = vec![F::zero(); n * n];
        for i in 0..n {
            data[i * n + i] = F::one();
        }
        Self { data, n }
    }
}
impl<F: Scalar> MatVec<Vec<F>> for Dense<F> {
    fn matvec(&self, x: &Vec<F>) -> Vec<F> {
        assert_eq!(x.len(), self.n);
        if self.n == 0 {
            return Vec::new();
        }
        self.data
            .chunks(self.n)
            .map(|row| row.iter().zip(x).map(|(&a, &b)| a * b).sum())
            .collect()
    }
}
impl<F: Scalar> ScaleInPlace<F> for Dense<F> {
    fn scale_in_place(&mut self, alpha: F) {
        for x in &mut self.data {
            *x = *x * alpha;
        }
    }
}
impl<F: Scalar> RankUpdate<Vec<F>, F> for Dense<F> {
    fn general_rank_one_update(&mut self, alpha: F, u: &Vec<F>, v: &Vec<F>) {
        assert_eq!(u.len(), self.n);
        assert_eq!(v.len(), self.n);
        if self.n == 0 {
            return;
        }
        for (row, &ui) in self.data.chunks_mut(self.n).zip(u) {
            for (a, &vj) in row.iter_mut().zip(v) {
                *a = *a + alpha * ui * vj;
            }
        }
    }
}

#[cfg(feature = "nalgebra_all")]
impl<F: Scalar + crate::backend_aliases::nalgebra::Scalar>
    RankUpdate<crate::backend_aliases::nalgebra::DVector<F>, F>
    for crate::backend_aliases::nalgebra::DMatrix<F>
{
    fn general_rank_one_update(
        &mut self,
        alpha: F,
        u: &crate::backend_aliases::nalgebra::DVector<F>,
        v: &crate::backend_aliases::nalgebra::DVector<F>,
    ) {
        for i in 0..self.nrows() {
            for j in 0..self.ncols() {
                self[(i, j)] = self[(i, j)] + alpha * u[i] * v[j];
            }
        }
    }
}
#[cfg(feature = "ndarray_all")]
impl<F: Scalar> RankUpdate<crate::backend_aliases::ndarray::Array1<F>, F>
    for crate::backend_aliases::ndarray::Array2<F>
{
    fn general_rank_one_update(
        &mut self,
        alpha: F,
        u: &crate::backend_aliases::ndarray::Array1<F>,
        v: &crate::backend_aliases::ndarray::Array1<F>,
    ) {
        for i in 0..self.nrows() {
            for j in 0..self.ncols() {
                self[(i, j)] = self[(i, j)] + alpha * u[i] * v[j];
            }
        }
    }
}
#[cfg(feature = "faer_all")]
impl<F: Scalar> RankUpdate<crate::backend_aliases::faer::Col<F>, F>
    for crate::backend_aliases::faer::Mat<F>
{
    fn general_rank_one_update(
        &mut self,
        alpha: F,
        u: &crate::backend_aliases::faer::Col<F>,
        v: &crate::backend_aliases::faer::Col<F>,
    ) {
        for i in 0..self.nrows() {
            for j in 0..self.ncols() {
                self[(i, j)] = self[(i, j)] + alpha * u[i] * v[j];
            }
        }
    }
}

/// A default matrix association fixes inference without fixing the matrix type.
/// The explicit `M` parameter on BFGS still allows custom matrix storage.
pub trait Backend<F: Scalar>: VectorLen {
    type Matrix;
}
impl<F: Scalar> Backend<F> for Vec<F> {
    type Matrix = Dense<F>;
}

#[cfg(feature = "nalgebra_all")]
impl<F: Scalar + crate::backend_aliases::nalgebra::Scalar> Backend<F>
    for crate::backend_aliases::nalgebra::DVector<F>
{
    type Matrix = crate::backend_aliases::nalgebra::DMatrix<F>;
}
#[cfg(feature = "ndarray_all")]
impl<F: Scalar> Backend<F> for crate::backend_aliases::ndarray::Array1<F> {
    type Matrix = crate::backend_aliases::ndarray::Array2<F>;
}
#[cfg(feature = "faer_all")]
impl<F: Scalar> Backend<F> for crate::backend_aliases::faer::Col<F> {
    type Matrix = crate::backend_aliases::faer::Mat<F>;
}

/// BFGS's existing update, with the matrix and scaling flag in the solver.
///
/// # Backends
///
/// Exercised by this prototype: Vec, nalgebra, ndarray, and faer.
pub struct Bfgs<
    V: Backend<F>,
    F: Scalar = f64,
    M = <V as Backend<F>>::Matrix,
    L = Wolfe<F>,
> {
    matrix: Option<M>,
    scaled: bool,
    search: L,
    make_search: fn() -> L,
    epsilon: F,
    param: PhantomData<V>,
}

impl<V: Backend<F>, F: Scalar> Bfgs<V, F> {
    pub fn new() -> Self {
        Self::with_search(Wolfe::new)
    }
}

impl<V: Backend<F>, F: Scalar, M, L> Bfgs<V, F, M, L> {
    pub fn with_search(make_search: fn() -> L) -> Self {
        Self {
            matrix: None,
            scaled: false,
            search: make_search(),
            make_search,
            epsilon: F::from_f64(1e-10).unwrap(),
            param: PhantomData,
        }
    }

    pub fn inverse_hessian(&self) -> Option<&M> {
        self.matrix.as_ref()
    }
}

impl<V: Backend<F> + Clone, F: Scalar, M, L> basin::InitialState<V>
    for Bfgs<V, F, M, L>
{
    type State = FirstOrderState<V, F>;
    fn seed(&self, point: &V) -> Self::State {
        FirstOrderState::new(point.clone())
    }
}

impl<P, V, F, M, L> Solver<P, FirstOrderState<V, F>> for Bfgs<V, F, M, L>
where
    F: Scalar,
    V: Backend<F>
        + Clone
        + Dot<F>
        + NormSquared<F>
        + ScaledAdd<F>
        + ScaleInPlace<F>
        + NegInPlace,
    M: MatrixIdentity + MatVec<V> + ScaleInPlace<F> + RankUpdate<V, F>,
    P: CostFunction<Param = V, Output = F> + Gradient<Gradient = V>,
    L: LineSearch<P, V, F, Error = P::Error>,
{
    type Error = P::Error;

    fn init(
        &mut self,
        problem: &mut Problem<P>,
        mut state: FirstOrderState<V, F>,
    ) -> Result<FirstOrderState<V, F>, P::Error> {
        self.matrix = Some(M::identity(state.param().vec_len()));
        self.scaled = false;
        self.search = (self.make_search)();
        let x = state.param().clone();
        let (cost, gradient) = problem.cost_and_gradient(&x)?;
        state.replace(x, cost, gradient).unwrap();
        Ok(state)
    }

    fn next_iter(
        &mut self,
        problem: &mut Problem<P>,
        mut state: FirstOrderState<V, F>,
    ) -> Result<basin::SolverStep<FirstOrderState<V, F>>, P::Error> {
        let (mut x, cost, gradient) = state.take_current().unwrap();
        let matrix = self.matrix.as_mut().unwrap();
        let mut direction = matrix.matvec(&gradient);
        direction.neg_in_place();
        let alpha = match self
            .search
            .next_with_outcome(problem, &x, cost, &gradient, &direction)?
        {
            LineSearchOutcome::Step(alpha) => alpha,
            _ => {
                state.replace(x, cost, gradient).unwrap();
                return Ok(basin::SolverStep::from((
                    state,
                    Some(basin::Termination::numerical_failure(
                        "External solver reported a numerical failure.",
                    )),
                )));
            }
        };
        if !(alpha.is_finite() && alpha > F::zero()) {
            state.replace(x, cost, gradient).unwrap();
            return Ok(basin::SolverStep::from((
                state,
                Some(basin::Termination::custom(
                    "external.SolverConverged",
                    "External solver stopping predicate: SolverConverged.",
                    vec![],
                )),
            )));
        }
        let mut s = direction;
        s.scale_in_place(alpha);
        x.scaled_add(F::one(), &s);
        let (cost_new, gradient_new) = problem.cost_and_gradient(&x)?;
        let mut y = gradient_new.clone();
        y.scaled_add(-F::one(), &gradient);
        let sy = s.dot(&y);
        if sy > self.epsilon * s.norm_squared().sqrt() * y.norm_squared().sqrt()
        {
            if !self.scaled {
                let yy = y.dot(&y);
                if yy > F::zero() {
                    *matrix = M::identity(x.vec_len());
                    matrix.scale_in_place(sy / yy);
                }
                self.scaled = true;
            }
            let rho = F::one() / sy;
            let hy = matrix.matvec(&y);
            let coef = rho * (F::one() + rho * y.dot(&hy));
            matrix.general_rank_one_update(coef, &s, &s);
            matrix.general_rank_one_update(-rho, &s, &hy);
            matrix.general_rank_one_update(-rho, &hy, &s);
        }
        state.replace(x, cost_new, gradient_new).unwrap();
        Ok(basin::SolverStep::from((state, None)))
    }
}

struct Pair<V, F> {
    s: V,
    y: V,
    sy: F,
}

/// Unbounded L-BFGS, retaining only the pairs needed by two-loop recursion.
///
/// # Backends
///
/// Exercised by this prototype: Vec, nalgebra, ndarray, and faer.
pub struct Lbfgs<V, F: Scalar = f64, L = MoreThuente<F>> {
    pairs: Vec<Pair<V, F>>,
    capacity: usize,
    theta: F,
    search: L,
    make_search: fn() -> L,
}

impl<V, F: Scalar> Lbfgs<V, F> {
    pub fn new(capacity: usize) -> Self {
        Self::with_search(capacity, MoreThuente::new)
    }
}

impl<V: Clone + VectorLen, F: Scalar, L> basin::InitialState<V>
    for Lbfgs<V, F, L>
{
    type State = FirstOrderState<V, F>;
    fn seed(&self, point: &V) -> Self::State {
        FirstOrderState::new(point.clone())
    }
}

impl<V, F: Scalar, L> Lbfgs<V, F, L> {
    pub fn with_search(capacity: usize, make_search: fn() -> L) -> Self {
        assert!(capacity > 0);
        Self {
            pairs: Vec::with_capacity(capacity),
            capacity,
            theta: F::one(),
            search: make_search(),
            make_search,
        }
    }

    pub fn history_len(&self) -> usize {
        self.pairs.len()
    }
}

impl<P, V, F, L> Solver<P, FirstOrderState<V, F>> for Lbfgs<V, F, L>
where
    F: Scalar,
    V: Clone + VectorLen + Dot<F> + ScaledAdd<F> + ScaleInPlace<F> + NegInPlace,
    P: CostFunction<Param = V, Output = F> + Gradient<Gradient = V>,
    L: LineSearch<P, V, F, Error = P::Error>,
{
    type Error = P::Error;

    fn init(
        &mut self,
        problem: &mut Problem<P>,
        mut state: FirstOrderState<V, F>,
    ) -> Result<FirstOrderState<V, F>, P::Error> {
        self.pairs.clear();
        self.theta = F::one();
        self.search = (self.make_search)();
        let x = state.param().clone();
        let (cost, gradient) = problem.cost_and_gradient(&x)?;
        state.replace(x, cost, gradient).unwrap();
        Ok(state)
    }

    fn next_iter(
        &mut self,
        problem: &mut Problem<P>,
        mut state: FirstOrderState<V, F>,
    ) -> Result<basin::SolverStep<FirstOrderState<V, F>>, P::Error> {
        let (mut x, cost, gradient) = state.take_current().unwrap();
        let mut direction = gradient.clone();
        let mut alpha = vec![F::zero(); self.pairs.len()];
        for (pair, a) in self.pairs.iter().zip(&mut alpha).rev() {
            *a = pair.s.dot(&direction) / pair.sy;
            direction.scaled_add(-*a, &pair.y);
        }
        direction.scale_in_place(F::one() / self.theta);
        for (pair, a) in self.pairs.iter().zip(alpha) {
            let beta = pair.y.dot(&direction) / pair.sy;
            direction.scaled_add(a - beta, &pair.s);
        }
        direction.neg_in_place();
        let gdold = gradient.dot(&direction);
        let result = self
            .search
            .next_with_evaluation(problem, &x, cost, &gradient, &direction)?;
        let step = match result.outcome {
            LineSearchOutcome::Step(step) => step,
            _ => F::zero(),
        };
        if !(step.is_finite() && step > F::zero()) {
            state.replace(x, cost, gradient).unwrap();
            return Ok(basin::SolverStep::from((
                state,
                Some(basin::Termination::numerical_failure(
                    "External solver reported a numerical failure.",
                )),
            )));
        }
        let (cost_new, gradient_new) =
            if let Some(evaluation) = result.evaluation {
                x = evaluation.param;
                (evaluation.cost, evaluation.gradient)
            } else {
                x.scaled_add(step, &direction);
                problem.cost_and_gradient(&x)?
            };
        let mut s = direction;
        s.scale_in_place(step);
        let mut y = gradient_new.clone();
        y.scaled_add(-F::one(), &gradient);
        let sy = s.dot(&y);
        let yy = y.dot(&y);
        if sy > F::epsilon() * (-gdold * step).abs()
            && sy.is_finite()
            && yy.is_finite()
        {
            if self.pairs.len() == self.capacity {
                self.pairs.remove(0);
            }
            self.theta = yy / sy;
            self.pairs.push(Pair { s, y, sy });
        }
        state.replace(x, cost_new, gradient_new).unwrap();
        Ok(basin::SolverStep::from((state, None)))
    }
}

/// Classical Nelder-Mead with authoritative vertices in the shared state.
///
/// # Backends
///
/// Exercised by this prototype: Vec, nalgebra, ndarray, and faer.
pub struct NelderMead<V, F: Scalar = f64> {
    scratch: Vec<V>,
    float: PhantomData<F>,
}

impl<V, F: Scalar> NelderMead<V, F> {
    pub fn new() -> Self {
        Self {
            scratch: Vec::new(),
            float: PhantomData,
        }
    }
    pub fn scratch(&self) -> &[V] {
        &self.scratch
    }
}

impl<P, V, F> Solver<P, SimplexState<V, F>> for NelderMead<V, F>
where
    F: Scalar,
    V: Clone + VectorLen + ScaleInPlace<F> + ScaledAdd<F>,
    P: CostFunction<Param = V, Output = F>,
{
    type Error = P::Error;

    fn init(
        &mut self,
        problem: &mut Problem<P>,
        mut state: SimplexState<V, F>,
    ) -> Result<SimplexState<V, F>, P::Error> {
        let (points, _) = state.take_members();
        self.scratch = vec![points[0].clone(); 3];
        let costs = points
            .iter()
            .map(|x| problem.cost(x))
            .collect::<Result<Vec<_>, _>>()?;
        state.replace_members(points, costs).unwrap();
        Ok(state)
    }

    fn next_iter(
        &mut self,
        problem: &mut Problem<P>,
        mut state: SimplexState<V, F>,
    ) -> Result<basin::SolverStep<SimplexState<V, F>>, P::Error> {
        let (mut points, mut costs) = state.take_members();
        let n = points.len() - 1;
        let one = F::one();
        let half = F::from_f64(0.5).unwrap();
        let (centroid, rest) = self.scratch.split_at_mut(1);
        let (reflected, alternate) = rest.split_at_mut(1);
        let center = &mut centroid[0];
        let reflected = &mut reflected[0];
        let alternate = &mut alternate[0];
        center.scale_in_place(F::zero());
        for point in &points[..n] {
            center.scaled_add(one / F::from_usize(n).unwrap(), point);
        }
        affine(reflected, center, &points[n], -one);
        let fr = problem.cost(reflected)?;
        let mut shrink = false;
        if costs[0] <= fr && fr < costs[n - 1] {
            std::mem::swap(&mut points[n], reflected);
            costs[n] = fr;
        } else if fr < costs[0] {
            affine(alternate, center, reflected, one + one);
            let fe = problem.cost(alternate)?;
            if fe < fr {
                std::mem::swap(&mut points[n], alternate);
                costs[n] = fe;
            } else {
                std::mem::swap(&mut points[n], reflected);
                costs[n] = fr;
            }
        } else {
            let outside = fr < costs[n];
            affine(
                alternate,
                center,
                if outside { reflected } else { &points[n] },
                half,
            );
            let fc = problem.cost(alternate)?;
            if (outside && fc <= fr) || (!outside && fc < costs[n]) {
                std::mem::swap(&mut points[n], alternate);
                costs[n] = fc;
            } else {
                shrink = true;
            }
        }
        if shrink {
            let (best, others) = points.split_at_mut(1);
            for (point, cost) in others.iter_mut().zip(&mut costs[1..]) {
                point.scale_in_place(half);
                point.scaled_add(half, &best[0]);
                *cost = problem.cost(point)?;
            }
        }
        state.replace_members(points, costs).unwrap();
        Ok(basin::SolverStep::from((state, None)))
    }
}

fn affine<V: ScaleInPlace<F> + ScaledAdd<F>, F: Scalar>(
    out: &mut V,
    a: &V,
    b: &V,
    t: F,
) {
    out.scale_in_place(F::zero());
    out.scaled_add(F::one() - t, a);
    out.scaled_add(t, b);
}

/// Metropolis annealing using Basin's neighbor and temperature interfaces.
///
/// # Backends
///
/// Exercised by this prototype: Vec, nalgebra, ndarray, and faer.
pub struct Annealing<N, F: Scalar = f64> {
    neighbor: N,
    make_neighbor: fn() -> N,
    rng: ChaCha8Rng,
    seed: u64,
    age: u64,
    initial_temperature: F,
    schedule: TemperatureSchedule<F>,
}

impl<N, F: Scalar> Annealing<N, F> {
    pub fn new(
        make_neighbor: fn() -> N,
        initial_temperature: F,
        schedule: TemperatureSchedule<F>,
        seed: u64,
    ) -> Self {
        assert!(
            initial_temperature.is_finite() && initial_temperature > F::zero()
        );
        Self {
            neighbor: make_neighbor(),
            make_neighbor,
            rng: ChaCha8Rng::seed_from_u64(seed),
            seed,
            age: 0,
            initial_temperature,
            schedule,
        }
    }

    pub fn seed_chain(&self, seed: u64) -> Self {
        Self::new(
            self.make_neighbor,
            self.initial_temperature,
            self.schedule,
            seed,
        )
    }

    pub fn temperature(&self) -> F {
        self.schedule
            .temperature(self.initial_temperature, self.age)
    }
    pub fn neighbor(&self) -> &N {
        &self.neighbor
    }
}

impl<P, V, N, F> Solver<P, PointState<V, F>> for Annealing<N, F>
where
    F: Scalar,
    V: Clone + VectorLen,
    P: CostFunction<Param = V, Output = F>,
    N: Neighbor<V, F, ChaCha8Rng, Error = P::Error>,
{
    type Error = P::Error;

    fn init(
        &mut self,
        problem: &mut Problem<P>,
        mut state: PointState<V, F>,
    ) -> Result<PointState<V, F>, P::Error> {
        self.neighbor = (self.make_neighbor)();
        self.rng = ChaCha8Rng::seed_from_u64(self.seed);
        self.age = 0;
        let cost = problem.cost(state.param())?;
        state.replace(state.param().clone(), cost);
        Ok(state)
    }

    fn next_iter(
        &mut self,
        problem: &mut Problem<P>,
        mut state: PointState<V, F>,
    ) -> Result<basin::SolverStep<PointState<V, F>>, P::Error> {
        let temperature = self.temperature();
        let candidate =
            self.neighbor
                .propose(state.param(), temperature, &mut self.rng)?;
        let cost = problem.cost(&candidate)?;
        if !cost.is_nan() && cost != F::infinity() {
            let accept = cost <= state.cost()
                || F::from_f64(self.rng.random::<f64>()).unwrap()
                    < (-(cost - state.cost()) / temperature).exp();
            if accept {
                state.replace(candidate, cost);
            }
        }
        self.age += 1;
        Ok(basin::SolverStep::from((state, None)))
    }
}