forked from hsayama/PyCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abm-ants.py
69 lines (54 loc) · 1.6 KB
/
abm-ants.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
import pycxsimulator
from pylab import *
width = 50
height = 50
populationSize = 50
free = 0
carrying = 1
garbageProb = 0.8
def initialize():
global time, agents, envir
time = 0
agents = []
for i in range(populationSize):
newAgent = [randint(width), randint(height), free]
agents.append(newAgent)
envir = zeros([height, width])
for y in range(height):
for x in range(width):
if random() < garbageProb:
state = 1
else:
state = 0
envir[y, x] = state
def observe():
cla()
imshow(envir, cmap = cm.YlOrRd, vmin = 0, vmax = 5)
axis('image')
x = [ag[0] for ag in agents]
y = [ag[1] for ag in agents]
s = [ag[2] for ag in agents]
scatter(x, y, c = s, cmap = cm.bwr)
title('t = ' + str(time))
def clip(a, amin, amax):
if a < amin: return amin
elif a > amax: return amax
else: return a
def update():
global time, agents, envir
time += 1
for ag in agents:
# simulate random motion
ag[0] += randint(-1, 2)
ag[1] += randint(-1, 2)
ag[0] = clip(ag[0], 0, width - 1)
ag[1] = clip(ag[1], 0, height - 1)
# simulate interaction between ants and environment
if envir[ag[1], ag[0]] > 0:
if ag[2] == free:
envir[ag[1], ag[0]] -= 1
ag[2] = carrying
else:
envir[ag[1], ag[0]] += 1
ag[2] = free
pycxsimulator.GUI().start(func=[initialize, observe, update])