-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
52 lines (41 loc) · 1.41 KB
/
test.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
import DarwinPy
import numpy as np
def hammingDist(goal, matrix):
result = []
for i in range(len(matrix)):
temp = 0.
for j in range(len(goal)):
if goal[j] == matrix[i][j]:
temp += 1.
result.append(temp)
return result
if __name__ == "__main__":
mouse_population = 5
mutation_rate = 0.5
search_space = (0,1)
goal = np.array([1,0,1,1,0,1,1, 0,1,1,0],int)
chromosome_length = len(goal)
mouse_species = DarwinPy.Genetics.Genetics(chromosome_length,
mouse_population, (0,1), int)
print(f"mouse species instantiated:\n {mouse_species}")
print("the goal:\n {}".format(goal))
mouse_species.populate()
print(f"get mouse matrix(GA):\n {mouse_species.getChromosomeMatrix()}")
fitness_vector = np.array(
hammingDist(goal,mouse_species.getChromosomeMatrix()),
float)
print(f"get fitness vector: {fitness_vector}")
is_goal = False
gen = 1
while is_goal == False:
print(f"Generation #{gen}\n")
gen += 1
mouse_species.evolve(fitness_vector,
mutation_rate, 0.5)
print(f"get mouse matrix(GA):\n {mouse_species.getChromosomeMatrix()}")
fitness_vector = np.array(
hammingDist(goal,mouse_species.getChromosomeMatrix()),
float)
print(f"get fitness vector: {fitness_vector}")
if chromosome_length in fitness_vector:
is_goal = True