chessai.tour.bin
The main executable for running a game of tour.
1""" 2The main executable for running a game of tour. 3""" 4 5import argparse 6import logging 7import typing 8 9import chessai.core.agentinfo 10import chessai.core.board 11import chessai.tour.game 12import chessai.tour.gamestate 13import chessai.util.bin 14import chessai.util.alias 15 16DEFAULT_BOARD: str = 'tour-base' 17 18def set_cli_args(parser: argparse.ArgumentParser, **kwargs: typing.Any) -> argparse.ArgumentParser: 19 """ 20 Set Tour-specific CLI arguments. 21 """ 22 23 parser.add_argument('--white-team', dest = 'white_team', 24 action = 'store', type = str, default = chessai.util.alias.AGENT_USER_INPUT.short, 25 help = ('Select the tour team that will play on the white team (default: %(default)s).' 26 + f' Builtin teams: {chessai.util.alias.AGENT_SHORT_NAMES}.')) 27 28 parser.add_argument('--black-team', dest = 'black_team', 29 action = 'store', type = str, default = chessai.util.alias.AGENT_RANDOM.short, 30 help = ('Select the tour team that will play on the black team (default: %(default)s).' 31 + f' Builtin teams: {chessai.util.alias.AGENT_SHORT_NAMES}.')) 32 33 parser.add_argument('--search-targets', dest = 'search_targets', 34 action = 'store', type = str, default = None, 35 help = ('The target squares for the search problem (default: %(default)s).' 36 + ' Separate multiple targets with commas (e.g., \'0,42,63\').')) 37 38 return parser 39 40def init_from_args(args: argparse.Namespace) -> tuple[dict[chessai.core.types.Color, chessai.core.agentinfo.AgentInfo], 41 list[chessai.core.types.Color], dict[str, typing.Any]]: 42 """ 43 Setup agents based on Tour rules. 44 """ 45 46 base_agent_infos: dict[chessai.core.types.Color, chessai.core.agentinfo.AgentInfo] = { 47 chessai.core.types.Color.WHITE: chessai.core.agentinfo.AgentInfo(name = args.white_team), 48 chessai.core.types.Color.BLACK: chessai.core.agentinfo.AgentInfo(name = args.black_team), 49 } 50 51 return base_agent_infos, [], {} 52 53def log_tour_results(results: list[chessai.core.game.GameResult], winning_agent_teams: set[chessai.core.types.Color], prefix: str = '') -> None: 54 """ 55 Log the result of running several Tour games. 56 """ 57 58 move_counts: list[int] = [len(result.history) for result in results] 59 scores: list[float] = [result.score for result in results] 60 61 record: list[str] = [] 62 for result in results: 63 if (result.score > 0): 64 record.append('Win') 65 elif (result.score < 0): 66 record.append('Loss') 67 else: 68 record.append('Tie') 69 70 average_score: float = (sum(scores)) / len(scores) 71 logging.info('Average Score: %0.2f', average_score) 72 73 # Avoid logging long lists (which can be a bit slow in Python's logging module). 74 log_lists_to_info: bool = (len(results) < chessai.util.bin.SCORE_LIST_MAX_INFO_LENGTH) 75 log_lists_to_debug: bool = (logging.getLogger().getEffectiveLevel() <= logging.DEBUG) 76 77 joined_scores: str = '' 78 joined_record: str = '' 79 joined_move_counts: str = '' 80 81 if (log_lists_to_info or log_lists_to_debug): 82 joined_scores = ', '.join([str(score) for score in scores]) 83 joined_record = ', '.join(record) 84 joined_move_counts = ', '.join([str(move_count) for move_count in move_counts]) 85 86 if (log_lists_to_info): 87 logging.info('%sScores: %s', prefix, joined_scores) 88 elif (log_lists_to_debug): 89 logging.debug('%sScores: %s', prefix, joined_scores) 90 91 if (log_lists_to_info): 92 logging.info('%sRecord: %s', prefix, joined_record) 93 elif (log_lists_to_debug): 94 logging.debug('%sRecord: %s', prefix, joined_record) 95 96 logging.info('%sAverage Moves: %s', prefix, sum(move_counts) / float(len(results))) 97 98 if (log_lists_to_info): 99 logging.info('%sMove Counts: %s', prefix, joined_move_counts) 100 elif (log_lists_to_debug): 101 logging.debug('%sMove Counts: %s', prefix, joined_move_counts) 102 103def main(argv: list[str] | None = None, 104 ) -> list[chessai.core.game.GameResult]: 105 """ 106 Invoke a game of Tour. 107 108 Will return the results of all of the games. 109 """ 110 111 return chessai.util.bin.run_main( 112 description = "Play a game of Tour.", 113 default_board = DEFAULT_BOARD, 114 game_class = chessai.tour.game.Game, 115 state_class = chessai.tour.gamestate.GameState, 116 custom_set_cli_args = set_cli_args, 117 custom_init_from_args = init_from_args, 118 log_results = log_tour_results, 119 argv = argv, 120 ) 121 122if (__name__ == '__main__'): 123 main()
DEFAULT_BOARD: str =
'tour-base'
def
set_cli_args( parser: argparse.ArgumentParser, **kwargs: Any) -> argparse.ArgumentParser:
19def set_cli_args(parser: argparse.ArgumentParser, **kwargs: typing.Any) -> argparse.ArgumentParser: 20 """ 21 Set Tour-specific CLI arguments. 22 """ 23 24 parser.add_argument('--white-team', dest = 'white_team', 25 action = 'store', type = str, default = chessai.util.alias.AGENT_USER_INPUT.short, 26 help = ('Select the tour team that will play on the white team (default: %(default)s).' 27 + f' Builtin teams: {chessai.util.alias.AGENT_SHORT_NAMES}.')) 28 29 parser.add_argument('--black-team', dest = 'black_team', 30 action = 'store', type = str, default = chessai.util.alias.AGENT_RANDOM.short, 31 help = ('Select the tour team that will play on the black team (default: %(default)s).' 32 + f' Builtin teams: {chessai.util.alias.AGENT_SHORT_NAMES}.')) 33 34 parser.add_argument('--search-targets', dest = 'search_targets', 35 action = 'store', type = str, default = None, 36 help = ('The target squares for the search problem (default: %(default)s).' 37 + ' Separate multiple targets with commas (e.g., \'0,42,63\').')) 38 39 return parser
Set Tour-specific CLI arguments.
def
init_from_args( args: argparse.Namespace) -> tuple[dict[chessai.core.types.Color, chessai.core.agentinfo.AgentInfo], list[chessai.core.types.Color], dict[str, typing.Any]]:
41def init_from_args(args: argparse.Namespace) -> tuple[dict[chessai.core.types.Color, chessai.core.agentinfo.AgentInfo], 42 list[chessai.core.types.Color], dict[str, typing.Any]]: 43 """ 44 Setup agents based on Tour rules. 45 """ 46 47 base_agent_infos: dict[chessai.core.types.Color, chessai.core.agentinfo.AgentInfo] = { 48 chessai.core.types.Color.WHITE: chessai.core.agentinfo.AgentInfo(name = args.white_team), 49 chessai.core.types.Color.BLACK: chessai.core.agentinfo.AgentInfo(name = args.black_team), 50 } 51 52 return base_agent_infos, [], {}
Setup agents based on Tour rules.
def
log_tour_results( results: list[chessai.core.game.GameResult], winning_agent_teams: set[chessai.core.types.Color], prefix: str = '') -> None:
54def log_tour_results(results: list[chessai.core.game.GameResult], winning_agent_teams: set[chessai.core.types.Color], prefix: str = '') -> None: 55 """ 56 Log the result of running several Tour games. 57 """ 58 59 move_counts: list[int] = [len(result.history) for result in results] 60 scores: list[float] = [result.score for result in results] 61 62 record: list[str] = [] 63 for result in results: 64 if (result.score > 0): 65 record.append('Win') 66 elif (result.score < 0): 67 record.append('Loss') 68 else: 69 record.append('Tie') 70 71 average_score: float = (sum(scores)) / len(scores) 72 logging.info('Average Score: %0.2f', average_score) 73 74 # Avoid logging long lists (which can be a bit slow in Python's logging module). 75 log_lists_to_info: bool = (len(results) < chessai.util.bin.SCORE_LIST_MAX_INFO_LENGTH) 76 log_lists_to_debug: bool = (logging.getLogger().getEffectiveLevel() <= logging.DEBUG) 77 78 joined_scores: str = '' 79 joined_record: str = '' 80 joined_move_counts: str = '' 81 82 if (log_lists_to_info or log_lists_to_debug): 83 joined_scores = ', '.join([str(score) for score in scores]) 84 joined_record = ', '.join(record) 85 joined_move_counts = ', '.join([str(move_count) for move_count in move_counts]) 86 87 if (log_lists_to_info): 88 logging.info('%sScores: %s', prefix, joined_scores) 89 elif (log_lists_to_debug): 90 logging.debug('%sScores: %s', prefix, joined_scores) 91 92 if (log_lists_to_info): 93 logging.info('%sRecord: %s', prefix, joined_record) 94 elif (log_lists_to_debug): 95 logging.debug('%sRecord: %s', prefix, joined_record) 96 97 logging.info('%sAverage Moves: %s', prefix, sum(move_counts) / float(len(results))) 98 99 if (log_lists_to_info): 100 logging.info('%sMove Counts: %s', prefix, joined_move_counts) 101 elif (log_lists_to_debug): 102 logging.debug('%sMove Counts: %s', prefix, joined_move_counts)
Log the result of running several Tour games.
104def main(argv: list[str] | None = None, 105 ) -> list[chessai.core.game.GameResult]: 106 """ 107 Invoke a game of Tour. 108 109 Will return the results of all of the games. 110 """ 111 112 return chessai.util.bin.run_main( 113 description = "Play a game of Tour.", 114 default_board = DEFAULT_BOARD, 115 game_class = chessai.tour.game.Game, 116 state_class = chessai.tour.gamestate.GameState, 117 custom_set_cli_args = set_cli_args, 118 custom_init_from_args = init_from_args, 119 log_results = log_tour_results, 120 argv = argv, 121 )
Invoke a game of Tour.
Will return the results of all of the games.