basin 2.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
//! Shared population records, independent of distribution and per-member models.

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

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
enum Current<V, F> {
    Seed(V),
    Member(usize),
    Representative(V, F),
}

/// Observable population members, matching costs, and historical progress.
///
/// Members live directly in this state; solvers own distributions, per-member
/// models, RNGs, and working buffers. [`replace`](Self::replace) validates the
/// entire replacement before changing anything and preserves member order so
/// solver-owned information indexed by member stays aligned. The current
/// record is the lowest-cost member unless [`publish_representative`](Self::publish_representative)
/// supplies another evaluated point, such as a distribution mean or a swarm's
/// global best. NaNs rank after every other cost;
/// equal costs retain the first member.
///
/// The executor considers both members and the representative for strict objective
/// improvements at publication boundaries. It retains a matching historical
/// point, cost, iteration, and all six evaluation counts even when the entire
/// population changes. NaN and positive infinity never establish an incumbent;
/// negative infinity can be retained without establishing unboundedness.
///
/// [`empty`](Self::empty) lets the solver generate its initial population.
/// [`from_population`](Self::from_population) supplies explicit unevaluated
/// members; [`from_point`](Self::from_point) supplies a representative seed
/// for distribution-based solvers. Construction supplies no current cost or incumbent. Use
/// [`current`](Self::current), [`best`](Self::best), and
/// [`evaluated_members`](Self::evaluated_members) to check availability.
/// Population size and algorithm settings belong to the solver.
///
/// Fresh [`reset`](Self::reset) preserves members and any representative as
/// unevaluated seeds and clears all
/// evaluated records and bookkeeping. Exact continuation requires a checkpoint
/// containing the solver, state, and counts. With `serde`, this state serializes
/// when its parameter and scalar types do; it contains no solver machinery.
///
/// # Backends
///
/// `Vec<F>`, nalgebra `DVector<F>`, ndarray `Array1<F>`, and faer `Col<F>`, with
/// `F = f32` or `f64`. Construction from members and replacement require
/// [`VectorLen`]; observation and bookkeeping require only `Clone`.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PopulationProgress<V, F: Scalar = f64> {
    pub(crate) candidates: Vec<V>,
    pub(crate) costs: Vec<F>,
    current: Option<Current<V, F>>,
    iter: u64,
    counts: EvalCounts,
    best: Option<Incumbent<V, F>>,
}

/// A proposed population or representative has inconsistent dimensions or availability.
///
/// New variants may be added in future releases. Include a fallback arm when
/// matching errors; existing variants remain directly constructible.
/// Exhaustive matching outside Basin is unsupported:
///
/// ```compile_fail
/// use basin::PopulationShapeError;
///
/// fn handle(error: PopulationShapeError) {
///     match error {
///         PopulationShapeError::EmptyPopulation
///         | PopulationShapeError::EmptyParameter
///         | PopulationShapeError::DimensionMismatch { .. }
///         | PopulationShapeError::CostCountMismatch { .. }
///         | PopulationShapeError::UnevaluatedPopulation => {}
///     }
/// }
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PopulationShapeError {
    /// An evaluated population requires at least one member.
    EmptyPopulation,
    /// Members must have at least one coordinate.
    EmptyParameter,
    /// All members and the representative must have the same coordinate count.
    DimensionMismatch {
        /// Member index, or the population length for an inconsistent representative.
        index: usize,
        /// Coordinate count of the first member.
        expected: usize,
        /// Coordinate count of the proposed member or representative.
        actual: usize,
    },
    /// Each member requires one cost.
    CostCountMismatch {
        /// Number of supplied members.
        members: usize,
        /// Number of supplied costs.
        costs: usize,
    },
    /// A representative can only accompany an evaluated population.
    UnevaluatedPopulation,
}
impl std::fmt::Display for PopulationShapeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyPopulation => {
                write!(f, "population requires at least one member")
            }
            Self::EmptyParameter => {
                write!(f, "population members require at least one coordinate")
            }
            Self::DimensionMismatch {
                index,
                expected,
                actual,
            } => write!(
                f,
                "population member or representative {index} has length {actual}, expected {expected}"
            ),
            Self::CostCountMismatch { members, costs } => write!(
                f,
                "{members} population members require {members} costs, got {costs}"
            ),
            Self::UnevaluatedPopulation => {
                write!(f, "population has not been evaluated")
            }
        }
    }
}
impl std::error::Error for PopulationShapeError {}

impl<V: VectorLen, F: Scalar> PopulationProgress<V, F> {
    fn validate(members: &[V]) -> Result<(), PopulationShapeError> {
        let dimension = members
            .first()
            .ok_or(PopulationShapeError::EmptyPopulation)?
            .vec_len();
        if dimension == 0 {
            return Err(PopulationShapeError::EmptyParameter);
        }
        for (index, member) in members.iter().enumerate().skip(1) {
            if member.vec_len() != dimension {
                return Err(PopulationShapeError::DimensionMismatch {
                    index,
                    expected: dimension,
                    actual: member.vec_len(),
                });
            }
        }
        Ok(())
    }

    /// Supply an unevaluated representative seed, such as a CMA distribution mean.
    ///
    /// # Panics
    ///
    /// Panics if the point is empty.
    pub fn from_point(point: V) -> Self {
        assert!(
            point.vec_len() > 0,
            "population seed requires a nonempty point"
        );
        Self {
            current: Some(Current::Seed(point)),
            ..Self::empty()
        }
    }

    /// Supply explicit unevaluated members for the solver's initialization.
    ///
    /// # Panics
    ///
    /// Panics for an empty population or unequal or zero coordinate lengths.
    pub fn from_population(candidates: Vec<V>) -> Self {
        Self::validate(&candidates).expect("invalid initial population");
        Self {
            candidates,
            ..Self::empty()
        }
    }

    /// Replace every member and cost together, retaining member order.
    /// The lowest-cost member becomes current, replacing any published representative.
    /// Errors preserve the entire old state. Valid replacements may change
    /// population size or parameter dimension. Counters and the incumbent
    /// remain unchanged until the executor publishes this record.
    pub fn replace(
        &mut self,
        candidates: Vec<V>,
        costs: Vec<F>,
    ) -> Result<(), PopulationShapeError> {
        Self::validate(&candidates)?;
        if candidates.len() != costs.len() {
            return Err(PopulationShapeError::CostCountMismatch {
                members: candidates.len(),
                costs: costs.len(),
            });
        }
        self.candidates = candidates;
        self.costs = costs;
        self.select_best_member();
        Ok(())
    }

    /// Publish an evaluated representative as the current record without
    /// changing any member. Its dimension must match the evaluated population.
    /// The executor still considers both the representative and members for the incumbent.
    pub fn publish_representative(
        &mut self,
        point: V,
        cost: F,
    ) -> Result<(), PopulationShapeError> {
        self.validate_representative(&point)?;
        self.current = Some(Current::Representative(point, cost));
        Ok(())
    }

    fn validate_representative(
        &self,
        point: &V,
    ) -> Result<(), PopulationShapeError> {
        let (members, _) = self
            .evaluated_members()
            .ok_or(PopulationShapeError::UnevaluatedPopulation)?;
        let expected = members[0].vec_len();
        if point.vec_len() != expected {
            return Err(PopulationShapeError::DimensionMismatch {
                index: members.len(),
                expected,
                actual: point.vec_len(),
            });
        }
        Ok(())
    }

    pub(crate) fn publish_representative_from(
        &mut self,
        point: &V,
        cost: F,
    ) -> Result<(), PopulationShapeError>
    where
        V: Clone,
    {
        self.validate_representative(point)?;
        // Reuse the observable record when the solver retains the underlying model point.
        if let Some(Current::Representative(current, current_cost)) =
            &mut self.current
        {
            current.clone_from(point);
            *current_cost = cost;
        } else {
            self.current = Some(Current::Representative(point.clone(), cost));
        }
        Ok(())
    }
}

impl<V, F: Scalar> PopulationProgress<V, F> {
    /// An unevaluated container whose initial members will be supplied by the solver.
    pub fn empty() -> Self {
        Self {
            candidates: Vec::new(),
            costs: Vec::new(),
            current: None,
            iter: 0,
            counts: EvalCounts::default(),
            best: None,
        }
    }
    /// Borrow authoritative members in their stored order, including unevaluated seeds.
    pub fn candidates(&self) -> &[V] {
        &self.candidates
    }
    /// Borrow matching costs, or an empty slice before evaluation.
    pub fn costs(&self) -> &[F] {
        &self.costs
    }
    /// Borrow complete member/cost arrays, or `None` before evaluation.
    pub fn evaluated_members(&self) -> Option<(&[V], &[F])> {
        (!self.candidates.is_empty()
            && self.candidates.len() == self.costs.len())
        .then_some((&self.candidates, &self.costs))
    }
    /// Borrow the evaluated representative, or `None` before evaluation.
    pub fn current(&self) -> Option<(&V, F)> {
        match self.current.as_ref()? {
            Current::Seed(_) => None,
            Current::Member(index) => {
                Some((&self.candidates[*index], self.costs[*index]))
            }
            Current::Representative(point, cost) => Some((point, *cost)),
        }
    }
    /// Borrow the historical objective incumbent, if one exists.
    pub fn best(&self) -> Option<(&V, F)> {
        self.best.as_ref().map(|best| (&best.param, best.cost))
    }
    /// All raw evaluation categories at the latest publication boundary.
    pub fn counts(&self) -> &EvalCounts {
        &self.counts
    }
    /// All raw evaluation categories at the incumbent's selection boundary.
    pub fn best_counts(&self) -> Option<&EvalCounts> {
        self.best.as_ref().map(|best| &best.counts)
    }
    /// Preserve members and the representative as unevaluated seeds, clearing all
    /// evaluated progress and bookkeeping.
    pub fn reset(&mut self) {
        self.costs.clear();
        self.current = match self.current.take() {
            Some(Current::Seed(point) | Current::Representative(point, _)) => {
                Some(Current::Seed(point))
            }
            _ => None,
        };
        self.iter = 0;
        self.counts = EvalCounts::default();
        self.best = None;
    }
    /// Move member storage out for in-place solver work, retaining historical
    /// bookkeeping. Restore a complete record with `replace` before publication.
    /// Current and evaluated-member readers return `None` in the meantime.
    pub fn take_members(&mut self) -> (Vec<V>, Vec<F>) {
        self.current = None;
        (
            std::mem::take(&mut self.candidates),
            std::mem::take(&mut self.costs),
        )
    }
    pub(crate) fn reset_segment_iter(&mut self) {
        self.iter = 0;
    }
    pub(crate) fn seed(&self) -> Option<&V> {
        match self.current.as_ref() {
            Some(Current::Seed(point) | Current::Representative(point, _)) => {
                Some(point)
            }
            Some(Current::Member(index)) => self.candidates.get(*index),
            None => self.candidates.first(),
        }
    }
    pub(crate) fn select_best_member(&mut self) {
        debug_assert_eq!(self.candidates.len(), self.costs.len());
        self.current = best_index(&self.costs).map(Current::Member);
    }
}
impl<V, F: Scalar> Default for PopulationProgress<V, F> {
    fn default() -> Self {
        Self::empty()
    }
}

fn precedes<F: Scalar>(a: F, b: F) -> bool {
    !a.is_nan() && (b.is_nan() || a < b)
}
fn best_index<F: Scalar>(costs: &[F]) -> Option<usize> {
    (!costs.is_empty()).then(|| {
        (1..costs.len()).fold(0, |best, i| {
            if precedes(costs[i], costs[best]) {
                i
            } else {
                best
            }
        })
    })
}

impl<V: Clone, F: Scalar> State for PopulationProgress<V, F> {
    type Param = V;
    type Float = F;
    fn iter(&self) -> u64 {
        self.iter
    }
    fn increment_iter(&mut self) {
        self.iter += 1;
    }
    fn cost_evals(&self) -> u64 {
        self.counts.cost_evals
    }
    fn param(&self) -> &V {
        self.current()
            .map(|record| record.0)
            .or_else(|| self.seed())
            .expect("population has no members")
    }
    fn cost(&self) -> F {
        self.current().expect("population has not been evaluated").1
    }
    fn best_param(&self) -> &V {
        &self
            .best
            .as_ref()
            .expect("no incumbent has been selected")
            .param
    }
    fn best_cost(&self) -> F {
        self.best.as_ref().map_or(F::infinity(), |best| best.cost)
    }
    fn best_iter(&self) -> u64 {
        self.best.as_ref().map_or(0, |best| best.iter)
    }
    fn best_cost_evals(&self) -> u64 {
        self.best.as_ref().map_or(0, |best| best.counts.cost_evals)
    }
    fn update_best(&mut self) {
        let Some(current) = &self.current else {
            return;
        };
        let index = match current {
            Current::Seed(_) => return,
            Current::Member(index) => *index,
            Current::Representative(_, _) => best_index(&self.costs)
                .expect("representative requires evaluated members"),
        };
        let mut point = &self.candidates[index];
        let mut cost = self.costs[index];
        if let Current::Representative(representative, representative_cost) =
            current
        {
            if precedes(*representative_cost, cost) {
                point = representative;
                cost = *representative_cost;
            }
        }
        Incumbent::consider(
            &mut self.best,
            point,
            cost,
            self.iter,
            self.counts,
        );
    }
    fn reset_best(&mut self) {
        self.best = None;
    }
}
impl<V: Clone, F: Scalar> CountsMirror for PopulationProgress<V, F> {
    fn mirror(&mut self, counts: &EvalCounts) {
        self.counts = *counts;
    }
}
impl<V: Clone, F: Scalar> PopulationState for PopulationProgress<V, F> {
    fn candidates(&self) -> &[V] {
        &self.candidates
    }
    fn costs(&self) -> &[F] {
        &self.costs
    }
}
impl<V: Clone, F: Scalar> EvaluatedState for PopulationProgress<V, F> {
    fn current_record(&self) -> Option<(&V, F)> {
        self.current()
    }
}
impl<V: Clone, F: Scalar> RawEvaluationState for PopulationProgress<V, F> {
    fn raw_counts(&self) -> &EvalCounts {
        &self.counts
    }
}
impl<V: Clone, F: Scalar> IncumbentState for PopulationProgress<V, F> {
    fn incumbent_record(&self) -> Option<IncumbentRef<'_, V, F>> {
        self.best.as_ref().map(|best| IncumbentRef {
            param: &best.param,
            cost: best.cost,
            iter: best.iter,
            counts: &best.counts,
        })
    }
}
impl<V: Clone, F: Scalar> ObjectiveIncumbentState for PopulationProgress<V, F> {}