forked from langerv/sugarscape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwdgAgent.py
More file actions
81 lines (69 loc) · 2.75 KB
/
wdgAgent.py
File metadata and controls
81 lines (69 loc) · 2.75 KB
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
'''
Created on 2010-05-23
@author: rv
'''
from Tkinter import *
class WdgAgent():
'''
classdocs
'''
def __init__(self, metabolismMean, visionMean, title = "Agents' metabolism and vision means", width = 600, height = 300):
'''
Constructor
'''
self.root = Tk()
self.root.title(title)
self.canvas = Canvas(self.root, width = width, height = height)
self.canvas.pack()
self.width = width
self.height = height
# Create axis
x0 = 20
y0 = self.height - 20
self.X = [0, y0, self.width, y0]
self.Y = [x0, self.height, x0, 0]
incry = float(self.height - 100) / 10
# Create metabolism mean series
seriesItr = self.createFormatSeries(x0, y0, self.width, self.height, 1, incry, metabolismMean)
self.metabolismSeries = []
while True:
try:
self.metabolismSeries.append(seriesItr.next())
except StopIteration:
break
# Create vision mean series
seriesItr = self.createFormatSeries(x0, y0, self.width, self.height, 1, incry, visionMean)
self.visionSeries = []
while True:
try:
self.visionSeries.append(seriesItr.next())
except StopIteration:
break
# Add text coordinates for metabolism
self.canvas.create_text(x0 + 10, y0 - metabolismMean[0] * incry, text = metabolismMean[0], fill = 'blue', anchor = SW)
self.canvas.create_text(self.metabolismSeries[-1][-2], y0 - 10 - metabolismMean[-1] * incry, text = metabolismMean[-1], fill = 'blue', anchor = SW)
# Add text coordinates for vision
self.canvas.create_text(x0 + 10, y0 - visionMean[0] * incry, text = visionMean[0], fill = 'red', anchor = SW)
self.canvas.create_text(self.visionSeries[-1][-2], y0 - 10 - visionMean[-1] * incry, text = visionMean[-1], fill = 'red', anchor = SW)
# Generator that formats data in series
def createFormatSeries(self, xmin, ymin, xmax, ymax, dx, dy, data):
curve = []
x = xmin
for datum in data:
curve.append(x)
curve.append(ymin - datum * dy)
x += dx
if x >= xmax:
yield curve
curve = []
x = xmin
yield curve
# Display widget
def execute(self):
self.canvas.create_line(*self.X, arrow = LAST)
self.canvas.create_line(*self.Y, arrow = LAST)
for curve in self.metabolismSeries:
self.canvas.create_line(*curve, fill = 'blue')
for curve in self.visionSeries:
self.canvas.create_line(*curve, fill = 'red')
self.root.mainloop()