-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutations.py
65 lines (48 loc) · 2.03 KB
/
mutations.py
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
from random import *
def flip_mutation(chromosome: list[int], size: int) -> list[int]:
"""
Flip Mutation chooses a random gene in the chromosome and flip it. If the gene is 0 it will be 1, and vice versa.
"""
index = randint(0, size-1)
# print(f"Index = {index}")
chromosome[index] = 1 - chromosome[index]
return chromosome
def flip_mutation_mask(chromosome: list[int], size: int) -> list[int]:
"""
Generate a mask of 0s and 1s, then flip the chromosome according to it. If the mask ith gene is 1, the chromosome's
ith gene will be flipped. the mask ith gene is 0, it won't change anything.
"""
temp = [randint(0, 1) for _ in range(size)]
# print(f"Temp chromosome = {temp}")
for i in range(size):
if(temp[i] == 1): chromosome[i] = 1 - chromosome[i]
return chromosome
# Note: This method is used for decimal numbers!!!
def divide_mutation(chromosome: list[int], size: int) -> list[int]:
"""
Choose a random gene in the chromosome and change its valur by dividing it by 2.
"""
index = randint(0, size-1)
# print(f"Index = {index}")
chromosome[index] = int(chromosome[index] / 2)
return chromosome
# Note: This method is used for decimal numbers!!!
def random_reset_mutation(chromosome: list[int], size: int) -> list[int]:
"""
Choose a random gene and change its value by a value in the same range of chromosome values.
"""
index = randint(0, size-1)
# print(f"Index = {index}")
chromosome[index] = randint(1, size)
return chromosome
'''
binary_chromosome = [1, 0, 1, 0, 1, 1, 0, 1]
dicimal_chromosome = [1, 3, 5, 7, 8, 2, 4, 6]
print(f"Before = {binary_chromosome}, After = {flip_mutation(binary_chromosome, 8)}")
print("\n")
print(f"Before = {binary_chromosome}, After = {flip_mutation_randomly(binary_chromosome, 8)}")
print("\n")
print(f"Before = {dicimal_chromosome}, After = {divide_mutation(dicimal_chromosome, 8)}")
print("\n")
print(f"Before = {dicimal_chromosome}, After = {replace_mutation(dicimal_chromosome, 8)}")
'''