-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (116 loc) · 3.97 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from Individual import Individual
import random
import json
import eel
import matplotlib.pyplot as plt
import math
POPULATION_SIZE = 1000
eel.init("frontend")
@eel.expose
def main():
address = "./levels/level8.txt"
with open(address) as reader :
mylevel = reader.read()
# mylevel = "____G_MLGL_G_"
generation = 1
population = []
avgfit = []
maxx = []
minn = []
solve = False
initial(POPULATION_SIZE , population , mylevel)
population = sorted(population, key = lambda x:x.fitness)
check = checkSolution(mylevel)
avgfit.append(0)
avg = 0
for i in population:
avg += i.fitness
avg = avg / len(population)
avgfit.append(avg)
maxx.append(population[-1].fitness)
minn.append(population[0].fitness)
while avgfit[-1] - avgfit[-2] > 0.00001 or avgfit[-1] < 0 :
new_generation = []
s = int((4*POPULATION_SIZE)/100)
new_generation.extend(population[-1*s :])
s = int((48*POPULATION_SIZE)/100)
s2 = int((20*POPULATION_SIZE)/100)
for _ in range(s):
parent1 = random.choice(population[-1*s2:])
parent2 = random.choice(population[-1*s2:])
(child1, child2) = parent1.crossover(parent2)
new_generation.append(child1)
new_generation.append(child2)
maxx.append(population[-1].fitness)
minn.append(population[0].fitness)
population = new_generation
avg = 0
for i in population:
avg += i.fitness
avg = avg / len(population)
avgfit.append(avg)
population = sorted(population, key = lambda x:x.fitness)
# =============================== print every generation's information ========================
# [print("{} , {}, {}".format(i.chromosome , i.fitness , generation)) for i in population]
generation += 1
changed = changeAnswerForMap(population[-1].chromosome)
# for i in avgfit:
# print(i , end=" ")
# =============================== show chart ==================================================
# chart(range(generation),avgfit[1::] , maxx , minn)
return get_json_result({
"map" : list(mylevel),
"answer" : changed,
"hasAnswer" : check
},
)
def checkSolution(level):
for i in range(len(level)-1):
if level[i] == 'G' and level[i+1] == 'L':
return False
return True
def chart(x , y , yx , ym ):
plt.scatter(x, yx, label= "stars", color= "red", marker= "*", s=50)
plt.scatter(x, ym, label= "stars", color= "green", marker= "*", s=50)
plt.plot(x, y)
plt.xlabel('generation')
plt.ylabel('fittnes')
plt.title('genetic algorithm ')
plt.show()
def initial(size , population , mylevel):
Individual.level = mylevel
Individual.CHROMOSOME_LENGTH = len(mylevel)
for _ in range(size):
chromosome = Individual.create_chromosome()
population.append(Individual(chromosome))
def get_json_result(results):
return json.dumps(results)
def changeAnswerForMap(answer):
changed = []
checkContinue = True
for i in range(len(answer)):
if(checkContinue):
if answer[i] == 0 :
changed.append(0)
elif answer[i] == 2:
changed.append(2)
changed.append(0)
changed.append(5)
elif answer[i] == 1:
if(i == len(answer) -1):
changed.append(1)
changed.append(0)
changed.append(4)
else:
changed.append(1)
changed.append(0)
changed.append(0)
changed.append(4)
checkContinue = False
else:
checkContinue = True
return changed
# if __name__ == "__main__":
# execute only if run as a script
main()
eel.start('index.html' ,size=(500,500))