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.

16.2. SIMULATING DYNAMICS ON NETWORKS 329dynamic is taking place on a network, <strong>the</strong>re is a chance for a tie (which never occurs onCA with von Neumann or Moore neighborhoods). In <strong>the</strong> example above, I just includeda tie-breaker coin <strong>to</strong>ss, <strong>to</strong> be fair. And <strong>the</strong> last swap <strong>of</strong> g <strong>and</strong> nextg is something we arealready familiar with.The entire simulation code looks like this:Code 16.4: net-majority.pyimport matplotlibmatplotlib.use(’TkAgg’)from pylab import *import networkx as nxdef initialize():global g, nextgg = nx.karate_club_graph()g.pos = nx.spring_layout(g)for i in g.nodes_iter():g.node[i][’state’] = 1 if r<strong>and</strong>om() < .5 else 0nextg = g.copy()def observe():global g, nextgcla()nx.draw(g, cmap = cm.binary, vmin = 0, vmax = 1,node_color = [g.node[i][’state’] for i in g.nodes_iter()],pos = g.pos)def update():global g, nextgfor i in g.nodes_iter():count = g.node[i][’state’]for j in g.neighbors(i):count += g.node[j][’state’]ratio = count / (g.degree(i) + 1.0)nextg.node[i][’state’] = 1 if ratio > .5 \else 0 if ratio < .5 \else 1 if r<strong>and</strong>om() < .5 else 0

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

Saved successfully!

Ooh no, something went wrong!