-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsolve_game.py
More file actions
377 lines (299 loc) · 12.1 KB
/
solve_game.py
File metadata and controls
377 lines (299 loc) · 12.1 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
Author: Rahul Savani
Note:
This is old and badly written code; one day I might clean it up. For now I just
do minor tweaks to get it working again when it breaks.
It has been tested with lrs 071a
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os
from fractions import Fraction
import string
import argparse
import subprocess
# import StringIO
from io import StringIO
import yaml
def pretty_print (my_matrix):
"""
pretty printing of a 2d matrix so that columns are justified
requires input to really be a matrix (dimension 2), not just a list
"""
# create list of max lengths in each column
max_lens = [max([len(str(r[i])) for r in my_matrix])
for i in range(len(my_matrix[0]))]
print("\n".join(["".join([str(e).rjust(l + 2)
for e, l in zip(r, max_lens)]) for r in my_matrix]))
def create_lrs_input_file(nrow, ncol, m1, m2):
# write the game as a *rational* matrix for use with lrs
import sympy as sy
m1_rat = sy.Matrix(m1).applyfunc(sy.Rational)
m2_rat = sy.Matrix(m2).applyfunc(sy.Rational)
fpath = os.path.join('tmp','rational_input.txt')
with open(fpath, 'w') as outfile:
outfile.write("%s %s\n\n" % (nrow,ncol))
tmp = m1_rat.tolist()
for row in tmp:
outfile.write(" ".join([str(t) for t in row]))
outfile.write("\n")
outfile.write("\n")
tmp = m2_rat.tolist()
for row in tmp:
outfile.write(" ".join([str(t) for t in row]))
outfile.write("\n")
return fpath
def process_lrs_output(string_input = None, fpath = 'tmp/out'):
"""
create:
- text output of equilibria in decimal and fractional format
- input for clique_enumeration
"""
global store
store = {}
if string_input is not None:
# string_input takes precedence
# buf = StringIO.StringIO(string_input)
buf = StringIO(string_input)
else:
# read from fpath
buf = open(fpath, 'r')
# x is an dictionary with integer line numbers as keys (so indexing from 1)
x, i = {}, 1
for line in buf.readlines():
x[i] = line.split()
i+=1
buf.close()
######################################################
# Number of extreme equilibria
######################################################
# changed with use of lrsnash to i-5
# lrsnash 062
# numberOfEq = int(x[i-5][4])
# Extra final row in lrsnash 071 compared to 062
numberOfEq = int(x[i-6][4])
store['number_of_extreme_eq'] = numberOfEq
# store mixed strategies as arrays of string probabilities
e1, e2 = {}, {}
# store payoffs
p1, p2 = {}, {}
######################################################
# DICTIONARIES for mixed strategies
######################################################
# Mixed strategies strings as keys (e.g. '1/2,1/4,1/4')
# Indices as values
dict1, dict2 = {}, {}
# store indices for mixed strategies for input to clique algorithm
index1, index2 = {}, {}
# next index for input to clique algorithm
c1, c2 = 1, 1
eq = -1 # index of current equilibrium (shared by e1,e2,p1,p2,index1,index2)
count = 0 # how many equilibria of II to match with one
# first line of out which may be a vertex (counting from 1 as used in range)
first_line = 2
for j in range(first_line,len(x)-5):
if not x[j]:
count = 0 # reset count, ready for next set of II's strategies
continue
elif x[j][0] == "2":
processII = True
count += 1 # one more of II's strategies to pair with I's
eq += 1
elif x[j][0] == "1":
processII = False
else:
# here we skip lines like e.g.,
# *Input linearity in row 16 is redundant--skipped
print("skipping line: %s", x[j])
continue
l = len(x[j])
##########################################
# Player II
##########################################
if processII : # loop through all mixed strategies of II
e2[eq] = x[j][1:l-1]
p1[eq] = x[j][l-1] # payoffs swapped in lrs output
e2string = ','.join(e2[eq])
if e2string not in dict2.keys():
dict2[e2string] = c2
c2 += 1
index2[eq] = dict2[e2string]
else:
#################################################
# Player I
#################################################
# Now match all these count-many strategies of II
# with # subsequent strategy of I
e1[eq] = x[j][1:l-1]
p2[eq] = x[j][l-1] # payoffs swapped in lrs output
e1string = ','.join(e1[eq])
if e1string not in dict1.values():
dict1[e1string] = c1
c1 += 1
index1[eq] = dict1[e1string]
for i in range(1,count):
e1[eq-i] = e1[eq]
p2[eq-i] = p2[eq]
index1[eq-i] = index1[eq]
rat = [] # list to contain list of strings (for pretty printing)
dec = [] # list to contain list of string (for pretty printing)
for i in range(numberOfEq):
# convert probability strings to fractions to floats to strings
e1decstr = ['{0:F}'.format(float(Fraction(s))) for s in e1[i]]
e2decstr = ['{0:F}'.format(float(Fraction(s))) for s in e2[i]]
# initialize empty rows
rat.append([])
dec.append([])
#
rat[i].append("EE")
dec[i].append("EE")
# EE index
rat[i].append(str(i+1))
dec[i].append(str(i+1))
#
rat[i].append("P1:")
dec[i].append("P1:")
# PI strategy index (for connected components)
rat[i].append("("+str(index1[i])+")")
dec[i].append("("+str(index1[i])+")")
# PI strategy probabilities
for entry in e1[i]:
rat[i].append(entry)
for entry in e1decstr:
dec[i].append(entry)
rat[i].append("EP=")
dec[i].append("EP=")
# PI payoff
rat[i].append(str(p1[i]))
dec[i].append(str(float(Fraction(str(p1[i])))))
#
rat[i].append("P2:")
dec[i].append("P2:")
# PII strategy index (for connected components)
rat[i].append("("+str(index2[i])+")")
dec[i].append("("+str(index2[i])+")")
# PII strategy probabilities
for entry in e2[i]:
rat[i].append(entry)
for entry in e2decstr:
dec[i].append(entry)
rat[i].append("EP=")
dec[i].append("EP=")
# PII payoff
rat[i].append(str(p2[i]))
dec[i].append(str(float(Fraction(str(p2[i])))))
store['decimal_output'] = dec
store['rational_output'] = rat
return store, index1, index2
def clique_enumeration(numberOfEq, index1, index2):
assert numberOfEq == len(index1) == len(index2)
# open clique enumeration input file
fcin = open('tmp/clique_input.txt', 'w')
# print indices to clique enumeration input file
for i in range(numberOfEq):
fcin.write("{0} {1}\n".format(index1[i],index2[i]))
# close file
fcin.close()
# do clique enumeration
os.system("bin/clique <tmp/clique_input.txt >tmp/clique_output.txt")
# open clique enumeration output file
fcout = open('tmp/clique_output.txt', 'r')
clique_output = fcout.read()
#############################################################
return clique_output
def print_output(store):
print("%d x %d payoff matrix A:\n" % (nrow, ncol))
pretty_print(store['m1'])
print()
print("%d x %d payoff matrix B:\n" % (nrow, ncol))
pretty_print(store['m2'])
print()
print("EE = Extreme Equilibrium, EP = Expected Payoff\n")
print("Decimal Output\n")
pretty_print(store['decimal_output'])
print()
print("Rational Output\n")
pretty_print(store['rational_output'])
print(store['clique_output'])
def parse_input_game(fpath):
"""
takes a path to an input file for lrsnash
returns nrow, ncol, m1, m2
where m1 and m2 are both lists of lists representing the payoff matrices
"""
f= open(fpath, 'r')
# list of individual lines
lines = f.readlines()
# drop any blank lines
lines = [l for l in lines if l != '\n']
# extract the dimensions
try:
nrow, ncol = [int(e) for e in lines[0].split()]
except:
print("First row of input file should be the number of rows, a space, and then the number of columns")
# get rid of first row of input
lines = lines[1:]
# check that the number of lines is correct
assert len(lines) == 2*nrow, "nrow = %d, so expected %d rows in the matrics, but got %d" % (nrow,2*nrow,len(lines))
m1 = lines[0:nrow]
m2 = lines[nrow:]
m1 = [r.split() for r in m1]
m2 = [r.split() for r in m2]
return nrow, ncol, m1, m2
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Bimatrix Solver')
input_dir = os.path.join('examples','input') # assumes we run from root of rrepo
choices = os.listdir(input_dir)
choices = [os.path.join(input_dir, c) for c in choices]
parser.add_argument('--input_path', '-i',
default=choices[0],
help='Path to game input text file, examples: %s' % " ".join(choices))
parser.add_argument('--store_lrs_output', '-o', action='store_true',
help='Store intermediate lrs output file')
args = parser.parse_args()
assert os.path.isfile(args.input_path), "%s is not a file" % args.input_path
###########################################################################
# Parses game, which is already in the right form for lrs input
# except if may contain decimals
###########################################################################
nrow, ncol, m1, m2 = parse_input_game(args.input_path)
###########################################################################
# Create input file for lrsnash, using sympy to create fractional inputs
###########################################################################
fpath = create_lrs_input_file(nrow, ncol, m1, m2)
###########################################################################
# system call to lrsnas2
###########################################################################
result = subprocess.check_output(['bin/lrsnash', fpath])
###########################################################################
# print and save lrs output
###########################################################################
result_string = result.decode('utf-8')
print(result_string)
if args.store_lrs_output:
text_file = open("tmp/out", "w")
text_file.write(result_string)
text_file.close()
###########################################################################
# process lrs output
###########################################################################
store, index1, index2 = process_lrs_output(result_string)
store['ncol'] = ncol
store['nrow'] = nrow
store['m1'] = m1
store['m2'] = m2
###########################################################################
# do clique enumeration
###########################################################################
store['clique_output'] = clique_enumeration(store['number_of_extreme_eq'],
index1,
index2)
###########################################################################
# print in original "banach.lse.ac.uk" format
# and store to yaml
###########################################################################
print_output(store)
# save dictionary to a yaml file
# TODO: tidy up contents of dictionary (e.g. strings to numbers)
with open('tmp/out.yaml', 'w') as outfile:
yaml.safe_dump(store, outfile)