diff --git a/src/components/mapping/mod.rs b/src/components/mapping/mod.rs index 550b180..699aa08 100644 --- a/src/components/mapping/mod.rs +++ b/src/components/mapping/mod.rs @@ -22,8 +22,8 @@ //! using [`ValueOf>>`] as input lens and //! [`ValueOf`] as output lens: //! -//! [`InertiaWeight`]: crate::components::swarm::InertiaWeight -//! [`ParticleVelocitiesUpdate`]: crate::components::swarm::ParticleVelocitiesUpdate +//! [`InertiaWeight`]: crate::components::swarm::pso::InertiaWeight +//! [`ParticleVelocitiesUpdate`]: crate::components::swarm::pso::ParticleVelocitiesUpdate //! [`ValueOf>>`]: crate::lens::ValueOf //! [`ValueOf`]: crate::lens::ValueOf //! diff --git a/src/components/replacement/bh.rs b/src/components/replacement/bh.rs index 2191886..132d5e2 100644 --- a/src/components/replacement/bh.rs +++ b/src/components/replacement/bh.rs @@ -12,20 +12,19 @@ pub struct EventHorizon; impl EventHorizon { pub fn new

() -> Box> - where - P: LimitedVectorProblem, - P: SingleObjectiveProblem, + where + P: LimitedVectorProblem, + P: SingleObjectiveProblem, { Box::new(Self) } } impl

Component

for EventHorizon - where - P: LimitedVectorProblem, - P: SingleObjectiveProblem, +where + P: LimitedVectorProblem, + P: SingleObjectiveProblem, { - fn execute(&self, problem: &P, state: &mut State

) -> ExecResult<()> { let mut populations = state.populations_mut(); let offspring = populations.pop(); diff --git a/src/components/swarm/bh.rs b/src/components/swarm/bh.rs index 2cb59fe..606238f 100644 --- a/src/components/swarm/bh.rs +++ b/src/components/swarm/bh.rs @@ -1,13 +1,15 @@ -use rand::distributions::{Distribution, Uniform}; -use serde::Serialize; use crate::population::{AsSolutionsMut, BestIndividual}; use crate::{ component::ExecResult, + components, components::Component, + heuristics, identifier::{Global, Identifier, PhantomId}, - problems::{LimitedVectorProblem}, - SingleObjectiveProblem, State + problems::LimitedVectorProblem, + SingleObjectiveProblem, State, }; +use rand::distributions::{Distribution, Uniform}; +use serde::Serialize; /// Updates the positions in the black hole algorithm. /// @@ -20,6 +22,8 @@ use crate::{ /// v_max = 1 /// /// [`bh`]: crate::heuristics::bh +/// [`ParticleVelocitiesUpdate`]: components::swarm::pso::ParticleVelocitiesUpdate` +/// [`pso`]: heuristics::pso #[derive(Clone, Serialize)] pub struct BlackHoleParticlesUpdate { id: PhantomId, @@ -33,8 +37,8 @@ impl BlackHoleParticlesUpdate { } pub fn new_with_id

() -> Box> - where - P: SingleObjectiveProblem + LimitedVectorProblem, + where + P: SingleObjectiveProblem + LimitedVectorProblem, { Box::new(Self::from_params()) } @@ -42,17 +46,17 @@ impl BlackHoleParticlesUpdate { impl BlackHoleParticlesUpdate { pub fn new

() -> Box> - where - P: SingleObjectiveProblem + LimitedVectorProblem, + where + P: SingleObjectiveProblem + LimitedVectorProblem, { Self::new_with_id() } } impl Component

for BlackHoleParticlesUpdate - where - P: SingleObjectiveProblem + LimitedVectorProblem, - I: Identifier, +where + P: SingleObjectiveProblem + LimitedVectorProblem, + I: Identifier, { fn init(&self, _problem: &P, _state: &mut State

) -> ExecResult<()> { Ok(()) diff --git a/src/components/swarm/fa.rs b/src/components/swarm/fa.rs index 260ed71..56f80f6 100644 --- a/src/components/swarm/fa.rs +++ b/src/components/swarm/fa.rs @@ -12,8 +12,9 @@ use crate::{ component::ExecResult, components::Component, identifier::{Global, Identifier, PhantomId}, - problems::{LimitedVectorProblem}, - CustomState, State}; + problems::LimitedVectorProblem, + CustomState, State, +}; /// Updates the and firefly positions. /// @@ -40,13 +41,9 @@ impl FireflyPositionsUpdate { } } - pub fn new_with_id

( - alpha: f64, - beta: f64, - gamma: f64, - ) -> Box> - where - P: LimitedVectorProblem, + pub fn new_with_id

(alpha: f64, beta: f64, gamma: f64) -> Box> + where + P: LimitedVectorProblem, { Box::new(Self::from_params(alpha, beta, gamma)) } @@ -54,17 +51,17 @@ impl FireflyPositionsUpdate { impl FireflyPositionsUpdate { pub fn new

(alpha: f64, beta: f64, gamma: f64) -> Box> - where - P: LimitedVectorProblem, + where + P: LimitedVectorProblem, { Self::new_with_id(alpha, beta, gamma) } } impl Component

for FireflyPositionsUpdate - where - P: LimitedVectorProblem, - I: Identifier, +where + P: LimitedVectorProblem, + I: Identifier, { fn init(&self, _problem: &P, state: &mut State

) -> ExecResult<()> { state.insert(RandomizationParameter(self.alpha)); @@ -97,24 +94,27 @@ impl Component

for FireflyPositionsUpdate .map(|_| state.random_mut().gen_range(0.0..1.0)) .collect(); let mut current = individuals[i].clone(); - izip!(current.solution_mut(), individuals[j].solution(), &scales, rands) - .map(|(xi, xj, scale, rand)| { - let pos = beta * (-gamma * (*xi - xj).powf(2.0)).exp() * (xj - *xi) - + a * (rand - 0.5) * scale; - (xi, pos) - }) - .for_each(|(xi, pos)| *xi += pos); + izip!( + current.solution_mut(), + individuals[j].solution(), + &scales, + rands + ) + .map(|(xi, xj, scale, rand)| { + let pos = beta * (-gamma * (*xi - xj).powf(2.0)).exp() * (xj - *xi) + + a * (rand - 0.5) * scale; + (xi, pos) + }) + .for_each(|(xi, pos)| *xi += pos); individuals[i] = current; state.holding::>( |evaluator: &mut Evaluator, state| { - evaluator - .as_inner_mut() - .evaluate( - problem, - state, - from_mut(&mut individuals[i]) - ); + evaluator.as_inner_mut().evaluate( + problem, + state, + from_mut(&mut individuals[i]), + ); Ok(()) }, )?; @@ -135,4 +135,4 @@ pub struct RandomizationParameter( pub f64, ); -impl CustomState<'_> for RandomizationParameter {} \ No newline at end of file +impl CustomState<'_> for RandomizationParameter {} diff --git a/src/components/swarm/pso.rs b/src/components/swarm/pso.rs index 14b870d..6e62af4 100644 --- a/src/components/swarm/pso.rs +++ b/src/components/swarm/pso.rs @@ -55,17 +55,17 @@ impl ParticleVelocitiesInit { } pub fn new

(v_max: f64) -> ExecResult>> - where - P: LimitedVectorProblem, + where + P: LimitedVectorProblem, { Ok(Box::new(Self::from_params(v_max)?)) } } impl Component

for ParticleVelocitiesInit - where - P: LimitedVectorProblem, - I: Identifier, +where + P: LimitedVectorProblem, + I: Identifier, { fn init(&self, _problem: &P, state: &mut State

) -> ExecResult<()> { state.insert(ParticleVelocities::::new(Vec::new())); @@ -78,8 +78,8 @@ impl Component

for ParticleVelocitiesInit .take(problem.dimension()) .collect::>() }) - .take(state.populations().current().len()) - .collect::>(); + .take(state.populations().current().len()) + .collect::>(); state.set_value::>(velocities); @@ -144,8 +144,8 @@ impl ParticleVelocitiesUpdate { c_2: f64, v_max: f64, ) -> ExecResult>> - where - P: LimitedVectorProblem, + where + P: LimitedVectorProblem, { Ok(Box::new(Self::from_params(weight, c_1, c_2, v_max)?)) } @@ -153,17 +153,17 @@ impl ParticleVelocitiesUpdate { impl ParticleVelocitiesUpdate { pub fn new

(weight: f64, c_1: f64, c_2: f64, v_max: f64) -> ExecResult>> - where - P: LimitedVectorProblem, + where + P: LimitedVectorProblem, { Self::new_with_id(weight, c_1, c_2, v_max) } } impl Component

for ParticleVelocitiesUpdate - where - P: LimitedVectorProblem, - I: Identifier, +where + P: LimitedVectorProblem, + I: Identifier, { fn init(&self, _problem: &P, state: &mut State

) -> ExecResult<()> { state.insert(InertiaWeight::::new(self.weight)); @@ -249,17 +249,17 @@ impl PersonalBestParticlesInit { } pub fn new

() -> Box> - where - P: LimitedVectorProblem, + where + P: LimitedVectorProblem, { Box::new(Self::from_params()) } } impl Component

for PersonalBestParticlesInit - where - P: LimitedVectorProblem, - I: Identifier, +where + P: LimitedVectorProblem, + I: Identifier, { fn init(&self, _problem: &P, state: &mut State

) -> ExecResult<()> { state.insert(BestParticles::::new(Vec::new())); @@ -282,17 +282,17 @@ impl PersonalBestParticlesUpdate { } pub fn new

() -> Box> - where - P: LimitedVectorProblem, + where + P: LimitedVectorProblem, { Box::new(Self::from_params()) } } impl Component

for PersonalBestParticlesUpdate - where - P: LimitedVectorProblem, - I: Identifier, +where + P: LimitedVectorProblem, + I: Identifier, { fn execute(&self, _problem: &P, state: &mut State

) -> ExecResult<()> { let populations = state.populations(); diff --git a/src/utils.rs b/src/utils.rs index e322537..ebf09a7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -46,6 +46,6 @@ impl serde::Serialize for SerializablePhantom { } /// Calculates squared Euclidean distance between two vectors. -pub fn squared_euclidean (a: &[f64], b: &Vec) -> f64 { +pub fn squared_euclidean(a: &[f64], b: &Vec) -> f64 { a.iter().zip(b).map(|(p, q)| (p - q).powf(2.0)).sum::() }