|
27 | 27 | [[-19., -19., 1.], [ -8., -8., 2.], [ 3., 3., 3.]]]] |
28 | 28 |
|
29 | 29 | """ |
| 30 | +import io |
| 31 | +import sys |
30 | 32 | import numbers |
31 | 33 | import numpy as np |
32 | 34 | from .normal_form_game import Player, NormalFormGame |
@@ -326,17 +328,27 @@ def to_string(cls, g): |
326 | 328 |
|
327 | 329 | @staticmethod |
328 | 330 | def _dump(g): |
329 | | - s = str(g.N) + '\n' |
330 | | - s += ' '.join(map(str, g.nums_actions)) + '\n\n' |
| 331 | + p = GAMPayoffVector.from_nfg(g) |
331 | 332 |
|
332 | | - for i, player in enumerate(g.players): |
333 | | - payoffs = np.array2string( |
334 | | - player.payoff_array.transpose( |
335 | | - (*range(g.N-i, g.N), *range(g.N-i)) |
336 | | - ).ravel(order='F'))[1:-1] |
337 | | - s += ' '.join(payoffs.split()) + ' ' |
| 333 | + buf = io.StringIO() |
| 334 | + |
| 335 | + buf.write(str(p.N)) |
| 336 | + buf.write("\n") |
| 337 | + buf.write(" ".join(map(str, p.nums_actions))) |
| 338 | + buf.write("\n\n") |
| 339 | + |
| 340 | + payoffs_str = np.array2string( |
| 341 | + p.payoffs, |
| 342 | + separator=" ", |
| 343 | + threshold=sys.maxsize, # no truncation '...' |
| 344 | + # suppress_small helps avoid scientific notation for small |x|; |
| 345 | + # large |x| values may still print with e+... |
| 346 | + suppress_small=True |
| 347 | + )[1:-1] # strip brackets |
| 348 | + |
| 349 | + buf.write(" ".join(payoffs_str.split())) |
338 | 350 |
|
339 | | - return s.rstrip() |
| 351 | + return buf.getvalue().rstrip() |
340 | 352 |
|
341 | 353 |
|
342 | 354 | def from_gam(filename: str) -> NormalFormGame: |
@@ -364,6 +376,14 @@ def from_gam(filename: str) -> NormalFormGame: |
364 | 376 | return GAMReader.from_file(filename) |
365 | 377 |
|
366 | 378 |
|
| 379 | +def from_gam_string(string): |
| 380 | + return GAMReader.from_string(string) |
| 381 | + |
| 382 | + |
| 383 | +def from_gam_url(url): |
| 384 | + return GAMReader.from_url(url) |
| 385 | + |
| 386 | + |
367 | 387 | def to_gam(g, file_path=None): |
368 | 388 | """ |
369 | 389 | Write a NormalFormGame to a file in .gam format. |
|
0 commit comments