-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor support profile implementation in pygambit.
* Cleans up support profile constructor calling signature, implementation of operators in cython * Test suite migrated to pytest style.
- Loading branch information
Showing
3 changed files
with
110 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,89 @@ | ||
import unittest | ||
import pytest | ||
|
||
import pygambit | ||
import pygambit as gbt | ||
|
||
|
||
class TestGambitStrategySupportProfile(unittest.TestCase): | ||
def setUp(self): | ||
self.game = pygambit.Game.read_game("test_games/mixed_strategy.nfg") | ||
self.support_profile = self.game.support_profile() | ||
self.restriction = self.support_profile.restrict() | ||
def test_remove_strategy(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
strategy = support_profile[0] | ||
new_profile = support_profile.remove(strategy) | ||
assert len(support_profile) == len(new_profile) + 1 | ||
assert strategy not in new_profile | ||
|
||
def tearDown(self): | ||
del self.game | ||
del self.support_profile | ||
del self.restriction | ||
|
||
def test_num_strategies(self): | ||
"""Ensure the support profile of the full game still has all strategies""" | ||
assert len(self.support_profile) == len(self.game.strategies) | ||
assert len(self.support_profile) == len(self.restriction.strategies) | ||
|
||
def test_remove(self): | ||
"""Test removing strategies from a support profile""" | ||
strategy = self.support_profile[0] | ||
new_profile = self.support_profile.remove(strategy) | ||
assert len(self.support_profile) == len(new_profile) + 1 | ||
def test_difference(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
strat_list = [support_profile[0], support_profile[4]] | ||
dif_profile = gbt.StrategySupportProfile(game, strat_list) | ||
new_profile = support_profile - dif_profile | ||
assert len(new_profile) == 3 | ||
for strategy in strat_list: | ||
assert strategy not in new_profile | ||
|
||
def test_difference(self): | ||
"""Test the subtraction of two support profiles""" | ||
strat_list = [self.support_profile[0], self.support_profile[4]] | ||
dif_profile = pygambit.StrategySupportProfile( | ||
strat_list, self.game) | ||
new_profile = self.support_profile - dif_profile | ||
assert len(new_profile) == 3 | ||
for strategy in strat_list: | ||
assert strategy not in new_profile | ||
|
||
def test_difference_error(self): | ||
"""Ensure an error is raised when the difference isn't a | ||
valid support profile | ||
""" | ||
def foo(): | ||
strat_list = [self.support_profile[0], self.support_profile[4]] | ||
dif_profile = pygambit.StrategySupportProfile( | ||
strat_list, self.game | ||
) | ||
dif_profile - self.support_profile | ||
self.assertRaises(ValueError, foo) | ||
|
||
def test_intersection(self): | ||
"""Test the intersection between two support profiles""" | ||
strat_list = [self.support_profile[0], self.support_profile[2], | ||
self.support_profile[4]] | ||
fir_profile = pygambit.StrategySupportProfile( | ||
strat_list, self.game) | ||
sec_profile = self.support_profile.remove(self.support_profile[2]) | ||
new_profile = fir_profile & sec_profile | ||
assert len(new_profile) == 2 | ||
assert new_profile <= sec_profile | ||
assert new_profile <= fir_profile | ||
|
||
def test_intersection_error(self): | ||
"""Ensure an error is raised when the intersection isn't a | ||
valid support profile. | ||
""" | ||
def foo(): | ||
strat_list = [self.support_profile[0], self.support_profile[2], | ||
self.support_profile[4]] | ||
fir_profile = pygambit.StrategySupportProfile( | ||
strat_list, self.game | ||
) | ||
sec_profile = self.support_profile.remove(self.support_profile[4]) | ||
fir_profile & sec_profile | ||
self.assertRaises(ValueError, foo) | ||
|
||
def test_union(self): | ||
"""Test the union between two support profiles""" | ||
strat_list = [self.support_profile[0], self.support_profile[2], | ||
self.support_profile[4]] | ||
fir_profile = pygambit.StrategySupportProfile( | ||
strat_list, self.game) | ||
sec_profile = self.support_profile.remove(self.support_profile[4]) | ||
new_profile = fir_profile | sec_profile | ||
assert new_profile == self.support_profile | ||
|
||
def test_undominated(self): | ||
"""Test removing undominated strategies from the support profile""" | ||
new_profile = self.support_profile | ||
loop_profile = pygambit.supports.undominated_strategies_solve(new_profile) | ||
while loop_profile != new_profile: | ||
new_profile = loop_profile | ||
loop_profile = pygambit.supports.undominated_strategies_solve(new_profile) | ||
assert len(loop_profile) == 2 | ||
assert loop_profile == pygambit.StrategySupportProfile( | ||
[self.support_profile[0], self.support_profile[3]], self.game) | ||
|
||
def test_remove_error(self): | ||
"""Test removing the last strategy of a player""" | ||
def foo(): | ||
profile = self.support_profile.remove(self.support_profile[3]) | ||
profile.remove(profile[3]) | ||
self.assertRaises(pygambit.UndefinedOperationError, foo) | ||
|
||
def test_difference_error(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
strat_list = [support_profile[0], support_profile[4]] | ||
dif_profile = gbt.StrategySupportProfile(game, strat_list) | ||
with pytest.raises(ValueError): | ||
dif_profile - support_profile | ||
|
||
|
||
def test_intersection(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
strat_list = [support_profile[0], support_profile[2], | ||
support_profile[4]] | ||
fir_profile = gbt.StrategySupportProfile(game, strat_list) | ||
sec_profile = support_profile.remove(support_profile[2]) | ||
new_profile = fir_profile & sec_profile | ||
assert len(new_profile) == 2 | ||
assert new_profile <= sec_profile | ||
assert new_profile <= fir_profile | ||
|
||
|
||
def test_intersection_error(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
strat_list = [support_profile[0], support_profile[2], | ||
support_profile[4]] | ||
fir_profile = gbt.StrategySupportProfile(game, strat_list) | ||
sec_profile = support_profile.remove(support_profile[4]) | ||
with pytest.raises(ValueError): | ||
fir_profile & sec_profile | ||
|
||
|
||
def test_union(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
strat_list = [support_profile[0], support_profile[2], | ||
support_profile[4]] | ||
fir_profile = gbt.StrategySupportProfile(game, strat_list) | ||
sec_profile = support_profile.remove(support_profile[4]) | ||
new_profile = fir_profile | sec_profile | ||
assert new_profile == support_profile | ||
|
||
|
||
def test_undominated(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
new_profile = support_profile | ||
loop_profile = gbt.supports.undominated_strategies_solve(new_profile) | ||
while loop_profile != new_profile: | ||
new_profile = loop_profile | ||
loop_profile = gbt.supports.undominated_strategies_solve(new_profile) | ||
assert len(loop_profile) == 2 | ||
assert loop_profile == gbt.StrategySupportProfile( | ||
game, [support_profile[0], support_profile[3]] | ||
) | ||
|
||
|
||
def test_remove_error(): | ||
game = gbt.Game.read_game("test_games/mixed_strategy.nfg") | ||
support_profile = game.support_profile() | ||
profile = support_profile.remove(support_profile[3]) | ||
with pytest.raises(gbt.UndefinedOperationError): | ||
profile.remove(profile[3]) |