forked from shaodan/PyEGT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
197 lines (184 loc) · 6.39 KB
/
simulation.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import networkx as nx
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from evolution import Evolution, CoEvolution, StaticStrategyEvolution
from population import Population, DynamicPopulation
import game
import rule
import adapter
# 绘图参数
colors = 'bgrcmykw'
markers = '.,ov^v<>1234sp*hH+xDd|-'
lines = ['-', '--', '-.', ':']
fmt = ['bd-', 'ro--', 'g^-.', 'c+:', 'mx--', 'y*-.']
class Simulation:
def __init__(self):
self.G = nx.barabasi_albert_graph(1000, 3) # 生成网络
self.p = Population(self.G) # 网络结构
self.g = game.PDG(b=5) # 博弈类型
self.u = rule.DeathBirth() # 学习策略
self.a = adapter.Preference(3) # 连接策略
def once(self):
e = Evolution(has_mut=True)
e.set_population(self.p).set_game(self.g).set_rule(self.u)
e.evolve(1000)
e.show()
def lattice(self):
l = 100
def observe(s, f):
plt.imshow(s.reshape((l, l)), interpolation='sinc', cmap='bwr')
plt.show()
G = nx.grid_2d_graph(l, l)
p = Population(G)
e = Evolution()
e.set_population(p).set_game(self.g).set_rule(self.u)
observe(p.strategy, p.fitness)
def cora(self):
G = nx.barabasi_albert_graph(1000, 3)
p = Population(G)
e = Evolution()
g = game.PDG(2)
e.set_population(p).set_game(g)
f, axs = plt.subplots(3, 4)
axs = axs.reshape(12)
for r in range(11):
if r > 5:
r_ = 10 - r
p.strategy = np.zeros(len(p), dtype=np.int)
s = 1
else:
r_ = r
p.strategy = np.ones(len(p), dtype=np.int)
s = 0
n = int(round(len(p) / 10.0 * r_))
selected_list = np.random.choice(range(len(p)), n)
p.strategy[selected_list] = s
g.strategy = p.strategy
g.play()
p.show_degree(axs[r])
plt.show()
def once_co(self):
dp = DynamicPopulation(self.G)
c = CoEvolution(has_mut=False)
c.set_population(dp).set_game(self.g).set_rule(self.u)
c.set_adapter(self.a)
c.evolve(50000)
c.show()
def repeat2d(self):
e = Evolution()
bs = np.linspace(1, 10, 3)
colors = 'brgcmykwa'
symbs = '.ox+*sdph-'
for i in range(1, 10):
G = nx.random_regular_graph(i + 1, 1000)
p = Population(G)
a = [0] * len(bs)
for j, b in enumerate(bs):
g = game.PDG(b)
e.set_population(p).set_game(g).set_rule(self.u)
e.evolve(10000)
a[j] = e.cooperate[-1]
plt.plot(bs, a, colors[j] + symbs[j], label='b=%f' % b)
break
plt.show()
def repeat_k(self):
e = Evolution(has_mut=False)
k = 2
for i in range(k):
G = nx.random_regular_graph(i * 2 + 2, 1000)
p = Population(G)
e.set_population(p).set_game(self.g).set_rule(self.u)
print('Control Variable k: %d' % (i * 2 + 2))
e.evolve(100000, restart=True, quiet=True)
e.show(label="k=%d" % (i * 2 + 2))
plt.legend(loc='lower right')
plt.show()
plt.savefig('outputpk.png')
def repeat_b(self):
e = Evolution(has_mut=False)
G = nx.random_regular_graph(4, 1000)
p = Population(G)
b = 5
for i in range(b):
g = game.PDG(i * 2 + 2)
e.set_population(p).set_game(g).set_rule(self.u)
print('Control Variable b: %d' % (i * 2 + 2))
e.evolve(100000, restart=True, quiet=True, autostop=False)
e.show(label="b=%d" % (i * 2 + 2))
plt.legend(loc='lower right')
plt.show()
plt.savefig('outputb.png')
def repeat_start_pc(self):
G = nx.barabasi_albert_graph(1000, 3)
p = Population(G)
g = game.PDG(b=10)
e = Evolution(has_mut=False)
e.set_population(p).set_game(g).set_rule(self.u)
for i in range(5):
pc = (2 * i + 1) / 10.0
p.init_strategies(g, [pc, 1 - pc])
print('Initial P(C) is %.2f' % pc)
e.evolve(100000, restart=True, quiet=True, autostop=False)
e.show(label=r'start $P_C$=%.2f' % pc)
plt.legend(loc='lower right')
plt.title(r'Evolution under Different Start $P_C$')
plt.xlabel('Number of generations')
plt.ylabel(r'Fraction of cooperations, $\rho_c$')
plt.show()
plt.savefig('outputpc.png')
def repeat_ss_rewire(self):
from network import LatticeWithLongTie
p = LatticeWithLongTie(30)
g = game.PDG(b=8)
u = rule.Fermi(0.1)
a = adapter.Preference(3)
e = StaticStrategyEvolution()
e.set_game(g).set_rule(u).set_adapter(a)
e.set_population(p)
e.bind_process()
p.init_longtie()
p_copy1 = p.copy()
plt.figure()
for i in range(6):
pc = i / 5.0
p_copy = p.copy()
e.set_population(p)
a.dynamic = p.dynamics
a.bind(p)
p.init_strategies(g, [pc, 1 - pc])
p = p_copy
print('static P(C) is %.2f' % pc)
e.evolve(100, restart=True, quiet=True, autostop=False)
e.show(label=r'static $P_C$=%.2f ' % pc)
plt.legend(loc='upper left')
plt.title('Static Strategy Evolution')
plt.xlabel('Number of generations')
plt.ylabel('Rewire Strategies')
plt.ylim(0, len(p))
plt.show()
def repeat_ll_rewire(self):
from network import LatticeWithLongTie
p = LatticeWithLongTie(30)
g = game.PDG(b=8)
u = rule.DeathBirth()
a = adapter.Preference(3)
e = CoEvolution()
e.set_game(g).set_rule(u).set_adapter(a)
e.set_population(p)
e.bind_process()
p.degree_distribution()
print('start evolving')
e.evolve(10000, restart=True, quiet=True, autostop=False)
plt.figure()
e.show(label='LLN Co-Evolution')
plt.legend(loc='upper left')
plt.title('LLN Co-Evolution')
plt.xlabel('Number of generations')
plt.ylabel('Rewire Strategies')
plt.show()
p.degree_distribution()
if __name__ == "__main__":
sim = Simulation()
sim.repeat_b()