-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAmethods.py
83 lines (69 loc) · 1.64 KB
/
GAmethods.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import string
import operator
import matplotlib.pyplot as plt
standard='The quick brown fox jumps over the lazy dog'
N=len(standard)
pop=50
mu = 0.02
letters = ' '+string.ascii_letters+string.punctuation
def get_word():
word=''
for i in range(N):
word += letters[np.random.randint(len(letters))]
return word
def calculate_fitness(word):
count=0
for j in range(N):
if word[j] == standard[j]:
count+=1
return count
def f(x):
y=[]
for i in range(len(x)):
y.append(calculate_fitness(x[i]))
return y
#relative fitness
def g(y):
z=[]
total_fitness = sum(y)
for i in y:
try:
z.append(i/total_fitness)
except:
z.append(0)
return z
def initialise():
x=[]
for i in range(pop):
x.append(get_word())
return x
def crossover(x,z):
for i in range(len(x)):
child=[]
a = np.random.randint(pop)
b = np.random.randint(pop)
cut_point = (int)(N/2)
A = x[a][:cut_point]
B = x[a][cut_point:]
C = x[b][:cut_point]
D = x[b][cut_point:]
child.append(A+D)
child.append(C+B)
child.append(D+A)
child.append(B+C)
cy = f(child)
x[i] = child[operator.indexOf(cy,max(cy))]
return x
def calculate_mut(p):
p_lst=[]
for i in p:
p_lst.append(i)
for i in range(len(p_lst)):
if np.random.rand()<mu:
p_lst[i] = letters[np.random.randint(len(letters))]
return ''.join(p_lst)
def mutation(x):
for i in range(len(x)):
x[i] = calculate_mut(x[i])
return x