/
gen.py
615 lines (504 loc) · 21.9 KB
/
gen.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# -*- coding: utf-8 -*-
'''
Pohjakoodia geneettisen algoritmin projektiin
Kehitysideoita:
pienempiä:
- tallennus/lataus (yksittäisen otuksen tai koko sukupolven)
- gui-härpäkkeitä (asetuksia, tilastoja ym.)
- asetuksia
- otusten määrä ja tyyppi
- ruoan määrä
- kartan koko
- vuoden pituus
- genomin koko
- algoritmi
- haju (otus 'haistaa' onko lähellä ruokaa)
- erilaisia ruoan asetteluita ja asetuksia
- määrä
- onko ruoan määrä aina sama (eli ilmestyykö uutta kun vanhaa syödään)
- ruokaa vain tietyillä alueilla
- ruoka on ryppäissä
- DONE vesi,seinät,
- DONE kartan generointia, karttaeditori tai kartan tuonti kuvasta
- kartassa useampia tasoja
- lentävät otukset
- uivat otukset(joita rannoilla kävelevät syövät)
- DONE vesi(erikseen uinti ja kävely eteenpäin)
- erilaiset ruoat(hidastaa, pysäyttää, sekoittaa, ym.)
- toisia otuksia syövät otukset (oma Generation)
- samassa maailmassa kaksi eri sukupolvea jotka kehittyvät eri algoritmeilla
- eri algoritmeja
isompia:
- parempi algoritmi
- pieni skriptikieli jolla voisi kirjoitella omia otuksia
- sen sijaan että optimoitaisiin yksittäisen otuksen fitnessiä, vertailtaisiin sukupolvia ja valittaisiin jatkoon se joka söi yhteensä eniten
- voisi yrittää saada olioita tekemään jotain yksinkertaisia tehtäviä esim. kuljeta esineitä 'pesään', ole tietyllä alueella. Näissä pitäisi keksiä miten oliot pisteytetään.
- otusten välinen kommunikointi(lähetä oma tila)
- hiiriohjaus
- DONE ruokaa tähän
- näytä olion tiedot
- siirrä oliota
- zoomaus
- ym.
'''
'''
Created on Jun 16, 2011
@author: anttir
'''
import sys, time
from PySide.QtCore import *
from PySide.QtGui import *
from random import randint
from random import choice
import csv
#from creature import *#Creature, CreatureLabel, Generation
from world import *
from food import *
from globals import *
import terrain
import io
from statisticsWindow import StatisticsWindow
#Todo: from statisticsWindow import *
#import creature
class MainWindow(QMainWindow):
def __init__(self):
self.running = True
self.highscore = 0
QMainWindow.__init__(self)
self.setWindowTitle('Geneettinen algoritmi')
self.mousePos = (0, 0)
self.menubar = QMenuBar(self)
self.setMenuBar(self.menubar)
self.statusbar = QStatusBar(self)
self.setStatusBar(self.statusbar)
self.status = QLabel('DAYS')
self.statusbar.addWidget(self.status)
self.fileMenu = self.menuBar().addMenu('&File')
self.load = self.fileMenu.addAction('Load')
self.save = self.fileMenu.addAction('Save')
self.fileMenu.addSeparator()
self.quit = self.fileMenu.addAction('Quit')
self.quit.triggered.connect(app.quit)
self.load.triggered.connect(self.loadA)
self.save.triggered.connect(self.saveA)
self.editMenu = self.menuBar().addMenu('&Edit')
self.fastest = self.editMenu.addAction('Fastest')
self.fast = self.editMenu.addAction('Fast')
self.normal = self.editMenu.addAction('Normal')
self.slow = self.editMenu.addAction('Slow')
self.pause = self.editMenu.addAction('Pause')
self.editMenu.addSeparator()
self.settings = self.editMenu.addAction('Settings')
self.statisticMenu = self.menuBar().addMenu('&Statistics')
self.showStatistics = self.statisticMenu.addAction('Show statistics')
self.exportStatistics = self.statisticMenu.addAction('Export statistics')
self.showStatistics.triggered.connect(self.showStatisticsA)
self.exportStatistics.triggered.connect(self.exportStatisticsA)
self.fastest.triggered.connect(self.fastestA)
self.fast.triggered.connect(self.fastA)
self.normal.triggered.connect(self.normalA)
self.slow.triggered.connect(self.slowA)
self.pause.triggered.connect(self.pauseA)
self.settings.triggered.connect(self.settingsA)
self.terrainGenerator = terrain.NoiseMapGenerator(width=WIDTH+1, height=HEIGHT+1)
#self.terrainGenerator = terrain.DrunkardTerrainGenerator(width=WIDTH+1, height=HEIGHT+1)
self.world = WorldLabel() if USE_GRAPHICS else World(False)
if USE_GRAPHICS:
self.setCentralWidget(self.world)
self.adjustSize()
self.paused=False
self.makeNewTerrain(terrainGenerator=self.terrainGenerator)
self.gen = Generation(SCALEDPOPULATION,self.world)
if GAMEOFLIFE:
self.foodgen = FoodGeneration(SCALEDFOOD, self.world)
self.world.populate(self.gen, self.foodgen)
else:
self.world.populate(self.gen)
self.year = 1
self.day = 1
self.timer = QTimer(self)
self.timer.timeout.connect(self.doTurn)
self.timer.start(1)
self.statWindow = None
self.AddFoodAct = QAction("&Add food",self,
statusTip="Add food to location",
triggered=self.AddFood)
self.whatsHereAct = QAction(
"&Whats Here?", self,
statusTip="Click to see whats under cursor!",
triggered=self.whatsHere)
self.saveCreatureToFileAct = QAction("&Save creature", self,
statusTip="Save creature to modify it",
triggered=self.saveCreatureToFile)
self.releaseCreatureAct = QAction("&Release creature", self,
statusTip="Release creature",
triggered=self.releaseCreature)
#stats
self.statEaten = []
self.statWalked = []
self.statAverageEaten = []
self.statAverageWalked = []
self.maximum = 0
self.totalEaten = 0
self.totalWalked = 0
self.average =0.0
def showStatisticsA(self, Statistics=None):
if not self.statWindow:
self.statWindow = StatisticsWindow(self)
self.statWindow.show()
def closeStatWindow(self):
self.statWindow = None
def exportStatisticsA(self,statistics=None):
if self.statWindow:
self.statWindow.exportStats()
else:
StatisticsWindow(self).exportStats()
def getStatisticString(self):
string = ""
i = 1
for i in range(len(self.statEaten)):
string += "year: " + str(i+1) + "\n"
string += "Total eaten : " + str(self.statEaten[i]) + "\n"
string += "Total walked :" + str(self.statWalked[i])
string += "\n"
string += "\n"
return string
def contextMenuEvent(self, event):
pos = QCursor.pos()
pos = self.mapFromGlobal(pos)
loc = pos.toTuple()
h = self.size().height()
w = self.size().width()
location = (loc[0] / GRIDSIZE, loc[1] / GRIDSIZE - 2)
self.mousePos = location
menu = QMenu(self)
self.DockAct = QAction("&Open cmd", self, triggered = self.showdoc)
self.PauseAct = QAction("&Pause",self,
statusTip="Pause algorithm",
triggered=self.pauseA)
self.SlowAct = QAction("&Slow", self,
statusTip="Set mode for Slow",
triggered=self.slowA)
self.NormalAct = QAction("&Normal", self,
statusTip="Set mode for Normal",
triggered=self.normalA)
self.FastAct = QAction("&Fast", self,
statusTip="Set mode for Fast",
triggered=self.fastA)
self.FastestAct = QAction("&Fastest", self,
statusTip="Set mode for Fastest",
triggered=self.fastestA)
self.saveCreatureToFileAct = QAction("&Save creature", self,
statusTip="Save creature to modify it",
triggered=self.saveCreatureToFile)
self.releaseCreatureAct = QAction("&Release creature", self,
statusTip="Release creature",
triggered=self.releaseCreature)
SpeedMenu = menu.addMenu("&Mode")
SpeedMenu.addAction(self.PauseAct)
SpeedMenu.addAction(self.SlowAct)
SpeedMenu.addAction(self.NormalAct)
SpeedMenu.addAction(self.FastAct)
SpeedMenu.addAction(self.FastestAct)
menu.addAction(self.AddFoodAct)
menu.addAction(self.whatsHereAct)
menu.addAction(self.DockAct)
menu.addAction(self.saveCreatureToFileAct)
menu.addAction(self.releaseCreatureAct)
menu.exec_(event.globalPos())
def makeNewTerrain(self, terrainGenerator=None, filename=None):
"""
Luo uuden maaston, mutta ei uutta maailmaa
Arguments:
- `terrainGenerator`: which terrainGenerator you want to use
- `filename`: if you want to load terrain from file, specify it here
- If you specify both filename and terrainGenerator, terrain will be loaded
from file
"""
if filename is None and isinstance(terrainGenerator, terrain.DrunkardTerrainGenerator) or isinstance(terrainGenerator, terrain.NoiseMapGenerator):
self.world.makeTerrain(terrainGenerator)
elif isinstance(filename, str):
self.world.loadTerrain(filename)
def AddFood(self):
location = self.mousePos
print location
if not self.world.USE_GRAPHICS:
self.world.addFood(Food(location))
else:
self.world.addFood(FoodLabel(self.world, location), rando=False)
def whatsHere(self):
location = self.mousePos
if self.world.getFood(location):
obj = mapObject(Food, self)
elif self.world.getCreature(location):
if self.world.getCreature(location).isDead():
obj = mapObject("Corpse", self)
else:
obj = mapObject(Creature, self)
else:
msgBox = QMessageBox()
msgBox.setText("Nothing special here!")
msgBox.exec_()
def saveCreatureToFile(self):
location = self.mousePos
creature = self.world.getCreature(location)
if creature:
self.saveA({location: creature})
else:
print "Missed me HAHAHA >:D"
def releaseCreature(self):
location = self.mousePos
self.loadA(clearFirst=False, location=location)
def closeA(self):
self.running = False
print self.running
def closeEvent(self, event):
self.closeA()
def loadA(self, clearFirst=True, location=None):
dialog = QFileDialog(self, caption="Load creatures")
dialog.setFileMode(QFileDialog.ExistingFile)
dialog.setNameFilter("XML (*.xml)")
dialog.setViewMode(QFileDialog.Detail)
filename = ""
if dialog.exec_():
filename = dialog.selectedFiles()
if filename == "" or filename is None:
pass
elif filename[0]:
with open(filename[0], 'r') as f:
if clearFirst:
self.world.removeCreatures()
io.xmlToCreatures(filename[0], self.world, location)
print "new creatures loaded"
def saveA(self, creaturesToSave=None):
dialog = QFileDialog(self, "Save creatures","creatures.xml")
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setAcceptMode(QFileDialog.AcceptSave)
dialog.setNameFilter("XML (*.xml)")
dialog.setDefaultSuffix(u'xml')
dialog.setViewMode(QFileDialog.Detail)
filename = ""
if dialog.exec_():
filename = dialog.selectedFiles()
if filename == "" or filename is None:
pass
elif filename[0]:
with open(filename[0], 'w') as f:
if creaturesToSave is None:
io.creaturesToXML(self.world.creatures, filename[0])
else:
io.creaturesToXML(creaturesToSave, filename[0])
print "Creature(s) saved to", filename
def fastestA(self):
self.paused = False
if not self.world.USE_GRAPHICS:
return
# global USE_GRAPHICS
# USE_GRAPHICS = False
# self.world.USE_GRAPHICS = False
# print 'changed to no graph'
self.world = self.world.changeToWorld()
creatures = []
for cre in self.world.creatures:
creatures.append(self.world.creatures[cre])
self.gen = Generation(self.gen.size, self.world,creatures)
self.setCentralWidget(None)
self.timer.setInterval(0)
def fastA(self):
self.paused = False
if not self.world.USE_GRAPHICS:
self.changeToGraphics()
self.timer.setInterval(10)
def normalA(self):
self.paused = False
if not self.world.USE_GRAPHICS:
self.changeToGraphics()
self.timer.setInterval(100)
def slowA(self):
self.paused = False
if not self.world.USE_GRAPHICS:
self.changeToGraphics()
self.timer.setInterval(500)
def pauseA(self):
self.paused = True
def settingsA(self):
print 'Not implemented yet'
def changeToGraphics(self):
global USE_GRAPHICS
USE_GRAPHICS = False
self.world = self.world.changeToWorldLabel(self)
creatures = []
for cre in self.world.creatures:
creatures.append(self.world.creatures[cre])
self.gen = Generation(self.gen.size, self.world,creatures)
self.world.update()
def doTurn(self):
if self.paused:
return
if self.day < 200:
i = 0
for cre in self.world.creatures.values():
cre.doTurn()
i += 1
# for food in self.world.foods.values():
# pass
# food.animate()
if self.world.USE_GRAPHICS:
self.world.update()
#realtime stats
self.highscore = self.gen.totalEaten()
self.status.setText('Year: {0:} Day: {1:03d} Total eaten: {2:03d} Maximum: {3:03d} Average: {4:03d}'\
.format(self.year, self.day, self.highscore, self.maximum, int(self.average)))
self.day += 1
else:
self.statEaten.append(self.gen.totalEaten())
self.statWalked.append(self.gen.totalWalked())
self.totalEaten += self.statEaten[-1]
self.totalWalked += self.statWalked[-1]
if self.gen.totalEaten() > self.maximum:
self.maximum = self.gen.totalEaten()
self.statAverageEaten.append(self.totalEaten / self.year)
self.statAverageWalked.append(self.totalWalked / self.year)
self.average = self.statAverageEaten[-1]
self.highscore = self.statEaten[-1]
self.gen = self.gen.nextGeneration()
if GAMEOFLIFE:
self.foodgen = self.foodgen.nextFoodGeneration()
self.world.removeEverything()
self.world.populate(self.gen, self.foodgen)
else:
self.world.removeEverything()
self.world.populate(self.gen)
self.year += 1
self.day = 1
self.status.setText('Year: {0:} Day: {1:03d} Total eaten: {2:03d} Maximum: {3:03d} Average: {4:03d}'\
.format(self.year, self.day, self.highscore, self.maximum, int(self.average)))
if self.statWindow:
self.statWindow.dataChanged()
self.statWindow.draw()
def exec_comand_string(self, string):
if len(string) <= 0:
return ""
if string[0] != '/':
return "Command must start with '/'\n"
string = string[1:]
Parts = string.split(' ')
command = Parts[0]
Args = Parts[1:]
if command == "help":
return self.prthelp()
elif command == "quit":
app.quit()
elif command == "addfood":
try:
self.world.addFood(Food((int(Args[0]), int(Args[1]))) if not self.world.USE_GRAPHICS else FoodLabel(self, (int(Args[0]), int(Args[1]))))
except:
return "Unknown parameters in command" +string
elif command == "addcreature":
try:
heading = None
if Args[2] == "NORTH":
heading = (0, -1)
elif Args[2] == "EAST":
heading = (1, 0)
elif Args[2] == "SOUTH":
heading = (0, 1)
elif Args[2] == "WEST":
heading = (-1, 0)
else:
return "Unknown parameters in command" +string
if len(Args) == 3:
creature = Creature((int(Args[0]), int(Args[1]), heading, self.world))
self.world.addCreature(creature)
elif len(Args) == 4:
creature = Creature((int(Args[0]), int(Args[1])), heading, self.world, Args[4])
self.world.addCreature(creature)
except:
return "Unknown parameters in command" + string
else:
return "Unknown command " + command
def prthelp(self):
return ("Available commands:\n"
"/help | Prints available commands and instructions to use commandline.\n"\
"/quit | Shuts down gen.py.\n"
"/addfood x y | Adds food to given location. Parameters x and y must be integers in range (0, 40).\n"
"/deleteall | Erases every object from the map.\n"
"/addcreature x y heading genome| Adds new creature defined by given genome to location (x, y) with heading towards given parameter.\n"
" | Given genome must be proper tuple as defined in creature constructor or genome can be given as a empty parameter,\n"
" | thus genome is constructed by random. Heading must be one of the following: NORTH, EAST, SOUTH, WEST.\n")
def showdoc(self):
self.cmd=Cmd(self)
class Cmd(QWidget):
def __init__(self, parent=None):
super(Cmd, self).__init__(parent)
super(Cmd, self).setWindowFlags(Qt.Window)
self.commands = []
self.currentcommand = 0
self.parent = parent
self.commandList = QListWidget(self)
self.commandList.addItem(parent.prthelp())
self.input = QLineEdit(self)
self.button = QPushButton("&Enter", self)
self.button.clicked.connect(self.button_click_event)
layout = QVBoxLayout()
layout.addWidget(self.commandList)
layout.addWidget(self.input)
layout.addWidget(self.button)
self.setLayout(layout)
self.show()
def button_click_event(self):
self.commandList.addItem(self.input.text())
self.commands[len(self.commands)]=self.input.text()
self.commands.append("")
text = self.parent.exec_comand_string(self.input.text())
self.commandList.addItem(text)
self.input.setText("")
self.currentcommand = len(self.commands)
def keyPressEvent(self, event):
if event.key() == 16777220:
self.commandList.addItem(self.input.text())
self.commands.append("")
self.commands[len(self.commands)-1]=self.input.text()
text = self.parent.exec_comand_string(self.input.text())
self.commandList.addItem(text)
self.input.setText("")
self.currentcommand = len(self.commands)
if event.key() == 16777235:
self.currentcommand -=1
self.currentcommand = self.currentcommand % len(self.commands)
self.input.setText(self.commands[self.currentcommand])
if event.key() == 16777237:
self.currentcommand -=1
self.currentcommand = self.currentcommand % len(self.commands)
self.input.setText(self.commands[self.currentcommand])
class mapObject(QWidget):
def __init__(self, type, parent=None):
super(mapObject, self).__init__(parent)
super(mapObject, self).setWindowFlags(Qt.Window)
self.parent = parent
self.lbl = QLabel()
self.label = QLabel()
if type == Creature:
self.lbl.setPixmap(QPixmap.fromImage(CREATURE_PIC[0]))
self.label.setText("Its a creature")
if type == "Corpse":
self.lbl.setPixmap(QPixmap.fromImage(CORPSE_PIC))
self.label.setText("Its a dead creature :(")
if type == Food:
self.lbl.setPixmap(QPixmap.fromImage(FOOD_PIC))
self.label.setText("Its food")
layout = QVBoxLayout()
layout.addWidget(self.lbl)
layout.addWidget(self.label)
self.setLayout(layout)
self.adjustSize()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
import cProfile
command = """app.exec_()"""
cProfile.runctx(command, globals(), locals(), filename="ga.profile" )
#app.exec_()
sys.exit()