-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
440 lines (362 loc) · 13.4 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import numpy as np
import random
import copy
import math
import time
#mody ovladajuce program
class Modes:
def __init__(self, print: bool, opt: bool, path: bool):
self.NONE = 0
self.PRINT = print
self.OPTIMAL = opt
self.PATH = path
self.MINE = 3
self.ROULETTE = 4
self.ELITEROULETTE = 5
class Specimen:
def __init__(self, chromozome):
self.fitness = 0
self.num_found = 0
self.chromozome = chromozome
self.path = None
#inicializujem nahodny chromozome
def initchromozome(self):
self.chromozome = np.zeros(64, dtype=np.uint8)
for i in range(64):
self.chromozome[i] = random.getrandbits(8)
def print(self):
print("Fit: " + str(round(self.fitness, 3)) + " Pok: " + str(self.num_found) + " Dlz: " + str(
int(len(self.path) / 2)) + " | " + self.path)
#utility funkcia pri triedeni
def getfit(self):
return self.fitness
#dve funkcie na pocitanie fitness
def calcfit(self):
if m.PATH:
if len(self.path) == 0:
self.fitness = 0
else:
res = (0.75 / _map.num_treasures) * self.num_found
res += (0.25 / (len(self.path) / 2)) * self.num_found
self.fitness = res
else:
self.fitness = (1 / _map.num_treasures) * self.num_found
#kazdy gen ma danu % sancu na mutaciu
def mutate(self, max_mutations):
for i in range(64):
chance = random.random() * 100
if chance < max_mutations:
self.chromozome[i] = random.getrandbits(8)
class Point:
def __init__(self, y, x):
self.x = x
self.y = y
#virtualny stroj definovany podla zadania
class Machine:
def __init__(self, start, gen):
self.arr = copy.deepcopy(gen)
self.path = ""
self.position = copy.deepcopy(start)
self.num_found = 0
def generate(self):
i = 0
#ukazovatel
actual = 0
while i < 500 and self.num_found != _map.num_treasures:
instruction = self.arr[actual] >> 6
address = self.arr[actual] & 0b00111111
if instruction == 0:
self.inc(address)
elif instruction == 1:
self.dec(address)
elif instruction == 2:
actual = address
else:
#ak som vyskocil z hracej mriezky
if self.move(address) == -1:
break
if _map.pole[self.position.y][self.position.x]:
self.num_found += 1
_map.pole[self.position.y][self.position.x] = False
actual += 1
if actual % 64 == 0:
actual = 0
i += 1
#inkrementujem cyklicky tak, aby som nemenil instrukciu
def inc(self, address):
if (self.arr[address] & 0b00111111) == 63:
self.arr[address] &= 0b11000000
else:
self.arr[address] += 1
# dekrementujem cyklicky tak, aby som nemenil instrukciu
def dec(self, address):
if (self.arr[address] & 0b00111111) == 0:
self.arr[address] |= 0b00111111
else:
self.arr[address] += 1
#pri vypise rovno robim pohyb, modifikujem startovaciu poziciu stroja
def move(self, address):
#orientujem sa podla poslednych dvoch bitov
val = self.arr[address] & 0b00000011
#idem dole
if val == 0:
self.path += "D "
self.position.y += 1
if self.position.y == _map.MAXY:
return -1
#idem vpravo
elif val == 1:
self.path += "P "
self.position.x += 1
if self.position.x == _map.MAXX:
return -1
#idem hore
elif val == 2:
self.path += "H "
self.position.y -= 1
if self.position.y == -1:
return -1
#idem vlavo
else:
self.path += "L "
self.position.x -= 1
if self.position.x == -1:
return -1
return 0
def cross_breed(j1, j2):
sizes = [3, 5, 7, 11, 15]
output = np.zeros(64, dtype=np.uint8)
part = 0
switch = True
for i in range(64):
#beriem si random casti
if part == 0:
#taky random aby som nevybehol z pola
part = sizes[random.randint(0, 4)]
#na striedacku beriem z jedneho a druheho
switch = not switch
if switch:
output[i] = j1.chromozome[i]
else:
output[i] = j2.chromozome[i]
part -= 1
return output
#utility funkcia do popolvara :D
def generate_path(cesta):
cesta += "x"
x = _map.start_pos.x
y = _map.start_pos.y
for c in cesta:
if c == " ":
continue
print(str(x) + " " + str(y))
if c == "P":
x += 1
if c == "H":
y -= 1
if c == "D":
y += 1
if c == "L":
x -= 1
class Population:
def __init__(self, num_gen, num_spec, num_mut):
self.generation = None
self.elite = None
self.num_gen = num_gen
self.num_spec = num_spec
self.num_mut = num_mut
#elite je vzdy 5% populacie
self.num_elite = int(math.ceil((num_spec / 100) * 5))
def initialize(self):
i = 0
self.generation = []
self.elite = []
while i < self.num_spec:
_map.reset_treasures()
specimen = Specimen(None)
specimen.initchromozome()
m1 = Machine(_map.start_pos, specimen.chromozome)
m1.generate()
specimen.path = m1.path
specimen.num_found = m1.num_found
specimen.calcfit()
self.generation.append(specimen)
i += 1
self.generation.sort(key=Specimen.getfit)
if MODE == m.MINE:
for i in range(self.num_spec - self.num_elite, self.num_spec):
self.elite.append(self.generation[i])
if m.PRINT:
self.print_top()
if MODE == m.MINE:
self.print_elite()
def print_top(self):
for i in range(self.num_spec - 10, self.num_spec):
self.generation[i].print()
def store_elite(self):
# ukladam unikatnu elitu
for i in range(self.num_spec - self.num_elite, self.num_spec):
for j in range(0, self.num_elite):
if self.elite[j].path == self.generation[i].path:
break
if self.elite[j].fitness < self.generation[i].fitness:
self.elite[j] = copy.deepcopy(self.generation[i])
break
def print_elite(self):
print("ELITA-----")
for x in self.elite:
x.vypis()
def create_elite_specimen(self):
count = 0
# prvych x bude elite x top10%
top10 = int(math.ceil(9 * self.num_spec / 10))
for i in range(top10, self.num_spec):
for j in range(0, self.num_elite):
specimen = Specimen(cross_breed(self.generation[i], self.elite[j]))
specimen.mutate(self.num_mut)
self.generation[count] = specimen
count += 1
remainder = self.num_spec - count
# dalsich x bude vyskladanych z 75% - 90% najlepsich
for i in range(remainder):
s1 = random.randint(int(3 * self.num_spec / 4), int(math.ceil(9 * self.num_spec / 10)))
s2 = random.randint(int(3 * self.num_spec / 4), int(math.ceil(9 * self.num_spec / 10)))
specimen = Specimen(cross_breed(self.generation[s1], self.generation[s2]))
specimen.mutate(self.num_mut)
self.generation[count] = specimen
count += 1
def create_specimen_roulette(self):
sumf = 0
newgen = []
count = 0
for x in self.generation:
sumf += x.fitness
if MODE == m.ELITEROULETTE:
for i in range(self.num_spec - self.num_elite, self.num_spec):
newgen.append(copy.deepcopy(self.generation[i]))
count += 1
for j in range(self.num_spec - count):
p1 = self.get_roulette_parent(sumf)
p2 = self.get_roulette_parent(sumf)
new_spec = Specimen(cross_breed(p1, p2))
new_spec.mutate(self.num_mut)
newgen.append(new_spec)
self.generation = newgen
def get_roulette_parent(self, sumf):
chance = random.random() * sumf
partf = 0
for i in range(self.num_spec - 1, -1, -1):
partf += self.generation[i].fitness
if partf > chance:
return self.generation[i]
return self.generation[0]
#mapa je 2D arr False hodnot, kde True su len poklady
class Map:
def __init__(self, maxy, maxx, sy, sx):
self.pole = None
self.MAXX = maxx
self.MAXY = maxy
self.num_treasures = 0
self.treasures_positions = []
self.start_pos = Point(sy, sx)
def initialize(self):
self.MAXX = 7
self.MAXY = 7
self.pole = np.full((self.MAXX, self.MAXY), False)
#definujem suradnice pokladov
self.treasures_positions.append(Point(5, 4))
self.treasures_positions.append(Point(4, 1))
self.treasures_positions.append(Point(3, 6))
self.treasures_positions.append(Point(2, 2))
self.treasures_positions.append(Point(1, 4))
self.num_treasures = len(self.treasures_positions)
#po kazdom prejdeni obnovujem poklady
def reset_treasures(self):
for sur in self.treasures_positions:
self.pole[sur.y][sur.x] = True
def solve():
global averages
population.initialize()
gen_counter = 1
optimal = copy.deepcopy(population.generation[population.num_spec - 1])
while gen_counter < population.num_gen:
if m.PRINT:
print("------ Generacia: " + str(gen_counter) + ", " + str(len(population.generation)))
if MODE == m.MINE:
population.create_elite_specimen()
elif MODE == m.ROULETTE or MODE == m.ELITEROULETTE:
population.create_specimen_roulette()
#generovanie rieseni
for i in range(population.num_spec):
_map.reset_treasures()
m1 = Machine(_map.start_pos, population.generation[i].chromozome)
m1.generate()
population.generation[i].path = m1.path
population.generation[i].num_found = m1.num_found
population.generation[i].calcfit()
population.generation.sort(key=Specimen.getfit)
#ukladam optimalneho jedinca ak je zvolene
if m.OPTIMAL and population.generation[population.num_spec - 1].fitness > optimal.fitness:
optimal = copy.deepcopy(population.generation[population.num_spec - 1])
#ukladam elitu pre moje riesenie
if MODE == m.MINE:
population.store_elite()
#ak som nasiel riesenie a nehladam optimalne tak koncim
if not m.OPTIMAL and population.generation[population.num_spec - 1].num_found == _map.num_treasures:
print("Riesenie najdene v generacii " + str(gen_counter))
population.generation[population.num_spec - 1].print()
# generujcestu(population.generation[population.num_spec - 1].path)
return gen_counter
#vypisujem top 10 jedincov
if m.PRINT:
population.print_top()
if MODE == m.MINE:
population.print_elite()
gen_counter += 1
#ked skoncili vsetky generacie vypisujem najoptimalnejsie riesenie
if m.OPTIMAL:
print("Najoptimalnejsie riesenie:")
optimal.vypis()
if optimal.num_found == 5:
averages.append(len(optimal.path) / 2)
else:
averages.append(0)
# generujcestu(population.elite[0].path)
return gen_counter
def run():
f = open("vystup.txt", "a")
global averages
_map.initialize()
average = 0
avg_count = 0
xxx = 0
totalt = 0
while xxx < num_repeats:
bt = time.time()
print("Beh " + str(xxx))
found = solve()
et = time.time()
if population.generation[population.num_spec - 1].num_found != _map.num_treasures:
print("Riesenie nebolo najdene")
else:
avg_count += 1
totalt += et - bt
average += found
xxx += 1
print("Vysledok nebol najdeny " + str(xxx - avg_count) + " krat z " + str(xxx) + " pokusov")
f.write("Vysledok nebol najdeny " + str(xxx - avg_count) + " krat z " + str(xxx) + " pokusov")
if avg_count == 0:
avg_count = 1
print("Vysledok najdeny priemerne v generacii: " + str(average / avg_count))
print("Priemerne trvanie: " + str(totalt / avg_count))
f.close()
#vypisy, optimalne hladanie, path
m = Modes(False, False, False)
MODE = m.MINE
#pocetGeneracii, num_spec, max%Mutacii
population = Population(200, 100, 1.5)
#maxY, maxX, startY, startX
_map = Map(7, 7, 6, 3)
num_repeats = 1
averages = []
run()