-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutation.py
54 lines (46 loc) · 1.19 KB
/
Mutation.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
import random
def main():
gene_str = input("enter genes in binary representation")
genes = list(gene_str)
mutation_prob = float(input("enter mutation probability"))
mutation_prob_vector = list()
n = len(genes)
for i in range(n):
random_prob = random.random()
if random_prob > mutation_prob:
mutation_prob_vector.append(1)
else:
mutation_prob_vector.append(0)
final_genes = list()
for i in range(n):
old_gene = genes[i]
if mutation_prob_vector[i] == 1:
if old_gene == '1':
new_gene = "0"
else:
new_gene = "1"
else:
new_gene = old_gene
final_genes.append(new_gene)
print("old genes")
print(arr_to_str(genes))
print("prob vector")
print(arr_to_str(mutation_prob_vector))
print("new genes")
print(arr_to_str(final_genes))
def arr_to_str(arr):
string = ""
for item in arr:
string += str(item)
return string
main()
'''
enter genes in binary representation1100110110110011
enter mutation probability0.63
old genes
1100110110110011
prob vector
1100000100010001
new genes
0000110010100010
'''