Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add __repr__ for PolymatrixGame #749

Merged
merged 1 commit into from
Dec 15, 2024
Merged

Add __repr__ for PolymatrixGame #749

merged 1 commit into from
Dec 15, 2024

Conversation

oyamad
Copy link
Member

@oyamad oyamad commented Dec 8, 2024

Similar to that of NormalFormGame.

>>> g = gt.random_game((2, 3, 4))
>>> g
<2x3x4 3-player NormalFormGame of dtype float64>
>>> polymg = gt.random_polymatrix_game((2, 3, 4))
>>> polymg
<2x3x4 3-player PolymatrixGame>

@GautamsGitHub By the way, shall we enforce homogeneous dtype across players in PolymatrixGame, as in NormalFormGame?

def __init__(self, data, dtype=None):
# data represents an array_like of Players
if hasattr(data, '__getitem__') and isinstance(data[0], Player):
N = len(data)
# Check that the shapes of the payoff arrays are consistent
# and the dtypes coincide
shape_0 = data[0].payoff_array.shape
dtype_0 = data[0].payoff_array.dtype
for i in range(1, N):
shape = data[i].payoff_array.shape
if not (
len(shape) == N and
shape == shape_0[i:] + shape_0[:i]
):
raise ValueError(
'shapes of payoff arrays must be consistent'
)
dtype = data[i].payoff_array.dtype
if dtype != dtype_0:
raise ValueError(
'dtypes of payoff arrays must coincide'
)
self.players = tuple(data)
self.dtype = dtype_0

@coveralls
Copy link

Coverage Status

coverage: 93.117% (+0.001%) from 93.116%
when pulling 7ff0896 on polymg_repr
into b733469 on main.

@oyamad oyamad merged commit 3921861 into main Dec 15, 2024
11 checks passed
@oyamad oyamad deleted the polymg_repr branch December 15, 2024 07:03
@GautamsGitHub
Copy link
Contributor

This sounds good. It is helpful because we can combine player's payoffs into vectors without worrying about there being different dtypes. I also think raising an error is better than casting everything to some datatype.

@oyamad
Copy link
Member Author

oyamad commented Jan 5, 2025

Sorry, I realized that the dtype is set float whatever the dtypes of the input...

matrix_builder = np.full((rows, cols), -np.inf)
matrix_builder[:incoming.shape[0],
:incoming.shape[1]] = incoming[:rows, :cols]
self.polymatrix[(p1, p2)] = matrix_builder

(matrix_builder is created as an nparray of float.)

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants