Skip to content

Commit 146edd6

Browse files
authored
RFC: support_enumeration: Reduce intermediate array creation (#764)
1 parent 3810f9d commit 146edd6

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

quantecon/game_theory/support_enumeration.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,25 @@ def _support_enumeration_gen(payoff_matrices):
8383
"""
8484
nums_actions = payoff_matrices[0].shape
8585
n_min = min(nums_actions)
86+
flags_vecs = (np.empty(nums_actions[0], np.bool_),
87+
np.empty(nums_actions[1], np.bool_))
8688

8789
for k in range(1, n_min+1):
8890
supps = (np.arange(0, k, 1, np.int_), np.empty(k, np.int_))
8991
actions = (np.empty(k+1), np.empty(k+1))
9092
A = np.empty((k+1, k+1))
9193

9294
while supps[0][-1] < nums_actions[0]:
93-
supps[1][:] = np.arange(k)
95+
for i in range(k):
96+
supps[1][i] = i
9497
while supps[1][-1] < nums_actions[1]:
9598
if _indiff_mixed_action(
96-
payoff_matrices[0], supps[0], supps[1], A, actions[1]
99+
payoff_matrices[0], supps[0], supps[1],
100+
A, flags_vecs[0], actions[1]
97101
):
98102
if _indiff_mixed_action(
99-
payoff_matrices[1], supps[1], supps[0], A, actions[0]
103+
payoff_matrices[1], supps[1], supps[0],
104+
A, flags_vecs[1], actions[0]
100105
):
101106
out = (np.zeros(nums_actions[0]),
102107
np.zeros(nums_actions[1]))
@@ -109,7 +114,8 @@ def _support_enumeration_gen(payoff_matrices):
109114

110115

111116
@jit(nopython=True, cache=True)
112-
def _indiff_mixed_action(payoff_matrix, own_supp, opp_supp, A, out):
117+
def _indiff_mixed_action(payoff_matrix, own_supp, opp_supp,
118+
A, own_supp_flags, out):
113119
"""
114120
Given a player's payoff matrix `payoff_matrix`, an array `own_supp`
115121
of this player's actions, and an array `opp_supp` of the opponent's
@@ -118,7 +124,8 @@ def _indiff_mixed_action(payoff_matrix, own_supp, opp_supp, A, out):
118124
among the actions in `own_supp`, if any such exists. Return `True`
119125
if such a mixed action exists and actions in `own_supp` are indeed
120126
best responses to it, in which case the outcome is stored in `out`;
121-
`False` otherwise. Array `A` is used in intermediate steps.
127+
`False` otherwise. Arrays `A` and `own_supp_flags` are used in
128+
intermediate steps.
122129
123130
Parameters
124131
----------
@@ -134,6 +141,9 @@ def _indiff_mixed_action(payoff_matrix, own_supp, opp_supp, A, out):
134141
A : ndarray(float, ndim=2)
135142
Array used in intermediate steps, of shape (k+1, k+1).
136143
144+
own_supp_flags : ndarray(bool, ndim=1)
145+
Array used in intermediate steps, of length m.
146+
137147
out : ndarray(float, ndim=1)
138148
Array of length k+1 to store the k nonzero values of the desired
139149
mixed action in `out[:-1]` (and the payoff value in `out[-1]`).
@@ -167,7 +177,7 @@ def _indiff_mixed_action(payoff_matrix, own_supp, opp_supp, A, out):
167177
if k == m:
168178
return True
169179

170-
own_supp_flags = np.zeros(m, np.bool_)
180+
own_supp_flags[:] = False
171181
own_supp_flags[own_supp] = True
172182

173183
for i in range(m):

0 commit comments

Comments
 (0)