basin 1.13.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
//! Shared progress storage for external and new solvers.
//!
//! [`PointState`] carries a point and its cost. [`FirstOrderState`] also
//! carries its gradient. Solvers publish complete records with `replace`;
//! the executor mirrors counts, advances completed iterations, and retains
//! strict objective improvements. Algorithm settings, models, history, RNGs,
//! and scratch buffers belong in the solver.
//!
//! These are additive state types. Existing solvers keep their existing
//! state types and constructors. Both shared states preserve all six raw
//! [`EvalCounts`] categories through their `counts` and `best_counts` readers.
//! Their [`State`] and [`GradientState`] readers use the existing Basin 1.x
//! folded accounting described by [`CountsMirror`].
//!
//! Both states implement [`EvaluatedState`], [`RawEvaluationState`],
//! [`IncumbentState`], and [`ObjectiveIncumbentState`]; [`FirstOrderState`]
//! additionally implements [`EvaluatedGradientState`]. These unsealed traits
//! also support external states. Use `require_evaluated_state` to validate
//! publication, `max_evaluations` for raw budgets, and `target_objective` or
//! `no_objective_improvement` for controls requiring objective ordering.
//!
//! # Lifecycle
//!
//! Construction supplies an unevaluated seed. In [`Solver::init`](crate::Solver::init),
//! call the state's `reset` method, reset solver-owned machinery, evaluate
//! the seed through [`Problem`](crate::Problem), and `replace` the record.
//! Each successful iteration must likewise leave a complete record. `reset`
//! retains the current parameter as a warm-start seed while clearing derived
//! values and per-run bookkeeping. [`State::reset_best`] clears only the
//! incumbent, as it does for the existing state types.
//!
//! Exact continuation uses a solver-aware [`ExactCheckpoint`](crate::ExactCheckpoint)
//! and [`Executor::resume_from_checkpoint`](crate::Executor::resume_from_checkpoint),
//! which skips initialization. Shared progress alone does not contain the
//! solver's evolution data, so these types do not implement
//! [`ExactResumeState`](super::ExactResumeState).
//!
//! # Example
//!
//! An external solver needs no custom state implementation or counters:
//!
//! ```
//! use basin::{
//!     CostFunction, Executor, FirstOrderState, Gradient, Problem, Solver,
//!     State, TerminationReason,
//! };
//! use std::convert::Infallible;
//!
//! struct Quadratic;
//! impl CostFunction for Quadratic {
//!     type Param = Vec<f64>;
//!     type Output = f64;
//!     type Error = Infallible;
//!     fn cost(&self, x: &Vec<f64>) -> Result<f64, Infallible> {
//!         Ok(x.iter().map(|v| v * v).sum())
//!     }
//! }
//! impl Gradient for Quadratic {
//!     type Gradient = Vec<f64>;
//!     fn gradient(&self, x: &Vec<f64>) -> Result<Vec<f64>, Infallible> {
//!         Ok(x.iter().map(|v| 2.0 * v).collect())
//!     }
//! }
//!
//! struct Descent;
//! impl Solver<Quadratic, FirstOrderState<Vec<f64>>> for Descent {
//!     type Error = Infallible;
//!     fn init(
//!         &mut self,
//!         problem: &mut Problem<Quadratic>,
//!         mut state: FirstOrderState<Vec<f64>>,
//!     ) -> Result<FirstOrderState<Vec<f64>>, Infallible> {
//!         state.reset();
//!         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<Quadratic>,
//!         mut state: FirstOrderState<Vec<f64>>,
//!     ) -> Result<(FirstOrderState<Vec<f64>>, Option<TerminationReason>), Infallible> {
//!         let (x, _, gradient) = state.current().unwrap();
//!         let next = x.iter().zip(gradient).map(|(x, g)| x - 0.25 * g).collect();
//!         let (cost, gradient) = problem.cost_and_gradient(&next)?;
//!         state.replace(next, cost, gradient).unwrap();
//!         Ok((state, None))
//!     }
//! }
//! let result = Executor::new(
//!     Quadratic, Descent, FirstOrderState::new(vec![2.0]),
//! ).require_evaluated_state()
//!     .max_evaluations(basin::EvaluationKind::Gradient, 100)
//!     .target_objective(0.01)
//!     .max_iter(3).run().unwrap();
//! assert_eq!(result.state.cost(), 0.0625);
//! assert_eq!(result.state.counts().gradient_evals, 4);
//! ```

use super::{
    CountsMirror, EvaluatedGradientState, EvaluatedState, GradientState,
    IncumbentRef, IncumbentState, ObjectiveIncumbentState, RawEvaluationState,
    State,
};
use crate::core::math::{Scalar, VectorLen};
use crate::core::problem::EvalCounts;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
struct Incumbent<V, F: Scalar> {
    param: V,
    cost: F,
    iter: u64,
    counts: EvalCounts,
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
struct Progress<V, D, F: Scalar> {
    param: V,
    // Cost and derivatives become available together, including after reset.
    evaluated: Option<(F, D)>,
    best: Option<Incumbent<V, F>>,
    iter: u64,
    counts: EvalCounts,
}

impl<V, D, F: Scalar> Progress<V, D, F> {
    fn new(param: V) -> Self {
        Self {
            param,
            evaluated: None,
            best: None,
            iter: 0,
            counts: EvalCounts::default(),
        }
    }

    fn reset(&mut self) {
        self.evaluated = None;
        self.best = None;
        self.iter = 0;
        self.counts = EvalCounts::default();
    }

    fn replace(&mut self, param: V, cost: F, derivatives: D) {
        self.param = param;
        self.evaluated = Some((cost, derivatives));
    }
}

impl<V: Clone, D, F: Scalar> Progress<V, D, F> {
    fn update_best(&mut self) {
        let Some((cost, _)) = self.evaluated.as_ref() else {
            return;
        };
        if !cost.is_nan()
            && *cost != F::infinity()
            && self.best.as_ref().is_none_or(|best| *cost < best.cost)
        {
            self.best = Some(Incumbent {
                param: self.param.clone(),
                cost: *cost,
                iter: self.iter,
                counts: self.counts,
            });
        }
    }
}

/// Shared single-point progress for derivative-free and external solvers.
///
/// [`replace`](Self::replace) publishes a matching point and cost. The
/// executor retains the lowest-cost published record separately, so a worse
/// current point cannot overwrite the historical best. Equal costs preserve
/// the incumbent and its publication metadata. NaN and positive infinity
/// never establish an incumbent. Negative infinity can be retained but does
/// not itself signal an unbounded problem.
///
/// Before evaluation, [`current`](Self::current) and [`best`](Self::best)
/// return `None`. [`State::cost`] panics without a current record, and
/// [`State::best_param`] panics without an incumbent, including when all
/// published costs are NaN or positive infinity. [`State::best_cost`] returns
/// positive infinity in that case, and best iteration/evaluation readers
/// return zero. These objective-only semantics are unsuitable for solvers
/// whose incumbent selection prioritizes constraint feasibility.
///
/// See the [module documentation](self) for initialization and continuation.
/// With `serde`, serialization is available when `V` and `F` support it.
///
/// # Backends
///
/// `Vec<F>`, `nalgebra::DVector<F>`, `ndarray::Array1<F>`, and `faer::Col<F>`,
/// with `F = f64` (default) or `f32`. No vector math capability is required:
/// scalar parameters and other `Clone` parameter types also implement [`State`].
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct PointState<V, F: Scalar = f64> {
    progress: Progress<V, (), F>,
}

impl<V, F: Scalar> PointState<V, F> {
    /// Read the evaluated current point and cost, or `None` for a seed.
    pub fn current(&self) -> Option<(&V, F)> {
        let (cost, ()) = self.progress.evaluated.as_ref()?;
        Some((&self.progress.param, *cost))
    }

    /// Replace the current point and cost together without changing counters
    /// or the incumbent. The executor considers the final record returned
    /// from initialization or an iteration, including a clean mid-step stop.
    /// Intermediate replacements within a step are not retained automatically.
    pub fn replace(&mut self, param: V, cost: F) {
        self.progress.replace(param, cost, ());
    }
}

/// Shared first-order progress with a matching point, cost, and gradient.
///
/// [`replace`](Self::replace) requires the point and gradient together and
/// checks their lengths before changing any field. [`GradientState::gradient`]
/// is therefore available whenever [`current`](Self::current) is available.
/// The gradient belongs to the current point; historical incumbents retain
/// only their point, cost, and publication metadata.
///
/// Incumbent selection, unavailable readers, lifecycle, and optional serde
/// support follow [`PointState`]. This state stores progress while the solver
/// owns algorithm machinery such as an inverse Hessian or L-BFGS history.
///
/// # Backends
///
/// `Vec<F>`, `nalgebra::DVector<F>`, `ndarray::Array1<F>`, and `faer::Col<F>`,
/// with `F = f64` (default) or `f32`. Replacement requires [`VectorLen`];
/// [`State`] and [`GradientState`] require only `V: Clone`.
///
/// A point-only state does not advertise a gradient capability:
///
/// ```compile_fail
/// use basin::{GradientState, PointState};
/// fn requires_gradient<S: GradientState>(_: S) {}
/// requires_gradient(PointState::<Vec<f64>>::new(vec![1.0]));
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct FirstOrderState<V, F: Scalar = f64> {
    progress: Progress<V, V, F>,
}

impl<V, F: Scalar> FirstOrderState<V, F> {
    /// Read the evaluated current point, cost, and gradient, or `None` for a seed.
    pub fn current(&self) -> Option<(&V, F, &V)> {
        let (cost, gradient) = self.progress.evaluated.as_ref()?;
        Some((&self.progress.param, *cost, gradient))
    }
}

impl<V: VectorLen, F: Scalar> FirstOrderState<V, F> {
    /// Replace the current point, cost, and gradient as one record.
    ///
    /// The point and gradient must have equal lengths; on error the entire
    /// previous state is retained. The new dimension may differ from the
    /// previous record's dimension. Numerical validity and whether a point
    /// was actually evaluated remain the solver's responsibility.
    ///
    /// Counters and the incumbent are unchanged until the executor publishes
    /// the returned state, as with [`PointState::replace`].
    pub fn replace(
        &mut self,
        param: V,
        cost: F,
        gradient: V,
    ) -> Result<(), GradientDimensionMismatch> {
        let param_len = param.vec_len();
        let gradient_len = gradient.vec_len();
        if param_len != gradient_len {
            return Err(GradientDimensionMismatch {
                param_len,
                gradient_len,
            });
        }
        self.progress.replace(param, cost, gradient);
        Ok(())
    }
}

/// A point and gradient passed to [`FirstOrderState::replace`] differ in length.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GradientDimensionMismatch {
    /// Number of coordinates in the proposed point.
    pub param_len: usize,
    /// Number of coordinates in the proposed gradient.
    pub gradient_len: usize,
}

impl std::fmt::Display for GradientDimensionMismatch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "point length {} does not match gradient length {}",
            self.param_len, self.gradient_len,
        )
    }
}

impl std::error::Error for GradientDimensionMismatch {}

fn cost_work(counts: &EvalCounts) -> u64 {
    counts.cost_evals + counts.residual_evals
}

fn gradient_work(counts: &EvalCounts) -> u64 {
    counts.gradient_evals
        + counts.jacobian_evals
        + counts.hessian_evals
        + counts.hessian_product_evals
}

macro_rules! impl_progress_state {
    ($state:ident, $cost_work:path) => {
        impl<V, F: Scalar> $state<V, F> {
            /// Construct an unevaluated seed with zero counters and no incumbent.
            pub fn new(param: V) -> Self {
                Self {
                    progress: Progress::new(param),
                }
            }

            /// Retain the current parameter as a seed and clear all evaluated
            /// data, the incumbent, iteration number, and evaluation counts.
            /// Call this during fresh solver initialization, then reevaluate
            /// the seed. Exact checkpoint resumes skip initialization.
            pub fn reset(&mut self) {
                self.progress.reset();
            }

            /// Read the retained incumbent point and cost, if one exists.
            pub fn best(&self) -> Option<(&V, F)> {
                self.progress
                    .best
                    .as_ref()
                    .map(|best| (&best.param, best.cost))
            }

            /// Raw counts mirrored from the problem at the latest publication
            /// boundary. Fresh and nested runs report per-run work; exact
            /// checkpoint resumes report cumulative work across the continuation.
            pub fn counts(&self) -> &EvalCounts {
                &self.progress.counts
            }

            /// Raw counts at the boundary that selected the incumbent, if any.
            /// These include all work charged before publication, which can
            /// exceed the work at the evaluation that found the point.
            pub fn best_counts(&self) -> Option<&EvalCounts> {
                self.progress.best.as_ref().map(|best| &best.counts)
            }
        }

        impl<V: Clone, F: Scalar> State for $state<V, F> {
            type Param = V;
            type Float = F;

            fn iter(&self) -> u64 {
                self.progress.iter
            }
            fn increment_iter(&mut self) {
                self.progress.iter += 1;
            }
            fn cost_evals(&self) -> u64 {
                $cost_work(&self.progress.counts)
            }
            fn param(&self) -> &V {
                &self.progress.param
            }
            fn cost(&self) -> F {
                self.progress
                    .evaluated
                    .as_ref()
                    .expect("current point has not been evaluated")
                    .0
            }
            fn best_param(&self) -> &V {
                &self
                    .progress
                    .best
                    .as_ref()
                    .expect("no incumbent has been selected")
                    .param
            }
            fn best_cost(&self) -> F {
                self.progress
                    .best
                    .as_ref()
                    .map_or(F::infinity(), |best| best.cost)
            }
            fn best_iter(&self) -> u64 {
                self.progress.best.as_ref().map_or(0, |best| best.iter)
            }
            fn best_cost_evals(&self) -> u64 {
                self.progress
                    .best
                    .as_ref()
                    .map_or(0, |best| $cost_work(&best.counts))
            }
            fn update_best(&mut self) {
                self.progress.update_best();
            }
            fn reset_best(&mut self) {
                self.progress.best = None;
            }
        }

        impl<V: Clone, F: Scalar> CountsMirror for $state<V, F> {
            fn mirror(&mut self, counts: &EvalCounts) {
                self.progress.counts = *counts;
            }
        }

        impl<V: Clone, F: Scalar> RawEvaluationState for $state<V, F> {
            fn raw_counts(&self) -> &EvalCounts {
                self.counts()
            }
        }

        impl<V: Clone, F: Scalar> IncumbentState for $state<V, F> {
            fn incumbent_record(&self) -> Option<IncumbentRef<'_, V, F>> {
                let best = self.progress.best.as_ref()?;
                Some(IncumbentRef {
                    param: &best.param,
                    cost: best.cost,
                    iter: best.iter,
                    counts: &best.counts,
                })
            }
        }

        impl<V: Clone, F: Scalar> ObjectiveIncumbentState for $state<V, F> {}
    };
}

impl_progress_state!(PointState, EvalCounts::total_work);
impl_progress_state!(FirstOrderState, cost_work);

impl<V: Clone, F: Scalar> EvaluatedState for PointState<V, F> {
    fn current_record(&self) -> Option<(&V, F)> {
        self.current()
    }
}

impl<V: Clone, F: Scalar> EvaluatedState for FirstOrderState<V, F> {
    fn current_record(&self) -> Option<(&V, F)> {
        self.current().map(|(param, cost, _)| (param, cost))
    }
}

impl<V: Clone, F: Scalar> EvaluatedGradientState for FirstOrderState<V, F> {
    fn current_gradient_record(&self) -> Option<(&V, F, &V)> {
        self.current()
    }
}

impl<V: Clone, F: Scalar> GradientState for FirstOrderState<V, F> {
    fn gradient(&self) -> Option<&V> {
        self.progress
            .evaluated
            .as_ref()
            .map(|(_, gradient)| gradient)
    }

    fn gradient_evals(&self) -> u64 {
        gradient_work(&self.progress.counts)
    }

    fn best_gradient_evals(&self) -> u64 {
        self.best_counts().map_or(0, gradient_work)
    }
}