use crate::{Group, Particle, Unit};
use rand::Rng;
pub trait ParticleInitDependent {
type TIn: Unit;
type TOut: Unit;
fn init_pos<R: Rng>(&self, rng: &mut R, p: Self::TIn) -> Vec<Self::TOut>;
fn init_vel<R: Rng>(&self, rng: &mut R, p: Self::TIn) -> Vec<Self::TOut>;
fn init_dep<R: Rng>(&self, rng: &mut R, p: Self::TIn) -> Group<Self::TOut> {
let positions = self.init_pos(rng, p);
let velocities = self.init_vel(rng, p);
debug_assert_eq!(
positions.len(),
velocities.len(),
"init_pos and init_vel must produce equal-length vectors",
);
let mut group = Group::<Self::TOut>::with_capacity(positions.len());
for (pos, vel) in positions.into_iter().zip(velocities) {
group.push(Particle {
pos,
vel,
fit: f64::MIN,
best_pos: pos,
best_fit: f64::MIN,
});
}
group
}
}
pub trait ParticleInit {
type T: Unit;
fn init_pos<R: Rng>(&self, rng: &mut R) -> Vec<Self::T>;
fn init_vel<R: Rng>(&self, rng: &mut R) -> Vec<Self::T>;
fn init<R: Rng>(&self, rng: &mut R) -> Group<Self::T> {
let positions = self.init_pos(rng);
let velocities = self.init_vel(rng);
debug_assert_eq!(
positions.len(),
velocities.len(),
"init_pos and init_vel must produce equal-length vectors",
);
let mut group = Group::<Self::T>::with_capacity(positions.len());
for (pos, vel) in positions.into_iter().zip(velocities) {
group.push(Particle {
pos,
vel,
fit: f64::MIN,
best_pos: pos,
best_fit: f64::MIN,
});
}
group
}
}