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
//! Composable Particle Swarm Optimization.
//!
//! Pick a [`Unit`] type, plug in a [`FitCalc`] and a [`ParticleMover`],
//! hand them to a [`Searcher`], iterate.
//!
//! # Kit
//!
//! Position space: [`Unit`] (any `Copy + Default + Debug + Send + Sync +
//! 'static` qualifies; [`Floats<N>`] is the off-the-shelf element-wise
//! `[f64; N]` newtype). Scoring: [`FitCalc`] (higher = better).
//! Constraints: [`Boundary`]. Per-step / per-iteration reparameterization:
//! [`Contextful`]. Particle storage: [`Particle`] / [`Group`] /
//! [`ParticleRefMut`]. Initial swarms: [`ParticleInit`] +
//! [`ParticleInitDependent`]. Sub-unit views for compound particles:
//! [`ParticleRefFrom`] + [`SetTo`].
//!
//! Mover combinators: [`BoundedMover`], [`ChainedMover`], [`AdapterMover`]
//! (lift a sub-unit mover to a parent unit), [`NestedMover`] (run an inner
//! search inside an outer mover step).
//!
//! Concrete searchers: [`GBestSearcher`] (one swarm-wide attractor),
//! [`LBestSearcher`] (ring / Von Neumann neighbourhoods — slower but
//! preserves diversity), [`NichedSearcher`] (static multi-swarm partition).
//!
//! For writing your own [`Searcher`], the shared parallel-pass and
//! bookkeeping helpers are also re-exported: [`SearcherCore`],
//! [`evaluate_pbests`], [`move_particles`], [`reduce_best`],
//! [`par_for_each_mut_rng`].
//!
//! # Example
//!
//! Minimal end-to-end: find `x = 3` by maximizing `-(x - 3)^2` on `[-10, 10]`.
//!
//! ```
//! use rand::rngs::SmallRng;
//! use rand::{Rng, RngExt, SeedableRng};
//! use swarmkit::{
//! Boundary, Contextful, FieldwiseClamp, FitCalc, Floats, GBestMover,
//! IntoGBestSearcher, PSOCoeffs, ParticleInit, ParticleMover, Searcher,
//! };
//!
//! type X = Floats<1>;
//!
//! struct Fit;
//! impl Contextful for Fit { type TContext = (); }
//! impl FitCalc for Fit {
//! type T = X;
//! fn calculate_fit(&self, v: X) -> f64 { -(v[0] - 3.0).powi(2) }
//! }
//!
//! struct Bound;
//! impl Contextful for Bound { type TContext = (); }
//! impl Boundary for Bound {
//! type T = X;
//! fn handle(&self, v: X) -> X { v.clamp(Floats([-10.0]), Floats([10.0])) }
//! }
//!
//! struct Init;
//! impl ParticleInit for Init {
//! type T = X;
//! fn init_pos<R: Rng>(&self, r: &mut R) -> Vec<X> {
//! (0..16).map(|_| Floats([r.random_range(-10.0..10.0)])).collect()
//! }
//! fn init_vel<R: Rng>(&self, r: &mut R) -> Vec<X> {
//! (0..16).map(|_| Floats([r.random_range(-1.0..1.0)])).collect()
//! }
//! }
//!
//! fn main() {
//! let mut rng = SmallRng::seed_from_u64(0);
//! let mut group = Init.init(&mut rng);
//! let mut searcher = GBestMover::<X>::new(PSOCoeffs::new(0.5, 1.5, 1.5))
//! .bounded_by(Bound)
//! .into_gbest_searcher(Fit);
//!
//! let best = searcher
//! .iter(40, &mut group, None)
//! .last()
//! .expect("40 iterations produce a final Best");
//! assert!((best.best_pos[0] - 3.0).abs() < 0.5);
//! }
//! ```
//!
//! The 2D analogue lives at `cargo run --example elliptical`. For a
//! non-trivial real-world use, see `swarmkit-sailing` in the
//! [`bywind`](https://github.com/Anvoker/bywind) repo: sailboat route
//! optimization on a spherical-Earth grid, exercising [`NestedMover`],
//! [`ParticleRefFrom`] / [`SetTo`], iteration-aware schedules, and a
//! custom tangent-frame replacement for the generic gbest update.
//!
//! # Parallelism
//!
//! Rayon drives the fitness and mover passes. Each trait carries a
//! `PAR_LEAF_SIZE` constant; the default `usize::MAX` keeps cheap work
//! serial. Override per impl when per-particle cost amortizes a split.
pub use Best;
pub use ;
pub use Floats;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;