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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16.2. SIMULATING DYNAMICS ON NETWORKS 327• State updating takes place simultaneously on all individuals in <strong>the</strong> network.• Individuals’ states are initially r<strong>and</strong>om.Let’s continue <strong>to</strong> use pycxsimula<strong>to</strong>r.py <strong>and</strong> implement <strong>the</strong> simula<strong>to</strong>r code by definingthree essential components—initialization, observation, <strong>and</strong> updating.The initialization part is <strong>to</strong> create a model <strong>of</strong> social network <strong>and</strong> <strong>the</strong>n assign r<strong>and</strong>omstates <strong>to</strong> all <strong>the</strong> nodes. Here I propose <strong>to</strong> perform <strong>the</strong> simulation on our favorite KarateClub graph. Just like <strong>the</strong> CA simulation, we need <strong>to</strong> prepare two network objects, onefor <strong>the</strong> current time step <strong>and</strong> <strong>the</strong> o<strong>the</strong>r for <strong>the</strong> next time step, <strong>to</strong> avoid conflicts during <strong>the</strong>state updating process. Here is an example <strong>of</strong> <strong>the</strong> initialization part:Code 16.1:def 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()Here, we pre-calculate node positions using spring_layout <strong>and</strong> s<strong>to</strong>re <strong>the</strong> results under gas an attribute g.pos. Python is so flexible that you can dynamically add any attribute <strong>to</strong>an object without declaring it beforeh<strong>and</strong>; we will utilize this trick later in <strong>the</strong> agent-basedmodeling as well. g.pos will be used in <strong>the</strong> visualization so that nodes won’t jump aroundevery time you draw <strong>the</strong> network.The g.nodes_iter() comm<strong>and</strong> used in <strong>the</strong> for loop above works essentially <strong>the</strong> sameway as g.nodes() in this context, but it is much more memory efficient because it doesn’tgenerate an actual fully spelled-out list, but instead it generates a much more compactrepresentation called an itera<strong>to</strong>r, an object that can return next values iteratively. Thereis also g.edges_iter() for similar purposes for edges. It is generally a good idea <strong>to</strong> usenodes_iter() <strong>and</strong> edges_iter() in loops, especially if your network is large. Finally, in<strong>the</strong> last line, <strong>the</strong> copy comm<strong>and</strong> is used <strong>to</strong> create a duplicate copy <strong>of</strong> <strong>the</strong> network.The observation part is about drawing <strong>the</strong> network. We have already done this manytimes, but <strong>the</strong>re is a new challenge here: We need <strong>to</strong> visualize <strong>the</strong> node states in addition<strong>to</strong> <strong>the</strong> network <strong>to</strong>pology. We can use <strong>the</strong> node_color option for this purpose:Code 16.2:def observe():

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

Saved successfully!

Ooh no, something went wrong!