|
| 1 | +use std::cmp::min; |
| 2 | + |
1 | 3 | use noirc_abi::input_parser::InputValue;
|
2 | 4 | use rand::Rng;
|
3 | 5 | use rand_xorshift::XorShiftRng;
|
4 | 6 |
|
5 | 7 | use crate::mutation::configurations::{SpliceMutation, BASIC_SPLICE_MUTATION_CONFIGURATION};
|
6 | 8 |
|
7 |
| -use super::configurations::{SpliceCandidate, BASIC_SPLICE_CANDIDATE_PRIORITIZATION_CONFIGURATION}; |
8 |
| -struct ArraySplicer<'a> { |
| 9 | +use super::configurations::{ |
| 10 | + SpliceCandidate, StructuralMutation, BASIC_SPLICE_CANDIDATE_PRIORITIZATION_CONFIGURATION, |
| 11 | + BASIC_STRUCTURE_MUTATION_CONFIGURATION, BASIC_VECTOR_STRUCTURE_MUTATION_CONFIGURATION, |
| 12 | +}; |
| 13 | +struct ArrayMutator<'a> { |
9 | 14 | prng: &'a mut XorShiftRng,
|
10 | 15 | }
|
11 | 16 |
|
12 |
| -impl<'a> ArraySplicer<'a> { |
| 17 | +impl<'a> ArrayMutator<'a> { |
13 | 18 | pub fn new(prng: &'a mut XorShiftRng) -> Self {
|
14 | 19 | Self { prng }
|
15 | 20 | }
|
@@ -99,13 +104,108 @@ impl<'a> ArraySplicer<'a> {
|
99 | 104 | };
|
100 | 105 | InputValue::Vec(result)
|
101 | 106 | }
|
| 107 | + |
| 108 | + /// Perform one of structural mutations on the vector of input values |
| 109 | + pub fn perform_structure_mutation_on_vector( |
| 110 | + &mut self, |
| 111 | + input_buffer: &Vec<InputValue>, |
| 112 | + ) -> Vec<InputValue> { |
| 113 | + let result = match BASIC_VECTOR_STRUCTURE_MUTATION_CONFIGURATION.select(self.prng) { |
| 114 | + StructuralMutation::ChaoticSelfSplice => { |
| 115 | + self.chaotic_splice(input_buffer, input_buffer) |
| 116 | + } |
| 117 | + StructuralMutation::ChunkDuplication => self.duplicate_chunk(input_buffer), |
| 118 | + StructuralMutation::Swap => self.swap(input_buffer), |
| 119 | + StructuralMutation::RandomValueDuplication => { |
| 120 | + panic!("Vector mutations should have a value duplication weight of zero") |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + result |
| 125 | + } |
| 126 | + |
| 127 | + /// Perform structural mutations on a InputValue that is a vector |
| 128 | + fn perform_structure_mutation(&mut self, input_buffer: &InputValue) -> InputValue { |
| 129 | + let input_vec = match input_buffer { |
| 130 | + InputValue::Vec(internal_vector) => internal_vector, |
| 131 | + _ => panic!("Expect to get a vector input value"), |
| 132 | + }; |
| 133 | + InputValue::Vec(self.perform_structure_mutation_on_vector(input_vec)) |
| 134 | + } |
| 135 | + /// Swap 2 random chunks in the buffer |
| 136 | + fn swap(&mut self, buffer: &Vec<InputValue>) -> Vec<InputValue> { |
| 137 | + let mut result = Vec::new(); |
| 138 | + let buffer_length = buffer.len(); |
| 139 | + |
| 140 | + // We need to leave at least the last byte for the second chunk |
| 141 | + let first_chunk_position = self.prng.gen_range(0..(buffer_length - 1)); |
| 142 | + |
| 143 | + // The second chunk starts after the first |
| 144 | + let second_chunk_position = self.prng.gen_range((first_chunk_position + 1)..buffer_length); |
| 145 | + |
| 146 | + let first_chunk_end = |
| 147 | + self.prng.gen_range((first_chunk_position + 1)..=second_chunk_position); |
| 148 | + |
| 149 | + let second_chunk_end = self.prng.gen_range((second_chunk_position + 1)..=buffer_length); |
| 150 | + |
| 151 | + // Leave the start in place |
| 152 | + result.extend_from_slice(&buffer[0..first_chunk_position]); |
| 153 | + |
| 154 | + // Insert second chunk |
| 155 | + result.extend_from_slice(&buffer[second_chunk_position..(second_chunk_end)]); |
| 156 | + |
| 157 | + // Insert what's in between the chunks |
| 158 | + result.extend_from_slice(&buffer[first_chunk_end..(second_chunk_position)]); |
| 159 | + |
| 160 | + // Insert first chunk |
| 161 | + result.extend_from_slice(&buffer[first_chunk_position..first_chunk_end]); |
| 162 | + |
| 163 | + // Insert the tail |
| 164 | + result.extend_from_slice(&buffer[second_chunk_end..buffer_length]); |
| 165 | + |
| 166 | + result |
| 167 | + } |
| 168 | + |
| 169 | + /// Take a random chunk of the input and insert it several times into the input |
| 170 | + fn duplicate_chunk(&mut self, input_buffer: &Vec<InputValue>) -> Vec<InputValue> { |
| 171 | + let mut result = input_buffer.clone(); |
| 172 | + let buffer_length = input_buffer.len(); |
| 173 | + // The maximum length of the chunk is half the total length |
| 174 | + let maximum_chunk_length = buffer_length / 2; |
| 175 | + |
| 176 | + // Get a random position for the chunk |
| 177 | + let chunk_position = self.prng.gen_range(0..=buffer_length - 1); |
| 178 | + |
| 179 | + // Pick size |
| 180 | + let chunk_size = |
| 181 | + self.prng.gen_range(1..=min(buffer_length - chunk_position, maximum_chunk_length)); |
| 182 | + |
| 183 | + // Find an insertion position with enough space |
| 184 | + let insertion_position = self.prng.gen_range(0..(buffer_length - chunk_size)); |
| 185 | + |
| 186 | + // Determine how many times to repeat |
| 187 | + let maximum_insertion_count = (buffer_length - insertion_position) / chunk_size; |
| 188 | + let insertion_count = self.prng.gen_range(0..=maximum_insertion_count); |
| 189 | + for i in 0..insertion_count { |
| 190 | + result.splice( |
| 191 | + (insertion_position + i * chunk_size)..(insertion_position + (i + 1) * chunk_size), |
| 192 | + input_buffer[chunk_position..(chunk_position + chunk_size)].iter().cloned(), |
| 193 | + ); |
| 194 | + } |
| 195 | + result |
| 196 | + } |
102 | 197 | }
|
103 | 198 |
|
104 | 199 | pub fn splice_array_structure(
|
105 | 200 | first_input: &InputValue,
|
106 | 201 | second_input: &InputValue,
|
107 | 202 | prng: &mut XorShiftRng,
|
108 | 203 | ) -> InputValue {
|
109 |
| - let mut array_splicer = ArraySplicer::new(prng); |
| 204 | + let mut array_splicer = ArrayMutator::new(prng); |
110 | 205 | array_splicer.splice(first_input, second_input)
|
111 | 206 | }
|
| 207 | + |
| 208 | +pub fn mutate_vector_structure(input: &Vec<InputValue>, prng: &mut XorShiftRng) -> Vec<InputValue> { |
| 209 | + let mut array_mutator = ArrayMutator::new(prng); |
| 210 | + array_mutator.perform_structure_mutation_on_vector(input) |
| 211 | +} |
0 commit comments