-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprob2.py
119 lines (102 loc) · 3.66 KB
/
prob2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
import networkx as nx
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.animation
matrix1 = np.array([[0,1,0,0,0,0,1],
[1,0,1,0,0,0,0],
[0,1,0,1,0,0,0],
[0,0,1,0,1,0,0],
[0,0,0,1,0,1,0],
[0,0,0,0,1,0,1],
[1,0,0,0,0,1,0]])
matrix2 = np.array([[0,1,1,0,0,1,1],
[1,0,1,1,0,0,1],
[1,1,0,1,1,0,0],
[0,1,1,0,1,1,0],
[0,0,1,1,0,1,1],
[1,0,0,1,1,0,1],
[1,1,0,0,1,1,0]])
matrixc = np.array([[0,1,1,1,1,1,1],
[1,0,1,1,1,1,1],
[1,1,0,1,1,1,1],
[1,1,1,0,1,1,1],
[1,1,1,1,0,1,1],
[1,1,1,1,1,0,1],
[1,1,1,1,1,1,0]])
adhoc = np.zeros((17,17))
adhoc[0,[1,2]] = 1
adhoc[1,[0,2]] = 1
adhoc[2,[0,1,3]] = 1
adhoc[3,[2,4,8]] = 1
adhoc[4,[3,5,6]] = 1
adhoc[5,[4,6,7]] = 1
adhoc[6,[4,5,7,8,9]] = 1
adhoc[7,[5,6,9,16]] = 1
adhoc[8,[3,6,9,10]] = 1
adhoc[9,[6,7,8,11]] = 1
adhoc[10,[8,11,12]] = 1
adhoc[11,[9,10,13,14]] = 1
adhoc[12,[10,13]] = 1
adhoc[13,[11,12,14,15]] = 1
adhoc[14,[11,13,15,16]] = 1
adhoc[15,[13,14,16]] = 1
adhoc[16,[7,14,15]] = 1
class DiffusionGraph():
DEFAULT_UTILITY = np.array([[1,0],[1,2]])
def __init__(self,adjacency_matrix):
self.matrix = adjacency_matrix
self.n = len(self.matrix)
self.nodes = np.zeros(self.n)
def get_adjacent_values(self, agent_num):
adj = np.zeros(2)
for neighbor in np.where(self.matrix[agent_num] == 1)[0]:
adj[int(self.nodes[neighbor])] += 1
return adj
def run(self,filename=None,utility_matrix = DEFAULT_UTILITY,early_adopters = [0]):
# Reset
self.nodes = np.zeros(self.n)
# Set up with early adopters
for i in early_adopters:
self.nodes[i] = 1
choices = [self.nodes.copy()]
# Run until equilibrium is reached
while(True):
this_round = np.zeros(self.n)
for agent in range(self.n):
if agent in early_adopters:
print(agent, "doesnt change, they are an early adopter")
this_round[agent] = 1
continue
adj = self.get_adjacent_values(agent)
p = adj @ utility_matrix
print(f"agent {agent} has adj {adj}, p {p}")
# Default to switching in case of a tie
print(agent)
this_round[agent] = 0 if p[1] < p[0] else 1
choices.append(this_round)
print(this_round)
if np.allclose(this_round, choices[-2]):
break
self.nodes = this_round
# Animate
fig, ax = plt.subplots(figsize=(6,4))
G = nx.Graph(self.matrix)
pos = nx.spring_layout(G)
print('choices')
print(choices)
def update(num):
frame = num % len(choices)
print(frame)
ax.clear()
plt.title(f"Early Adopters: {early_adopters}")
hare = mpatches.Patch(color='gray', label='Hare')
stag = mpatches.Patch(color='cyan', label='Stag')
plt.legend(handles=[hare, stag])
colors = [['gray','cyan'][int(i)] for i in choices[frame]]
nx.draw(G,pos=pos,node_color=colors,with_labels = True)
# create and draw networkx graph
ani = matplotlib.animation.FuncAnimation(fig, update, interval=1000, repeat=True)
if filename:
ani.save(filename)
plt.show()