Created
February 20, 2019 15:16
-
-
Save owstron/4ff6385bbced456f175ce14c3132622c to your computer and use it in GitHub Desktop.
The Pre-Class Work for CS166 6.2 Dynamics on networks class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib | |
| matplotlib.use('TkAgg') | |
| from pylab import * | |
| import networkx as nx | |
| import random as rd | |
| def initialize(): | |
| global g, num_steps | |
| g = nx.karate_club_graph() | |
| g.pos = nx.spring_layout(g) | |
| for i in g.nodes: | |
| g.nodes[i]['state'] = 1 if random() < .5 else 0 | |
| num_steps = 0 | |
| def observe(): | |
| global g, last_state | |
| cla() | |
| nx.draw(g, vmin = 0, vmax = 1, | |
| node_color = [g.nodes[i]['state'] for i in g.nodes], | |
| pos = g.pos) | |
| def update_listener(): | |
| global g, num_steps | |
| listener = rd.choice(list(g.nodes)) | |
| speaker = rd.choice(list(g.neighbors(listener))) | |
| g.nodes[listener]['state'] = g.nodes[speaker]['state'] | |
| num_steps += 1 # counts number of steps | |
| state = sum([g.nodes[i]['state'] for i in g.nodes]) | |
| # The current state of the system represented as the sum of all states | |
| # The network reach consensus if either every node is zero (state==0) or state == length of network (every node is one.) | |
| if state == 0 or state == len(list(g.nodes)): | |
| return num_steps | |
| return False | |
| def update_speaker(): | |
| global g, num_steps | |
| speaker = rd.choice(list(g.nodes)) | |
| listener = rd.choice(list(g.neighbors(speaker))) | |
| g.nodes[listener]['state'] = g.nodes[speaker]['state'] | |
| num_steps += 1 | |
| state = sum([g.nodes[i]['state'] for i in g.nodes]) | |
| if state == 0 or state == len(list(g.nodes)): | |
| # print('Homegenity reached at {0} steps'.format(num_steps)) | |
| return num_steps | |
| return False | |
| def update_edges(): | |
| global g, num_steps | |
| chosen_edge = rd.choice(list(g.edges)) | |
| listener,speaker = rd.sample(chosen_edge, 2) | |
| g.nodes[listener]['state'] = g.nodes[speaker]['state'] | |
| num_steps += 1 | |
| state = sum([g.nodes[i]['state'] for i in g.nodes]) | |
| if state == 0 or state == len(list(g.nodes)): | |
| # print('Homegenity reached at {0} steps'.format(num_steps)) | |
| return num_steps | |
| return False | |
| INTERACTIVE = False | |
| update_modes = { | |
| 'listener' : update_listener, | |
| 'speaker' : update_speaker, | |
| 'edges' : update_edges | |
| } | |
| def run_experiment(num_simulations, mode): | |
| cumulative_num_steps = 0 | |
| for _ in range(num_simulations): | |
| initialize() | |
| while not update_modes[mode](): | |
| pass | |
| cumulative_num_steps += num_steps | |
| return round(cumulative_num_steps/num_simulations) | |
| num_simulations = 200 | |
| if INTERACTIVE: | |
| import pycxsimulator | |
| mode = 'listener' | |
| pycxsimulator.GUI().start(func=[initialize, observe, update_modes['mode']]) | |
| else: | |
| for mode in update_modes: | |
| print('Mode {0}: Average time to reach consensus is {1} steps.'.format(mode, run_experiment(num_simulations, mode))) | |
| ''' | |
| For 200 simulations: The results were: | |
| >> Mode listener: Average time to reach consensus is 625 steps. | |
| >> Mode speaker: Average time to reach consensus is 3682 steps. | |
| >> Mode edges: Average time to reach consensus is 1015 steps. | |
| ''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment