-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevolution.py
executable file
·347 lines (296 loc) · 11 KB
/
evolution.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -*- coding: utf-8 -*-
# -*- Author: shaodan -*-
# -*- 2015.07.11 -*-
import os
import datetime
import numpy as np
import networkx as nx
import scipy.io as sio
import matplotlib.pyplot as plt
from population import Population, DynamicPopulation
import rule
import game
import adapter
class Evolution(object):
""" Evolutionary Game agent-based Simulation """
def __init__(self, has_mut=True):
# 是否突变
self.has_mut = has_mut
self.population = None
self.game = None
self.rule = None
# 迭代次数,中断续演
self.gen = 0
# 合作率记录
self.cooperate = None
self.death = None
self.rewire = None
def set_population(self, p):
assert isinstance(p, Population)
self.population = p
return self
def set_game(self, g):
assert isinstance(g, game.Game)
self.game = g
return self
def set_rule(self, r):
assert isinstance(r, rule.Rule)
self.rule = r
return self
def bind_process(self):
self.population.init_strategies(self.game)
self.game.bind(self.population)
self.rule.bind(self.population)
def ready(self):
self.bind_process()
def next_generation(self, i):
self.game.play(self.death, self.rewire)
birth, death = self.rule.update()
if self.has_mut and np.random.random() <= 0.01:
new_s = np.random.randint(self.game.order)
mutate = True
else:
new_s = self.population.strategy[birth]
mutate = False
if new_s == self.population.strategy[death]:
increase = 0
else:
self.population.strategy[death] = new_s
increase = 1-new_s*2
self.death = death
return increase, mutate
def evolve(self, turns, profile=None, restart=True, quiet=False, autostop=True):
# self.bind_process()
self.ready()
start_time = datetime.datetime.now()
if restart:
self.gen = 0
self.death = None
self.rewire = None
self.cooperate = np.zeros(turns, dtype=int)
if profile is None:
profile = turns/10
if profile < 1:
profile = 10
# fig, axes = plt.subplots(2, 5, subplot_kw={'xticks': [], 'yticks': []})
# j = 0
for i in xrange(turns):
self.gen += 1
if self.gen % profile == 0:
if not quiet:
print('turn: %d/%d' % (self.gen, turns))
period = min(profile, 500)
delta = self.cooperate[i-period]-self.cooperate[i-1]
if autostop and self.time_to_stop(delta):
print("stop at turn: %d, delta: %d" % (self.gen, delta))
break
# 精度修正 accuracy correction
err = self.game.correct_error(self.death, self.rewire)
# grid = self.population.dynamics.reshape((30, -1))
# fig.subplots_adjust(hspace=0.3, wspace=0.05)
# ax = axes.flat[j]
# im = ax.imshow(grid, interpolation='nearest')
# ax.set_title("Generation %d" % i)
# j += 1
# self.population.check_cache()
if not quiet:
print(self.death, self.rewire, ' error:', err, ' delta:', delta)
self.death, self.rewire = -1, None
inc, _ = self.next_generation(i)
self.cooperate[i] = self.population.cooperate(inc)
if inc == 0:
self.death = -1
end_time = datetime.datetime.now()
# cbar = fig.colorbar(im, ax=axes.ravel().tolist())
# cbar.ax.set_yticklabels(['RD', 'K', 'CNN']) # vertically oriented colorbar
print("Evolution Duration " + str(end_time - start_time))
def evolve_syn(self, turns, profile=None):
"""
同步演化,更新策略只能是Imitation
"""
assert isinstance(self.rule, rule.Imitation)
self.evolve(turns, profile)
def time_to_stop(self, delta):
"""
收敛判断:合作率变化小于1/512
"""
if abs(delta) < (len(self.population) >> 9):
return True
return False
# 合作率曲线
def show(self, *args, **kwargs):
# f = plt.figure(1)
sampling = 20
l = len(self.cooperate)
if sampling <= 0:
x = range(l)
y = self.cooperate
else:
x = np.linspace(0, l, sampling+1, dtype=int)
x[-1] = l-1
y = self.cooperate[x]
plt.plot(x, y, *args, **kwargs)
# x_old = range(len(self.log))
# from scipy.interpolate import spline
# x = np.linspace(x_old[0], x_old[-1], 300)
# y = spline(x_old,self.log,x)
# plt.plot(x,y)
# plt.title('Evolutionary Game')
# plt.xlabel('Step')
# plt.ylabel('Cooperation Ratio')
# plt.ylim([0, len(self.population)])
# if wait:
# f.show()
# else:
# plt.show()
# 保存演化结果
def save(self):
time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
path = os.path.dirname(os.path.realpath(__file__)) + time
if not os.path.exists(path):
os.makedirs(path)
nx.write_pajek(self.population, path+'/graph.net')
sio.savemat(path+'/data.mat', mdict={'gen': self.gen,
'fitness': self.population.fitness,
'strategy': self.population.strategy,
'cooperate': self.cooperate})
# 读取演化结果
def load(self, path):
self.population = nx.read_pajek(path+'/graph.net')
mat = sio.loadmat(path+'/data.mat')
self.gen = mat['gen']
self.population.fitness = mat['fitness']
self.population.strategy = mat['strategy']
self.cooperate = mat['cooperate']
class StaticStrategyEvolution(Evolution):
def __init__(self):
super(self.__class__, self).__init__()
self.adapter = None
self.prefer = None
self.rewire = None
def set_adapter(self, a):
assert (isinstance(a, adapter.Adapter))
self.adapter = a
return self
def bind_process(self):
super(self.__class__, self).bind_process()
self.population.init_dynamics(self.adapter)
self.adapter.bind(self.population)
def evolve(self, turns, **kwargs):
self.rewire = None
self.prefer = np.zeros((turns, self.adapter.category), dtype=np.int)
super(self.__class__, self).evolve(turns, **kwargs)
def next_generation(self, i):
self.game.entire_play()
bdpairs = self.rule.update_all()
# todo 策略如果进行突变会怎么样
# if self.has_mut and np.random.random() <= 0.01:
# new_d = np.random.randint(self.adapter.category)
# mutate = True
# else:
mutate = False
# todo 无论是否更新,都重连?
# if birth == death:
# return 0, False
# todo 无论有没有改变,都进行重连
for birth, death in bdpairs:
# print birth, death
new_d = self.population.dynamics[birth]
self.population.dynamics[death] = new_d
old, new = self.adapter.adapt2(death, birth)
self.prefer[i] = self.population.prefer()
return 0, mutate
def next_generation0(self, i):
if i == 0:
self.game.entire_play()
else:
self.game.rewire_play(self.rewire)
birth, death = self.rule.update()
# todo 策略如果进行突变会怎么样
# if self.has_mut and np.random.random() <= 0.01:
# new_d = np.random.randint(self.adapter.category)
# mutate = True
# else:
new_d = self.population.dynamics[birth]
mutate = False
# todo 无论是否更新,都重连?
# if birth == death:
# return 0, False
# todo 无论有没有改变,都进行重连
old_d = self.population.dynamics[death]
if new_d == old_d:
old_d = None
else:
self.population.dynamics[death] = new_d
self.prefer[i] = self.population.prefer(old_d, new_d)
old, new = self.adapter.adapt2(death, birth)
if old < 0:
self.rewire = None
else:
self.rewire = death, old, new
self.death = None
return 0, mutate
def show(self, fmt, label, *args, **kwargs):
sampling = 20
l = len(self.cooperate)
x = np.linspace(0, l, sampling+1, dtype=int)
x[-1] = l-1
y = self.prefer[x]
colors = 'bgrcmykw'
for i in range(self.adapter.category):
plt.plot(x, y[:, i], colors[i]+fmt[1:], label=label+self.adapter.category_words[i])
class CoEvolution(Evolution):
def __init__(self, has_mut=True):
super(self.__class__, self).__init__(has_mut)
self.adapter = None
self.prefer = None
self.rewire = None
def set_population(self, p):
assert isinstance(p, DynamicPopulation)
self.population = p
return self
def set_adapter(self, a):
assert(isinstance(a, adapter.Adapter))
self.adapter = a.bind(self.population)
def next_generation(self, i):
inc, mutate = super(self.__class__, self).next_generation(i)
new_p = np.random.randint(self.adapter.category) if mutate else self.population.dynamics[self.birth]
old_p = self.population.dynamics[self.death]
if new_p == old_p:
old_p = None
else:
self.population.dynamics[self.death] = new_p
self.prefer[i] = self.population.prefer(old_p, new_p)
old, new = self.adapter.adapt_once(self.death)
if old != new:
self.rewire = (self.death, old, new)
else:
self.rewire = None
return inc, mutate
def evolve(self, turns, **kwargs):
self.rewire = None
self.prefer = np.zeros((turns, self.adapter.category), dtype=np.int)
super(self.__class__, self).evolve(turns, **kwargs)
def show(self, fmt, label, wait=False, *args, **kwargs):
super(self.__class__, self).show(True)
f = plt.figure(2)
color = 'brgcmykw'
# symb = '.ox+*sdph'
label = ['random', 'popularity', 'knn', 'pop*sim', 'similarity']
for i in xrange(self.adapter.category):
plt.plot(self.prefer[:, i], color[i], label=label[i])
plt.title('CoEvolutionary Game')
plt.xlabel('Step')
plt.ylabel('Strategies')
plt.legend()
# plt.show()
f.show()
if __name__ == '__main__':
G = nx.random_regular_graph(5, 10)
pp = Population(G)
gg = game.PDG()
rr = rule.BirthDeath()
e = Evolution()
e.set_population(pp).set_game(gg).set_rule(rr)
e.evolve(10000)
e.show()