def test_game_parameters_from_string_with_options(self): self.assertEqual( pyspiel.game_parameters_from_string("foo(x=2,y=true)"), { "name": pyspiel.GameParameter("foo"), "x": pyspiel.GameParameter(2), "y": pyspiel.GameParameter(True) })
def test_game_parameters_from_string_with_options(self): self.assertEqual( pyspiel.game_parameters_from_string("foo(x=2,y=true)"), { "name": "foo", "x": 2, "y": True })
def test_game_parameters_from_string_with_subgame(self): self.assertEqual( pyspiel.game_parameters_from_string( "foo(x=2,y=true,subgame=bar(z=False))"), { "name": "foo", "x": 2, "y": True, "subgame": { "name": "bar", "z": False } })
def test_leduc_info_state_as_single_tensor(self): game = pyspiel.load_game("leduc_poker") observation = make_observation( game, INFO_STATE_OBS_TYPE, pyspiel.game_parameters_from_string("single_tensor")) state = game.new_initial_state() state.apply_action(1) # Deal 1 state.apply_action(2) # Deal 2 state.apply_action(2) # Bet state.apply_action(1) # Call state.apply_action(3) # Deal 3 observation.set_from(state, player=0) self.assertEqual(list(observation.dict), ["info_state"]) np.testing.assert_array_equal(observation.dict["info_state"], [ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
def test_game_parameters_from_string_simple(self): self.assertEqual(pyspiel.game_parameters_from_string("foo"), {"name": pyspiel.GameParameter("foo")})
def test_game_parameters_from_string_empty(self): self.assertEqual(pyspiel.game_parameters_from_string(""), {})
def playthrough_lines(game_string, alsologtostdout=False, action_sequence=None, observation_params_string=None): """Returns a playthrough of the specified game as a list of lines. Actions are selected uniformly at random, including chance actions. Args: game_string: string, e.g. 'markov_soccer' or 'kuhn_poker(players=4)'. alsologtostdout: Whether to also print the trace to stdout. This can be useful when an error occurs, to still be able to get context information. action_sequence: A (possibly partial) list of action choices to make. observation_params_string: Optional observation parameters for constructing an observer. """ should_display_state_fn = ShouldDisplayStateTracker() lines = [] action_sequence = action_sequence or [] should_display = True def add_line(v, force=False): if force or should_display: if alsologtostdout: print(v) lines.append(v) game = pyspiel.load_game(game_string) add_line("game: {}".format(game_string)) if observation_params_string: add_line("observation_params: {}".format(observation_params_string)) seed = np.random.randint(2**32 - 1) game_type = game.get_type() default_observation = None try: observation_params = pyspiel.game_parameters_from_string( observation_params_string) if observation_params_string else None default_observation = make_observation( game, imperfect_information_observation_type=None, params=observation_params) except (RuntimeError, ValueError): pass infostate_observation = None # TODO(author11) reinstate this restriction # if game_type.information in (pyspiel.IMPERFECT_INFORMATION, # pyspiel.ONE_SHOT): try: infostate_observation = make_observation( game, pyspiel.IIGObservationType(perfect_recall=True)) except (RuntimeError, ValueError): pass public_observation = None try: public_observation = make_observation( game, pyspiel.IIGObservationType( public_info=True, perfect_recall=False, private_info=pyspiel.PrivateInfoType.NONE)) except (RuntimeError, ValueError): pass private_observation = None try: private_observation = make_observation( game, pyspiel.IIGObservationType( public_info=False, perfect_recall=False, private_info=pyspiel.PrivateInfoType.SINGLE_PLAYER)) except (RuntimeError, ValueError): pass add_line("") add_line("GameType.chance_mode = {}".format(game_type.chance_mode)) add_line("GameType.dynamics = {}".format(game_type.dynamics)) add_line("GameType.information = {}".format(game_type.information)) add_line("GameType.long_name = {}".format('"{}"'.format(game_type.long_name))) add_line("GameType.max_num_players = {}".format(game_type.max_num_players)) add_line("GameType.min_num_players = {}".format(game_type.min_num_players)) add_line("GameType.parameter_specification = {}".format("[{}]".format( ", ".join('"{}"'.format(param) for param in sorted(game_type.parameter_specification))))) add_line("GameType.provides_information_state_string = {}".format( game_type.provides_information_state_string)) add_line("GameType.provides_information_state_tensor = {}".format( game_type.provides_information_state_tensor)) add_line("GameType.provides_observation_string = {}".format( game_type.provides_observation_string)) add_line("GameType.provides_observation_tensor = {}".format( game_type.provides_observation_tensor)) add_line("GameType.provides_factored_observation_string = {}".format( game_type.provides_factored_observation_string)) add_line("GameType.reward_model = {}".format(game_type.reward_model)) add_line("GameType.short_name = {}".format('"{}"'.format( game_type.short_name))) add_line("GameType.utility = {}".format(game_type.utility)) add_line("") add_line("NumDistinctActions() = {}".format(game.num_distinct_actions())) add_line("PolicyTensorShape() = {}".format(game.policy_tensor_shape())) add_line("MaxChanceOutcomes() = {}".format(game.max_chance_outcomes())) add_line("GetParameters() = {{{}}}".format(",".join( "{}={}".format(key, _escape(str(value))) for key, value in sorted(game.get_parameters().items())))) add_line("NumPlayers() = {}".format(game.num_players())) add_line("MinUtility() = {:.5}".format(game.min_utility())) add_line("MaxUtility() = {:.5}".format(game.max_utility())) try: utility_sum = game.utility_sum() except RuntimeError: utility_sum = None add_line("UtilitySum() = {}".format(utility_sum)) if infostate_observation and infostate_observation.tensor is not None: add_line("InformationStateTensorShape() = {}".format( format_shapes(infostate_observation.dict))) add_line("InformationStateTensorLayout() = {}".format( game.information_state_tensor_layout())) add_line("InformationStateTensorSize() = {}".format( len(infostate_observation.tensor))) if default_observation and default_observation.tensor is not None: add_line("ObservationTensorShape() = {}".format( format_shapes(default_observation.dict))) add_line("ObservationTensorLayout() = {}".format( game.observation_tensor_layout())) add_line("ObservationTensorSize() = {}".format( len(default_observation.tensor))) add_line("MaxGameLength() = {}".format(game.max_game_length())) add_line('ToString() = "{}"'.format(str(game))) players = list(range(game.num_players())) state = game.new_initial_state() state_idx = 0 rng = np.random.RandomState(seed) while True: should_display = should_display_state_fn(state) add_line("", force=True) add_line("# State {}".format(state_idx), force=True) for line in str(state).splitlines(): add_line("# {}".format(line).rstrip()) add_line("IsTerminal() = {}".format(state.is_terminal())) add_line("History() = {}".format([int(a) for a in state.history()])) add_line('HistoryString() = "{}"'.format(state.history_str())) add_line("IsChanceNode() = {}".format(state.is_chance_node())) add_line("IsSimultaneousNode() = {}".format(state.is_simultaneous_node())) add_line("CurrentPlayer() = {}".format(state.current_player())) if infostate_observation: for player in players: s = infostate_observation.string_from(state, player) if s is not None: add_line(f'InformationStateString({player}) = "{_escape(s)}"') if infostate_observation and infostate_observation.tensor is not None: for player in players: infostate_observation.set_from(state, player) for name, tensor in infostate_observation.dict.items(): label = f"InformationStateTensor({player})" label += f".{name}" if name != "info_state" else "" for line in _format_tensor(tensor, label): add_line(line) if default_observation: for player in players: s = default_observation.string_from(state, player) if s is not None: add_line(f'ObservationString({player}) = "{_escape(s)}"') if public_observation: s = public_observation.string_from(state, 0) if s is not None: add_line('PublicObservationString() = "{}"'.format(_escape(s))) for player in players: s = private_observation.string_from(state, player) if s is not None: add_line(f'PrivateObservationString({player}) = "{_escape(s)}"') if default_observation and default_observation.tensor is not None: for player in players: default_observation.set_from(state, player) for name, tensor in default_observation.dict.items(): label = f"ObservationTensor({player})" label += f".{name}" if name != "observation" else "" for line in _format_tensor(tensor, label): add_line(line) if game_type.chance_mode == pyspiel.GameType.ChanceMode.SAMPLED_STOCHASTIC: add_line('SerializeState() = "{}"'.format(_escape(state.serialize()))) if not state.is_chance_node(): add_line("Rewards() = {}".format(state.rewards())) add_line("Returns() = {}".format(state.returns())) if state.is_terminal(): break if state.is_chance_node(): add_line("ChanceOutcomes() = {}".format(state.chance_outcomes())) if state.is_simultaneous_node(): for player in players: add_line("LegalActions({}) = [{}]".format( player, ", ".join(str(x) for x in state.legal_actions(player)))) for player in players: add_line("StringLegalActions({}) = [{}]".format( player, ", ".join('"{}"'.format(state.action_to_string(player, x)) for x in state.legal_actions(player)))) if state_idx < len(action_sequence): actions = action_sequence[state_idx] else: actions = [rng.choice(state.legal_actions(pl)) for pl in players] add_line("") add_line("# Apply joint action [{}]".format( format(", ".join( '"{}"'.format(state.action_to_string(player, action)) for player, action in enumerate(actions)))), force=True) add_line("actions: [{}]".format(", ".join( str(action) for action in actions)), force=True) state.apply_actions(actions) else: add_line("LegalActions() = [{}]".format(", ".join( str(x) for x in state.legal_actions()))) add_line("StringLegalActions() = [{}]".format(", ".join( '"{}"'.format(state.action_to_string(state.current_player(), x)) for x in state.legal_actions()))) if state_idx < len(action_sequence): action = action_sequence[state_idx] else: action = rng.choice(state.legal_actions()) add_line("") add_line('# Apply action "{}"'.format( state.action_to_string(state.current_player(), action)), force=True) add_line("action: {}".format(action), force=True) state.apply_action(action) state_idx += 1 return lines