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
use crate::{genes::Activation, mutations::Mutations}; use config::{Config, ConfigError, File}; use serde::{Deserialize, Serialize}; /// This struct captures configuration about the basic ANN structure and [available mutations]. /// /// It can be constructed manually or from a `.toml` file. /// /// # Examples /// /// Manual: /// ``` /// use set_genome::{Parameters, Structure, Mutations, activations::Activation}; /// /// let parameters = Parameters { /// seed: None, /// structure: Structure { /// inputs: 25, /// inputs_connected_percent: 1.0, /// outputs: 3, /// outputs_activation: Activation::Tanh, /// weight_std_dev: 0.1, /// weight_cap: 1.0, /// }, /// mutations: vec![ /// Mutations::ChangeWeights { /// chance: 1.0, /// percent_perturbed: 0.5, /// }, /// Mutations::ChangeActivation { /// chance: 0.05, /// activation_pool: vec![ /// Activation::Linear, /// Activation::Sigmoid, /// Activation::Tanh, /// Activation::Gaussian, /// Activation::Step, /// Activation::Sine, /// Activation::Cosine, /// Activation::Inverse, /// Activation::Absolute, /// Activation::Relu, /// ], /// }, /// Mutations::AddNode { /// chance: 0.005, /// activation_pool: vec![ /// Activation::Linear, /// Activation::Sigmoid, /// Activation::Tanh, /// Activation::Gaussian, /// Activation::Step, /// Activation::Sine, /// Activation::Cosine, /// Activation::Inverse, /// Activation::Absolute, /// Activation::Relu, /// ], /// }, /// Mutations::RemoveNode { chance: 0.001 }, /// Mutations::AddConnection { chance: 0.1 }, /// Mutations::AddRecurrentConnection { chance: 0.01 }, /// ], /// }; /// ``` /// /// Write a config file like so: /// ```toml /// [structure] /// inputs = 9 /// outputs = 2 /// inputs_connected_percent = 1.0 /// outputs_activation = "Tanh" /// weight_std_dev = 0.1 /// weight_cap = 1.0 /// /// [[mutations]] /// type = "add_connection" /// chance = 0.1 /// /// [[mutations]] /// type = "add_recurrent_connection" /// chance = 0.01 /// /// [[mutations]] /// type = "add_node" /// chance = 0.005 /// activation_pool = [ /// "Sigmoid", /// "Tanh", /// "Relu", /// "Linear", /// "Gaussian", /// "Step", /// "Sine", /// "Cosine", /// "Inverse", /// "Absolute", /// ] /// /// [[mutations]] /// type = "remove_node" /// chance = 0.001 /// /// [[mutations]] /// type = "change_weights" /// chance = 1.0 /// percent_perturbed = 0.5 /// /// [[mutations]] /// type = "change_activation" /// chance = 0.05 /// activation_pool = [ /// "Sigmoid", /// "Tanh", /// "Relu", /// "Linear", /// "Gaussian", /// "Step", /// "Sine", /// "Cosine", /// "Inverse", /// "Absolute", /// ] /// ``` /// And then read the file: /// /// ```text /// // let parameters = Parameters::new("path/to/file"); /// ``` /// /// [available mutations]: `Mutations` /// #[derive(Deserialize, Serialize, Debug, Clone)] pub struct Parameters { /// Seed for the RNG. pub seed: Option<u64>, /// Describes basic structure of the ANN. pub structure: Structure, /// List of mutations that execute on [`crate::Genome::mutate_with_context`] pub mutations: Vec<Mutations>, } impl Default for Parameters { fn default() -> Self { Self { seed: Some(42), structure: Structure::default(), mutations: vec![ Mutations::ChangeWeights { chance: 1.0, percent_perturbed: 0.5, }, Mutations::ChangeActivation { chance: 0.05, activation_pool: vec![ Activation::Linear, Activation::Sigmoid, Activation::Tanh, Activation::Gaussian, Activation::Step, Activation::Sine, Activation::Cosine, Activation::Inverse, Activation::Absolute, Activation::Relu, ], }, Mutations::AddNode { chance: 0.005, activation_pool: vec![ Activation::Linear, Activation::Sigmoid, Activation::Tanh, Activation::Gaussian, Activation::Step, Activation::Sine, Activation::Cosine, Activation::Inverse, Activation::Absolute, Activation::Relu, ], }, Mutations::AddConnection { chance: 0.1 }, Mutations::AddRecurrentConnection { chance: 0.01 }, ], } } } /// This struct describes the invariants of the ANN structure. #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Structure { /// Number of input nodes. pub inputs: usize, /// Percent of input nodes initially connected to all poutput nodes. pub inputs_connected_percent: f64, /// Number of output nodes. pub outputs: usize, /// Activation function for all output nodes. pub outputs_activation: Activation, /// Standard deviation of a normal distribution that provides samples for weight perturbations. pub weight_std_dev: f64, /// Constrains connection weights to the range [-weight_cap, weight_cap]. pub weight_cap: f64, } impl Default for Structure { fn default() -> Self { Self { inputs: 1, inputs_connected_percent: 1.0, outputs: 1, outputs_activation: Activation::Tanh, weight_std_dev: 0.1, weight_cap: 1.0, } } } impl Parameters { pub fn new(path: &str) -> Result<Self, ConfigError> { let mut s = Config::new(); // Start off by merging in the "default" configuration file s.merge(File::with_name(path))?; // You can deserialize (and thus freeze) the entire configuration as s.try_into() } }