-
Notifications
You must be signed in to change notification settings - Fork 0
/
agentframework.py
414 lines (335 loc) · 11.5 KB
/
agentframework.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
407
408
409
410
411
412
413
414
# Class module for agent-based Model of sheep and wolf.
# Called by main file: model.py.
# Author: Anonymous160609 (Github).
# Version: 10.3.
import random # geenrate random numbers
class Agent():
"""
A class of sheep for ABM.
Arguments:
y: y coordinate of individual sheep in matplotlib
x: x coordinate of individual sheep in matplotlib
environment: environment of grassland for sheep to move and consume
agents: list of all other sheep for interaction
store: consumed grass by sheep
colour: colour of sheep
methods:
.getx: print x value
.setx: change x value
.delx: delete x value
.gety: print y value
.sety: change y value
move: randomly move sheep for integer distances in 2 directions out of four
eat: consume the grass in environment, shift values from envirnment to .store
attribute
distance_betwen: EuEuclidian distance between any 2 sheep
share_with_neighbours: average all consumed grass by a number of sheep within
a distance
__str__: override .print method to get the .store value, rather than a momory
location
Returns:
make encapsulated agents in ABM in main module.
"""
def __init__ (self,_y,_x,environment,agents, colour):
"""
>>>agent(1,1,1,1,1)
<agentframework.Agent at 0x195a05d8340>
initail attributes of Agent class
Arguments:
x: x coordinate, by default integer between (0,255)
y: y coordinate, by default integer between (0,255)
environment: environment of grassland for sheep to move and consume, a list
agents: list of all other sheep for interaction, a 2-D list
Returns:
set up mandatory values to ABM to run
"""
if (_y == None):
self._y = random.randint(0,255)
else:
self._y =_y
if (_x == None):
self._x = random.randint(0,255)
else:
self._x =_x
self.environment = environment
self.agents = agents
self.store = 0 #grass eaten
self.colour = colour
def getx(self):
"""
>>>agent = agentframwork.Agent(1,y,environment,agents,colour)
>>>agent.getx
1
Print the vlaue of class attribute: x coordinate
returns:
the value of x
"""
return self._x
def setx(self, value):
"""
>>>agent = agentframwork.Agent(1,y,environment,agents,colour)
>>>agent.setx(2)
>>>agent.getx
2
change the vlaue of class attribute: x coordinate
returns:
the value of input
"""
self._x = value
x = property(getx, setx, "I am the 'x' property.")
def gety(self):
"""
>>>agent = agentframwork.Agent(x,1,environment,agents,colour)
>>>agent.gety
1
print the vlaue of class attribute: y coordinate
returns:
the value of y
"""
return self._y
def sety(self, value):
"""
>>>agent = agentframwork.Agent(x,1,environment,agents,colour)
>>>agent.sety(2)
>>>agent.gety
2
change the vlaue of class attribute: y coordinate
returns:
the value of input
"""
self._y = value
y = property(gety, sety, "I am the 'y' property.")
def move(self):
"""
>>>agent = agentframwork.Agent(1,1,environment,agents,colour)
>>>a = agent.x
>>>agent.move
>>>b = agent.x
>>>a - b != 0
True
movement method of sheep
arguments:
x: x coordinate of sheep
y: y coordinate of sheep
Returns:
changes in values of x and y coordinates
"""
# conditional clause to detect sheep corps, only living sheep can move
if self.colour != 'Red':
# 50% change to move up
if random.random() < 0.5:
self._y = self._y + random.randint(1,10)
# if run into fence, turn around
if self._y > 254:
self._y = 254
# 50% change to move down
else:
self._y = self._y - random.randint(1,10)
if self._y < 1:
self._y = 1
if random.random() < 0.5:
self._x = self._x + random.randint(1,10)
if self._x > 254:
self._x = 254
else:
self._x = self._x - random.randint(1,10)
if self._x < 1:
self._x = 1
# if corps, avoid moving
else:
self._y = self._y
self._x = self._x
# old movement on theoritical physics coordinates
#self._y = (self._y - 1) % 100
#self._y = (self._y + 1) % 100
#self._x = (self._x - 1) % 100
#self._x = (self._x + 1) % 100
def eat(self): # can you make it eat what is left?
"""
>>>agent = agentframework.Agent(x,y,11,agents,colour)
>>>agent.store = 0
>>>agent.eat
>>>agent.store
10
eat the grass from grassland, transmit value from environemnt attribute
to store attribute
arguments:
self.environment: a list of integer value
self.store: an integer value to accumulate
returns:
transmit a value
"""
if self.environment[self._x][self._y] > 10:
self.environment[self._x][self._y] -= 10
self.store += 10
#define distance function
#This function takes in two arbitary agents
#(at the moment, two rows in our agents list $[[...],[...]]$, each containing two values),
#and will return the distance between them.
#for-each loop iterator, not index
def distance_between(self, agent):
"""
>>>agent1 = agentframework.Agent(1,1,environment,agents,colour)
>>>agent2 = agentframework.Agent(2,2,environment,agent1,colour)
>>>agent2.distance_bewteen(agent2)
1.414
return the Euclidian distance between class instance sheep and an object
in its attribute of agents list
arguments:
agent: another class instacne of sheep, needs x and y attributes
returns:
an Euclidian distance
"""
return (((self._x - agent._x)**2) + ((self._y - agent._y)**2))**0.5
def share_with_neighbours(self,neighbourhood):
"""
>>>agent1 = agentframework.Agent(1,1,environment,agents,colour)
>>>agent2 = agentframework.Agent(2,2,environment,agent1,colour)
>>>agent1.store = 30
>>>agent2.store = 10
>>>agent1.share_with_neighbours
>>>agent1.store
20
share grass consumed with other sheep nearby
arguments:
x: x coordinates of multiple sheep
y: y coordinates of multiple sheep
store: grass consumed by sheep
returns:
change and average of consumed grass in each sheep
"""
for agent in self.agents:
random.shuffle(self.agents)
dist = self.distance_between(agent)
if dist <= neighbourhood:
sum = self.store + agent.store
ave = sum /2
self.store = ave
agent.store = ave
#print("sharing " + str(dist) + " " + str(ave)
def __str__(self):
"""
>>>agent = agentframework.Agent(x,y,environment,agents,colour)
>>>agent.store = 20
>>>print(agent)
20
override printed string describing the agent
argument:
string conten
"""
return str(self.store)
#return str(self.store) and hex(id(self))
class Wolf():
"""
A class of wolf for ABM.
Arguments:
y: y coordinate of individual wolf in matplotlib
x: x coordinate of individual wolf in matplotlib
colour: colour of wolf
methods:
.getx: print x value
.setx: change x value
.delx: delete x value
.gety: print y value
.sety: change y value
.dely: delete y value
move: randomly move wolf for integer distances in 2 directions out of four
Returns:
make encapsulated wolf agent in ABM in main module.
"""
def __init__ (self,_y,_x,colour):
"""
>>>agent(1,1,1,1,1)
<agentframework.Agent at 0x195a05d8340>
initail attributes of Wolf class
Arguments:
x: x coordinate, by default integer between (0,255)
y: y coordinate, by default integer between (0,255)
colour: colour of wolf, text string in matplotlib colour list
Returns:
set up mandatory values to ABM to run
"""
self._y = _y
self._x = _x
self.colour = colour
#def __init__ (self,_y,_x,colour,_acc_y,_acc_x):
# self._acc_y = _acc_y
# self._acc_x = _acc_y
def getx(self):
"""
>>>agent = agentframwork.Wolf(1,y,colour)
>>>agent.getx
1
Print the vlaue of class attribute: x coordinate
returns:
the value of x
"""
return self._x
def setx(self, value):
"""
>>>agent = agentframwork.Wolf(1,y,colour)
>>>agent.setx(2)
>>>agent.x
2
Change the vlaue of class attribute x to input
returns:
the value of input
"""
self._x = value
x = property(getx, setx, "I am the 'x' property.")
def gety(self):
"""
>>>agent = agentframwork.Wolf(x,1,colour)
>>>agent.gety
1
Print the vlaue of class attribute: y coordinate
returns:
the value of y
"""
return self._y
def sety(self, value):
"""
>>>agent = agentframwork.Wolf(x,1,colour)
>>>agent.sety(2)
>>>agent.y
2
Change the vlaue of class attribute y to input
returns:
the value of input
"""
self._y = value
y = property(gety, sety, "I am the 'y' property.")
def move(self):
"""
>>>wolf = agentframwork.Wolf(1,1,colour)
>>>a = wolf.x
>>>wolf.move
>>>b = wolf.x
>>>a - b != 0
True
movement method of wolf
arguments:
x: x coordinate of wolf
y: y coordinate of wolf
Returns:
changes in values of x and y coordinates
"""
if random.random() > 0.5:
self._y = (self._y + 1) % 100
else:
self._y = (self._y - 1) % 100
if random.random() > 0.5:
self._x = (self._x + 1) % 100
else:
self._x = (self._x - 1) % 100
#def move(self):
#if self._acc_y > 0.5:
#self._y = (self._y + 1) % 100
#else:
#self._y = (self._y - 1) % 100
#if self._acc_x > 0.5:
#self._x = (self._x + 1) % 100
#else:
#self._x = (self._x - 1) % 100
#self._acc_y = random.randint(0,1)
#self._acc_x = random.randint(0,1)