-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSean_the_sheep_of_the_deadtest.py
More file actions
144 lines (95 loc) · 3.6 KB
/
Sean_the_sheep_of_the_deadtest.py
File metadata and controls
144 lines (95 loc) · 3.6 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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 14:12:33 2019
@author: gynjkm
"""
#imports required packages
import matplotlib.pyplot as plt
plt.use('TkAgg')
import matplotlib.animation
import agentframework_zombies
import csv
import random
#defines our arguments and creating the lists of sheep and zombiesheep
num_of_agents = 100
num_of_iterations = 150
neighbourhood = 15
num_of_zombsheep = 2
agents = []
zombsheep = []
#creates the environment from the csv file
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)
#Tests whether the environment has read in properly
"""
plt.imshow(environment)
plt.show()
"""
#Assign starting points to all our agents in their environment
for i in range (num_of_agents):
agents.append(agentframework_zombies.Agent(environment, agents))
for i in range (num_of_zombsheep):
zombsheep.append(agentframework_zombies.Zombiesheep(environment, zombsheep, agents))
fig = plt.figure(figsize=(12, 12))
'''
#Testing to see if our agents have acces to the locations of other agents
print("Our first sheep is at", agents[0].x, agents[0].y, ", some other sheep he knows are at:")
for i in range(10):
print(agents[0].agents[i].x, agents[0].agents[i].y)
'''
'''
This makes the model run until the zombies have wiped out all ofthe sheep or
the desired number of iterations has been reached.
'''
def update(frame_number):
fig.clear()
plt.imshow(environment)
plt.xlim(0, agents[0].environment_width)
plt.ylim(0, agents[0].environment_height)
plt.xlim(0, zombsheep[0].environment_width)
plt.ylim(0, zombsheep[0].environment_height)
#shuffles the order in which agents in a list move to avoid "first mover" advantages
random.shuffle(agents)
random.shuffle(zombsheep)
for agent in agents:
agent.move()
agent.eat()
agent.share_with_neighbours(neighbourhood)
for zombiesheep in zombsheep:
zombiesheep.move()
#creates a list of all sheep within "biting range"
target_agents = zombiesheep.bite(neighbourhood, agents, zombsheep)
for target in target_agents:
#adds a new zombie in place of the target's location
zombsheep.append(agentframework_zombies.Zombiesheep(environment, zombsheep, agents, [target.y, target.x]))
#kills the target
agents.remove(target)
#this is done in this order to avoid losing the coordinates of the target
#plots our sheep in white and our zombies in red
for agent in agents:
plt.scatter(agent.x,agent.y, c="snow")
for zombiesheep in zombsheep:
plt.scatter(zombiesheep.x,zombiesheep.y, c="red")
print(frame_number)
#Prints an update on how the sheep vs zombie battle is going
print("There are", str(len(agents)), "sheep, and ", str(len(zombsheep)), "zombie sheep.")
#prints a victory message for the zombies if they manage to convert all the sheep
if len(agents) == 0:
print("Braiiiiins! Zombies win!")
#prints a victory message for the sheep if they manage to survive until dawn
if int(frame_number) == int(num_of_iterations)-1:
print("Baaaahhhh! Sheep win!")
#Showing our agents in animation
plt.ylim(0, 299)
plt.xlim(0, 299)
plt.imshow(environment)
for i in range (num_of_agents):
plt.scatter(agents[i].x,agents[i].y)
animation = matplotlib.animation.FuncAnimation(fig, update, interval=0.1, repeat=False, frames=num_of_iterations)
plt.show()