/
main.py
406 lines (338 loc) · 14.7 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
# coding: utf-8
from id3.id3 import ID3Tree
import gtk
#import cairo
import gobject
import pygtk
import random
from operator import indexOf
pygtk.require('2.0')
from sprite import Sprite
from virus import Virus
from cell import Cell
from virus import DEFAULT_WIDTH as VIRUS_WIDTH, DEFAULT_HEIGHT as VIRUS_HEIGHT
from cell import DEFAULT_WIDTH as CELL_WIDTH, DEFAULT_HEIGHT as CELL_HEIGHT
from display import display_simulation
from hud import Hud
from constants import WINDOW_SIZE, TOTAL_VIRUS, MAX_CELLS, TRAIN_CELLS
from constants import CHARACTERISTICS_DICT
from constants import TRAINING_ZONE_LIMIT
virList =[]
cellList =[]
#Lienzo es donde se pintara todo
class Lienzo(gtk.DrawingArea):
def __init__(self, ventana):
super(Lienzo, self).__init__()
#Cambiar el color de fondo de la ventana
self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(0,0,0))
# Pedir el tamano de la ventana
self.set_size_request(WINDOW_SIZE,WINDOW_SIZE)
#Asignar la ventana que recibe de paramentro a la ventana que se
#utilizara en el lienzo
self.ventana=ventana
#expose-event es una propiedad de DrawingArea que le dice como
#dibujares, aqui le decimos que utilize nuestra funcion paint
#para ese evento en vez del que trae por defaul.
self.connect("expose-event", self.paint)
#reconocer cuando oprimes y sueltas el mouse
self.connect("button_press_event",self.button_press)
self.connect("button_release_event",self.button_release)
self.connect("motion_notify_event",self.actualizar_dragged)
self.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.POINTER_MOTION_MASK)
self.hud=Hud()
self.minTimeToNextCell=150
self.maxTimeToNextCell=250
self.ticksToNextCell=random.randint(self.minTimeToNextCell,self.maxTimeToNextCell)
#cells
self.virus=[]
self.cells=[]
self.draggingObject = None
self.objetoSeleccionado=[]
self.currentState="Training"
self.classificationList=["Target","Enemy","Food"]
self.divisionPoints=[]
self.trainingSet=[]
self.trainingZoneLimit=WINDOW_SIZE-100
self.tree = None
self.init_simulation()
def actualizar_dragged(self,widget,event):
if self.draggingObject:
self.draggingObject.posX=event.x
self.draggingObject.posY=event.y
def on_timer(self):
self.update()
return True
def init_simulation(self):
"""Inicializacion de valores"""
self.reset()
gobject.timeout_add(20, self.on_timer)
def run_simulation(self,extra=0):
if self.currentState=="Training":
self.currentState="Running"
for cell in self.cells:
cell.width=20
cell.height=20
cell.velX=random.randint(1,5)/5.0
cell.velY=random.random()
for i in xrange(len(self.divisionPoints)):
if cell.posX+cell.width/2<self.divisionPoints[i]:
if i==0:
cell.posX=random.randint(0,self.divisionPoints[i]-cell.width)
else:
cell.posX=random.randint(self.divisionPoints[i-1],self.divisionPoints[i]-cell.width)
cell.posY=random.randint(WINDOW_SIZE-100+cell.height, WINDOW_SIZE-cell.height)
self.trainingSet.append((cell,self.classificationList[i]))
break
self.cells =[Cell(
random.randint(0,WINDOW_SIZE),
random.randint(0,TRAINING_ZONE_LIMIT-CELL_HEIGHT),
-random.random()*2,0, "NormalCell"
) for i in xrange(MAX_CELLS)]
self.virus =[Virus(
random.randint(0,WINDOW_SIZE-VIRUS_WIDTH),
random.randint(0,TRAINING_ZONE_LIMIT-CELL_HEIGHT),
) for i in xrange(TOTAL_VIRUS)]
print self.trainingSet
#ID3Tree Generation
self.generate_id3()
else:
pass
def generate_id3(self):
self.tree = ID3Tree(
self.classificationList,
CHARACTERISTICS_DICT.keys(),
self.trainingSet
)
self.tree.calculate()
print self.tree.entropyDict
self.tree.build_tree()
self.tree.print_tree()
def classify_cell(self, widget):
for virus in self.virus:
virus.targetCell = random.choice(self.cells)
classification = self.tree.classify(virus.targetCell)
if classification==self.classificationList[0]:
virus.attack()
elif classification==self.classificationList[1]:
virus.defend()
elif classification==self.classificationList[2]:
virus.eat()
elif classification=="Unknown":
virus.analyze()
def reset(self,extra=0):
self.currentState="Training"
self.trainingSet=[]
for i in xrange(len(self.classificationList)):
self.divisionPoints.append((WINDOW_SIZE/len(self.classificationList))*(i+1))
self.cells =[Cell(
random.randint(0,WINDOW_SIZE-CELL_WIDTH),
random.randint(0,WINDOW_SIZE-CELL_HEIGHT),
) for i in xrange(TRAIN_CELLS)]
def update(self):
self.queue_draw()
cellsToPop=[]
for cell in self.cells:
cell.update(self.currentState)
if cell.type=="NormalCell":
if cell.posX+cell.width<0 or (cell.status=="Dead" and len(cell.dyingParticles)<=0):
cellsToPop.append(cell)
for cell in cellsToPop:
self.cells.pop(indexOf(self.cells,cell))
if cell==self.virus[0].targetCell:
self.virus[0].targetCell=None
if self.currentState=="Running":
self.ticksToNextCell-=1
if self.ticksToNextCell<=0:
self.ticksToNextCell=random.randint(self.minTimeToNextCell,self.maxTimeToNextCell)
newCell=Cell(WINDOW_SIZE,
random.randint(0,TRAINING_ZONE_LIMIT-CELL_HEIGHT))
newCell.velX=-random.random()*2
newCell.type="NormalCell"
self.cells.append(newCell)
#update virus
for virus in self.virus:
if not virus.isDead:
virus.update(self.currentState)
if len(self.cells)>0 and virus.targetCell==None:
virus.targetCell=self.cells[len(self.cells)-1]
#This is a temprorary decision function
#Actual classification should do this
self.classify_cell(widget=None)
if virus.is_colliding_with(virus.targetCell):
if not virus.targetCell.status:
if virus.status=="Attacking":
virus.targetCell.status="Dying"
if virus.status=="Eating":
virus.targetCell.status="BeingEaten"
if virus.targetCell.status=="Dead":
virus.targetCell=None
for (cell,type) in self.trainingSet:
for i in xrange(len(self.classificationList)):
if type==self.classificationList[i]:
rightLimit=self.divisionPoints[i]
if i==0:
leftLimit=0
else:
leftLimit=self.divisionPoints[i-1]
break
cell.update(self.currentState,[leftLimit,rightLimit-cell.width,TRAINING_ZONE_LIMIT,WINDOW_SIZE-cell.height])
def paint(self, widget, event):
"""Nuestro metodo de pintado propio"""
#Se crea un widget de cairo que despues se usara para desplegar
#todo en la ventana
cr = widget.window.cairo_create()
#Le decimos a cairo que pinte su widget por primera vez.
cr.set_source_rgb(0,0,0)
cr.paint()
#paint game info
cr.set_source_rgb(1,1,1)
cr.save()
cr.move_to(15,15)
text="To next cell: %d" % (self.ticksToNextCell)
cr.show_text(text)
cr.restore()
#pintar a los agentes
if self.currentState=="Training":
for point in self.divisionPoints:
cr.set_source_rgb(1,1,1)
cr.move_to(point, 15)
cr.line_to(point,WINDOW_SIZE-15)
cr.set_line_width(0.6)
cr.stroke()
for i in xrange(len(self.classificationList)):
text=str(self.classificationList[i])
if i==0:
posXText=(self.divisionPoints[i])/2-(len(text)/2)*5
else:
posXText=(self.divisionPoints[i-1]+self.divisionPoints[i])/2-(len(text)/2)*5
posYText=15
cr.save()
cr.move_to(posXText,posYText)
cr.set_source_rgba(1,1,1,0.7)
cr.show_text(text)
cr.restore()
display_simulation(cr,[],self.cells)
self.hud.display_cells(cr,self.cells)
self.hud.display_viruses(cr, [])
if self.currentState=="Running":
cr.set_source_rgb(1,1,1)
cr.move_to(15, WINDOW_SIZE-100)
cr.line_to(WINDOW_SIZE-15,WINDOW_SIZE-100)
cr.set_line_width(0.6)
cr.stroke()
for point in self.divisionPoints:
cr.set_source_rgb(1,1,1)
cr.move_to(point, WINDOW_SIZE-85)
cr.line_to(point,WINDOW_SIZE-15)
cr.set_line_width(0.6)
cr.stroke()
for i in xrange(len(self.classificationList)):
text=str(self.classificationList[i])
if i==0:
posXText=(self.divisionPoints[i])/2-(len(text)/2)*5
else:
posXText=(self.divisionPoints[i-1]+self.divisionPoints[i])/2-(len(text)/2)*5
posYText=TRAINING_ZONE_LIMIT+15
cr.save()
cr.move_to(posXText,posYText)
cr.set_source_rgba(1,1,1,0.7)
cr.show_text(text)
cr.restore()
for (cell,type) in self.trainingSet:
cell.paint(cr)
display_simulation(cr,self.virus,self.cells)
self.hud.display_cells(cr,self.cells)
self.hud.display_viruses(cr, self.virus)
#pintar efecto de selección sobre un agente
if self.objetoSeleccionado:
cr.set_line_width(2)
cr.set_source_rgba(random.random(), 1, random.random(), 0.3)
cr.rectangle(self.objetoSeleccionado.posX-20,self.objetoSeleccionado.posY-20,
self.objetoSeleccionado.width+40, self.objetoSeleccionado.height+40)
cr.stroke()
#coso
if self.currentState == "Running":
if self.virus[0].status == "Defending":
cr.set_line_width(2)
cr.set_source_rgba(1, random.random(), random.random(), 0.7)
cr.arc(self.virus[0].posX+25,self.virus[0].posY+25, random.randint(40, 60),0, 360)
cr.stroke()
#Para drag & drop
def button_press(self,widget,event):
if event.button == 1:
self.objetoSeleccionado=[]
lstTemp = self.virus+self.cells
for ob in lstTemp:
if ob.drag(event.x,event.y):
self.draggingObject = ob
self.objetoSeleccionado=ob
break
def button_release(self,widget,event):
if self.draggingObject:
self.draggingObject.drop(event.x,event.y)
self.draggingObject = None
def pausar(self):
self.corriendo=False
def correr(self):
self.corriendo=True
# def mainloop(self):
# while self.corriendo:
# # Process all pending events.
# self.update()
# while gtk.events_pending():
# gtk.main_iteration(False)
# # Generate an expose event (could just draw here as well).
# self.queue_draw()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Main(gtk.Window):
def __init__(self):
super(Main, self).__init__()
self.set_title('Biokterii')
self.set_size_request(WINDOW_SIZE,WINDOW_SIZE+20)
self.set_resizable(True)
self.set_position(gtk.WIN_POS_CENTER)
#mainBox contiene el menu superior, contentBox(menu,lienzo) y el menu inferior
self.mainBox = gtk.VBox(False,0)
self.mainBox.set_size_request(WINDOW_SIZE,WINDOW_SIZE)
#contentBox contiene el menu lateral y lienzo
self.contentBox= gtk.HBox(False,0) #Recibe False para no se homogeneo
self.lienzo=Lienzo(self)
self.lienzo.set_size_request(WINDOW_SIZE+20,WINDOW_SIZE)
self.contentBox.pack_start(self.lienzo, expand=True, fill=True, padding=0)
#Menu bar
menuBar = gtk.MenuBar()
filemenu = gtk.Menu()
filem = gtk.MenuItem("Actions")
filem.set_submenu(filemenu)
annealMenu = gtk.MenuItem("Reset & Train")
annealMenu.connect("activate", self.lienzo.reset)
filemenu.append(annealMenu)
annealMenu = gtk.MenuItem("Start Simulation")
annealMenu.connect("activate", self.lienzo.run_simulation)
filemenu.append(annealMenu)
annealMenu = gtk.MenuItem("Test random cell")
annealMenu.connect("activate", self.lienzo.classify_cell)
filemenu.append(annealMenu)
exit = gtk.MenuItem("Exit")
exit.connect("activate", gtk.main_quit)
filemenu.append(exit)
menuBar.append(filem)
menuBox = gtk.HBox(False, 2)
menuBox.pack_start(menuBar, False, False, 0)
#Empaquetado de todos los controles
self.mainBox.pack_start(menuBox,expand=True,fill=True,padding=0)
self.mainBox.pack_start(self.contentBox,expand=True, fill=True, padding=0)
#Agregar la caja que contiene todo a la ventana
self.add(self.mainBox)
self.connect("destroy", gtk.main_quit)
self.show_all()
def pausar_lienzo(self, widget):
self.lienzo.pausar()
def correr_lienzo(self, widget):
self.lienzo.correr()
# def cerrar_lienzo(self,widget):
# self.lienzo.corriendo=False
# gtk.main_quit
if __name__ == '__main__':
Main()
gtk.main()