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
use crate::{
    genetic::Genotype,
    operator::{GeneticOperator, MutationOp},
    random::{random_index, Rng},
};
use rand::seq::SliceRandom;
use std::fmt::Debug;

#[derive(Clone, Debug, PartialEq)]
pub struct RandomValueMutator<G>
where
    G: Genotype + RandomGenomeMutation,
{
    mutation_rate: f64,
    min_value: <G as Genotype>::Dna,
    max_value: <G as Genotype>::Dna,
}

impl<G> RandomValueMutator<G>
where
    G: Genotype + RandomGenomeMutation,
{
    pub fn new(
        mutation_rate: f64,
        min_value: <G as Genotype>::Dna,
        max_value: <G as Genotype>::Dna,
    ) -> Self {
        RandomValueMutator {
            mutation_rate,
            min_value,
            max_value,
        }
    }
}

impl<G> GeneticOperator for RandomValueMutator<G>
where
    G: Genotype + RandomGenomeMutation,
{
    fn name() -> String {
        "Random-Value-Mutator".to_string()
    }
}

impl<G> MutationOp<G> for RandomValueMutator<G>
where
    G: Genotype + RandomGenomeMutation,
{
    fn mutate<R>(&self, genome: G, rng: &mut R) -> G
    where
        R: Rng + Sized,
    {
        RandomGenomeMutation::mutate_genome(
            genome,
            self.mutation_rate,
            &self.min_value,
            &self.max_value,
            rng,
        )
    }
}

pub trait RandomGenomeMutation: Genotype {
    type Dna: Clone;

    fn mutate_genome<R>(
        genome: Self,
        mutation_rate: f64,
        min_value: &<Self as Genotype>::Dna,
        max_value: &<Self as Genotype>::Dna,
        rng: &mut R,
    ) -> Self
    where
        R: Rng + Sized;
}

impl<V> RandomGenomeMutation for Vec<V>
where
    V: Clone + Debug + PartialEq + Send + Sync + RandomValueMutation,
{
    type Dna = V;

    fn mutate_genome<R>(
        genome: Self,
        mutation_rate: f64,
        min_value: &V,
        max_value: &V,
        rng: &mut R,
    ) -> Self
    where
        R: Rng + Sized,
    {
        let genome_length = genome.len();
        let num_mutations =
            ((genome_length as f64 * mutation_rate) + rng.gen::<f64>()).floor() as usize;
        let mut mutated = genome;
        for _ in 0..num_mutations {
            let index = random_index(rng, genome_length);
            mutated[index] = RandomValueMutation::random_mutated(
                mutated[index].clone(),
                min_value,
                max_value,
                rng,
            );
        }
        mutated
    }
}

#[cfg(feature = "fixedbitset")]
mod fixedbitset_random_genome_mutation {
    use super::{random_index, RandomGenomeMutation};
    use crate::genetic::Genotype;
    use fixedbitset::FixedBitSet;
    use rand::Rng;

    impl RandomGenomeMutation for FixedBitSet {
        type Dna = bool;

        fn mutate_genome<R>(
            genome: Self,
            mutation_rate: f64,
            _: &<Self as Genotype>::Dna,
            _: &<Self as Genotype>::Dna,
            rng: &mut R,
        ) -> Self
        where
            R: Rng + Sized,
        {
            let genome_length = genome.len();
            let num_mutations =
                ((genome_length as f64 * mutation_rate) + rng.gen::<f64>()).floor() as usize;
            let mut mutated = genome;
            for _ in 0..num_mutations {
                let bit = random_index(rng, genome_length);
                let value = rng.gen();
                mutated.set(bit, value);
            }
            mutated
        }
    }
}

#[cfg(feature = "smallvec")]
mod smallvec_random_genome_mutation {
    use super::{random_index, RandomGenomeMutation, RandomValueMutation};
    use rand::Rng;
    use smallvec::{Array, SmallVec};
    use std::fmt::Debug;

    impl<V, A> RandomGenomeMutation for SmallVec<A>
    where
        A: Array<Item = V> + Sync,
        V: Clone + Debug + PartialEq + Send + Sync + RandomValueMutation,
    {
        type Dna = V;

        fn mutate_genome<R>(
            genome: Self,
            mutation_rate: f64,
            min_value: &V,
            max_value: &V,
            rng: &mut R,
        ) -> Self
        where
            R: Rng + Sized,
        {
            let genome_length = genome.len();
            let num_mutations =
                ((genome_length as f64 * mutation_rate) + rng.gen::<f64>()).floor() as usize;
            let mut mutated = genome;
            for _ in 0..num_mutations {
                let index = random_index(rng, genome_length);
                mutated[index] = RandomValueMutation::random_mutated(
                    mutated[index].clone(),
                    min_value,
                    max_value,
                    rng,
                );
            }
            mutated
        }
    }
}

pub trait RandomValueMutation {
    fn random_mutated<R>(value: Self, min_value: &Self, max_value: &Self, rng: &mut R) -> Self
    where
        R: Rng + Sized;
}

macro_rules! impl_random_value_mutation {
    ($($t:ty),*) => {
        $(
            impl RandomValueMutation for $t {
                #[inline]
                fn random_mutated<R>(_: $t, min_value: &$t, max_value: &$t, rng: &mut R) -> $t
                    where R: Rng + Sized
                {
                    rng.gen_range(*min_value, *max_value)
                }
            }
        )*
    }
}

impl_random_value_mutation!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);

impl RandomValueMutation for bool {
    #[inline]
    fn random_mutated<R>(_value: bool, _min_value: &bool, _max_value: &bool, rng: &mut R) -> bool
    where
        R: Rng + Sized,
    {
        rng.gen_bool(0.5)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct BreederValueMutator<G>
where
    G: Genotype + BreederGenomeMutation,
{
    mutation_rate: f64,
    mutation_range: <G as Genotype>::Dna,
    mutation_precision: u8,
    min_value: <G as Genotype>::Dna,
    max_value: <G as Genotype>::Dna,
}

impl<G> BreederValueMutator<G>
where
    G: Genotype + BreederGenomeMutation,
{
    pub fn new(
        mutation_rate: f64,
        mutation_range: <G as Genotype>::Dna,
        mutation_precision: u8,
        min_value: <G as Genotype>::Dna,
        max_value: <G as Genotype>::Dna,
    ) -> Self {
        BreederValueMutator {
            mutation_rate,
            mutation_range,
            mutation_precision,
            min_value,
            max_value,
        }
    }
}

impl<G> GeneticOperator for BreederValueMutator<G>
where
    G: Genotype + BreederGenomeMutation,
{
    fn name() -> String {
        "Breeder-Value-Mutator".to_string()
    }
}

impl<G> MutationOp<G> for BreederValueMutator<G>
where
    G: Genotype + BreederGenomeMutation,
{
    fn mutate<R>(&self, genome: G, rng: &mut R) -> G
    where
        R: Rng + Sized,
    {
        BreederGenomeMutation::mutate_genome(
            genome,
            self.mutation_rate,
            &self.mutation_range,
            self.mutation_precision,
            &self.min_value,
            &self.max_value,
            rng,
        )
    }
}

pub trait BreederGenomeMutation: Genotype {
    type Dna: Clone;

    fn mutate_genome<R>(
        genome: Self,
        mutation_rate: f64,
        range: &<Self as Genotype>::Dna,
        precision: u8,
        min_value: &<Self as Genotype>::Dna,
        max_value: &<Self as Genotype>::Dna,
        rng: &mut R,
    ) -> Self
    where
        R: Rng + Sized;
}

impl<V> BreederGenomeMutation for Vec<V>
where
    V: Clone
        + Debug
        + PartialEq
        + PartialOrd
        + Send
        + Sync
        + BreederValueMutation
        + RandomValueMutation,
{
    type Dna = V;

    fn mutate_genome<R>(
        genome: Vec<V>,
        mutation_rate: f64,
        range: &<Self as Genotype>::Dna,
        precision: u8,
        min_value: &<Self as Genotype>::Dna,
        max_value: &<Self as Genotype>::Dna,
        rng: &mut R,
    ) -> Vec<V>
    where
        R: Rng + Sized,
    {
        let genome_length = genome.len();
        let num_mutations =
            ((genome_length as f64 * mutation_rate) + rng.gen::<f64>()).floor() as usize;
        let mut mutated = genome;
        for _ in 0..num_mutations {
            let index = random_index(rng, genome_length);
            let sign = *[-1, 1].choose(rng).unwrap();
            let adjustment = if *[true, false].choose(rng).unwrap() {
                1. / (1i64 << precision) as f64
            } else {
                1.
            };
            let value_mut = BreederValueMutation::breeder_mutated(
                mutated[index].clone(),
                range,
                adjustment,
                sign,
            );
            if value_mut < *min_value {
                mutated[index] =
                    RandomValueMutation::random_mutated(value_mut, min_value, max_value, rng)
            } else if value_mut > *max_value {
                mutated[index] = max_value.clone();
            } else {
                mutated[index] = value_mut;
            }
        }
        mutated
    }
}

pub trait BreederValueMutation {
    fn breeder_mutated(value: Self, range: &Self, adjustment: f64, sign: i8) -> Self;
}

macro_rules! impl_breeder_mutation {
    ($($t:ty),*) => {
        $(
            #[allow(trivial_numeric_casts)]
            impl BreederValueMutation for $t {
                #[inline]
                fn breeder_mutated(value: $t, range: &$t, adjustment: f64, sign: i8) -> $t {
                    (value as f64 + *range as f64 * adjustment * sign as f64) as $t
                }
            }
        )*
    }
}

impl_breeder_mutation!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64);