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
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{genes::Activation, genome::Genome};

pub use self::error::MutationError;

pub type MutationResult = Result<(), MutationError>;

mod add_connection;
mod add_node;
mod add_recurrent_connection;
mod change_activation;
mod change_weights;
mod duplicate_node;
mod error;
mod remove_connection;
mod remove_node;
mod remove_recurrent_connection;

/// Lists all possible mutations with their corresponding parameters.
///
/// Each mutation acts as a self-contained unit and has to be listed in the [`crate::Parameters::mutations`] field in order to take effect when calling [`crate::Genome::mutate_with`].
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum Mutations {
    /// See [`Mutations::change_weights`].
    ChangeWeights {
        chance: f64,
        percent_perturbed: f64,
        standard_deviation: f64,
    },
    /// See [`Mutations::change_activation`].
    ChangeActivation {
        chance: f64,
        activation_pool: Vec<Activation>,
    },
    /// See [`Mutations::add_node`].
    AddNode {
        chance: f64,
        activation_pool: Vec<Activation>,
    },
    /// See [`Mutations::add_connection`].
    AddConnection { chance: f64 },
    /// See [`Mutations::add_recurrent_connection`].
    AddRecurrentConnection { chance: f64 },
    /// See [`Mutations::remove_node`].
    RemoveNode { chance: f64 },
    /// See [`Mutations::remove_connection`].
    RemoveConnection { chance: f64 },
    /// See [`Mutations::remove_recurrent_connection`].
    RemoveRecurrentConnection { chance: f64 },
    /// See [`Mutations::duplicate_node`].
    DuplicateNode { chance: f64 },
}

impl Mutations {
    /// Mutate a [`Genome`] but respects the associate `chance` field of the [`Mutations`] enum variants.
    /// The user needs to supply some RNG manually when using this method directly.
    /// Use [`crate::Genome::mutate`] as the default API.
    pub fn mutate(&self, genome: &mut Genome, rng: &mut impl Rng) -> MutationResult {
        match self {
            &Mutations::ChangeWeights {
                chance,
                percent_perturbed,
                standard_deviation,
            } => {
                if rng.gen::<f64>() < chance {
                    Self::change_weights(percent_perturbed, standard_deviation, genome, rng);
                }
            }
            Mutations::AddNode {
                chance,
                activation_pool,
            } => {
                if rng.gen::<f64>() < *chance {
                    Self::add_node(activation_pool, genome, rng)
                }
            }
            &Mutations::AddConnection { chance } => {
                if rng.gen::<f64>() < chance {
                    return Self::add_connection(genome, rng);
                }
            }
            &Mutations::AddRecurrentConnection { chance } => {
                if rng.gen::<f64>() < chance {
                    return Self::add_recurrent_connection(genome, rng);
                }
            }
            Mutations::ChangeActivation {
                chance,
                activation_pool,
            } => {
                if rng.gen::<f64>() < *chance {
                    Self::change_activation(activation_pool, genome, rng)
                }
            }
            &Mutations::RemoveNode { chance } => {
                if rng.gen::<f64>() < chance {
                    return Self::remove_node(genome, rng);
                }
            }
            &Mutations::RemoveConnection { chance } => {
                if rng.gen::<f64>() < chance {
                    return Self::remove_connection(genome, rng);
                }
            }
            &Mutations::RemoveRecurrentConnection { chance } => {
                if rng.gen::<f64>() < chance {
                    return Self::remove_recurrent_connection(genome, rng);
                }
            }
            &Mutations::DuplicateNode { chance } => {
                if rng.gen::<f64>() < chance {
                    return Self::duplicate_node(genome, rng);
                }
            }
        }
        Ok(())
    }
}