Skip to content

Commit 8de2bdc

Browse files
committed
revert node traversal order back to the correct one
1 parent 05ac552 commit 8de2bdc

4 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/pygambit/game.pxi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ class GameNodes:
186186

187187
def __iter__(self) -> typing.Iterator[Node]:
188188
def dfs(node):
189+
yield node
189190
for child in node.children:
190191
yield from dfs(child)
191-
yield node
192192
if not self.game.deref().IsTree():
193193
return
194194
yield from dfs(Node.wrap(self.game.deref().GetRoot()))
@@ -703,7 +703,14 @@ class Game:
703703

704704
@property
705705
def nodes(self) -> GameNodes:
706-
"""The set of nodes in the game."""
706+
"""The set of nodes in the game.
707+
708+
Iteration over this property yields the nodes in the order of depth-first search.
709+
710+
.. versionchanged:: 16.4
711+
Changed from a method ``nodes()`` to a property. Access as
712+
``game.nodes`` instead of ``game.nodes()``.
713+
"""
707714
return GameNodes.wrap(self.game)
708715

709716
@property

tests/test_behav.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def test_realiz_prob_nodes_reference(game: gbt.Game, node_idx: int,
641641
realiz_prob: typing.Union[str, float], rational_flag: bool):
642642
profile = game.mixed_behavior_profile(rational=rational_flag)
643643
realiz_prob = (gbt.Rational(realiz_prob) if rational_flag else realiz_prob)
644-
node = game.nodes()[node_idx]
644+
node = list(game.nodes)[node_idx]
645645
assert profile.realiz_prob(node) == realiz_prob
646646

647647

@@ -890,7 +890,7 @@ def test_martingale_property_of_node_value(game: gbt.Game, rational_flag: bool):
890890
realization probabilities of those children
891891
"""
892892
profile = game.mixed_behavior_profile(rational=rational_flag)
893-
for node in game.nodes():
893+
for node in game.nodes:
894894
if node.is_terminal or node.player.is_chance:
895895
continue
896896
expected_val = 0
@@ -1080,23 +1080,23 @@ def _get_and_check_answers(game: gbt.Game, action_probs1: tuple, action_probs2:
10801080
######################################################################################
10811081
# belief (at nodes)
10821082
(games.create_mixed_behav_game_efg(), PROBS_1A_doub, PROBS_2A_doub, False,
1083-
lambda x, y: x.belief(y), lambda x: x.nodes()),
1083+
lambda x, y: x.belief(y), lambda x: x.nodes),
10841084
(games.create_mixed_behav_game_efg(), PROBS_1A_rat, PROBS_2A_rat, True,
1085-
lambda x, y: x.belief(y), lambda x: x.nodes()),
1085+
lambda x, y: x.belief(y), lambda x: x.nodes),
10861086
(games.create_myerson_2_card_poker_efg(), PROBS_1B_doub, PROBS_2B_doub, False,
1087-
lambda x, y: x.belief(y), lambda x: x.nodes()),
1087+
lambda x, y: x.belief(y), lambda x: x.nodes),
10881088
(games.create_myerson_2_card_poker_efg(), PROBS_1A_rat, PROBS_2A_rat, True,
1089-
lambda x, y: x.belief(y), lambda x: x.nodes()),
1089+
lambda x, y: x.belief(y), lambda x: x.nodes),
10901090
######################################################################################
10911091
# realiz_prob (at nodes)
10921092
(games.create_mixed_behav_game_efg(), PROBS_1A_doub, PROBS_2A_doub, False,
1093-
lambda x, y: x.realiz_prob(y), lambda x: x.nodes()),
1093+
lambda x, y: x.realiz_prob(y), lambda x: x.nodes),
10941094
(games.create_mixed_behav_game_efg(), PROBS_1A_rat, PROBS_2A_rat, True,
1095-
lambda x, y: x.realiz_prob(y), lambda x: x.nodes()),
1095+
lambda x, y: x.realiz_prob(y), lambda x: x.nodes),
10961096
(games.create_myerson_2_card_poker_efg(), PROBS_1B_doub, PROBS_2B_doub, False,
1097-
lambda x, y: x.realiz_prob(y), lambda x: x.nodes()),
1097+
lambda x, y: x.realiz_prob(y), lambda x: x.nodes),
10981098
(games.create_myerson_2_card_poker_efg(), PROBS_1A_rat, PROBS_2A_rat, True,
1099-
lambda x, y: x.realiz_prob(y), lambda x: x.nodes()),
1099+
lambda x, y: x.realiz_prob(y), lambda x: x.nodes),
11001100
######################################################################################
11011101
# infoset_prob
11021102
(games.create_mixed_behav_game_efg(), PROBS_1A_doub, PROBS_2A_doub, False,
@@ -1141,16 +1141,16 @@ def _get_and_check_answers(game: gbt.Game, action_probs1: tuple, action_probs2:
11411141
# node_value
11421142
(games.create_mixed_behav_game_efg(), PROBS_1A_doub, PROBS_2A_doub, False,
11431143
lambda x, y: x.node_value(player=y[0], node=y[1]),
1144-
lambda x: list(product(x.players, x.nodes()))),
1144+
lambda x: list(product(x.players, x.nodes))),
11451145
(games.create_mixed_behav_game_efg(), PROBS_1A_rat, PROBS_2A_rat, True,
11461146
lambda x, y: x.node_value(player=y[0], node=y[1]),
1147-
lambda x: list(product(x.players, x.nodes()))),
1147+
lambda x: list(product(x.players, x.nodes))),
11481148
(games.create_myerson_2_card_poker_efg(), PROBS_1B_doub, PROBS_2B_doub, False,
11491149
lambda x, y: x.node_value(player=y[0], node=y[1]),
1150-
lambda x: list(product(x.players, x.nodes()))),
1150+
lambda x: list(product(x.players, x.nodes))),
11511151
(games.create_myerson_2_card_poker_efg(), PROBS_1A_rat, PROBS_2A_rat, True,
11521152
lambda x, y: x.node_value(player=y[0], node=y[1]),
1153-
lambda x: list(product(x.players, x.nodes()))),
1153+
lambda x: list(product(x.players, x.nodes))),
11541154
######################################################################################
11551155
# liap_value (of profile, hence [1] for objects_to_test, any singleton collection would do)
11561156
(games.create_mixed_behav_game_efg(), PROBS_1A_doub, PROBS_2A_doub, False,

tests/test_extensive.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ def test_game_add_players_nolabel():
5555
game.add_player()
5656

5757

58-
def test_game_num_nodes():
59-
game = games.read_from_file("basic_extensive_game.efg")
60-
assert len(game.nodes) == 15
61-
62-
6358
def test_game_is_perfect_recall():
6459
game = games.read_from_file("perfect_recall.efg")
6560
assert game.is_perfect_recall

tests/test_node.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_copy_tree_onto_nondescendent_terminal_node():
202202
g = games.read_from_file("e01.efg")
203203
list_nodes = list(g.nodes)
204204
src_node = list_nodes[3] # path=[1, 0]
205-
dest_node = list_nodes[0] # path=[0, 0]
205+
dest_node = list_nodes[2] # path=[0, 0]
206206

207207
g.copy_tree(src_node, dest_node)
208208

@@ -213,8 +213,8 @@ def test_copy_tree_onto_descendent_terminal_node():
213213
"""Test copying a subtree to a node that's a descendent of the original."""
214214
g = games.read_from_file("e01.efg")
215215
list_nodes = list(g.nodes)
216-
src_node = list_nodes[4] # path=[0]
217-
dest_node = list_nodes[1] # path=[0, 1, 0]
216+
src_node = list_nodes[1] # path=[0]
217+
dest_node = list_nodes[4] # path=[0, 1, 0]
218218

219219
g.copy_tree(src_node, dest_node)
220220

@@ -478,7 +478,7 @@ def test_len_after_append_move():
478478
initial_number_of_nodes = len(game.nodes)
479479
list_nodes = list(game.nodes)
480480

481-
terminal_node = list_nodes[1]
481+
terminal_node = list_nodes[5] # path=[1, 1, 0]
482482
player = game.players[0]
483483
actions_to_add = ["T", "M", "B"]
484484

@@ -494,10 +494,10 @@ def test_len_after_append_infoset():
494494
initial_number_of_nodes = len(game.nodes)
495495
list_nodes = list(game.nodes)
496496

497-
member_node = list_nodes[5] # path=[1]
497+
member_node = list_nodes[2] # path=[1]
498498
infoset_to_modify = member_node.infoset
499499
number_of_infoset_actions = len(infoset_to_modify.actions)
500-
terminal_node_to_add = list_nodes[3] # path=[1, 1, 1]
500+
terminal_node_to_add = list_nodes[6] # path=[1, 1, 1]
501501

502502
game.append_infoset(terminal_node_to_add, infoset_to_modify)
503503

@@ -564,9 +564,9 @@ def test_len_after_insert_infoset():
564564
initial_number_of_nodes = len(game.nodes)
565565
list_nodes = list(game.nodes)
566566

567-
member_node = list_nodes[4] # path=[1, 1]
567+
member_node = list_nodes[6] # path=[1]
568568
infoset_to_modify = member_node.infoset
569-
node_to_insert_above = list_nodes[1] # path=[0, 1]
569+
node_to_insert_above = list_nodes[7] # path=[0, 1]
570570
number_of_infoset_actions = len(infoset_to_modify.actions)
571571

572572
game.insert_infoset(node_to_insert_above, infoset_to_modify)
@@ -581,7 +581,7 @@ def test_len_after_copy_tree():
581581
initial_number_of_nodes = len(game.nodes)
582582
list_nodes = list(game.nodes)
583583
src_node = list_nodes[3] # path=[1, 0]
584-
dest_node = list_nodes[0] # path=[0, 0]
584+
dest_node = list_nodes[2] # path=[0, 0]
585585
number_of_src_ancestors = _count_subtree_nodes(src_node)
586586

587587
game.copy_tree(src_node, dest_node)

0 commit comments

Comments
 (0)