In [1]:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from __future__ import division
from localint import LocalInteraction

%matplotlib inline

In [2]:
cost = 1
payoff_matrix = np.asarray([[11, 11,  0], [11-cost, 11-cost, 10-cost], [3, 10, 10]])
payoff_matrix


Out[2]:
array([[11, 11,  0],
       [10, 10,  9],
       [ 3, 10, 10]])

In [3]:
G1 = nx.complete_graph(8)

In [4]:
li1 = LocalInteraction(payoff_matrix, nx.adjacency_matrix(G1))
def drawg(li, pos=None):
    G = nx.Graph(li.adj_matrix)
    if pos is None:
        pos=nx.circular_layout(G)
    nx.draw_networkx_nodes(G, pos, nodelist=G.nodes(), node_size=500, node_color='w')
    nx.draw_networkx_edges(G, pos, arrows=True)

    labels={}
    for i, action in zip(G.nodes(), li.current_actions):
        labels[i] = r'${0}$'.format(action)
    nx.draw_networkx_labels(G, pos, labels)

    plt.axis('off')
    plt.show()

In [5]:
li1.simulate(ts_length=10000,epsilon=0.01, init_actions=[2,2,2,2,0,0,0,0], revision='sequential')[-1] # cost=1


Out[5]:
array([2, 2, 2, 2, 2, 2, 2, 2])

In [6]:
for i in range(500):
    outcome = li1.simulate(ts_length=1000,epsilon=0, init_actions=[2,2,0,0,0,0,0,0], revision='sequential')[-1]
    print outcome
    if all(outcome == 0):
        break
print i


[0 0 0 0 0 0 0 0]
0

In [7]:
for i in range(500):
    outcome = li1.simulate(ts_length=1000,epsilon=0.01, init_actions=[2,2,0,0,0,0,0,0], revision='sequential')[-1]
    print outcome
    if all(outcome == 0):
        break
print i


[2 2 2 2 2 2 2 2]
[0 0 0 0 0 0 0 0]
1

In [16]:
G2 = nx.cycle_graph(8)
li2 = LocalInteraction(payoff_matrix, nx.adjacency_matrix(G2))
li2.simulate(ts_length=10000,epsilon=0.01, init_actions=[2,2,2,2,0,0,0,0], revision='sequential')[-1]


Out[16]:
array([0, 0, 0, 0, 0, 0, 2, 0])

In [17]:
cost2 = 3
payoff_matrix2 = np.asarray([[11, 11,  0], [11-cost2, 11-cost2, 10-cost2], [3, 10, 10]])
li2 = LocalInteraction(payoff_matrix2, nx.adjacency_matrix(G2))

In [20]:
for i in range(500):
    outcome = li2.simulate(ts_length=10000,epsilon=0, init_actions=[2,2,2,0,0,0,0,0], revision='sequential')[-1]
    if all(outcome == 2):
        break
print i


499

In [22]:
li1 = LocalInteraction(payoff_matrix2, nx.adjacency_matrix(G1))
for i in range(500):
    outcome = li1.simulate(ts_length=10000,epsilon=0, init_actions=[2,2,2,0,0,0,0,0], revision='sequential')[-1]
    if all(outcome == 2):
        break
print i


14

In [23]:
G3 = nx.ladder_graph(4)
li3 = LocalInteraction(payoff_matrix2, nx.adjacency_matrix(G3))

In [25]:
for i in range(500):
    outcome = li3.simulate(ts_length=10000,epsilon=0, init_actions=[2,2,2,0,0,0,0,0], revision='sequential')[-1]
    print outcome
    if all(outcome == 2):
        break
print i


[2 2 2 2 2 2 2 2]
0

In [29]:
G4 = nx.dorogovtsev_goltsev_mendes_graph(2)
nx.draw(G4)
import matplotlib.pyplot as plt
plt.show()



In [32]:
G5 = nx.wheel_graph(8)
nx.draw(G5)
plt.show()



In [37]:
li3.set_init_actions([2,2,2,0,0,0,0,0])
drawg(li3)



In [38]:
li5 = LocalInteraction(payoff_matrix2, nx.adjacency_matrix(G5))
li5.set_init_actions([2,0,0,0,0,0,0,0])
drawg(li5)



In [39]:
for i in range(500):
    outcome = li5.simulate(ts_length=10000,epsilon=0, init_actions=[2,0,0,0,0,0,0,0], revision='sequential')[-1]
    if all(outcome == 2):
        break
print i


9

In [41]:
for i in range(500):
    outcome = li5.simulate(ts_length=10000,epsilon=0, init_actions=[2,2,2,0,0,0,0,0], revision='sequential')[-1]
    if all(outcome == 2):
        break
print i


0

In [ ]: