-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_animate.py
More file actions
165 lines (97 loc) · 3.34 KB
/
GUI_animate.py
File metadata and controls
165 lines (97 loc) · 3.34 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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 14:12:33 2019
@author: gynjkm
"""
#importing packages and telling python to use TkAgg
import matplotlib
matplotlib.use('TkAgg')
from bs4 import BeautifulSoup
import tkinter
import requests
import matplotlib.pyplot
import matplotlib.animation
import math
import agentframework
import csv
import random
#requesting web data
r = requests.get('http://www.geog.leeds.ac.uk/courses/computing/practicals/python/agent-framework/part9/data.html')
content = r.text
soup = BeautifulSoup(content, 'html.parser')
td_ys = soup.find_all(attrs={"class" : "y"})
td_xs = soup.find_all(attrs={"class" : "x"})
#testing to see if beautifullsoup has got the right data
print(td_ys)
the_ys = []
the_ys.append(td_ys)
print(td_xs)
the_xs = []
the_xs.append(td_xs)
#print(content) #testing to see if content had been pulled
#defining our arguments
num_of_agents = 10
#
num_of_iterations = 100
neighbourhood = 20
agents = []
#Reading in data
environment = []
with open('in.txt', newline='') as f:
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
rowlist = []
for value in row:
rowlist.append(value)
environment.append(rowlist)
#Testing to see if the environment has read in properly
"""
matplotlib.pyplot.imshow(environment)
matplotlib.pyplot.show()
"""
#Working out the distance between two agents
def distance_between(agents_row_a, agents_row_b):
return math.sqrt( ((agents_row_a.x - agents_row_b.x)**2) + ((agents_row_a.y-agents_row_b.y)**2))
#Assign starting points to all our agents in their environment
for i in range (num_of_agents):
agents.append(agentframework.Agent(environment, agents))
#First we randomise the order of agents acting for every iteration, then we move
#them, make them eat, and share food with their neighbours
fig = matplotlib.pyplot.figure(figsize=(12, 12))
def update(frame_number):
fig.clear()
matplotlib.pyplot.imshow(environment)
matplotlib.pyplot.xlim(0, agents[0].environment_width)
matplotlib.pyplot.ylim(0, agents[0].environment_height)
random.shuffle(agents)
for agent in agents:
agent.move()
agent.eat()
agent.share_with_neighbours(neighbourhood)
for agent in agents:
matplotlib.pyplot.scatter(agent.x,agent.y)
for j in range(num_of_iterations):
random.shuffle(agents)
for i in range (num_of_agents):
agents[i].move()
agents[i].eat()
agents[i].share_with_neighbours(neighbourhood)
#Showing our agents on a plot
matplotlib.pyplot.ylim(0, 299)
matplotlib.pyplot.xlim(0, 299)
matplotlib.pyplot.imshow(environment)
for i in range (num_of_agents):
matplotlib.pyplot.scatter(agents[i].x,agents[i].y)
root = tkinter.Tk()
root.wm_title("Model")
canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, master=root)
canvas._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def run():
animation = matplotlib.animation.FuncAnimation(fig, update, frames=num_of_iterations, repeat=False)
canvas.show()
menu = tkinter.Menu(root)
root.config(menu=menu)
model_menu = tkinter.Menu(menu)
menu.add_cascade(label="Model", menu=model_menu)
model_menu.add_command(label="Run model", command=run)
tkinter.mainloop()