Skip to content

Commit

Permalink
Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
d-kad committed Jul 18, 2024
1 parent ae01060 commit ec628ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
34 changes: 15 additions & 19 deletions src/pygambit/game.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import fractions
import itertools
import pathlib

Expand Down Expand Up @@ -336,36 +337,31 @@ class Game:
g.title = title
return g

def to_arrays(self) -> typing.List[np.array]:
"""Reverse function to from_arrays:

To_arrays, for a given game, generates players’ payoff tables represented as numpy arrays.
The number of produced arrays is equal to the number of players.
def to_arrays(self, dtype=fractions.Fraction) -> typing.List[np.array]:
"""Generate the payoff tables for players represented as numpy arrays.
Parameters
----------
dtype : type
The desired datatype of the payoff values

Returns
-------
list of np.arrays
list of np.array

Raises
------
UndefinedOperationError
If the game does not have a tree representation.
See Also
--------
from_arrays : Create game from list-like of array-like
"""
arrays = []

if self.is_tree:
raise UndefinedOperationError(
"Operation only defined for games with a strategic representation"
)

if len(self.players) == 0:
raise RuntimeError("There are no players in the game")
if dtype != fractions.Fraction and dtype != float:
raise NotImplementedError("Unsupported type of payoff")

shape = tuple(len(player.strategies) for player in self.players)
for player in self.players:
array = np.zeros(shape=shape)
array = np.zeros(shape=shape, dtype=object)
for profile in itertools.product(*(range(s) for s in shape)):
array[profile] = self[profile][player]
array[profile] = dtype(self[profile][player])
arrays.append(array)
return arrays

Expand Down
18 changes: 11 additions & 7 deletions tests/test_game.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from fractions import Fraction

import numpy as np
import pygambit as gbt
import pytest
Expand All @@ -13,22 +15,24 @@ def test_from_arrays():
assert len(game.players[1].strategies) == 2


def test_extensive_form_to_arrays():
game = gbt.Game.new_tree()
with pytest.raises(ValueError):
_ = game.to_arrays()


def test_empty_array_to_arrays():
game = gbt.Game.from_arrays([])
a = game.to_arrays()
assert len(a) == 1
assert (a[0] == np.array([])).all()


def test_different_num_representations_to_arrays():
def test_different_num_representations_to_arrays_fraction():
game = gbt.Game.from_arrays([1, 2 / 1, "6/2", 0.25, ".99"])
A = game.to_arrays()[0]
correct_output = [Fraction(1, 1), Fraction(2, 1), Fraction(3, 1), Fraction(1, 4),
Fraction(99, 100)]
assert (correct_output == A).all()


def test_different_num_representations_to_arrays_float():
game = gbt.Game.from_arrays([1, 2 / 1, "6/2", 0.25, ".99"])
A = game.to_arrays(dtype=float)[0]
correct_output = [1.0, 2.0, 3.0, 0.25, 0.99]
assert (correct_output == A).all()

Expand Down

0 comments on commit ec628ab

Please sign in to comment.