Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from __future__ import print_function 

 

class Parser(): 

r""" 

A class for parsing the outputs of different algorithms called in other 

software packages. 

 

Two parsers are included, one for the ``'lrs'`` algorithm and another for 

the ``'LCP'`` algorithm. 

""" 

 

def __init__(self, raw_string): 

""" 

Initialise a Parser instance by storing a raw_string 

(currently only used with H representation of a game). 

 

TESTS: 

 

Simply checking that we have the correct string output 

for the H representation (which is the format required 

for the ``'lrs'`` algorithm):: 

 

sage: from sage.game_theory.parser import Parser 

sage: A = matrix([[1, 2], [3, 2]]) 

sage: g = NormalFormGame([A]) 

sage: raw_string = g._Hrepresentation(A, -A) 

sage: P = Parser(raw_string) 

sage: print(P.raw_string[0]) 

H-representation 

linearity 1 5 

begin 

5 4 rational 

0 1 0 0 

0 0 1 0 

0 1 3 1 

0 2 2 1 

-1 1 1 0 

end 

<BLANKLINE> 

 

sage: print(P.raw_string[1]) 

H-representation 

linearity 1 5 

begin 

5 4 rational 

0 -1 -2 1 

0 -3 -2 1 

0 1 0 0 

0 0 1 0 

-1 1 1 0 

end 

<BLANKLINE> 

 

The specific case of a game with 1 strategy for each player:: 

 

sage: A = matrix([[1]]) 

sage: B = matrix([[5]]) 

sage: g = NormalFormGame([A,B]) 

sage: raw_string = g._Hrepresentation(A, B) 

sage: P = Parser(raw_string) 

sage: print(P.raw_string[0]) 

H-representation 

linearity 1 3 

begin 

3 3 rational 

0 1 0 

0 -5 1 

-1 1 0 

end 

<BLANKLINE> 

 

sage: print(P.raw_string[1]) 

H-representation 

linearity 1 3 

begin 

3 3 rational 

0 -1 1 

0 1 0 

-1 1 0 

end 

<BLANKLINE> 

 

Another test:: 

 

sage: from sage.game_theory.parser import Parser 

sage: A = matrix([[-7, -5, 5], 

....: [5, 5, 3], 

....: [1, -6, 1]]) 

sage: B = matrix([[-9, 7, 9], 

....: [6, -2, -3], 

....: [-4, 6, -10]]) 

sage: g = NormalFormGame([A, B]) 

sage: raw_string = g._Hrepresentation(A, B) 

sage: P = Parser(raw_string) 

sage: print(P.raw_string[0]) 

H-representation 

linearity 1 7 

begin 

7 5 rational 

0 1 0 0 0 

0 0 1 0 0 

0 0 0 1 0 

0 9 -6 4 1 

0 -7 2 -6 1 

0 -9 3 10 1 

-1 1 1 1 0 

end 

<BLANKLINE> 

 

sage: print(P.raw_string[1]) 

H-representation 

linearity 1 7 

begin 

7 5 rational 

0 7 5 -5 1 

0 -5 -5 -3 1 

0 -1 6 -1 1 

0 1 0 0 0 

0 0 1 0 0 

0 0 0 1 0 

-1 1 1 1 0 

end 

<BLANKLINE> 

 

This class is also used to parse the output of algorithms from the gambit 

python interface using the `format_gambit` function. 

""" 

self.raw_string = raw_string 

 

def format_lrs(self): 

""" 

Parses the output of lrs so as to return vectors 

corresponding to equilibria. 

 

TESTS:: 

 

sage: from sage.game_theory.parser import Parser 

sage: from subprocess import Popen, PIPE 

sage: A = matrix([[1, 2], [3, 2]]) 

sage: g = NormalFormGame([A]) 

sage: game1_str, game2_str = g._Hrepresentation(A, -A) 

sage: g1_name = tmp_filename() 

sage: g2_name = tmp_filename() 

sage: g1_file = open(g1_name, 'w') 

sage: g2_file = open(g2_name, 'w') 

sage: _ = g1_file.write(game1_str) 

sage: g1_file.close() 

sage: _ = g2_file.write(game2_str) 

sage: g2_file.close() 

sage: process = Popen(['lrsnash', g1_name, g2_name], stdout=PIPE, stderr=PIPE) # optional - lrslib 

sage: lrs_output = [row for row in process.stdout] # optional - lrslib 

 

The above creates a game, writes the H representation to 

temporary files, calls lrs and stores the output in `lrs_output` 

(here slicing to get rid of some system parameters that get returned):: 

 

sage: lrs_output[5:16] # optional - lrslib 

['\n', 

'***** 4 4 rational\n', 

'2 0 1 2 \n', 

'1 1/2 1/2 -2 \n', 

'\n', 

'2 0 1 2 \n', 

'1 0 1 -2 \n', 

'\n', 

'\n', 

'*Number of equilibria found: 2\n', 

'*Player 1: vertices=3 bases=3 pivots=5\n'] 

 

The above is pretty messy, here is the output when we put it through 

the parser:: 

 

sage: nasheq = Parser(lrs_output).format_lrs() # optional - lrslib 

sage: nasheq # optional - lrslib 

[[(1/2, 1/2), (0, 1)], [(0, 1), (0, 1)]] 

 

Another game:: 

 

sage: A = matrix([[-7, -5, 5], 

....: [5, 5, 3], 

....: [1, -6, 1]]) 

sage: B = matrix([[-9, 7, 9], 

....: [6, -2, -3], 

....: [-4, 6, -10]]) 

sage: g = NormalFormGame([A, B]) 

sage: game1_str, game2_str = g._Hrepresentation(A, B) 

sage: g1_name = tmp_filename() 

sage: g2_name = tmp_filename() 

sage: g1_file = open(g1_name, 'w') 

sage: g2_file = open(g2_name, 'w') 

sage: _ = g1_file.write(game1_str) 

sage: g1_file.close() 

sage: _ = g2_file.write(game2_str) 

sage: g2_file.close() 

sage: process = Popen(['lrsnash', g1_name, g2_name], stdout=PIPE, stderr=PIPE) # optional - lrslib 

sage: lrs_output = [row for row in process.stdout] # optional - lrslib 

sage: print(lrs_output[5:20]) # optional - lrslib 

['\n', 

'***** 5 5 rational\n', 

'2 1/7 0 6/7 23/7 \n', 

'2 0 1/6 5/6 10/3 \n', 

'1 1/3 2/3 0 1 \n', 

'\n', 

'2 0 0 1 5 \n', 

'1 1 0 0 9 \n', 

'\n', 

'2 1 0 0 5 \n', 

'1 0 1 0 6 \n', 

'\n', 

'\n', 

'*Number of equilibria found: 4\n', 

'*Player 1: vertices=6 bases=7 pivots=10\n'] 

 

sage: nasheq = Parser(lrs_output).format_lrs() # optional - lrslib 

sage: sorted(nasheq) # optional - lrslib 

[[(0, 1, 0), (1, 0, 0)], 

[(1/3, 2/3, 0), (0, 1/6, 5/6)], 

[(1/3, 2/3, 0), (1/7, 0, 6/7)], 

[(1, 0, 0), (0, 0, 1)]] 

""" 

equilibria = [] 

from sage.misc.sage_eval import sage_eval 

from itertools import groupby 

for collection in [list(x[1]) for x in groupby(self.raw_string[7:], lambda x: x == '\n')]: 

if collection[0].startswith('2'): 

s1 = tuple([sage_eval(k) for k in collection[-1].split()][1:-1]) 

for s2 in collection[:-1]: 

s2 = tuple([sage_eval(k) for k in s2.split()][1:-1]) 

equilibria.append([s1, s2]) 

 

return equilibria 

 

def format_gambit(self, gambit_game): 

""" 

Parses the output of gambit so as to return vectors 

corresponding to equilibria obtained using the LCP algorithm. 

 

TESTS: 

 

Here we construct a two by two game in gambit:: 

 

sage: import gambit # optional - gambit 

sage: from sage.game_theory.parser import Parser 

sage: g = gambit.Game.new_table([2,2]) # optional - gambit 

sage: g[int(0), int(0)][int(0)] = int(2) # optional - gambit 

sage: g[int(0), int(0)][int(1)] = int(1) # optional - gambit 

sage: g[int(0), int(1)][int(0)] = int(0) # optional - gambit 

sage: g[int(0), int(1)][int(1)] = int(0) # optional - gambit 

sage: g[int(1), int(0)][int(0)] = int(0) # optional - gambit 

sage: g[int(1), int(0)][int(1)] = int(0) # optional - gambit 

sage: g[int(1), int(1)][int(0)] = int(1) # optional - gambit 

sage: g[int(1), int(1)][int(1)] = int(2) # optional - gambit 

sage: solver = gambit.nash.ExternalLCPSolver() # optional - gambit 

 

Here is the output of the LCP algorithm:: 

 

sage: LCP_output = solver.solve(g) # optional - gambit 

sage: LCP_output # optional - gambit 

[<NashProfile for '': [[1.0, 0.0], [1.0, 0.0]]>, 

<NashProfile for '': [[0.6666666667, 0.3333333333], [0.3333333333, 0.6666666667]]>, 

<NashProfile for '': [[0.0, 1.0], [0.0, 1.0]]>] 

 

The Parser class outputs the equilibrium:: 

 

sage: nasheq = Parser(LCP_output).format_gambit(g) # optional - gambit 

sage: nasheq # optional - gambit 

[[(1.0, 0.0), (1.0, 0.0)], [(0.6666666667, 0.3333333333), (0.3333333333, 0.6666666667)], [(0.0, 1.0), (0.0, 1.0)]] 

 

Here is another game:: 

 

sage: g = gambit.Game.new_table([2,2]) # optional - gambit 

sage: g[int(0), int(0)][int(0)] = int(4) # optional - gambit 

sage: g[int(0), int(0)][int(1)] = int(8) # optional - gambit 

sage: g[int(0), int(1)][int(0)] = int(0) # optional - gambit 

sage: g[int(0), int(1)][int(1)] = int(1) # optional - gambit 

sage: g[int(1), int(0)][int(0)] = int(1) # optional - gambit 

sage: g[int(1), int(0)][int(1)] = int(3) # optional - gambit 

sage: g[int(1), int(1)][int(0)] = int(1) # optional - gambit 

sage: g[int(1), int(1)][int(1)] = int(0) # optional - gambit 

sage: solver = gambit.nash.ExternalLCPSolver() # optional - gambit 

 

Here is the LCP output:: 

 

sage: LCP_output = solver.solve(g) # optional - gambit 

sage: LCP_output # optional - gambit 

[<NashProfile for '': [[1.0, 0.0], [1.0, 0.0]]>] 

 

The corresponding parsed equilibrium:: 

 

sage: nasheq = Parser(LCP_output).format_gambit(g) # optional - gambit 

sage: nasheq # optional - gambit 

[[(1.0, 0.0), (1.0, 0.0)]] 

 

Here is a larger degenerate game:: 

 

sage: g = gambit.Game.new_table([3,3]) # optional - gambit 

sage: g[int(0), int(0)][int(0)] = int(-7) # optional - gambit 

sage: g[int(0), int(0)][int(1)] = int(-9) # optional - gambit 

sage: g[int(0), int(1)][int(0)] = int(-5) # optional - gambit 

sage: g[int(0), int(1)][int(1)] = int(7) # optional - gambit 

sage: g[int(0), int(2)][int(0)] = int(5) # optional - gambit 

sage: g[int(0), int(2)][int(1)] = int(9) # optional - gambit 

sage: g[int(1), int(0)][int(0)] = int(5) # optional - gambit 

sage: g[int(1), int(0)][int(1)] = int(6) # optional - gambit 

sage: g[int(1), int(1)][int(0)] = int(5) # optional - gambit 

sage: g[int(1), int(1)][int(1)] = int(-2) # optional - gambit 

sage: g[int(1), int(2)][int(0)] = int(3) # optional - gambit 

sage: g[int(1), int(2)][int(1)] = int(-3) # optional - gambit 

sage: g[int(2), int(0)][int(0)] = int(1) # optional - gambit 

sage: g[int(2), int(0)][int(1)] = int(-4) # optional - gambit 

sage: g[int(2), int(1)][int(0)] = int(-6) # optional - gambit 

sage: g[int(2), int(1)][int(1)] = int(6) # optional - gambit 

sage: g[int(2), int(2)][int(0)] = int(1) # optional - gambit 

sage: g[int(2), int(2)][int(1)] = int(-10) # optional - gambit 

sage: solver = gambit.nash.ExternalLCPSolver() # optional - gambit 

 

Here is the LCP output:: 

 

sage: LCP_output = solver.solve(g) # optional - gambit 

sage: LCP_output # optional - gambit 

[<NashProfile for '': [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]>, 

<NashProfile for '': [[0.3333333333, 0.6666666667, 0.0], [0.1428571429, 0.0, 0.8571428571]]>, 

<NashProfile for '': [[0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]>] 

 

The corresponding parsed equilibrium:: 

 

sage: nasheq = Parser(LCP_output).format_gambit(g) # optional - gambit 

sage: nasheq # optional - gambit 

[[(1.0, 0.0, 0.0), (0.0, 0.0, 1.0)], 

[(0.3333333333, 0.6666666667, 0.0), (0.1428571429, 0.0, 0.8571428571)], 

[(0.0, 1.0, 0.0), (1.0, 0.0, 0.0)]] 

 

Note, that this differs from the same output of the lrs algorithm due 

the fact that the game is degenerate. 

""" 

nice_stuff = [] 

for gambitstrategy in self.raw_string: 

gambitstrategy = list(gambitstrategy) 

profile = [tuple(gambitstrategy[:len(gambit_game.players[int(0)].strategies)])] 

for player in list(gambit_game.players)[1:]: 

previousplayerstrategylength = len(profile[-1]) 

profile.append(tuple(gambitstrategy[previousplayerstrategylength: previousplayerstrategylength + len(player.strategies)])) 

nice_stuff.append(profile) 

 

return nice_stuff