11@testset " games simulation" begin
22
3- MAX_ACTIONS_PER_GAME = 1000
3+ MAX_ACTIONS_PER_GAME = 1000
44
5- SPIEL_GAMES_LIST = registered_games ()
5+ SPIEL_GAMES_LIST = registered_games ()
66
7- SPIEL_LOADABLE_GAMES_LIST = [
8- g for g in SPIEL_GAMES_LIST if default_loadable (g)
9- ]
7+ SPIEL_LOADABLE_GAMES_LIST = [
8+ g for g in SPIEL_GAMES_LIST if default_loadable (g)
9+ ]
1010
11- @test length (SPIEL_LOADABLE_GAMES_LIST) >= 38
11+ @test length (SPIEL_LOADABLE_GAMES_LIST) >= 38
1212
13- SPIEL_SIMULTANEOUS_GAMES_LIST = [
14- g for g in SPIEL_LOADABLE_GAMES_LIST
15- if dynamics (g) == SIMULTANEOUS
16- ]
13+ SPIEL_SIMULTANEOUS_GAMES_LIST = [
14+ g for g in SPIEL_LOADABLE_GAMES_LIST
15+ if dynamics (g) == SIMULTANEOUS
16+ ]
1717
18- @test length (SPIEL_SIMULTANEOUS_GAMES_LIST) >= 14
18+ @test length (SPIEL_SIMULTANEOUS_GAMES_LIST) >= 14
1919
20- SPIEL_MULTIPLAYER_GAMES_LIST = [
21- (g, p)
22- for g in SPIEL_LOADABLE_GAMES_LIST
23- for p in max (min_num_players (g), 2 ) : min (max_num_players (g), 6 )
24- if (max_num_players (g) > 2 ) &&
20+ SPIEL_MULTIPLAYER_GAMES_LIST = [
21+ (g, p)
22+ for g in SPIEL_LOADABLE_GAMES_LIST
23+ for p in max (min_num_players (g), 2 ): min (max_num_players (g), 6 )
24+ if (max_num_players (g) > 2 ) &&
2525 (max_num_players (g) > min_num_players (g)) &&
2626 (short_name (g) != " tiny_hanabi" ) && # default payoff only works for 2p
2727 (short_name (g) != " universal_poker" )
28- ]
28+ ]
2929
30- @test length (SPIEL_MULTIPLAYER_GAMES_LIST) >= 35
30+ @test length (SPIEL_MULTIPLAYER_GAMES_LIST) >= 35
3131
32- function apply_action_test_clone (state, action)
33- state_copy = copy (state)
34- @test string (state) == string (state_copy)
35- @test history (state) == history (state_copy)
32+ function apply_action_test_clone (state, action)
33+ state_copy = copy (state)
34+ @test string (state) == string (state_copy)
35+ @test history (state) == history (state_copy)
3636
37- apply_action (state, action)
38- apply_action (state_copy, action)
37+ apply_action (state, action)
38+ apply_action (state_copy, action)
3939
40- @test string (state) == string (state_copy)
41- @test history (state) == history (state_copy)
42- end
40+ @test string (state) == string (state_copy)
41+ @test history (state) == history (state_copy)
42+ end
4343
44- function serialize_deserialize (game, state)
45- ser_str = serialize_game_and_state (game, state)
46- new_game, new_state = deserialize_game_and_state (ser_str)
47- @test string (game) == string (new_game)
48- @test string (state) == string (new_state)
49- end
44+ function serialize_deserialize (game, state)
45+ ser_str = serialize_game_and_state (game, state)
46+ new_game, new_state = deserialize_game_and_state (ser_str)
47+ @test string (game) == string (new_game)
48+ @test string (state) == string (new_state)
49+ end
5050
51- function simulate_game (game)
52- @info " simulating game $(short_name (get_type (game))) "
53- min_u, max_u = min_utility (game), max_utility (game)
54- @test min_u < max_u
51+ function simulate_game (game)
52+ @info " simulating game $(short_name (get_type (game))) "
53+ min_u, max_u = min_utility (game), max_utility (game)
54+ @test min_u < max_u
55+
56+ state = new_initial_state (game)
57+ total_actions = 0
58+
59+ next_serialize_check = 1
60+
61+ while ! is_terminal (state) && (total_actions <= MAX_ACTIONS_PER_GAME)
62+ total_actions += 1
63+
64+ # Serialize/Deserialize is costly. Only do it every power of 2 actions.
65+ if total_actions >= next_serialize_check
66+ serialize_deserialize (game, state)
67+ next_serialize_check *= 2
68+ end
69+
70+ # The state can be of four different types: chance node,
71+ # simultaneous node, decision node or mean field node.
72+ if is_chance_node (state)
73+ # Chance node: sample an outcome
74+ outcomes = chance_outcomes (state)
75+ @test length (outcomes) > 0
76+ action_list, prob_list = zip (outcomes... )
77+ action = action_list[sample (weights (collect (prob_list)))]
78+ apply_action (state, action)
79+ elseif is_simultaneous_node (state)
80+ chosen_actions = [
81+ rand (legal_actions (state, pid - 1 ))
82+ for pid in 1 : num_players (game)
83+ ] # in julia, index starts with 1
84+ # Apply the joint action and test cloning states.
85+ apply_action_test_clone (state, chosen_actions)
86+ elseif is_mean_field_node (state)
87+ num_states = length (distribution_support (state))
88+ update_distribution (
89+ state, StdVector ([1.0 / num_states for _ in 1 : num_states]))
90+ else
91+ @test is_player_node (state)
92+ # Decision node: sample action for the single current player
93+ action = rand (legal_actions (state, current_player (state)))
94+ # Apply action and test state cloning.
95+ apply_action_test_clone (state, action)
96+ end
97+ end
5598
56- state = new_initial_state (game)
57- total_actions = 0
99+ @test total_actions > 0
58100
59- next_serialize_check = 1
101+ if is_terminal (state)
102+ # Check there are no legal actions.
103+ @test length (legal_actions (state)) == 0
104+ for player in 0 : (num_players (game)- 1 )
105+ @test length (legal_actions (state, player)) == 0
106+ end
60107
61- while ! is_terminal (state) && (total_actions <= MAX_ACTIONS_PER_GAME)
62- total_actions += 1
108+ utilities = returns (state)
63109
64- # Serialize/Deserialize is costly. Only do it every power of 2 actions.
65- if total_actions >= next_serialize_check
66- serialize_deserialize (game, state)
67- next_serialize_check *= 2
68- end
110+ for u in utilities
111+ @test u >= min_utility (game)
112+ @test u <= max_utility (game)
113+ end
69114
70- # The state can be three different types: chance node,
71- # simultaneous node, or decision node
72- if is_chance_node (state)
73- # Chance node: sample an outcome
74- outcomes = chance_outcomes (state)
75- @test length (outcomes) > 0
76- action_list, prob_list = zip (outcomes... )
77- action = action_list[sample (weights (collect (prob_list)))]
78- apply_action (state, action)
79- elseif is_simultaneous_node (state)
80- chosen_actions = [
81- rand (legal_actions (state, pid- 1 ))
82- for pid in 1 : num_players (game)
83- ] # in julia, index starts with 1
84- # Apply the joint action and test cloning states.
85- apply_action_test_clone (state, chosen_actions)
115+ @info " Simulation of game $game " total_actions utilities
86116 else
87- # Decision node: sample action for the single current player
88- action = rand (legal_actions (state, current_player (state)))
89- # Apply action and test state cloning.
90- apply_action_test_clone (state, action)
117+ @info " Simulation of game $game terminated after maximum number of actions $MAX_ACTIONS_PER_GAME "
91118 end
92119 end
93120
94- @test total_actions > 0
95-
96- if is_terminal (state)
97- # Check there are no legal actions.
98- @test length (legal_actions (state)) == 0
99- for player in 0 : (num_players (game)- 1 )
100- @test length (legal_actions (state, player)) == 0
101- end
102-
103- utilities = returns (state)
104-
105- for u in utilities
106- @test u >= min_utility (game)
107- @test u <= max_utility (game)
108- end
109-
110- @info " Simulation of game $game " total_actions utilities
111- else
112- @info " Simulation of game $game terminated after maximum number of actions $MAX_ACTIONS_PER_GAME "
121+ for game_info in SPIEL_LOADABLE_GAMES_LIST
122+ game = load_game (short_name (game_info))
123+ @test num_players (game) >= min_num_players (game_info)
124+ @test num_players (game) <= max_num_players (game_info)
125+ simulate_game (game)
113126 end
114- end
115127
116- for game_info in SPIEL_LOADABLE_GAMES_LIST
117- game = load_game (short_name (game_info))
118- @test num_players (game) >= min_num_players (game_info)
119- @test num_players (game) <= max_num_players (game_info)
120- simulate_game (game)
121- end
122-
123- for game_info in SPIEL_SIMULTANEOUS_GAMES_LIST
124- converted_game = load_game_as_turn_based (short_name (game_info))
125- simulate_game (converted_game)
126- end
128+ for game_info in SPIEL_SIMULTANEOUS_GAMES_LIST
129+ converted_game = load_game_as_turn_based (short_name (game_info))
130+ simulate_game (converted_game)
131+ end
127132
128- for (game_info, n) in SPIEL_MULTIPLAYER_GAMES_LIST
129- game = load_game (short_name (game_info); players= GameParameter (n))
130- simulate_game (game)
131- end
133+ for (game_info, n) in SPIEL_MULTIPLAYER_GAMES_LIST
134+ game = load_game (short_name (game_info); players= GameParameter (n))
135+ simulate_game (game)
136+ end
132137
133- simulate_game (load_game (" breakthrough(rows=6,columns=6)" ))
134- simulate_game (load_game (" pig(players=2,winscore=15)" ))
138+ simulate_game (load_game (" breakthrough(rows=6,columns=6)" ))
139+ simulate_game (load_game (" pig(players=2,winscore=15)" ))
135140
136- end
141+ end
0 commit comments