-
Notifications
You must be signed in to change notification settings - Fork 81
/
ca-panic.py
31 lines (26 loc) · 869 Bytes
/
ca-panic.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
import pycxsimulator
from pylab import *
n = 100 # size of space: n x n
p = 0.24 # probability of initially panicky individuals
def initialize():
global config, nextconfig
config = zeros([n, n])
for x in range(n):
for y in range(n):
config[x, y] = 1 if random() < p else 0
nextconfig = zeros([n, n])
def observe():
global config, nextconfig
cla()
imshow(config, vmin = 0, vmax = 1, cmap = cm.binary)
def update():
global config, nextconfig
for x in range(n):
for y in range(n):
count = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
count += config[(x + dx) % n, (y + dy) % n]
nextconfig[x, y] = 1 if count >= 4 else 0
config, nextconfig = nextconfig, config
pycxsimulator.GUI().start(func=[initialize, observe, update])