Skip to content

Commit 2eb31d9

Browse files
d-kadtturocy
andauthored
Add tests for Node.is_subgame_root
This adds better tests for `Node.is_subgame_root`. In particular, it adds a test for a known case in which the existing implementation fails (an absent-minded information set that is not at the root of the game tree). Closes #583 --------- Co-authored-by: Ted Turocy <drarbiter@gmail.com>
1 parent 4d669e7 commit 2eb31d9

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
EFG 2 R "Untitled Extensive Game" { "Player 1" "Player 2" }
2+
""
3+
4+
p "" 1 1 "" { "1" "2" } 0
5+
p "" 1 1 "" { "1" "2" } 0
6+
t "" 1 "Outcome 1" { 1, -1 }
7+
p "" 2 1 "" { "1" "2" } 0
8+
t "" 2 "Outcome 2" { 2, -2 }
9+
t "" 3 "Outcome 3" { 3, -3 }
10+
t "" 4 "Outcome 4" { 4, -4 }

tests/test_games/subgames.efg

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
EFG 2 R "Untitled Extensive Game" { "Player 1" "Player 2" }
2+
""
3+
4+
p "" 2 1 "" { "1" "2" } 0
5+
p "" 1 1 "" { "1" "2" } 0
6+
t "" 1 "Outcome 1" { 1, -1 }
7+
t "" 2 "Outcome 2" { 2, -2 }
8+
p "" 1 2 "" { "1" "2" } 0
9+
p "" 2 2 "" { "1" "2" } 0
10+
t "" 3 "Outcome 3" { 3, -3 }
11+
p "" 1 3 "" { "1" "2" } 0
12+
t "" 4 "Outcome 4" { 4, -4 }
13+
t "" 5 "Outcome 5" { 5, -5 }
14+
p "" 2 2 "" { "1" "2" } 0
15+
p "" 2 3 "" { "1" "2" } 0
16+
p "" 1 4 "" { "1" "2" } 0
17+
p "" 2 4 "" { "1" "2" } 0
18+
t "" 6 "Outcome 6" { 6, -6 }
19+
t "" 7 "Outcome 7" { 7, -7 }
20+
t "" 8 "Outcome 8" { 8, -8 }
21+
p "" 1 5 "" { "1" "2" } 0
22+
p "" 2 6 "" { "1" "2" } 0
23+
t "" 9 "Outcome 9" { 9, -9 }
24+
t "" 10 "Outcome 10" { 10, -10 }
25+
p "" 2 6 "" { "1" "2" } 0
26+
p "" 1 7 "" { "1" "2" } 0
27+
p "" 2 5 "" { "1" "2" } 0
28+
p "" 1 4 "" { "1" "2" } 0
29+
t "" 11 "Outcome 11" { 11, -11 }
30+
t "" 12 "Outcome 12" { 12, -12 }
31+
p "" 1 4 "" { "1" "2" } 0
32+
t "" 13 "Outcome 13" { 13, -13 }
33+
t "" 14 "Outcome 14" { 14, -14 }
34+
t "" 15 "Outcome 15" { 15, -15 }
35+
p "" 1 7 "" { "1" "2" } 0
36+
t "" 16 "Outcome 16" { 16, -16 }
37+
t "" 17 "Outcome 17" { 17, -17 }
38+
p "" 1 6 "" { "1" "2" } 0
39+
t "" 18 "Outcome 18" { 18, -18 }
40+
t "" 19 "Outcome 19" { 19, -19 }

tests/test_node.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,30 @@ def test_is_successor_of():
8686
game.root.is_successor_of(game.players[0])
8787

8888

89-
def test_is_subgame_root():
90-
"""Test whether nodes are correctly labeled as roots of proper subgames."""
91-
game = games.read_from_file("basic_extensive_game.efg")
92-
assert game.root.is_subgame_root
93-
assert not game.root.children[0].is_subgame_root
89+
@pytest.mark.parametrize("game, expected_result", [
90+
# Games without Absent-Mindedness for which the legacy method is known to be correct.
91+
(games.read_from_file("wichardt.efg"), {0}),
92+
(games.read_from_file("e02.efg"), {0, 2, 4}),
93+
(games.read_from_file("subgames.efg"), {0, 1, 4, 7, 11, 13, 34}),
94+
95+
pytest.param(
96+
games.read_from_file("AM-driver-subgame.efg"),
97+
{0, 3}, # The correct set of subgame roots
98+
marks=pytest.mark.xfail(
99+
reason="Current method does not detect roots of proper subgames "
100+
"that are members of AM-infosets."
101+
)
102+
),
103+
])
104+
def test_legacy_is_subgame_root_set(game: gbt.Game, expected_result: set):
105+
"""
106+
Tests the legacy `node.is_subgame_root` against an expected set of nodes.
107+
Includes both passing cases and games with Absent-Mindedness where it is expected to fail.
108+
"""
109+
list_nodes = list(game.nodes)
110+
expected_roots = {list_nodes[i] for i in expected_result}
111+
legacy_roots = {node for node in game.nodes if node.is_subgame_root}
112+
assert legacy_roots == expected_roots
94113

95114

96115
def test_append_move_error_player_actions():

0 commit comments

Comments
 (0)