-
Notifications
You must be signed in to change notification settings - Fork 0
/
manygames.py
350 lines (335 loc) · 16.8 KB
/
manygames.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
348
import commoninterest as ci
import json
import os
import pickle
import time
def findgames(dimension): # The way I use this function is as follows:
# I uncomment lines 27-64, and comment lines 14-23. Run the function and
# kill it with ^C. This creates the pickled files. Then I comment and
# uncomment again until it is as shown here. This way I can halt the
# calculation whenever I want (with ^C). This creates pickled files which
# are read whenever I resume.
with open("pickledsender", "rb") as psender:
gamessender = pickle.load(psender)
with open("pickledreceiver", "rb") as preceiver:
gamesreceiver = pickle.load(preceiver)
with open("pickledpairsender", "rb") as psender:
pairsender = pickle.load(psender)
with open("pickledpairreceiver", "rb") as preceiver:
pairreceiver = pickle.load(preceiver)
with open("pickledpossiblepairs", "rb") as pp:
possible_pairs = pickle.load(pp)
try:
possible_cstars = []
#possible_pairs = []
#pairsender = []
#pairreceiver = []
#gamessender = {}
#gamesreceiver = {}
timestr = time.strftime("%d%b%H-%M")
#for pair in pairsender:
#gamessender[str(pair)] = []
#gamesreceiver[str(pair)] = []
#game = ci.Game(ci.payoffs(dimension))
#gamepairsender = [game.cistar, game.starsender]
#gamepairreceiver = [game.cistar, game.starreceiver]
#if gamepairsender not in possible_pairs or gamepairreceiver not in possible_pairs:
#equilibria = game.just_equilibria()
#if gamepairsender not in possible_pairs:
#ppold = possible_pairs
#possible_pairs.append(gamepairsender)
#pairsender.append(gamepairsender)
#pairreceiver.append(gamepairsender)
#gamessender[str(gamepairsender)] = []
#gamesreceiver[str(gamepairsender)] = []
#gameentry = {}
#gameentry[str(game.payoffs)] = equilibria
#gamessender[str(gamepairsender)].append(gameentry)
#print("sender", gamepairsender,
#len(gamessender[str(gamepairsender)]))
#if gamepairreceiver not in ppold:
#if gamepairreceiver not in possible_pairs:
#possible_pairs.append(gamepairreceiver)
#pairreceiver.append(gamepairreceiver)
#pairsender.append(gamepairreceiver)
#gamesreceiver[str(gamepairreceiver)] = []
#gamessender[str(gamepairreceiver)] = []
#gameentry = {}
#gameentry[str(game.payoffs)] = equilibria
#gamesreceiver[str(gamepairreceiver)].append(gameentry)
#print("receiver", gamepairreceiver,
#len(gamesreceiver[str(gamepairreceiver)]))
while pairsender != [] or pairreceiver != []:
game = ci.Game(ci.payoffs(dimension))
gamepairsender = [game.cistar, game.starsender]
gamepairreceiver = [game.cistar, game.starreceiver]
if gamepairsender not in possible_pairs or gamepairreceiver not in possible_pairs or gamepairsender in pairsender or gamepairreceiver in pairreceiver:
equilibria = game.just_equilibria()
if gamepairsender not in possible_pairs:
possible_pairs.append(gamepairsender)
pairsender.append(gamepairsender)
pairreceiver.append(gamepairsender)
gamessender[str(gamepairsender)] = []
gamesreceiver[str(gamepairsender)] = []
if gamepairreceiver not in possible_pairs:
possible_pairs.append(gamepairreceiver)
pairsender.append(gamepairreceiver)
gamessender[str(gamepairreceiver)] = []
pairreceiver.append(gamepairreceiver)
gamesreceiver[str(gamepairreceiver)] = []
for pair in pairsender:
if gamepairsender == pair:
gameentry = {}
gameentry[str(game.payoffs)] = equilibria
gamessender[str(pair)].append(gameentry)
print("sender", pair, len(gamessender[str(pair)]))
if len(gamessender[str(pair)]) > 1499:
pairsender.remove(pair)
print("sender", pair, "done.")
filename = ''.join(["sender", str(pair), timestr])
with open(filename, 'w') as senderpair:
json.dump(gamessender[str(pair)], senderpair)
for pair in pairreceiver:
if gamepairreceiver == pair:
gameentry = {}
gameentry[str(game.payoffs)] = equilibria
gamesreceiver[str(pair)].append(gameentry)
print("receiver", pair, len(gamesreceiver[str(pair)]))
if len(gamesreceiver[str(pair)]) > 1499:
pairreceiver.remove(pair)
print("receiver", pair, "done.")
filename = ''.join(["receiver", str(pair), timestr])
with open(filename, 'w') as receiverpair:
json.dump(gamesreceiver[str(pair)], receiverpair)
with open("gamessender", "w") as senderfile:
json.dump(gamessender, senderfile)
with open("gamesreceiver", "w") as receiverfile:
json.dump(gamesreceiver, receiverfile)
with open("pickledsender", "wb") as psender:
pickle.dump(gamessender, psender)
with open("pickledreceiver", "wb") as preceiver:
pickle.dump(gamesreceiver, preceiver)
with open("pickledpairsender", "wb") as psender:
pickle.dump(pairsender, psender)
with open("pickledpairreceiver", "wb") as preceiver:
pickle.dump(pairreceiver, preceiver)
with open("pickledpossiblepairs", "wb") as pp:
pickle.dump(possible_pairs, pp)
except KeyboardInterrupt:
with open("pickledsender", "wb") as psender:
pickle.dump(gamessender, psender)
with open("pickledreceiver", "wb") as preceiver:
pickle.dump(gamesreceiver, preceiver)
with open("pickledpairsender", "wb") as psender:
pickle.dump(pairsender, psender)
with open("pickledpairreceiver", "wb") as preceiver:
pickle.dump(pairreceiver, preceiver)
with open("pickledpossiblepairs", "wb") as pp:
pickle.dump(possible_pairs, pp)
def findgames_CKnostar(dimension):
with open("pickledsender", "rb") as psender:
gamessender = pickle.load(psender)
with open("pickledreceiver", "rb") as preceiver:
gamesreceiver = pickle.load(preceiver)
with open("pickledpairsender", "rb") as psender:
pairsender = pickle.load(psender)
with open("pickledpairreceiver", "rb") as preceiver:
pairreceiver = pickle.load(preceiver)
with open("pickledpossiblepairs", "rb") as pp:
possible_pairs = pickle.load(pp)
with open("pickledcs", "rb") as pp:
possible_cs = pickle.load(pp)
with open("pickledgames", "rb") as pp:
games = pickle.load(pp)
try:
#possible_cs = []
#possible_pairs = []
#pairsender = []
#pairreceiver = []
#games = {}
#gamessender = {}
#gamesreceiver = {}
timestr = time.strftime("%d%b%H-%M")
#for pair in pairsender:
#gamessender[str(pair)] = []
#gamesreceiver[str(pair)] = []
#game = ci.Game(ci.payoffs(dimension))
#gamec = game.kendalldistance
#gamepairsender = [game.kendalldistance, game.kendallsender]
#gamepairreceiver = [game.kendalldistance, game.kendallreceiver]
#if gamepairsender not in possible_pairs or gamepairreceiver not in possible_pairs:
#equilibria = game.just_equilibria()
#if gamepairsender not in possible_pairs:
#ppold = possible_pairs
#possible_cs.append(gamec)
#possible_pairs.append(gamepairsender)
#pairsender.append(gamepairsender)
#pairreceiver.append(gamepairsender)
#gamessender[str(gamepairsender)] = []
#gamesreceiver[str(gamepairsender)] = []
#games[str(gamec)] = []
#gameentry = {}
#gameentry[str(game.payoffs)] = equilibria
#gamessender[str(gamepairsender)].append(gameentry)
#games[str(gamec)].append(gameentry)
#print("sender", gamepairsender,
#len(gamessender[str(gamepairsender)]))
#if gamepairreceiver not in ppold:
#if gamepairreceiver not in possible_pairs:
#possible_pairs.append(gamepairreceiver)
#pairreceiver.append(gamepairreceiver)
#pairsender.append(gamepairreceiver)
#gamesreceiver[str(gamepairreceiver)] = []
#gamessender[str(gamepairreceiver)] = []
#gameentry = {}
#gameentry[str(game.payoffs)] = equilibria
#gamesreceiver[str(gamepairreceiver)].append(gameentry)
#print("receiver", gamepairreceiver,
#len(gamesreceiver[str(gamepairreceiver)]))
while pairsender != [] or pairreceiver != []:
game = ci.Game(ci.payoffs(dimension))
gamec = game.kendalldistance
gamepairsender = [game.kendalldistance, game.kendallsender]
gamepairreceiver = [game.kendalldistance, game.kendallreceiver]
if gamepairsender not in possible_pairs or gamepairreceiver not in possible_pairs or gamepairsender in pairsender or gamepairreceiver in pairreceiver:
equilibria = game.just_equilibria()
#if gamec not in possible_cs:
#possible_cs.append(gamec)
#games[str(gamec)] = []
if gamepairsender not in possible_pairs:
possible_pairs.append(gamepairsender)
pairsender.append(gamepairsender)
pairreceiver.append(gamepairsender)
gamessender[str(gamepairsender)] = []
gamesreceiver[str(gamepairsender)] = []
if gamepairreceiver not in possible_pairs:
possible_pairs.append(gamepairreceiver)
pairsender.append(gamepairreceiver)
gamessender[str(gamepairreceiver)] = []
pairreceiver.append(gamepairreceiver)
gamesreceiver[str(gamepairreceiver)] = []
for pair in pairsender:
if gamepairsender == pair:
gameentry = {}
gameentry[str(game.payoffs)] = equilibria
gamessender[str(pair)].append(gameentry)
print("sender", pair, len(gamessender[str(pair)]))
if len(gamessender[str(pair)]) > 1499:
pairsender.remove(pair)
print("sender", pair, "done.")
filename = ''.join(["sender", str(pair), timestr])
with open(filename, 'w') as senderpair:
json.dump(gamessender[str(pair)], senderpair)
for pair in pairreceiver:
if gamepairreceiver == pair:
gameentry = {}
gameentry[str(game.payoffs)] = equilibria
gamesreceiver[str(pair)].append(gameentry)
print("receiver", pair, len(gamesreceiver[str(pair)]))
if len(gamesreceiver[str(pair)]) > 1499:
pairreceiver.remove(pair)
print("receiver", pair, "done.")
filename = ''.join(["receiver", str(pair), timestr])
with open(filename, 'w') as receiverpair:
json.dump(gamesreceiver[str(pair)], receiverpair)
#for c in possible_cs:
#if gamec == c:
#gameentry = {}
#gameentry[str(game.payoffs)] = equilibria
#games[str(c)].append(gameentry)
#print("c", c, len(games[str(c)]))
#if len(games[str(c)]) > 1499:
#possible_cs.remove(c)
#print("c", c, "done.")
#filename = ''.join(["c", str(c), timestr])
#with open(filename, 'w') as cs:
#json.dump(games[str(c)], cs)
with open("gamessender", "w") as senderfile:
json.dump(gamessender, senderfile)
with open("gamesreceiver", "w") as receiverfile:
json.dump(gamesreceiver, receiverfile)
with open("pickledsender", "wb") as psender:
pickle.dump(gamessender, psender)
with open("pickledreceiver", "wb") as preceiver:
pickle.dump(gamesreceiver, preceiver)
with open("pickledpairsender", "wb") as psender:
pickle.dump(pairsender, psender)
with open("pickledpairreceiver", "wb") as preceiver:
pickle.dump(pairreceiver, preceiver)
with open("pickledpossiblepairs", "wb") as pp:
pickle.dump(possible_pairs, pp)
with open("pickledcs", "wb") as pp:
pickle.dump(possible_cs, pp)
with open("pickledgames", "wb") as pp:
pickle.dump(games, pp)
except KeyboardInterrupt:
with open("pickledsender", "wb") as psender:
pickle.dump(gamessender, psender)
with open("pickledreceiver", "wb") as preceiver:
pickle.dump(gamesreceiver, preceiver)
with open("pickledpairsender", "wb") as psender:
pickle.dump(pairsender, psender)
with open("pickledpairreceiver", "wb") as preceiver:
pickle.dump(pairreceiver, preceiver)
with open("pickledpossiblepairs", "wb") as pp:
pickle.dump(possible_pairs, pp)
with open("pickledcs", "wb") as pp:
pickle.dump(possible_cs, pp)
with open("pickledgames", "wb") as pp:
pickle.dump(games, pp)
def findgames_Cnostar(dimension):
with open("pickledoutcs", "rb") as pp:
outstanding_cs = pickle.load(pp)
with open("pickledcs", "rb") as pp:
possible_cs = pickle.load(pp)
with open("pickledgames", "rb") as pp:
games = pickle.load(pp)
try:
#possible_cs = []
#outstanding_cs = []
#games = {}
timestr = time.strftime("%d%b%H-%M")
#game = ci.Game(ci.payoffs(dimension))
#gamec = game.cistar
#if gamec not in possible_cs:
#equilibria = game.just_equilibria()
#possible_cs.append(gamec)
#outstanding_cs.append(gamec)
#games[str(gamec)] = []
#gameentry = {}
#gameentry[str(game.payoffs)] = equilibria
#games[str(gamec)].append(gameentry)
while outstanding_cs != []:
game = ci.Game(ci.payoffs(dimension))
gamec = game.cistar
if gamec not in possible_cs or gamec in outstanding_cs:
equilibria = game.just_equilibria()
if gamec not in possible_cs:
possible_cs.append(gamec)
outstanding_cs.append(gamec)
games[str(gamec)] = []
for c in possible_cs:
if gamec == c:
gameentry = {}
gameentry[str(game.payoffs)] = equilibria
games[str(c)].append(gameentry)
print("c", c, len(games[str(c)]))
if len(games[str(c)]) > 1499:
outstanding_cs.remove(c)
print("c", c, "done.")
filename = ''.join(["c", str(c), timestr])
with open(filename, 'w') as cs:
json.dump(games[str(c)], cs)
with open("pickledoutcs", "wb") as pp:
pickle.dump(outstanding_cs, pp)
with open("pickledcs", "wb") as pp:
pickle.dump(possible_cs, pp)
with open("pickledgames", "wb") as pp:
pickle.dump(games, pp)
except KeyboardInterrupt:
with open("pickledoutcs", "wb") as pp:
pickle.dump(outstanding_cs, pp)
with open("pickledcs", "wb") as pp:
pickle.dump(possible_cs, pp)
with open("pickledgames", "wb") as pp:
pickle.dump(games, pp)