-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNatural_Selection.py
54 lines (44 loc) · 1.34 KB
/
Natural_Selection.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
import numpy as np
def join(a, b):
ar = []
for i in range(2):
for j in range(6):
select_value = random.random()
if select_value <= 0.55:
ar.append(a[i][j])
else:
ar.append(b[i][j])
ar = np.array(ar)
ar = ar.reshape(2, 6)
return ar
def new_population(fitness):
new_pop = []
first = fitness[0][2].get_weights()
second = fitness[1][2].get_weights()
third = fitness[2][2].get_weights()
fourth = fitness[3][2].get_weights()
new_pop.append(first)
new_pop.append(second)
# print(first, second)
new_pop.append(join(first, second))
for i in range(20):
r1 = random.randint(0, 10)
r2 = random.randint(0, 19)
new_pop.append(join(fitness[r1][2].get_weights(), fitness[r2][2].get_weights()))
'''
for i in range(2):
new_pop.append(join(first, second))
for i in range(2):
new_pop.append(join(first, third))
for i in range(2):
new_pop.append(join(second, third))
for i in range(2):
new_pop.append(join(first, fourth))
for i in range(2):
new_pop.append(join(second, fourth))
for i in range(2):
new_pop.append(join(third, fourth))
'''
# print(new_pop)
return new_pop