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
use rand::prelude::ThreadRng;
use rand::Rng;
use std::fmt;

#[derive(Clone, Debug)]
pub struct SwarmConfig{
    motion_coeffs: [f64; 4],
    igf: f64,
    wbf: f64,
    tgse: Option<usize>,
}

impl SwarmConfig {
    pub fn new_collaborative (
        local: f64, tribal: f64, global: f64, 
        inertial: f64, inertial_growth_factor: f64, 
        wall_bounce_factor: f64,
        tribal_global_share_every: usize,
    ) -> Self {

        let l = correct_sine(local);
        let t = correct_sine(tribal);
        let g = correct_sine(global);
        let i = correct_sine(inertial);

        let igf = correct_sine(inertial_growth_factor);
        let wbf = correct_sine(wall_bounce_factor);

        SwarmConfig {
            motion_coeffs: [l, t, g, i],
            igf: igf,
            wbf: wbf,
            tgse: Some(tribal_global_share_every),
        }
    }

    pub fn new_independant (
        local: f64, tribal: f64,
        inertial: f64, inertial_growth_factor: f64,
        wall_bounce_factor: f64,
    ) -> Self {

        let l = correct_sine(local);
        let t = correct_sine(tribal);
        let i = correct_sine(inertial);

        let igf = correct_sine(inertial_growth_factor);
        let wbf = correct_sine(wall_bounce_factor);

        SwarmConfig {
            motion_coeffs: [l, t, 0.0, i],
            igf: igf,
            wbf: wbf,
            tgse: None
        }
    }

    pub fn default_collab() -> Self {
        SwarmConfig {
            motion_coeffs: [1.45, 1.65, 1.55, 0.4],
            igf: 1.125,
            wbf: 0.125,
            tgse: Some(8),
        }
    }

    pub fn default_independant() -> Self {
        SwarmConfig {
            motion_coeffs: [1.45, 1.65, 0.0, 0.4],
            igf: 1.125,
            wbf: 0.125,
            tgse: None,
        }
    }

    pub fn motion_coeffs(&self) -> [f64;4] {
        self.motion_coeffs
    }

    pub fn inertial_growth_factor(&self) -> f64 {
        self.igf
    }

    pub fn wall_bounce_factor(&self) -> f64 {
        self.wbf * -1.0
    }

    pub fn tribal_global_share_every(&self) -> usize {
        match self.tgse {
            Some(tgse) => tgse,
            None => panic!("Independant swarm configurations do not have a tribal-global sharing period!")
        }
    }

    pub fn is_collaborative(&self) -> bool {
        match self.tgse {
            Some(_) => true,
            None => false,
        }
    }
}

impl fmt::Display for SwarmConfig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.tgse {
            Some(tribal_global_share_every) => {
                write!(f, "Collaborative Swarm [shares record with global collective every {} itterations]: \n", tribal_global_share_every)?;
            },
            None => {
                write!(f, "Independant Swarm: \n")?;
            }
        }

        write!(f, "\t \t Motion coefficients: {:?}, inertial growth factor: {}, wall bounce factor: {}", 
            self.motion_coeffs, self.igf, self.wbf,
        )

    }
}

#[derive(Clone, Debug)]
pub struct SwarmConfigDistribution {
    l: ParamDist,
    t: ParamDist,
    g: ParamDist,
    i: ParamDist,
    igf: ParamDist,
    wbf: ParamDist,
    tgse: Option<usize>
}

impl SwarmConfigDistribution {
    pub fn new_collaborative(
        local: ParamDist, tribal: ParamDist, global: ParamDist,
        inertial: ParamDist, inertial_growth_factor: ParamDist,
        wall_bounce_factor: ParamDist,
        tribal_global_share_every: usize,
    ) -> Self {

        let l = correct_sine_dist(local);
        let t = correct_sine_dist(tribal);
        let g = correct_sine_dist(global);
        let i = correct_sine_dist(inertial);

        let ifg = correct_sine_dist(inertial_growth_factor);
        let wbf = correct_sine_dist(wall_bounce_factor);

        SwarmConfigDistribution{
            l: l,
            t: t,
            g: g,
            i: i,
            igf: ifg,
            wbf: wbf,
            tgse: Some(tribal_global_share_every),
        }
    }

    pub fn new_independant(
        local: ParamDist, tribal: ParamDist,
        inertial: ParamDist, inertial_growth_factor: ParamDist,
        wall_bounce_factor: ParamDist,
    ) -> Self {
        let l = correct_sine_dist(local);
        let t = correct_sine_dist(tribal);
        let i = correct_sine_dist(inertial);

        let ifg = correct_sine_dist(inertial_growth_factor);
        let wbf = correct_sine_dist(wall_bounce_factor);

        SwarmConfigDistribution{
            l: l,
            t: t,
            g: ParamDist::Fixed(0.0),
            i: i,
            igf: ifg,
            wbf: wbf,
            tgse: None,
        }
    }

    pub fn sample_configuration(&self, rng: &mut ThreadRng) -> SwarmConfig {

        SwarmConfig {
            motion_coeffs: [self.l.sample(rng), self.t.sample(rng), self.g.sample(rng), self.i.sample(rng)],
            igf: self.igf.sample(rng),
            wbf: self.wbf.sample(rng),
            tgse: self.tgse,
        }
    }
}

#[derive(Clone, Debug)]
pub enum ParamDist {
    Fixed(f64),
    Range([f64; 2]),
}

impl ParamDist {
    pub fn sample(&self, rng: &mut ThreadRng) -> f64 {
        match self {
            Self::Fixed(value) => *value,
            Self::Range(mean_var) => {
                let offset = rng.gen_range(-1.0 * mean_var[1], mean_var[1]);
                mean_var[0] * (1.0 + offset)
            }
        }
    }
}

fn correct_sine_dist(param: ParamDist) -> ParamDist {
    match param {
        ParamDist::Fixed(value) => ParamDist::Fixed(correct_sine(value)),
        ParamDist::Range(mean_var) => {
            assert!(mean_var[1] < 1.0 && mean_var[1] > 0.0, 
                "Parameter Distribution Variance must be between 0 and 1"
            );
            let mean = correct_sine(mean_var[0]);
            ParamDist::Range([mean, mean_var[1]])
        }
    }
}

fn correct_sine(param: f64) -> f64 {
    match param > 0.0 {
        true => param,
        false => -1.0 * param,
    }
}