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
use *;
/// Represents various mutation strategies for modifying an individual's genetic material.
///
/// In genetic algorithms, mutation is used to prevent the population from stagnating at a local
/// minima or maxima and introduces novel gene sequences that could potentially lead to better
/// solutions. By introducing small, random tweaks to the genetic material, mutation can help
/// explore new areas of the solution space that may not have been reachable via crossover alone.
///
/// Depending on the nature of the specific problem and the algorithm used, different mutation
/// strategies can be more effective. Therefore, it is often beneficial to have several mutation
/// strategies available.
/// Performs a swap mutation on the given genome.
///
/// This mutation operator is effective at making small changes in the solution structure,
/// allowing for local search and fine-tuning of the solution.
///
/// # Arguments
/// * `genome` - The sequence of genes to be mutated. This could represent a solution in the
/// problem space.
/// * `rng` - A mutable reference to a random number generator implementing the Rng trait
///
/// # Note
/// Swap mutation is applicable to permutation-based problems.
///
/// # Example
/// ```
/// use rusty_genes::*;
/// use rand::prelude::*;
///
/// let mut rng = SmallRng::seed_from_u64(999);
/// let mut genome = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
///
/// swap_mutation(&mut genome, &mut rng);
///
/// assert_eq!(genome, vec![0, 1, 6, 3, 4, 5, 2, 7, 8, 9]);
/// ```
/// Shuffles the elements within a randomly selected subset of the given genetic material.
///
/// This mutation operator is useful for introducing larger-scale changes in the solution,
/// promoting exploration and diversity in the population.
///
/// # Arguments
/// * `genome` - The sequence of genes to be mutated. This could represent a solution in the
/// problem space.
/// * `rng` - A mutable reference to a random number generator implementing the Rng trait
///
/// # Note
/// Scramble mutation is applicable to permutation-based problems.
///
/// # Example
/// ```
/// use rusty_genes::*;
/// use rand::prelude::*;
///
/// let mut rng = SmallRng::seed_from_u64(999);
/// let mut genome = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
///
/// scramble_mutation(&mut genome, &mut rng);
///
/// assert_eq!(genome, vec![0, 5, 6, 1, 2, 3, 4, 7, 8, 9]);
/// ```
/// Reverses the order of the elements within a randomly selected subset of the given genetic
/// material.
///
/// This mutation operator can introduce both small and large changes in the solution, balancing
/// exploration and exploitation in the search process.
///
/// # Arguments
/// * `genome` - The sequence of genes to be mutated. This could represent a solution in the
/// problem space.
/// * `rng` - A mutable reference to a random number generator implementing the Rng trait
///
/// # Note
/// Inversion mutation is applicable to permutation-based problems.
///
/// # Example
/// ```
/// use rusty_genes::*;
/// use rand::prelude::*;
///
/// let mut rng = SmallRng::seed_from_u64(999);
/// let mut genome = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
///
/// inversion_mutation(&mut genome, &mut rng);
///
/// assert_eq!(genome, vec![0, 6, 5, 4, 3, 2, 1, 7, 8, 9]);
/// ```
/// Removes a gene from a random position in the genome and insert it in another position. This
/// results in the shifting of the rest of the genes between two positions.
///
/// The mutation operator can introduce a small local rearrangement of genes which can be particularly
/// useful in permutation-based problems where the relative ordering of genes is important.
///
/// # Arguments
/// * `genome` - The sequence of genes to be mutated. This could represent a solution in the
/// problem space.
/// * `rng` - A mutable reference to a random number generator implementing the Rng trait
///
/// # Note
/// Insertion mutation is applicable to permutation-based problems.
///
/// # Example
/// ```
/// use rusty_genes::*;
/// use rand::prelude::*;
///
/// let mut rng = SmallRng::seed_from_u64(999);
/// let mut genome = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
///
/// insertion_mutation(&mut genome, &mut rng);
///
/// assert_eq!(genome, vec![0, 1, 6, 2, 3, 4, 5, 7, 8, 9]);
/// ```
/// Selects a gene from a random position in the genome and copies it to another position, replacing
/// the gene that was previously there.
///
/// This mutation operator can introduce variation into the genome, and can be useful in problems where
/// duplications of certain genes may lead to a better solution.
///
/// # Arguments
/// * `genome` - The sequence of genes to be mutated. This could represent a solution in the
/// problem space.
/// * `rng` - A mutable reference to a random number generator implementing the Rng trait
///
/// # Note
/// Duplicate mutation is not applicable to permutation-based problems where each gene should be unique.
///
/// # Example
/// ```
/// use rusty_genes::*;
/// use rand::prelude::*;
///
/// let mut rng = SmallRng::seed_from_u64(999);
/// let mut genome = vec!['G', 'T', 'A', 'G', 'C'];
///
/// duplicate_mutation(&mut genome, &mut rng);
///
/// assert_eq!(genome, vec!['G', 'T', 'A', 'A', 'C']);
/// ```