-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation.py
More file actions
46 lines (42 loc) · 1.39 KB
/
mutation.py
File metadata and controls
46 lines (42 loc) · 1.39 KB
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
import numpy as np
import random as rand
def random_resetting_mutation(parent):
temp = np.copy(parent)
invert = int(rand.uniform(0,11))
while not invert:
invert = int(rand.uniform(0,11))
random_number = rand.uniform(-10,10)
temp[invert] = random_number
return temp
def random_setting_mutation(parent):
temp = np.copy(parent)
invert = int(rand.uniform(0,11))
while not invert:
invert = int(rand.uniform(0,11))
random_number = rand.uniform(-0.5,0.5)
temp[invert] *= random_number
return temp
def swap_mutation(parent):
temp = np.copy(parent)
invert1 = int(rand.uniform(0,10))
while not invert1:
invert1 = int(rand.uniform(0,10))
invert2 = int(rand.uniform(invert1+1,11))
temp[invert1] , temp[invert2] = temp[invert2] , temp[invert1]
return temp
def scramble_mutation(parent):
temp = np.copy(parent)
invert1 = int(rand.uniform(0,10))
while not invert1:
invert1 = int(rand.uniform(0,11))
invert2 = int(rand.uniform(invert1+1,11))
temp[invert1:invert2] = np.random.permutation(temp[invert1:invert2])
return temp
def inversion_mutation(parent):
temp = np.copy(parent)
invert1 = int(rand.uniform(0,10))
while not invert1:
invert1 = int(rand.uniform(0,11))
invert2 = int(rand.uniform(invert1+1,11))
temp[invert1:invert2] = np.flip(temp[invert1:invert2])
return temp