Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.. [1] `Combinatorial number system
`_,
Wikipedia.
"""
m = scipy.special.comb(n, k, exact=True)
if m > np.iinfo(np.intp).max:
raise ValueError('Maximum allowed size exceeded')
payoff_arrays = tuple(np.zeros(shape) for shape in [(n, m), (m, n)])
tourn = random_tournament_graph(n, random_state=random_state)
indices, indptr = tourn.csgraph.indices, tourn.csgraph.indptr
_populate_tournament_payoff_array0(payoff_arrays[0], k, indices, indptr)
_populate_tournament_payoff_array1(payoff_arrays[1], k)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
nums_suboptimal = is_suboptimal.sum(axis=1)
while (nums_suboptimal==0).any():
payoff_arrays[1][:] = random_state.random_sample((n, n))
payoff_arrays[1].max(axis=0, out=maxes)
np.less(payoff_arrays[1], maxes, out=is_suboptimal)
is_suboptimal.sum(axis=1, out=nums_suboptimal)
for i in range(n):
one_ind = random_state.randint(n)
while not is_suboptimal[i, one_ind]:
one_ind = random_state.randint(n)
payoff_arrays[0][one_ind, i] = 1
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
Player(np.zeros(tuple(data[i:]) + tuple(data[:i]),
dtype=dtype))
for i in range(N)
)
self.dtype = self.players[0].payoff_array.dtype
elif data.ndim == 2 and data.shape[1] >= 2:
# data represents a payoff array for symmetric two-player game
# Number of actions must be >= 2
if data.shape[0] != data.shape[1]:
raise ValueError(
'symmetric two-player game must be represented ' +
'by a square matrix'
)
N = 2
self.players = tuple(Player(data) for i in range(N))
self.dtype = data.dtype
else: # data represents a payoff array
# data must be of shape (n_0, ..., n_{N-1}, N),
# where n_i is the number of actions available to player i,
# and the last axis contains the payoff profile
N = data.ndim - 1
if data.shape[-1] != N:
raise ValueError(
'size of innermost array must be equal to ' +
'the number of players'
)
payoff_arrays = tuple(
np.empty(data.shape[i:-1]+data.shape[:i], dtype=data.dtype)
for i in range(N)
)
Player([[-1.20042463, -1.39708658, -1.39708658, -1.39708658],
[-1.00376268, -1.20042463, -1.39708658, -1.39708658],
[-1.00376268, -1.00376268, -1.20042463, -1.39708658],
[-1.00376268, -1.00376268, -1.00376268, -1.20042463]])
"""
actions = simplex_grid(h, t)
n = actions.shape[0]
payoff_arrays = tuple(np.empty((n, n)) for i in range(2))
mean = np.array([mu, mu])
cov = np.array([[1, rho], [rho, 1]])
random_state = check_random_state(random_state)
values = random_state.multivariate_normal(mean, cov, h)
_populate_blotto_payoff_arrays(payoff_arrays, actions, values)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
N = data.ndim - 1
if data.shape[-1] != N:
raise ValueError(
'size of innermost array must be equal to ' +
'the number of players'
)
payoff_arrays = tuple(
np.empty(data.shape[i:-1]+data.shape[:i], dtype=data.dtype)
for i in range(N)
)
for i, payoff_array in enumerate(payoff_arrays):
payoff_array[:] = \
data.take(i, axis=-1).transpose(list(range(i, N)) +
list(range(i)))
self.players = tuple(
Player(payoff_array) for payoff_array in payoff_arrays
)
self.dtype = data.dtype
self.N = N # Number of players
self.nums_actions = tuple(
player.num_actions for player in self.players
)
self.payoff_arrays = tuple(
player.payoff_array for player in self.players
)
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
[ 0. , 0. , 0. , 0. , 0. , 0.75, 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0.75]])
>>> g.players[1]
Player([[ 0.75, 0.5 , 1. , 0.5 , 0.5 , 0.5 , 0.5 ],
[ 1. , 0.75, 0.5 , 0.5 , 0.5 , 0.5 , 0.5 ],
[ 0.5 , 1. , 0.75, 0.5 , 0.5 , 0.5 , 0.5 ],
[ 0. , 0. , 0. , 0. , 0.75, 0. , 0. ],
[ 0. , 0. , 0. , 0.75, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0.75],
[ 0. , 0. , 0. , 0. , 0. , 0.75, 0. ]])
"""
payoff_arrays = tuple(np.empty((4*k-1, 4*k-1)) for i in range(2))
_populate_sgc_payoff_arrays(payoff_arrays)
g = NormalFormGame(
[Player(payoff_array) for payoff_array in payoff_arrays]
)
return g
>>> player = Player([[3, 0], [0, 3], [1, 1]])
>>> player
Player([[3, 0],
[0, 3],
[1, 1]])
>>> player.delete_action(2)
Player([[3, 0],
[0, 3]])
>>> player.delete_action(0, player_idx=1)
Player([[0],
[3],
[1]])
"""
payoff_array_new = np.delete(self.payoff_array, action, player_idx)
return Player(payoff_array_new)
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.
Returns
-------
g : NormalFormGame
"""
N = len(nums_actions)
if N == 0:
raise ValueError('nums_actions must be non-empty')
random_state = check_random_state(random_state)
players = [
Player(random_state.random_sample(nums_actions[i:]+nums_actions[:i]))
for i in range(N)
]
g = NormalFormGame(players)
return g