0% found this document useful (0 votes)
26 views7 pages

Lab 3

This lab focuses on understanding network dynamics through simulations using the PyCX simulator. Students will implement models like the local majority rule and the SIS model on social networks, specifically the Karate Club graph, and visualize the results. The lab also explores how network topology affects the dynamics of disease spread and individual behavior in response to infections.

Uploaded by

Jam Muslim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Lab 3

This lab focuses on understanding network dynamics through simulations using the PyCX simulator. Students will implement models like the local majority rule and the SIS model on social networks, specifically the Karate Club graph, and visualize the results. The lab also explores how network topology affects the dynamics of disease spread and individual behavior in response to infections.

Uploaded by

Jam Muslim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Social Network – Network Dynamics

Lab 3
I. Objectives
This lab aims to help students acquire a better understanding of the dynamics on networks. To
do so, we will try and implement a few dynamical models and visualize the result of a few
simulations.

II. Simulating Dynamics on the Network using PyCX


To create interactive simulations of our network dynamics we will use the “pycxsimulator.py”
file. The

“pycxsimulator.py” file implements a simple interactive graphical user interface (GUI) for our
own dynamic simulation model, which is structured in three essential components:
initialization, visualization, and updating.

For the purpose of this Lab, we will use the Spyder IDE. Open a terminal and write the
command spyder to launch the application.

To use “pycxsimulator.py”, we need to place that file in the directory where our simulation
code is located. Our simulation code should be structured as follows:

import matplotlib
matplotlib.use(’TkAgg’)
import pylab as PL
# import necessary modules
# define model parameters
def initialize():
global # list global variables
# initialize system states
def observe():
global # list global variables
cla() # to clear the visualization space # visualize system states
Social Network – Network Dynamics

def update():
global # list global variables
# update system states for one discrete time step
import pycxsimulator
pycxsimulator.GUI().start(func=[initialize, observe, update])

The first three lines and the last two lines should always be in place; no modification is
needed.
NB.
For MacOS and Linux users, if you’re getting a backend issue, it is possible that the
TkAgg/Tkinter graphic
backends behaves differently (one time it shows a plot, one time it shows a blank canvas)
depending on the choice of the dialog. To solve the issue:
1. Select the graphic backend as Tkinter via Preferences-> IPython Console -> Graphics ->
Graphics -> Graphics backend
2. Restart Spyder

The GUI of “pycxsimulator” is very simple. Under the “Run” tab of the control window (which
shows up by default), there are three self-explanatory buttons to run/pause the simulation,
update the system just for one step, and reset the system’s state. When you run the
simulation, the system’s state will be updated dynamically and continuously in the other
visualization window. To close the simulator, close the control window.

Under the “Settings” tab of the control window, you can change how many times the system
is updated before each visualization (default = 1) and the length of waiting time between the
visualizations (default = 0 ms). The other two tabs (“Parameters” and “Info”) are initially
blank. You can include information to be displayed under the “Info” tab as a “docstring”
(string placed as a first statement) in the init function. For example:

def init():
'''
This is my first PyCX simulator code.
It simulates the growth of a network with preferential attachement.
Social Network – Network Dynamics

'''

This additional docstring appears under the “Info” tab.

III. Simulating the Majority model on the Network


For our first Dynamics on the network model, we will try and implement a simple local majority
rule on a social network, with the following assumptions:

• Nodes represent individuals, and edges represent their symmetric connections for
information sharing.
• Each individual takes either 0 or 1 as his or her state.
• Each individual changes his or her state to a majority choice within his or her local
neighborhood (i.e., the individual him- or herself and the neighbors connected to him
or her).
• State updating takes place simultaneously on all individuals in the network
• Individuals’ states are initially random.

III. 1. Initialization
The initialization part aims to create a model of social network and then assign random states
to all the nodes. For the purpose of this lab, we will perform the simulation on the Karate Club
graph (available standard graph in the NetworkX library).

We need to prepare two network objects, one for the current time step and the other for the
next time step.

1. Create global variables such as your network, the next network (the network of the
next step)
2. Create a Karate Club Graph
3. Pre-calculate node positions using spring_layout and store the results under g as an
attribute g.pos (we will use for the visualization).
4. Iterate on the nodes of the graph and assign 0 or 1 randomly to the nodes’ state
attribute. You can use the Iterator nodes_iter() and the choice method from the
random library.
5. Finally use the copy command to create a duplicate copy of the network in nextg.
Social Network – Network Dynamics

def init():
'''
This is my first PyCX simulator code.
It simulates the majority rule on the Karate Club Graph.
‘’'
global g, nextg
// Add Code here

III. 2. Visualization
The visualization part is about drawing the network. We need to visualize the node states in
addition to thennetwork topology. We can use the node_color and cmap options for this
purpose.

Note that you will need to use g.pos to keep the nodes in pre-calculated positions.

def draw():
global g, nextg
// Add Code here

III. 3. Updating
In the updating part, we need to sweep all the nodes, and for each node, sweep its
neighbors, counting how many 1’s we have in the local neighborhood.

1. Create a loop on the network nodes.


2. Loop on the neighbors of each node using NetworkX’s g.neighbors(i) function gives a
list of i’s neighbors to calculate (count = the number of nodes in this local
neighborhood with state 1). Note that we need to include the node we’re looping on
as well.
3. Calculate a state ratio to indicate an aggregated state of the local neighborhood :
count / number of nodes in the local neighborhood.
Social Network – Network Dynamics

4. Update the states of the network nodes following a threshold of 0.5 (local majority
model): If the state ratio is above 0.5, the local majority is 1, so the next state of node
i will be 1. If it is below 0.5, the local majority is 0, so the next state will be 0. In case
it’s 0.5, randomly assign 0 or 1 to the node.
5. Finally, swap g and nextg.

def update():
global g, nextg
// Add Code here

III. 4. Simulation
Run this code and visualize the dynamical network simulation. You will notice that the network
sometimes converges to a homogeneous state, while at other times it remains in a divided
condition.

Run the same code on networks other than the Karate Club graph. See how the dynamics are
affected by the change of network size and/or topology

IV. Simulating the SIS model on the Network


The second model of dynamics on the network we will try to implement is the SIS model. In
the SIS model, there are two states of nodes: Susceptible and Infected. A susceptible node can
get infected from an infected neighbor node with infection probability pi (per infected
neighbor), while an infected node can recover back to a susceptible node (i.e., no immunity
acquired) with recovery probability pr.

The SIS model is much simpler than that of the majority rule network model, because of the
asynchrony of state updating (Instead of having all the nodes update their states
simultaneously based on local majority choices, the SIS model considers only one transfer
event at a time between a pair of connected nodes that are randomly chosen from the
network). We no longer need to use two separate data structures; we can keep modifying just
one Graph object directly.
Social Network – Network Dynamics

IV. 1. Initialization
We will retain the same initialization code as the majority rule model.

IV. 2. Visualization
We will retain the same visualization code as the majority rule model.

IV. 3. Updating
The updating part should implement the following algorithm:

1. Let’s start by defining our probabilities pi and pr as global variables.


a. We will then choose a node i randomly:
b. if i is susceptible, then we will randomly choose one of its neighbors. And if
the neighbor is infected, we simulate the infection of the disease for our node
i using the probability pi.
2. if i is infected, we simulate the recovery from the disease using the probability pr

IV. 4. Simulation
This simulation takes a lot more steps for the system to reach a homogeneous state, because
each step involves only two nodes. It is probably a good idea to set the step size to 50 under
the “Settings” tab to speed up the simulations.

With these parameter settings (pi = pr = 0.5), you will probably find that the disease (state 1 =
black nodes for instance) quickly dies off and disappears from the network, even though half
of the initial population is infected at the beginning. This means that for these particular
parameter values and network topology, the system can successfully suppress a pandemic
without taking any special action.

Conduct simulations of the SIS model with either pi or pr varied systematically, while the other
one is fixed at 0.5. Determine the condition in which a pandemic occurs (i.e., the disease
persists for an indefinitely long period of time). Is the transition gradual or sharp? Once you
get the results, try varying the other parameter as well.

Generate a much larger random network of your choice and conduct the same SIS model
simulation on it. See how the dynamics are affected by the change of network size and/or
topology. Will the disease continue to persist on the network?
Social Network – Network Dynamics

V. Simulating an adaptive SIS model on the Network


The adaptive SIS model is built on the idea that human behavior, in response to the spread of
illnesses, can change the topology of social ties, which in turn will influence the pathways of
disease propagation: when you find out that one of your friends has caught a flu, you will
probably not visit his or her room until he or she recovers from the illness.

To the SIS model, we will need to add the assumption that: When a susceptible node finds its
neighbor is infected by the disease, it will sever the edge to the infected node with severance
probability ps.

Modify the update method from the previous SIS model to directly remove the edge (node,
neighbor) from the network as soon as the node a decides to cut it.

Resources
Sayama, Hiroki. "PyCX: a Python-based simulation code repository for complex systems
education." Complex Adaptive Systems Modeling 1.1 (2013): 2.

Networkx Documentation https://networkx.github.io/documentation/stable/

You might also like