optlib 0.4.0

The optimization algorithms realized in Rust. In given time realized genetic and particle swarm algorithms.
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
//! The module with most usable algorithms of crossing for various types.
//! The module contains struct which implements the `Cross` trait and functions to cross
//! chromosomes various types.

use std::mem;

use crate::genetic::Cross;
use num::{Float, Num, NumCast};
use rand::distributions::{Distribution, Uniform};
use rand::rngs::ThreadRng;

/// Struct to cross all genes (`G` - type of genes) in chromosome of type Vec<G>.
pub struct VecCrossAllGenes<G> {
    single_cross: Box<dyn Cross<G>>,
}

/// Child chromosome is arithmetic mean of parent chromosomes. Result of cross is single child.
/// The chromosomes must be numeric type.
pub struct CrossMean;

/// For float type chromosomes (f32, f64). Child chromosome is geometric mean of parent
/// chromosomes. Result of cross is single child.
pub struct FloatCrossGeometricMean;

/// Bitwise cross. Use single point crossing. Result of cross is single child.
pub struct CrossBitwise {
    random: ThreadRng,
}

/// Bitwise cross for float type chromosomes. Exponent and mantissa will be crossed independently.
/// Use single point crossing. The sign is taken from one of parents at random.
pub struct FloatCrossExp {
    random: ThreadRng,
}

impl CrossMean {
    pub fn new() -> Self {
        Self {}
    }
}

/// ```
/// use optlib::genetic::cross;
/// use optlib::genetic::Cross;
///
/// assert!(cross::CrossMean::new().cross(&vec![&1.0_f32, &2.0_f32]) == vec![1.5_f32]);
/// assert!(cross::CrossMean::new().cross(&vec![&0.0_f64, &1.0_f64]) == vec![0.5_f64]);
/// assert!(cross::CrossMean::new().cross(&vec![&0.0_f64, &1.0_f64, &2.0_f64]) == vec![1.0_f64]);
/// ```
impl<G: NumCast + Num + Clone> Cross<G> for CrossMean {
    fn cross(&mut self, parents_genes: &[&G]) -> Vec<G> {
        assert!(parents_genes.len() >= 2);
        let mut result: G = parents_genes
            .iter()
            .fold(G::zero(), |x, y| x + (**y).clone());

        result = result / G::from(parents_genes.len()).unwrap();
        vec![result]
    }
}

impl FloatCrossGeometricMean {
    /// Constructor.
    pub fn new() -> Self {
        Self {}
    }
}

/// ```
/// use optlib::genetic::cross;
/// use optlib::genetic::Cross;
///
/// let mut crosser = cross::FloatCrossGeometricMean::new();
///
/// let parents_genes_equal = vec![&1.0_f32, &1.0_f32];
/// assert!(crosser.cross(&parents_genes_equal).len() == 1);
/// assert!(crosser.cross(&parents_genes_equal)[0] - 1.0_f32 < 1e-7);
///
/// let parents_genes_1 = vec![&1.0_f32, &2.0_f32];
/// assert!(crosser.cross(&parents_genes_1).len() == 1);
/// assert!(crosser.cross(&parents_genes_1)[0] - 1.41421 < 1e-4);
///
/// let parents_genes_2 = vec![&1.0_f64, &2.0_f64, &3.0_f64];
/// assert!(crosser.cross(&parents_genes_2).len() == 1);
/// assert!(crosser.cross(&parents_genes_2)[0] - 1.81712 < 1e-4);
/// ```
impl<G: Float> Cross<G> for FloatCrossGeometricMean {
    fn cross(&mut self, parents_genes: &[&G]) -> Vec<G> {
        assert!(parents_genes.len() >= 2);
        let mut result: G = parents_genes.iter().fold(G::one(), |x, y| x * (**y));

        result = result.powf(G::one() / G::from(parents_genes.len()).unwrap());
        vec![result]
    }
}

impl CrossBitwise {
    /// Constructor.
    pub fn new() -> Self {
        let random = rand::thread_rng();
        Self { random }
    }
}

impl Cross<f64> for CrossBitwise {
    fn cross(&mut self, parents_genes: &[&f64]) -> Vec<f64> {
        assert_eq!(parents_genes.len(), 2);
        let size = mem::size_of::<f64>() * 8;
        let between = Uniform::new(1, size);
        let pos = between.sample(&mut self.random);

        vec![cross_f64(*parents_genes[0], *parents_genes[1], pos)]
    }
}

impl Cross<f32> for CrossBitwise {
    fn cross(&mut self, parents_genes: &[&f32]) -> Vec<f32> {
        assert_eq!(parents_genes.len(), 2);
        let size = mem::size_of::<f32>() * 8;
        let between = Uniform::new(1, size);
        let pos = between.sample(&mut self.random);

        vec![cross_f32(*parents_genes[0], *parents_genes[1], pos)]
    }
}

impl<G> VecCrossAllGenes<G> {
    pub fn new(single_cross: Box<dyn Cross<G>>) -> Self {
        Self { single_cross }
    }
}

impl<G> Cross<Vec<G>> for VecCrossAllGenes<G> {
    fn cross(&mut self, parents: &[&Vec<G>]) -> Vec<Vec<G>> {
        assert!(parents.len() == 2);

        let parent_1 = parents[0];
        let parent_2 = parents[1];

        let gene_count = parent_1.len();
        let mut child = vec![];

        for n in 0..gene_count {
            let mut new_gene = self
                .single_cross
                .cross(vec![&parent_1[n], &parent_2[n]].as_slice());
            child.append(&mut new_gene);
        }
        vec![child]
    }
}

impl FloatCrossExp {
    pub fn new() -> Self {
        let random = rand::thread_rng();
        Self { random }
    }
}

impl<T: Float> Cross<T> for FloatCrossExp {
    fn cross(&mut self, parents_genes: &[&T]) -> Vec<T> {
        assert_eq!(parents_genes.len(), 2);
        // mantissa: u64, exponent: i16, sign: i8
        let (mantissa_1, exponent_1, sign_1) = parents_genes[0].integer_decode();
        let (mantissa_2, exponent_2, sign_2) = parents_genes[1].integer_decode();

        let mantissa_size = mem::size_of_val(&mantissa_1) * 8;
        let exponent_size = mem::size_of_val(&exponent_1) * 8;

        let mantissa_between = Uniform::new(1, mantissa_size);
        let exponent_between = Uniform::new(1, exponent_size);

        let mantissa_pos = mantissa_between.sample(&mut self.random);
        let exponent_pos = exponent_between.sample(&mut self.random);

        let mantissa_child = cross_u64(mantissa_1, mantissa_2, mantissa_pos);
        let exponent_child = cross_i16(exponent_1, exponent_2, exponent_pos);

        let sign_child = match Uniform::new_inclusive(0i8, 1i8).sample(&mut self.random) {
            0 => sign_1,
            1 => sign_2,
            _ => panic!("Invalid random value in FloatCrossExp"),
        };

        vec![
            T::from(sign_child).unwrap()
                * T::from(mantissa_child).unwrap()
                * T::from(exponent_child).unwrap().exp2(),
        ]
    }
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
/// # Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_u64(0u64, std::u64::MAX, 1), 1u64);
/// assert_eq!(cross::cross_u64(0u64, std::u64::MAX, 4), 0b_1111_u64);
/// assert_eq!(cross::cross_u64(0u64, std::u64::MAX, 63),
/// 0b_0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_u64);
/// assert_eq!(cross::cross_u64(std::u64::MAX, 0u64, 4),
/// 0b_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_0000_u64);
/// ```
pub fn cross_u64(parent_1: u64, parent_2: u64, pos: usize) -> u64 {
    let size = mem::size_of::<f64>() * 8;
    let mask_parent_1 = !0u64 << pos;
    let mask_parent_2 = !0u64 >> (size - pos);
    (parent_1 & mask_parent_1) | (parent_2 & mask_parent_2)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
/// # Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_u32(0u32, std::u32::MAX, 1), 1u32);
/// assert_eq!(cross::cross_u32(0u32, std::u32::MAX, 4), 0b_1111_u32);
/// assert_eq!(cross::cross_u32(0u32, std::u32::MAX, 31), 0b_0111_1111_1111_1111_1111_1111_1111_1111_u32);
/// assert_eq!(cross::cross_u32(std::u32::MAX, 0u32, 4), 0b_1111_1111_1111_1111_1111_1111_1111_0000_u32);
/// ```
pub fn cross_u32(parent_1: u32, parent_2: u32, pos: usize) -> u32 {
    let size = mem::size_of::<u32>() * 8;
    let mask_parent_1 = !0u32 << pos;
    let mask_parent_2 = !0u32 >> (size - pos);
    (parent_1 & mask_parent_1) | (parent_2 & mask_parent_2)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
///# Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// // -1i16 == 0b_1111_1111_1111_1111
/// assert_eq!(cross::cross_i16(0i16, -1i16, 4), 0b_0000_0000_0000_1111_i16);
/// assert_eq!(cross::cross_i16(0i16, -1i16, 1), 0b_0000_0000_0000_0001_i16);
/// assert_eq!(cross::cross_i16(0i16, -1i16, 15), 0b_0111_1111_1111_1111_i16);
/// assert_eq!(cross::cross_i16(0i16, -1i16, 8), 0b_0000_0000_1111_1111_i16);
///
/// // -1i16 == 0b_1111_1111_1111_1111
/// // -16i16 == 0b_1111_1111_1111_0000
/// assert_eq!(cross::cross_i16(-1i16, 0i16, 4), -16i16);
///
/// // -1i16 == 0b_1111_1111_1111_1111
/// // -32768i16 == 0b_1000_0000_0000_0000
/// assert_eq!(cross::cross_i16(-1i16, 0i16, 15), -32768i16);
/// ```
pub fn cross_i16(parent_1: i16, parent_2: i16, pos: usize) -> i16 {
    let size = mem::size_of::<i16>() * 8;
    let mask_parent_1 = !0i16 << pos;
    let mask_parent_2 = std::i16::MAX >> (size - pos - 1);
    (parent_1 & mask_parent_1) | (parent_2 & mask_parent_2)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
/// # Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_u16(0b_0000_0000_0000_0000, 0b_1111_1111_1111_1111, 4), 0b_1111);
/// assert_eq!(cross::cross_u16(0b_0000_0000_0000_0000, 0b_1111_1111_1111_1111, 1), 0b_1);
/// assert_eq!(cross::cross_u16(0b_0000_0000_0000_0000, 0b_1111_1111_1111_1111, 8), 0b_0000_0000_1111_1111);
/// assert_eq!(cross::cross_u16(0b_0000_0000_0000_0000, 0b_1111_1111_1111_1111, 15), 0b_0111_1111_1111_1111);
/// ```
pub fn cross_u16(parent_1: u16, parent_2: u16, pos: usize) -> u16 {
    let size = mem::size_of::<u16>() * 8;
    let mask_parent_1 = !0u16 << pos;
    let mask_parent_2 = !0u16 >> (size - pos);
    (parent_1 & mask_parent_1) | (parent_2 & mask_parent_2)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
///# Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_i8(0b_0000_0000_i8, 0b_0111_1111_i8, 4), 0b_0000_1111_i8);
/// assert_eq!(cross::cross_i8(0b_0000_0000_i8, 0b_0111_1111_i8, 1), 0b_0000_0001_i8);
/// assert_eq!(cross::cross_i8(0b_0000_0000_i8, 0b_0111_1111_i8, 7), 0b_0111_1111_i8);
/// assert_eq!(cross::cross_i8(0b_0000_0000_i8, 0b_0111_1111_i8, 6), 0b_0011_1111_i8);
///
/// // -1i8 == 0b_1111_1111
/// // -16i8 == 0b_1111_0000
/// assert_eq!(cross::cross_i8(-1i8, 0i8, 4), -16i8);
///
/// // -1i8 == 0b_1111_1111
/// // -128i8 == 0b_1000_0000
/// assert_eq!(cross::cross_i8(-1i8, 0i8, 7), -128i8);
/// ```
pub fn cross_i8(parent_1: i8, parent_2: i8, pos: usize) -> i8 {
    let size = mem::size_of::<i8>() * 8;
    let mask_parent_1 = !0i8 << pos;
    let mask_parent_2 = std::i8::MAX >> (size - pos - 1);
    (parent_1 & mask_parent_1) | (parent_2 & mask_parent_2)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
/// # Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_u8(0b_0000_0000, 0b_1111_1111, 4), 0b_0000_1111);
/// assert_eq!(cross::cross_u8(0b_0000_0000, 0b_1111_1111, 1), 0b_0000_0001);
/// assert_eq!(cross::cross_u8(0b_0000_0000, 0b_1111_1111, 7), 0b_0111_1111);
/// ```
pub fn cross_u8(parent_1: u8, parent_2: u8, pos: usize) -> u8 {
    let size = mem::size_of::<u8>() * 8;
    let mask_parent_1 = !0u8 << pos;
    let mask_parent_2 = !0u8 >> (size - pos);
    (parent_1 & mask_parent_1) | (parent_2 & mask_parent_2)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
/// # Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_f32(0f32, f32::from_bits(std::u32::MAX), 1), f32::from_bits(0b_0001));
/// assert_eq!(cross::cross_f32(0f32, f32::from_bits(std::u32::MAX), 4), f32::from_bits(0b_1111));
/// assert_eq!(cross::cross_f32(0f32, f32::from_bits(std::u32::MAX), 30),
///                             f32::from_bits(0b_0011_1111_1111_1111_1111_1111_1111_1111_u32));
/// ```
pub fn cross_f32(parent_1: f32, parent_2: f32, pos: usize) -> f32 {
    let parent_1_bits = parent_1.to_bits();
    let parent_2_bits = parent_2.to_bits();

    let child_bits = cross_u32(parent_1_bits, parent_2_bits, pos);
    f32::from_bits(child_bits)
}

/// Single point crossing.
///
/// # Parameters
/// * `parent_1`, `parent_2` - parents for crossing.
/// * `pos` - position for bytes exchange. The position is counted from right.
///
/// Returns single child.
///
/// # Examples
///
/// ```
/// use optlib::genetic::cross;
///
/// assert_eq!(cross::cross_f64(0f64, f64::from_bits(std::u64::MAX), 1), f64::from_bits(0b_0001));
/// assert_eq!(cross::cross_f64(0f64, f64::from_bits(std::u64::MAX), 4), f64::from_bits(0b_1111));
/// assert_eq!(cross::cross_f64(0f64, f64::from_bits(std::u64::MAX), 62),
///                             f64::from_bits(0b_0011_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_u64));
/// ```
pub fn cross_f64(parent_1: f64, parent_2: f64, pos: usize) -> f64 {
    let parent_1_bits = parent_1.to_bits();
    let parent_2_bits = parent_2.to_bits();

    let child_bits = cross_u64(parent_1_bits, parent_2_bits, pos);
    f64::from_bits(child_bits)
}