<reponame>nalyd88/modeling-and-simulation
# This is an example from the book

import matplotlib
matplotlib.use('TkAgg')
from pylab import *

n = 100  # size of space: n x n
p = 0.1  # probability of initially panicky individuals


def initialize():
    global config, nextconfig
    config = zeros([n, n])
    for x in xrange(n):
        for y in xrange(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 xrange(n):
        for y in xrange(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


import pycxsimulator
pycxsimulator.GUI().start(func=[initialize, observe, update])

# This is an example from the book

import matplotlib
matplotlib.use('TkAgg')
from pylab import *

n = 100  # size of space: n x n
p = 0.1  # probability of initially panicky individuals


def initialize():
    global config, nextconfig
    config = zeros([n, n])
    for x in xrange(n):
        for y in xrange(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 xrange(n):
        for y in xrange(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


import pycxsimulator
pycxsimulator.GUI().start(func=[initialize, observe, update])
# This is an example from the book # size of space: n x n # probability of initially panicky individuals