eckity.genetic_operators.mutations.vector_random_mutation
1from random import random 2 3from eckity.genetic_operators.mutations.vector_n_point_mutation import ( 4 VectorNPointMutation, 5) 6 7""" 8This module contains the implementation of various vector mutations 9The key difference between these mutations is the way they generate the new 10value for the mutated cell (mut_val_getter). 11 12Additional parameters are used to control the mutation process, such as 13the way of selecting cells to mutate (cell_selector), 14the way of checking if the new value is legal (success_checker). 15For additional information, see the VectorNPointMutation class. 16 17Standard parameters for the mutation are number of mutation points (n), 18the probability of the operator itself to occur (probability), 19the probability of each point to be mutated (probability_for_each), 20the number of attempts to be made, etc. 21""" 22 23 24class FloatVectorUniformOnePointMutation(VectorNPointMutation): 25 """ 26 Uniform One Point Float Mutation. 27 Mutates a single cell of a float vector. 28 Mutated value is drawn from a uniform distribution, 29 with respect to the bounds of the vector. 30 31 Parameters 32 ---------- 33 probability : float, optional 34 probability of the operator to occur, by default 1.0 35 arity : int, optional 36 individuals required for the mutation, by default 1 37 events : List[str], optional 38 custom events to be published by the mutation, by default None 39 """ 40 41 def __init__(self, probability=1.0, arity=1, events=None): 42 super().__init__( 43 n=1, 44 probability=probability, 45 arity=arity, 46 mut_val_getter=lambda vec, index: vec.get_random_number_in_bounds( 47 index 48 ), 49 events=events, 50 ) 51 52 53class FloatVectorUniformNPointMutation(VectorNPointMutation): 54 """ 55 Uniform N Point Float Mutation. 56 Mutates exactly n cells of a float vector. 57 Mutated values are drawn from a uniform distribution, 58 with respect to the bounds of the vector. 59 60 Parameters 61 ---------- 62 probability : float, optional 63 probability of the operator to occur, by default 1.0 64 arity : int, optional 65 individuals required for the mutation, by default 1 66 events : List[str], optional 67 custom events to be published by the mutation, by default None 68 """ 69 70 def __init__(self, n=1, probability=1.0, arity=1, events=None): 71 super().__init__( 72 n=n, 73 probability=probability, 74 arity=arity, 75 mut_val_getter=lambda vec, index: vec.get_random_number_in_bounds( 76 index 77 ), 78 events=events, 79 ) 80 81 82class FloatVectorGaussOnePointMutation(VectorNPointMutation): 83 """ 84 Gaussian One Point Float Mutation. 85 Mutates a single cell of a float vector. 86 Mutated value is drawn from a Gaussian 87 distribution with mean mu and standard deviation sigma. 88 89 The mutation is repeated until the new value is legal 90 (within the bounds of the vector), or the number of attempts 91 is exceeded. 92 93 Parameters 94 ---------- 95 probability : float, optional 96 probability of the operator to occur, by default 1.0 97 arity : int, optional 98 individuals required for the mutation, by default 1 99 mu : float, optional 100 Gaussian distribution mean value, by default 0.0 101 sigma : float, optional 102 Gaussian distribution std value, by default 1.0 103 events : List[str], optional 104 custom events to be published by the mutation, by default None 105 attempts : int, optional 106 number of attempts till failure, by default 5 107 """ 108 109 def __init__( 110 self, 111 probability=1.0, 112 arity=1, 113 mu=0.0, 114 sigma=1.0, 115 events=None, 116 attempts=5, 117 ): 118 super().__init__( 119 n=1, 120 probability=probability, 121 arity=arity, 122 mut_val_getter=lambda vec, index: vec.get_random_number_with_gauss( 123 index, mu, sigma 124 ), 125 events=events, 126 attempts=attempts, 127 ) 128 129 def on_fail(self, payload): 130 """ 131 Handle gauss mutation failure by performing uniform mutation. 132 """ 133 mut = FloatVectorUniformOnePointMutation( 134 self.probability, self.arity, self.events 135 ) 136 return mut.apply_operator(payload) 137 138 139class FloatVectorGaussNPointMutation(VectorNPointMutation): 140 """ 141 Gaussian N Point Float Mutation. 142 Mutates exactly n cells of a float vector. 143 Mutated values are drawn from a Gaussian 144 distribution with mean mu and standard deviation sigma. 145 146 The mutation is repeated until the new value is legal 147 (within the bounds of the vector), or the number of attempts 148 is exceeded. 149 150 Parameters 151 ---------- 152 probability : float, optional 153 probability of the operator to occur, by default 1.0 154 arity : int, optional 155 individuals required for the mutation, by default 1 156 mu : float, optional 157 Gaussian distribution mean value, by default 0.0 158 sigma : float, optional 159 Gaussian distribution std value, by default 1.0 160 events : List[str], optional 161 custom events to be published by the mutation, by default None 162 attempts : int, optional 163 number of attempts till failure, by default 5 164 """ 165 166 def __init__( 167 self, 168 n=1, 169 probability=1.0, 170 arity=1, 171 mu=0.0, 172 sigma=1.0, 173 events=None, 174 attempts=5, 175 ): 176 super().__init__( 177 n=n, 178 probability=probability, 179 arity=arity, 180 mut_val_getter=lambda vec, index: vec.get_random_number_with_gauss( 181 index, mu, sigma 182 ), 183 events=events, 184 attempts=attempts, 185 ) 186 187 def on_fail(self, payload): 188 """ 189 Handle gauss mutation failure by performing uniform mutation. 190 """ 191 mut = FloatVectorUniformNPointMutation( 192 self.n, self.probability, self.arity, self.events 193 ) 194 return mut.apply_operator(payload) 195 196 197class IntVectorOnePointMutation(VectorNPointMutation): 198 """ 199 Uniform one point Int mutation. 200 Mutates a single cell of a int vector. 201 Mutated value is drawn from a uniform distribution, 202 with respect to the bounds of the vector. 203 204 Parameters 205 ---------- 206 probability : float, optional 207 probability of the operator to occur, by default 1.0 208 arity : int, optional 209 individuals required for the mutation, by default 1 210 events : List[str], optional 211 custom events to be published by the mutation, by default None 212 """ 213 214 def __init__(self, probability=0.5, arity=1, events=None, probability_for_each=0.1): 215 self.probability_for_each = probability_for_each 216 super().__init__(probability=probability, 217 arity=arity, 218 mut_val_getter=lambda individual, index: individual.get_random_number_in_bounds( 219 index) if random() <= self.probability_for_each else individual.cell_value(index), 220 events=events, cell_selector=lambda vec: list(range(vec.size()))) 221 222 223class IntVectorNPointMutation(VectorNPointMutation): 224 """ 225 Uniform N point Int mutation. 226 Mutates exactly n cells of a int vector. 227 Mutated value is drawn from a uniform distribution, 228 with respect to the bounds of the vector. 229 230 Parameters 231 ---------- 232 probability : float, optional 233 probability of the operator to occur, by default 1.0 234 arity : int, optional 235 individuals required for the mutation, by default 1 236 events : List[str], optional 237 custom events to be published by the mutation, by default None 238 """ 239 240 def __init__(self, probability=1.0, arity=1, events=None, n=1): 241 super().__init__( 242 probability=probability, 243 arity=arity, 244 mut_val_getter=lambda individual, index: individual.get_random_number_in_bounds( 245 index 246 ), 247 events=events, 248 n=n, 249 ) 250 251 252class BitStringVectorFlipMutation(VectorNPointMutation): 253 """ 254 One Point Bit-Flip Mutation 255 Flips a single bit of a bit vector. 256 257 Parameters 258 ---------- 259 probability : float, optional 260 probability of the operator to occur, by default 1.0 261 arity : int, optional 262 individuals required for the mutation, by default 1 263 events : List[str], optional 264 custom events to be published by the mutation, by default None 265 """ 266 267 def __init__(self, probability=1.0, arity=1, events=None): 268 super().__init__( 269 probability=probability, 270 arity=arity, 271 mut_val_getter=lambda individual, index: individual.bit_flip( 272 index 273 ), 274 n=1, 275 events=events, 276 ) 277 278 279class BitStringVectorNFlipMutation(VectorNPointMutation): 280 """ 281 Multiple Bit-Flip Mutation 282 Traverses the bit vector and flips each bit with a certain probability 283 (probability_for_each). 284 Note that this mutation is not guaranteed to flip an exact number of bits, 285 as the flipping is done with a certain probability. 286 287 Parameters 288 ---------- 289 probability : float, optional 290 probability of the operator to occur, by default 1.0 291 arity : int, optional 292 individuals required for the mutation, by default 1 293 events : List[str], optional 294 custom events to be published by the mutation, by default None 295 probability_for_each=0.2 : float, optional 296 probability of flipping each bit, by default 0.2 297 """ 298 299 def __init__( 300 self, 301 probability=1.0, 302 arity=1, 303 events=None, 304 probability_for_each=0.2, 305 n=1, 306 ): 307 self.probability_for_each = probability_for_each 308 super().__init__( 309 probability=probability, 310 arity=arity, 311 mut_val_getter=lambda individual, index: ( 312 individual.bit_flip(index) 313 if random() <= self.probability_for_each 314 else individual.cell_value(index) 315 ), 316 events=events, 317 n=n 318 )
25class FloatVectorUniformOnePointMutation(VectorNPointMutation): 26 """ 27 Uniform One Point Float Mutation. 28 Mutates a single cell of a float vector. 29 Mutated value is drawn from a uniform distribution, 30 with respect to the bounds of the vector. 31 32 Parameters 33 ---------- 34 probability : float, optional 35 probability of the operator to occur, by default 1.0 36 arity : int, optional 37 individuals required for the mutation, by default 1 38 events : List[str], optional 39 custom events to be published by the mutation, by default None 40 """ 41 42 def __init__(self, probability=1.0, arity=1, events=None): 43 super().__init__( 44 n=1, 45 probability=probability, 46 arity=arity, 47 mut_val_getter=lambda vec, index: vec.get_random_number_in_bounds( 48 index 49 ), 50 events=events, 51 )
Uniform One Point Float Mutation. Mutates a single cell of a float vector. Mutated value is drawn from a uniform distribution, with respect to the bounds of the vector.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- events (List[str], optional): custom events to be published by the mutation, by default None
Inherited Members
54class FloatVectorUniformNPointMutation(VectorNPointMutation): 55 """ 56 Uniform N Point Float Mutation. 57 Mutates exactly n cells of a float vector. 58 Mutated values are drawn from a uniform distribution, 59 with respect to the bounds of the vector. 60 61 Parameters 62 ---------- 63 probability : float, optional 64 probability of the operator to occur, by default 1.0 65 arity : int, optional 66 individuals required for the mutation, by default 1 67 events : List[str], optional 68 custom events to be published by the mutation, by default None 69 """ 70 71 def __init__(self, n=1, probability=1.0, arity=1, events=None): 72 super().__init__( 73 n=n, 74 probability=probability, 75 arity=arity, 76 mut_val_getter=lambda vec, index: vec.get_random_number_in_bounds( 77 index 78 ), 79 events=events, 80 )
Uniform N Point Float Mutation. Mutates exactly n cells of a float vector. Mutated values are drawn from a uniform distribution, with respect to the bounds of the vector.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- events (List[str], optional): custom events to be published by the mutation, by default None
Inherited Members
83class FloatVectorGaussOnePointMutation(VectorNPointMutation): 84 """ 85 Gaussian One Point Float Mutation. 86 Mutates a single cell of a float vector. 87 Mutated value is drawn from a Gaussian 88 distribution with mean mu and standard deviation sigma. 89 90 The mutation is repeated until the new value is legal 91 (within the bounds of the vector), or the number of attempts 92 is exceeded. 93 94 Parameters 95 ---------- 96 probability : float, optional 97 probability of the operator to occur, by default 1.0 98 arity : int, optional 99 individuals required for the mutation, by default 1 100 mu : float, optional 101 Gaussian distribution mean value, by default 0.0 102 sigma : float, optional 103 Gaussian distribution std value, by default 1.0 104 events : List[str], optional 105 custom events to be published by the mutation, by default None 106 attempts : int, optional 107 number of attempts till failure, by default 5 108 """ 109 110 def __init__( 111 self, 112 probability=1.0, 113 arity=1, 114 mu=0.0, 115 sigma=1.0, 116 events=None, 117 attempts=5, 118 ): 119 super().__init__( 120 n=1, 121 probability=probability, 122 arity=arity, 123 mut_val_getter=lambda vec, index: vec.get_random_number_with_gauss( 124 index, mu, sigma 125 ), 126 events=events, 127 attempts=attempts, 128 ) 129 130 def on_fail(self, payload): 131 """ 132 Handle gauss mutation failure by performing uniform mutation. 133 """ 134 mut = FloatVectorUniformOnePointMutation( 135 self.probability, self.arity, self.events 136 ) 137 return mut.apply_operator(payload)
Gaussian One Point Float Mutation. Mutates a single cell of a float vector. Mutated value is drawn from a Gaussian distribution with mean mu and standard deviation sigma.
The mutation is repeated until the new value is legal (within the bounds of the vector), or the number of attempts is exceeded.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- mu (float, optional): Gaussian distribution mean value, by default 0.0
- sigma (float, optional): Gaussian distribution std value, by default 1.0
- events (List[str], optional): custom events to be published by the mutation, by default None
- attempts (int, optional): number of attempts till failure, by default 5
110 def __init__( 111 self, 112 probability=1.0, 113 arity=1, 114 mu=0.0, 115 sigma=1.0, 116 events=None, 117 attempts=5, 118 ): 119 super().__init__( 120 n=1, 121 probability=probability, 122 arity=arity, 123 mut_val_getter=lambda vec, index: vec.get_random_number_with_gauss( 124 index, mu, sigma 125 ), 126 events=events, 127 attempts=attempts, 128 )
130 def on_fail(self, payload): 131 """ 132 Handle gauss mutation failure by performing uniform mutation. 133 """ 134 mut = FloatVectorUniformOnePointMutation( 135 self.probability, self.arity, self.events 136 ) 137 return mut.apply_operator(payload)
Handle gauss mutation failure by performing uniform mutation.
Inherited Members
140class FloatVectorGaussNPointMutation(VectorNPointMutation): 141 """ 142 Gaussian N Point Float Mutation. 143 Mutates exactly n cells of a float vector. 144 Mutated values are drawn from a Gaussian 145 distribution with mean mu and standard deviation sigma. 146 147 The mutation is repeated until the new value is legal 148 (within the bounds of the vector), or the number of attempts 149 is exceeded. 150 151 Parameters 152 ---------- 153 probability : float, optional 154 probability of the operator to occur, by default 1.0 155 arity : int, optional 156 individuals required for the mutation, by default 1 157 mu : float, optional 158 Gaussian distribution mean value, by default 0.0 159 sigma : float, optional 160 Gaussian distribution std value, by default 1.0 161 events : List[str], optional 162 custom events to be published by the mutation, by default None 163 attempts : int, optional 164 number of attempts till failure, by default 5 165 """ 166 167 def __init__( 168 self, 169 n=1, 170 probability=1.0, 171 arity=1, 172 mu=0.0, 173 sigma=1.0, 174 events=None, 175 attempts=5, 176 ): 177 super().__init__( 178 n=n, 179 probability=probability, 180 arity=arity, 181 mut_val_getter=lambda vec, index: vec.get_random_number_with_gauss( 182 index, mu, sigma 183 ), 184 events=events, 185 attempts=attempts, 186 ) 187 188 def on_fail(self, payload): 189 """ 190 Handle gauss mutation failure by performing uniform mutation. 191 """ 192 mut = FloatVectorUniformNPointMutation( 193 self.n, self.probability, self.arity, self.events 194 ) 195 return mut.apply_operator(payload)
Gaussian N Point Float Mutation. Mutates exactly n cells of a float vector. Mutated values are drawn from a Gaussian distribution with mean mu and standard deviation sigma.
The mutation is repeated until the new value is legal (within the bounds of the vector), or the number of attempts is exceeded.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- mu (float, optional): Gaussian distribution mean value, by default 0.0
- sigma (float, optional): Gaussian distribution std value, by default 1.0
- events (List[str], optional): custom events to be published by the mutation, by default None
- attempts (int, optional): number of attempts till failure, by default 5
167 def __init__( 168 self, 169 n=1, 170 probability=1.0, 171 arity=1, 172 mu=0.0, 173 sigma=1.0, 174 events=None, 175 attempts=5, 176 ): 177 super().__init__( 178 n=n, 179 probability=probability, 180 arity=arity, 181 mut_val_getter=lambda vec, index: vec.get_random_number_with_gauss( 182 index, mu, sigma 183 ), 184 events=events, 185 attempts=attempts, 186 )
188 def on_fail(self, payload): 189 """ 190 Handle gauss mutation failure by performing uniform mutation. 191 """ 192 mut = FloatVectorUniformNPointMutation( 193 self.n, self.probability, self.arity, self.events 194 ) 195 return mut.apply_operator(payload)
Handle gauss mutation failure by performing uniform mutation.
Inherited Members
198class IntVectorOnePointMutation(VectorNPointMutation): 199 """ 200 Uniform one point Int mutation. 201 Mutates a single cell of a int vector. 202 Mutated value is drawn from a uniform distribution, 203 with respect to the bounds of the vector. 204 205 Parameters 206 ---------- 207 probability : float, optional 208 probability of the operator to occur, by default 1.0 209 arity : int, optional 210 individuals required for the mutation, by default 1 211 events : List[str], optional 212 custom events to be published by the mutation, by default None 213 """ 214 215 def __init__(self, probability=0.5, arity=1, events=None, probability_for_each=0.1): 216 self.probability_for_each = probability_for_each 217 super().__init__(probability=probability, 218 arity=arity, 219 mut_val_getter=lambda individual, index: individual.get_random_number_in_bounds( 220 index) if random() <= self.probability_for_each else individual.cell_value(index), 221 events=events, cell_selector=lambda vec: list(range(vec.size())))
Uniform one point Int mutation. Mutates a single cell of a int vector. Mutated value is drawn from a uniform distribution, with respect to the bounds of the vector.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- events (List[str], optional): custom events to be published by the mutation, by default None
215 def __init__(self, probability=0.5, arity=1, events=None, probability_for_each=0.1): 216 self.probability_for_each = probability_for_each 217 super().__init__(probability=probability, 218 arity=arity, 219 mut_val_getter=lambda individual, index: individual.get_random_number_in_bounds( 220 index) if random() <= self.probability_for_each else individual.cell_value(index), 221 events=events, cell_selector=lambda vec: list(range(vec.size())))
Inherited Members
224class IntVectorNPointMutation(VectorNPointMutation): 225 """ 226 Uniform N point Int mutation. 227 Mutates exactly n cells of a int vector. 228 Mutated value is drawn from a uniform distribution, 229 with respect to the bounds of the vector. 230 231 Parameters 232 ---------- 233 probability : float, optional 234 probability of the operator to occur, by default 1.0 235 arity : int, optional 236 individuals required for the mutation, by default 1 237 events : List[str], optional 238 custom events to be published by the mutation, by default None 239 """ 240 241 def __init__(self, probability=1.0, arity=1, events=None, n=1): 242 super().__init__( 243 probability=probability, 244 arity=arity, 245 mut_val_getter=lambda individual, index: individual.get_random_number_in_bounds( 246 index 247 ), 248 events=events, 249 n=n, 250 )
Uniform N point Int mutation. Mutates exactly n cells of a int vector. Mutated value is drawn from a uniform distribution, with respect to the bounds of the vector.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- events (List[str], optional): custom events to be published by the mutation, by default None
Inherited Members
253class BitStringVectorFlipMutation(VectorNPointMutation): 254 """ 255 One Point Bit-Flip Mutation 256 Flips a single bit of a bit vector. 257 258 Parameters 259 ---------- 260 probability : float, optional 261 probability of the operator to occur, by default 1.0 262 arity : int, optional 263 individuals required for the mutation, by default 1 264 events : List[str], optional 265 custom events to be published by the mutation, by default None 266 """ 267 268 def __init__(self, probability=1.0, arity=1, events=None): 269 super().__init__( 270 probability=probability, 271 arity=arity, 272 mut_val_getter=lambda individual, index: individual.bit_flip( 273 index 274 ), 275 n=1, 276 events=events, 277 )
One Point Bit-Flip Mutation Flips a single bit of a bit vector.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- events (List[str], optional): custom events to be published by the mutation, by default None
Inherited Members
280class BitStringVectorNFlipMutation(VectorNPointMutation): 281 """ 282 Multiple Bit-Flip Mutation 283 Traverses the bit vector and flips each bit with a certain probability 284 (probability_for_each). 285 Note that this mutation is not guaranteed to flip an exact number of bits, 286 as the flipping is done with a certain probability. 287 288 Parameters 289 ---------- 290 probability : float, optional 291 probability of the operator to occur, by default 1.0 292 arity : int, optional 293 individuals required for the mutation, by default 1 294 events : List[str], optional 295 custom events to be published by the mutation, by default None 296 probability_for_each=0.2 : float, optional 297 probability of flipping each bit, by default 0.2 298 """ 299 300 def __init__( 301 self, 302 probability=1.0, 303 arity=1, 304 events=None, 305 probability_for_each=0.2, 306 n=1, 307 ): 308 self.probability_for_each = probability_for_each 309 super().__init__( 310 probability=probability, 311 arity=arity, 312 mut_val_getter=lambda individual, index: ( 313 individual.bit_flip(index) 314 if random() <= self.probability_for_each 315 else individual.cell_value(index) 316 ), 317 events=events, 318 n=n 319 )
Multiple Bit-Flip Mutation Traverses the bit vector and flips each bit with a certain probability (probability_for_each). Note that this mutation is not guaranteed to flip an exact number of bits, as the flipping is done with a certain probability.
Parameters
- probability (float, optional): probability of the operator to occur, by default 1.0
- arity (int, optional): individuals required for the mutation, by default 1
- events (List[str], optional): custom events to be published by the mutation, by default None
- probability_for_each=0.2 (float, optional): probability of flipping each bit, by default 0.2
300 def __init__( 301 self, 302 probability=1.0, 303 arity=1, 304 events=None, 305 probability_for_each=0.2, 306 n=1, 307 ): 308 self.probability_for_each = probability_for_each 309 super().__init__( 310 probability=probability, 311 arity=arity, 312 mut_val_getter=lambda individual, index: ( 313 individual.bit_flip(index) 314 if random() <= self.probability_for_each 315 else individual.cell_value(index) 316 ), 317 events=events, 318 n=n 319 )