optimal-pbil 0.0.0

Implementation of population-based incremental learning (PBIL)
Documentation
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use partial_min_max::min;
use rand::{
    distributions::{Bernoulli, Standard},
    prelude::*,
};
use rand_xoshiro::Xoshiro256PlusPlus;
use std::{
    fmt::Debug,
    ops::{Add, Mul, Sub},
};

use self::{sampleable::Sampleable, valued::Valued};

use super::types::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DynState<B> {
    Started(Started),
    SampledFirst(SampledFirst),
    EvaluatedFirst(EvaluatedFirst<B>),
    Sampled(Sampled<B>),
    Evaluated(Evaluated<B>),
    Compared(Compared<B>),
    Adjusted(Adjusted),
    Mutated(Mutated),
    Finished(Finished),
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Started {
    pub probabilities: Vec<Probability>,
    pub rng: Xoshiro256PlusPlus,
}

/// PBIL state ready to start sampling.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct InitializedSampling {
    pub probabilities: Sampleable,
    pub rng: Xoshiro256PlusPlus,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SampledFirst {
    pub probabilities: Sampleable,
    pub rng: Xoshiro256PlusPlus,
    pub sample: Vec<bool>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EvaluatedFirst<B> {
    pub probabilities: Sampleable,
    pub rng: Xoshiro256PlusPlus,
    pub sample: Valued<Vec<bool>, B>,
}

/// PBIL state for sampling
/// and adjusting probabilities
/// based on samples.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Sampled<B> {
    pub probabilities: Sampleable,
    pub rng: Xoshiro256PlusPlus,
    pub samples_generated: usize,
    pub best_sample: Valued<Vec<bool>, B>,
    pub sample: Vec<bool>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Evaluated<B> {
    pub probabilities: Sampleable,
    pub rng: Xoshiro256PlusPlus,
    pub samples_generated: usize,
    pub best_sample: Valued<Vec<bool>, B>,
    pub sample: Valued<Vec<bool>, B>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Compared<B> {
    pub probabilities: Sampleable,
    pub rng: Xoshiro256PlusPlus,
    pub samples_generated: usize,
    pub best_sample: Valued<Vec<bool>, B>,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Adjusted {
    pub probabilities: Vec<Probability>,
    pub rng: Xoshiro256PlusPlus,
}

/// PBIL state for mutating probabilities.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Mutated {
    pub probabilities: Vec<Probability>,
    pub rng: Xoshiro256PlusPlus,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Finished {
    pub probabilities: Vec<Probability>,
    pub rng: Xoshiro256PlusPlus,
}

impl<B> DynState<B> {
    pub fn new(probabilities: Vec<Probability>, rng: Xoshiro256PlusPlus) -> Self {
        Self::Started(Started::new(probabilities, rng))
    }
}

impl Started {
    fn new(probabilities: Vec<Probability>, rng: Xoshiro256PlusPlus) -> Self {
        Self { probabilities, rng }
    }

    pub fn into_initialized_sampling(self) -> InitializedSampling {
        InitializedSampling::new(self.probabilities, self.rng)
    }
}

impl InitializedSampling {
    fn new(probabilities: Vec<Probability>, rng: Xoshiro256PlusPlus) -> Self {
        Self {
            probabilities: Sampleable::new(probabilities),
            rng,
        }
    }

    pub fn into_sampled_first(self) -> SampledFirst {
        SampledFirst::new(self.probabilities, self.rng)
    }
}

impl SampledFirst {
    fn new(probabilities: Sampleable, mut rng: Xoshiro256PlusPlus) -> Self {
        Self {
            sample: probabilities.sample(&mut rng),
            probabilities,
            rng,
        }
    }

    pub fn into_evaluated_first<B>(self, f: impl FnOnce(&[bool]) -> B) -> EvaluatedFirst<B> {
        EvaluatedFirst::new(self.probabilities, self.rng, self.sample, f)
    }
}

impl<B> EvaluatedFirst<B> {
    fn new(
        probabilities: Sampleable,
        rng: Xoshiro256PlusPlus,
        sample: Vec<bool>,
        f: impl FnOnce(&[bool]) -> B,
    ) -> Self {
        Self {
            probabilities,
            rng,
            sample: Valued::new(sample, f),
        }
    }

    pub fn into_sampled(self) -> Sampled<B> {
        Sampled::new(self.probabilities, self.rng, 1, self.sample)
    }
}

impl<B> Sampled<B> {
    fn new(
        probabilities: Sampleable,
        mut rng: Xoshiro256PlusPlus,
        samples_generated: usize,
        best_sample: Valued<Vec<bool>, B>,
    ) -> Self {
        Self {
            sample: probabilities.sample(&mut rng),
            probabilities,
            rng,
            best_sample,
            samples_generated: samples_generated + 1,
        }
    }

    pub fn into_evaluated(self, f: impl FnOnce(&[bool]) -> B) -> Evaluated<B> {
        Evaluated::new(
            self.probabilities,
            self.rng,
            self.samples_generated,
            self.best_sample,
            self.sample,
            f,
        )
    }
}

impl<B> Evaluated<B> {
    fn new(
        probabilities: Sampleable,
        rng: Xoshiro256PlusPlus,
        samples_generated: usize,
        best_sample: Valued<Vec<bool>, B>,
        sample: Vec<bool>,
        f: impl FnOnce(&[bool]) -> B,
    ) -> Self {
        Self {
            probabilities,
            rng,
            samples_generated,
            best_sample,
            sample: Valued::new(sample, f),
        }
    }

    pub fn into_compared(self) -> Compared<B>
    where
        B: PartialOrd,
    {
        Compared::new(
            self.probabilities,
            self.rng,
            self.samples_generated,
            self.best_sample,
            self.sample,
        )
    }
}

impl<B> Compared<B> {
    fn new(
        probabilities: Sampleable,
        rng: Xoshiro256PlusPlus,
        samples_generated: usize,
        best_sample: Valued<Vec<bool>, B>,
        sample: Valued<Vec<bool>, B>,
    ) -> Self
    where
        B: PartialOrd,
    {
        Self {
            probabilities,
            rng,
            samples_generated,
            best_sample: min(sample, best_sample),
        }
    }

    pub fn into_sampled(self) -> Sampled<B> {
        Sampled::new(
            self.probabilities,
            self.rng,
            self.samples_generated,
            self.best_sample,
        )
    }

    pub fn into_adjusted(self, adjust_rate: AdjustRate) -> Adjusted {
        Adjusted::new(
            adjust_rate,
            self.probabilities.into_probabilities(),
            self.rng,
            self.best_sample.into_parts().0,
        )
    }
}

impl Adjusted {
    fn new(
        adjust_rate: AdjustRate,
        mut probabilities: Vec<Probability>,
        rng: Xoshiro256PlusPlus,
        best_sample: Vec<bool>,
    ) -> Self {
        adjust_probabilities(&mut probabilities, adjust_rate.into(), best_sample);
        Self { probabilities, rng }
    }

    pub fn into_mutated(self, chance: MutationChance, adjust_rate: MutationAdjustRate) -> Mutated {
        Mutated::new(chance, adjust_rate, self.probabilities, self.rng)
    }

    pub fn into_finished(self) -> Finished {
        Finished::new(self.probabilities, self.rng)
    }
}

impl Mutated {
    /// Step to 'Mutated' state
    /// by randomly mutating probabilities.
    ///
    /// With chance `chance`,
    /// adjust each probability towards a random probability
    /// at rate `adjust_rate`.
    pub fn new(
        chance: MutationChance,
        adjust_rate: MutationAdjustRate,
        mut probabilities: Vec<Probability>,
        mut rng: Xoshiro256PlusPlus,
    ) -> Self {
        let distr: Bernoulli = chance.into();
        probabilities.iter_mut().for_each(|p| {
            if rng.sample(distr) {
                // `Standard` distribution excludes `1`,
                // but it more efficient
                // than `Uniform::new_inclusive(0., 1.)`.
                // This operation is safe
                // because Probability is closed under `adjust`
                // with rate in [0,1].
                *p = unsafe {
                    Probability::new_unchecked(adjust(
                        adjust_rate.into(),
                        f64::from(*p),
                        rng.sample(Standard),
                    ))
                }
            }
        });
        Self { probabilities, rng }
    }

    pub fn into_finished(self) -> Finished {
        Finished::new(self.probabilities, self.rng)
    }
}

impl Finished {
    pub fn new(probabilities: Vec<Probability>, rng: Xoshiro256PlusPlus) -> Self {
        Self { probabilities, rng }
    }

    pub fn into_started(self) -> Started {
        Started::new(self.probabilities, self.rng)
    }
}

fn adjust_probabilities(probabilities: &mut [Probability], adjust_rate: f64, sample: Vec<bool>) {
    probabilities.iter_mut().zip(&sample).for_each(|(p, b)| {
        // This operation is safe
        // because Probability is closed under `adjust`
        // with rate in [0,1].
        *p = unsafe {
            Probability::new_unchecked(adjust(adjust_rate, f64::from(*p), *b as u8 as f64))
        }
    });
}

/// Adjust a number from `x` to `y`
/// at given rate.
fn adjust<T>(rate: T, x: T, y: T) -> T
where
    T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Copy,
{
    x + rate * (y - x)
}

mod sampleable {
    use rand::{distributions::Bernoulli, prelude::*};

    use crate::Probability;

    #[cfg(feature = "serde")]
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Debug, PartialEq)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    pub struct Sampleable {
        probabilities: Vec<Probability>,
        distrs: Vec<Bernoulli>,
    }

    impl Sampleable {
        pub fn new(probabilities: Vec<Probability>) -> Self {
            Self {
                distrs: probabilities
                    .iter()
                    .map(|p| Bernoulli::new(f64::from(*p)).expect("Invalid probability"))
                    .collect::<Vec<_>>(),
                probabilities,
            }
        }

        pub fn probabilities(&self) -> &[Probability] {
            &self.probabilities
        }

        pub fn into_probabilities(self) -> Vec<Probability> {
            self.probabilities
        }
    }

    impl Distribution<Vec<bool>> for Sampleable {
        fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<bool> {
            self.distrs.iter().map(|d| rng.sample(d)).collect()
        }
    }
}

mod valued {
    use std::borrow::Borrow;

    use derive_getters::{Dissolve, Getters};

    #[cfg(feature = "serde")]
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Debug, PartialEq, Eq, Dissolve, Getters)]
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[dissolve(rename = "into_parts")]
    pub struct Valued<T, B> {
        x: T,
        value: B,
    }

    impl<T, B> Valued<T, B> {
        pub fn new<Borrowed, F>(x: T, f: F) -> Self
        where
            Borrowed: ?Sized,
            T: Borrow<Borrowed>,
            F: FnOnce(&Borrowed) -> B,
        {
            Self {
                value: f(x.borrow()),
                x,
            }
        }
    }

    impl<T, B> PartialOrd for Valued<T, B>
    where
        T: PartialEq,
        B: PartialOrd,
    {
        fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
            self.value.partial_cmp(&other.value)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use num_traits::bounds::{LowerBounded, UpperBounded};
    use proptest::{prelude::*, test_runner::FileFailurePersistence};
    use test_strategy::proptest;

    #[proptest(failure_persistence = Some(Box::new(FileFailurePersistence::Off)))]
    fn probabilities_are_valid_after_adjusting(
        initial_probabilities: Vec<Probability>,
        seed: u64,
        adjust_rate: AdjustRate,
    ) {
        let state = InitializedSampling::new(
            initial_probabilities,
            Xoshiro256PlusPlus::seed_from_u64(seed),
        );
        let state = state.into_sampled_first();
        let state = state.into_evaluated_first(f).into_sampled();
        let state = state
            .into_evaluated(f)
            .into_compared()
            .into_adjusted(adjust_rate);
        prop_assert!(are_valid(&state.probabilities));
    }

    #[proptest(failure_persistence = Some(Box::new(FileFailurePersistence::Off)))]
    fn probabilities_are_valid_after_mutating(
        initial_probabilities: Vec<Probability>,
        seed: u64,
        mutation_chance: MutationChance,
        mutation_adjust_rate: MutationAdjustRate,
    ) {
        let state = Mutated::new(
            mutation_chance,
            mutation_adjust_rate,
            initial_probabilities,
            Xoshiro256PlusPlus::seed_from_u64(seed),
        );
        prop_assert!(are_valid(&state.probabilities));
    }

    macro_rules! arbitrary_from_bounded {
        ( $from:ident, $to:ident ) => {
            impl Arbitrary for $to {
                type Parameters = ();
                type Strategy = BoxedStrategy<$to>;
                fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
                    ($from::from($to::min_value())..=$to::max_value().into())
                        .prop_map(|x| x.try_into().unwrap())
                        .boxed()
                }
            }
        };
    }

    arbitrary_from_bounded!(f64, Probability);
    arbitrary_from_bounded!(f64, AdjustRate);
    arbitrary_from_bounded!(f64, MutationChance);
    arbitrary_from_bounded!(f64, MutationAdjustRate);

    fn f(point: &[bool]) -> usize {
        point.iter().filter(|x| **x).count()
    }

    fn are_valid(probabilities: &[Probability]) -> bool {
        probabilities
            .iter()
            .all(|p| Probability::try_from(f64::from(*p)).is_ok())
    }
}