-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #983 from deepmind:lanctot-patch-46
PiperOrigin-RevId: 499011506 Change-Id: I2e390f4b794edaa306d7a192e4a04a97edc55b69
- Loading branch information
Showing
9 changed files
with
162 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Copyright 2022 DeepMind Technologies Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Some helpers for normal-form games.""" | ||
|
||
import collections | ||
import numpy as np | ||
|
||
|
||
class StrategyAverager(object): | ||
"""A helper class for averaging strategies for players.""" | ||
|
||
def __init__(self, num_players, action_space_shapes, window_size=None): | ||
"""Initialize the average strategy helper object. | ||
Args: | ||
num_players (int): the number of players in the game, | ||
action_space_shapes: an vector of n integers, where each element | ||
represents the size of player i's actions space, | ||
window_size (int or None): if None, computes the players' average | ||
strategies over the entire sequence, otherwise computes the average | ||
strategy over a finite-sized window of the k last entries. | ||
""" | ||
self._num_players = num_players | ||
self._action_space_shapes = action_space_shapes | ||
self._window_size = window_size | ||
self._num = 0 | ||
if self._window_size is None: | ||
self._sum_meta_strategies = [ | ||
np.zeros(action_space_shapes[p]) for p in range(num_players) | ||
] | ||
else: | ||
self._window = collections.deque(maxlen=self._window_size) | ||
|
||
def append(self, meta_strategies): | ||
"""Append the meta-strategies to the averaged sequence. | ||
Args: | ||
meta_strategies: a list of strategies, one per player. | ||
""" | ||
if self._window_size is None: | ||
for p in range(self._num_players): | ||
self._sum_meta_strategies[p] += meta_strategies[p] | ||
else: | ||
self._window.append(meta_strategies) | ||
self._num += 1 | ||
|
||
def average_strategies(self): | ||
"""Return each player's average strategy. | ||
Returns: | ||
The averaged strategies, as a list containing one strategy per player. | ||
""" | ||
|
||
if self._window_size is None: | ||
avg_meta_strategies = [ | ||
np.copy(x) for x in self._sum_meta_strategies | ||
] | ||
num_strategies = self._num | ||
else: | ||
avg_meta_strategies = [ | ||
np.zeros(self._action_space_shapes[p]) | ||
for p in range(self._num_players) | ||
] | ||
for i in range(len(self._window)): | ||
for p in range(self._num_players): | ||
avg_meta_strategies[p] += self._window[i][p] | ||
num_strategies = len(self._window) | ||
for p in range(self._num_players): | ||
avg_meta_strategies[p] /= num_strategies | ||
return avg_meta_strategies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2019 DeepMind Technologies Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from absl.testing import absltest | ||
|
||
import numpy as np | ||
from open_spiel.python.algorithms import nfg_utils | ||
|
||
|
||
class NfgUtilsTest(absltest.TestCase): | ||
|
||
def test_strategy_averager_len_smaller_than_window(self): | ||
averager = nfg_utils.StrategyAverager(2, [2, 2], window_size=50) | ||
averager.append([np.array([1.0, 0.0]), np.array([0.0, 1.0])]) | ||
averager.append([np.array([0.0, 1.0]), np.array([1.0, 0.0])]) | ||
avg_strategies = averager.average_strategies() | ||
self.assertLen(avg_strategies, 2) | ||
self.assertAlmostEqual(avg_strategies[0][0], 0.5) | ||
self.assertAlmostEqual(avg_strategies[0][1], 0.5) | ||
self.assertAlmostEqual(avg_strategies[1][0], 0.5) | ||
self.assertAlmostEqual(avg_strategies[1][1], 0.5) | ||
|
||
def test_strategy_averager(self): | ||
first_action_strat = np.array([1.0, 0.0]) | ||
second_action_strat = np.array([0.0, 1.0]) | ||
averager_full = nfg_utils.StrategyAverager(2, [2, 2]) | ||
averager_window5 = nfg_utils.StrategyAverager(2, [2, 2], window_size=5) | ||
averager_window6 = nfg_utils.StrategyAverager(2, [2, 2], window_size=6) | ||
for _ in range(5): | ||
averager_full.append([first_action_strat, first_action_strat]) | ||
averager_window5.append([first_action_strat, first_action_strat]) | ||
averager_window6.append([first_action_strat, first_action_strat]) | ||
for _ in range(5): | ||
averager_full.append([second_action_strat, second_action_strat]) | ||
averager_window5.append([second_action_strat, second_action_strat]) | ||
averager_window6.append([second_action_strat, second_action_strat]) | ||
avg_full = averager_full.average_strategies() | ||
avg_window5 = averager_window5.average_strategies() | ||
avg_window6 = averager_window6.average_strategies() | ||
self.assertAlmostEqual(avg_full[0][1], 0.5) | ||
self.assertAlmostEqual(avg_window5[0][1], 5.0 / 5.0) | ||
self.assertAlmostEqual(avg_window6[0][1], 5.0 / 6.0) | ||
|
||
|
||
if __name__ == '__main__': | ||
absltest.main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters