15.08.2015 Views

Introduction to the Modeling and Analysis of Complex Systems

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

11.3. SIMULATING CELLULAR AUTOMATA 195Note that <strong>the</strong> cmap option in imshow is <strong>to</strong> specify <strong>the</strong> color scheme used in <strong>the</strong> plot. Withoutit, pylab uses dark blue <strong>and</strong> red for binary values by default, which are ra<strong>the</strong>r hard <strong>to</strong> see.The updating part requires some algorithmic thinking. The state-transition functionneeds <strong>to</strong> count <strong>the</strong> number <strong>of</strong> panicky individuals within a neighborhood. How can we dothis? The positions <strong>of</strong> <strong>the</strong> neighbor cells within <strong>the</strong> Moore neighborhood around a position(x, y) can be written as{(x ′ , y ′ ) | x − 1 ≤ x ′ ≤ x + 1, y − 1 ≤ y ′ ≤ y + 1}. (11.3)This suggests that <strong>the</strong> neighbor cells can be swept through using nested for loops forrelative coordinate variables, say, dx <strong>and</strong> dy, each ranging from −1 <strong>to</strong> +1. Countingpanicky individuals (1’s) using this idea can be implemented in Python as follows:Code 11.3:count = 0for dx in [-1, 0, 1]:for dy in [-1, 0, 1]:count += config[(x + dx) % n, (y + dy) % n]Here, dx <strong>and</strong> dy are relative coordinates around (x, y), each varying from −1 <strong>to</strong> +1. Theyare added <strong>to</strong> x <strong>and</strong> y, respectively, <strong>to</strong> look up <strong>the</strong> current state <strong>of</strong> <strong>the</strong> cell located at(x + dx, y + dy) in config. The expression (...) % n means that <strong>the</strong> value inside <strong>the</strong>paren<strong>the</strong>ses is contained inside <strong>the</strong> [0, n − 1] range by <strong>the</strong> mod opera<strong>to</strong>r (%). This isa useful coding technique <strong>to</strong> implement periodic boundary conditions in a very simplemanner.The counting code given above needs <strong>to</strong> be applied <strong>to</strong> all <strong>the</strong> cells in <strong>the</strong> space, soit should be included in ano<strong>the</strong>r set <strong>of</strong> nested loops for x <strong>and</strong> y <strong>to</strong> sweep over <strong>the</strong> entirespace. For each spatial location, <strong>the</strong> counting will be executed, <strong>and</strong> <strong>the</strong> next state <strong>of</strong> <strong>the</strong>cell will be determined based on <strong>the</strong> result. Here is <strong>the</strong> completed code for <strong>the</strong> updating:Code 11.4:def update():global config, nextconfigfor x in xrange(n):for y in xrange(n):count = 0for dx in [-1, 0, 1]:for dy in [-1, 0, 1]:count += config[(x + dx) % n, (y + dy) % n]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!