-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimating_the_abm.py
More file actions
123 lines (78 loc) · 2.78 KB
/
Animating_the_abm.py
File metadata and controls
123 lines (78 loc) · 2.78 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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 14:12:33 2019
@author: gynjkm
"""
#importing packages and creating list
import matplotlib.pyplot
import matplotlib.animation
import math
import agentframework
import csv
import random
#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)
#Testing to see if our agents have acces to the locations of other agents
"""
print("Our first agent is at", agents[0].x, agents[0].y, ", some other agents he's heard of are at:")
for i in range(10):
print(agents[0].agents[i].x, agents[0].agents[i].y)
"""
#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)
animation = matplotlib.animation.FuncAnimation(fig, update, interval=0.01,repeat=False, frames=num_of_iterations)
matplotlib.pyplot.show()
#Working out the distances between all agents
"""
for j in range(num_of_agents):
for i in range(num_of_agents):
distance = distance_between(agents[i], agents[j])
"""