swarmkit 0.1.0

Composable particle swarm optimization with nested searches
Documentation
//! [`Best<T>`] — position paired with its observed fitness.

/// A position together with its observed fitness.
///
/// Invariant: `best_fit` always corresponds to `best_pos`. A fresh `Best`
/// is seeded with `f64::MIN` so the first real evaluation replaces it.
#[derive(Copy, Clone, Debug)]
pub struct Best<T: Copy + Default> {
    /// Fitness at `best_pos`.
    pub best_fit: f64,
    /// Position whose fitness is `best_fit`.
    pub best_pos: T,
}

impl<T: Copy + Default> Best<T> {
    /// Seeded with `f64::MIN` so any real fitness replaces it.
    #[must_use]
    pub fn new() -> Self {
        Self {
            best_fit: f64::MIN,
            best_pos: T::default(),
        }
    }
}

impl<T: Copy + Default> Default for Best<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, TSource, TTarget> From<&'a Best<TSource>> for Best<TTarget>
where
    TSource: Copy + Default,
    TTarget: From<TSource> + Copy + Default,
{
    fn from(best: &'a Best<TSource>) -> Self {
        Self {
            best_fit: best.best_fit,
            best_pos: best.best_pos.into(),
        }
    }
}