pacai.gridworld.bin

The main executable for running a game of GridWorld.

  1"""
  2The main executable for running a game of GridWorld.
  3"""
  4
  5import argparse
  6import logging
  7import typing
  8
  9import pacai.core.agentinfo
 10import pacai.gridworld.game
 11import pacai.gridworld.gamestate
 12import pacai.gridworld.mdp
 13import pacai.util.bin
 14import pacai.util.alias
 15
 16DEFAULT_BOARD: str = 'gridworld-book'
 17DEFAULT_SPRITE_SHEET: str = 'gridworld'
 18
 19def set_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
 20    """
 21    Set GridWorld-specific CLI arguments.
 22    This is a sibling to init_from_args(), as the arguments set here can be interpreted there.
 23    """
 24
 25    parser.add_argument('--agent', dest = 'agent', metavar = 'AGENT_TYPE',
 26            action = 'store', type = str, default = pacai.util.alias.AGENT_USER_INPUT.short,
 27            help = ('Select the agent type will be used (default: %(default)s).'
 28                    + f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
 29
 30    parser.add_argument('--noise', dest = 'noise',
 31            action = 'store', type = float, default = pacai.gridworld.mdp.DEFAULT_NOISE,
 32            help = 'How often agent actions result in unintended movement (default %(default)s).')
 33
 34    parser.add_argument('--living-reward', dest = 'living_reward',
 35            action = 'store', type = float, default = pacai.gridworld.mdp.DEFAULT_LIVING_REWARD,
 36            help = 'The Reward for living for a time step (default %(default)s).')
 37
 38    parser.add_argument('--qdisplay', dest = 'qdisplay',
 39            action = 'store_true', default = False,
 40            help = 'Display MDP state values, poilcies, and Q-values (default %(default)s).')
 41
 42    return parser
 43
 44def init_from_args(args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
 45    """
 46    Take in args from a parser that was passed to set_cli_args(),
 47    and initialize the proper components.
 48    """
 49
 50    mdp = pacai.gridworld.mdp.GridWorldMDP(
 51            noise = args.noise,
 52            living_reward = args.living_reward)
 53
 54    logging.debug("Creating a GridWorld with a noise of %0.2f and a living reward of %0.2f.", args.noise, args.living_reward)
 55
 56    data = {
 57        'name': args.agent,
 58        'remember_last_action': False,
 59        'mdp': mdp,
 60        'mdp_state_class': pacai.util.alias.MDP_STATE_CLASS_POSITION.long,
 61    }
 62
 63    base_agent_infos: dict[int, pacai.core.agentinfo.AgentInfo] = {
 64        pacai.gridworld.gamestate.AGENT_INDEX: pacai.core.agentinfo.AgentInfo(**data),
 65    }
 66
 67    board_options = {
 68        'qdisplay': args.qdisplay,
 69    }
 70
 71    return base_agent_infos, [], board_options
 72
 73def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
 74    """ Get additional options for the UI. """
 75
 76    return {
 77        'sprite_sheet_path': DEFAULT_SPRITE_SHEET,
 78    }
 79
 80def main(argv: list[str] | None = None,
 81        ) -> tuple[list[pacai.core.game.GameResult], list[pacai.core.game.GameResult]]:
 82    """
 83    Invoke a game of GridWorld.
 84
 85    Will return the results of any training games followed by the results of any non-training games.
 86    """
 87
 88    return pacai.util.bin.run_main(
 89        description = "Play a game of GridWorld.",
 90        default_board = DEFAULT_BOARD,
 91        game_class = pacai.gridworld.game.Game,
 92        custom_set_cli_args = set_cli_args,
 93        get_additional_ui_options = get_additional_ui_options,
 94        custom_init_from_args = init_from_args,
 95        winning_agent_indexes = {pacai.gridworld.gamestate.AGENT_INDEX},
 96        argv = argv,
 97    )
 98
 99if (__name__ == '__main__'):
100    main()
DEFAULT_BOARD: str = 'gridworld-book'
DEFAULT_SPRITE_SHEET: str = 'gridworld'
def set_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
20def set_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
21    """
22    Set GridWorld-specific CLI arguments.
23    This is a sibling to init_from_args(), as the arguments set here can be interpreted there.
24    """
25
26    parser.add_argument('--agent', dest = 'agent', metavar = 'AGENT_TYPE',
27            action = 'store', type = str, default = pacai.util.alias.AGENT_USER_INPUT.short,
28            help = ('Select the agent type will be used (default: %(default)s).'
29                    + f' Builtin agents: {pacai.util.alias.AGENT_SHORT_NAMES}.'))
30
31    parser.add_argument('--noise', dest = 'noise',
32            action = 'store', type = float, default = pacai.gridworld.mdp.DEFAULT_NOISE,
33            help = 'How often agent actions result in unintended movement (default %(default)s).')
34
35    parser.add_argument('--living-reward', dest = 'living_reward',
36            action = 'store', type = float, default = pacai.gridworld.mdp.DEFAULT_LIVING_REWARD,
37            help = 'The Reward for living for a time step (default %(default)s).')
38
39    parser.add_argument('--qdisplay', dest = 'qdisplay',
40            action = 'store_true', default = False,
41            help = 'Display MDP state values, poilcies, and Q-values (default %(default)s).')
42
43    return parser

Set GridWorld-specific CLI arguments. This is a sibling to init_from_args(), as the arguments set here can be interpreted there.

def init_from_args( args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
45def init_from_args(args: argparse.Namespace) -> tuple[dict[int, pacai.core.agentinfo.AgentInfo], list[int], dict[str, typing.Any]]:
46    """
47    Take in args from a parser that was passed to set_cli_args(),
48    and initialize the proper components.
49    """
50
51    mdp = pacai.gridworld.mdp.GridWorldMDP(
52            noise = args.noise,
53            living_reward = args.living_reward)
54
55    logging.debug("Creating a GridWorld with a noise of %0.2f and a living reward of %0.2f.", args.noise, args.living_reward)
56
57    data = {
58        'name': args.agent,
59        'remember_last_action': False,
60        'mdp': mdp,
61        'mdp_state_class': pacai.util.alias.MDP_STATE_CLASS_POSITION.long,
62    }
63
64    base_agent_infos: dict[int, pacai.core.agentinfo.AgentInfo] = {
65        pacai.gridworld.gamestate.AGENT_INDEX: pacai.core.agentinfo.AgentInfo(**data),
66    }
67
68    board_options = {
69        'qdisplay': args.qdisplay,
70    }
71
72    return base_agent_infos, [], board_options

Take in args from a parser that was passed to set_cli_args(), and initialize the proper components.

def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
74def get_additional_ui_options(args: argparse.Namespace) -> dict[str, typing.Any]:
75    """ Get additional options for the UI. """
76
77    return {
78        'sprite_sheet_path': DEFAULT_SPRITE_SHEET,
79    }

Get additional options for the UI.

def main( argv: list[str] | None = None) -> tuple[list[pacai.core.game.GameResult], list[pacai.core.game.GameResult]]:
81def main(argv: list[str] | None = None,
82        ) -> tuple[list[pacai.core.game.GameResult], list[pacai.core.game.GameResult]]:
83    """
84    Invoke a game of GridWorld.
85
86    Will return the results of any training games followed by the results of any non-training games.
87    """
88
89    return pacai.util.bin.run_main(
90        description = "Play a game of GridWorld.",
91        default_board = DEFAULT_BOARD,
92        game_class = pacai.gridworld.game.Game,
93        custom_set_cli_args = set_cli_args,
94        get_additional_ui_options = get_additional_ui_options,
95        custom_init_from_args = init_from_args,
96        winning_agent_indexes = {pacai.gridworld.gamestate.AGENT_INDEX},
97        argv = argv,
98    )

Invoke a game of GridWorld.

Will return the results of any training games followed by the results of any non-training games.