def main(): args = parser.parse_args() signal(SIGCHLD, signal_handler) boards_played = 0 try: for board_definition in board_definitions(): boards_played += 1 run_ai_only_game( args.port, args.address, procs, PLAYING_AIs, board_definition, fixed=random.randint(1, 10**10), client_seed=random.randint(1, 10**10), debug=True, logdir='logs', ) print(f'Played {boards_played} games.', file=sys.stderr) except (Exception, KeyboardInterrupt) as e: sys.stderr.write("Breaking the tournament because of {}\n".format( repr(e))) for p in procs: p.kill() raise
def main(): """ Run the Dice Wars game among AI's. Example: ./dicewars.py --nb-games 16 --ai 4 4 2 1 # runs 16 games four-player games with AIs 4 (two players), 2, and 1 """ args = parser.parse_args() signal(SIGCHLD, signal_handler) if len(args.ai) < 2 or len(args.ai) > 8: print("Unsupported number of AIs") exit(1) summaries = [] for i in range(args.nb_games): if args.report: sys.stdout.write('\r{}'.format(i)) try: board_seed = None if args.board is None else args.board + i board_definition = BoardDefinition(board_seed, args.ownership, args.strength) game_summary = run_ai_only_game( args.port, args.address, procs, args.ai, board_definition, fixed=args.fixed, client_seed=args.client_seed, logdir=args.logdir, debug=args.debug, ) summaries.append(game_summary) except KeyboardInterrupt: lockfile = open('/mnt/w/lock', 'w') fcntl.flock(lockfile, fcntl.LOCK_EX) for p in procs: p.kill() fcntl.flock(lockfile, fcntl.LOCK_UN) break except AttributeError: lockfile = open('/mnt/w/lock', 'w') fcntl.flock(lockfile, fcntl.LOCK_EX) for p in procs: p.kill() fcntl.flock(lockfile, fcntl.LOCK_UN) if args.report: sys.stdout.write('\r') win_numbers = get_win_rates(summaries, len(args.ai)) sys.stdout.write("Win counts {}\n".format(win_numbers)) nb_battles_stats = ListStats([s.nb_battles for s in summaries]) sys.stdout.write("Nb battles {}\n".format(nb_battles_stats))
def main(): args = parser.parse_args() if args.ai_under_test is not None: combatants_provider = EvaluationCombatantsProvider( PLAYING_AIs, args.ai_under_test) else: combatants_provider = TournamentCombatantsProvider(PLAYING_AIs) random.seed(args.seed) signal(SIGCHLD, signal_handler) if args.load: with open(args.load, 'rb') as f: all_games = pickle.load(f) else: all_games = [] boards_played = 0 reporter = SingleLineReporter(not args.report) try: for board_definition in board_definitions(args.board): if boards_played == args.nb_boards: break boards_played += 1 combatants = combatants_provider.get_combatants(args.game_size) nb_permutations, permutations_generator = rotational_permunations_generator( combatants) for i, permuted_combatants in enumerate(permutations_generator): reporter.report('\r{} {}/{} {}'.format( boards_played, i + 1, nb_permutations, ' vs. '.join(permuted_combatants))) game_summary = run_ai_only_game( args.port, args.address, procs, permuted_combatants, board_definition, fixed=UNIVERSAL_SEED, client_seed=UNIVERSAL_SEED, logdir=args.logdir, debug=args.debug, ) all_games.append(game_summary) except (Exception, KeyboardInterrupt) as e: sys.stderr.write("Breaking the tournament because of {}\n".format( repr(e))) lockfile = open('/mnt/w/lock', 'w') fcntl.flock(lockfile, fcntl.LOCK_EX) for p in procs: p.kill() fcntl.flock(lockfile, fcntl.LOCK_UN) reporter.clean() if args.save: with open(args.save, 'wb') as f: pickle.dump(all_games, f) for game in all_games: participants = game.participants() for player in players_info: if get_nickname(player) in participants: players_info[player]['games'].append(game) performances = [ PlayerPerformance(player, info['games'], PLAYING_AIs) for player, info in players_info.items() ] performances.sort(key=lambda perf: perf.winrate, reverse=True) perf_strings = [performances[0].competitors_header() ] + [str(perf) for perf in performances] fields = [perf.split() for perf in perf_strings] print(column_t(fields))
def main(): args = parser.parse_args() PLAYING_AIs.append(args.user_ai_name) players_info = {ai: {'games': []} for ai in PLAYING_AIs} if args.ai_under_test is not None: combatants_provider = EvaluationCombatantsProvider( PLAYING_AIs, args.ai_under_test) else: combatants_provider = TournamentCombatantsProvider(PLAYING_AIs) random.seed(args.seed) signal(SIGCHLD, signal_handler) all_games = [] boards_played = 0 try: for board_definition in board_definitions(args.board): if boards_played == args.nb_boards: break boards_played += 1 combatants = combatants_provider.get_combatants(args.game_size) nb_permutations, permutations_generator = rotational_permunations_generator( combatants) for i, permuted_combatants in enumerate(permutations_generator): game_summary = run_ai_only_game( args.port, args.address, procs, permuted_combatants, board_definition, fixed=UNIVERSAL_SEED, client_seed=UNIVERSAL_SEED, ) all_games.append(game_summary) except (Exception, KeyboardInterrupt) as e: sys.stderr.write("Breaking the tournament because of {}\n".format( repr(e))) for p in procs: p.kill() raise for game in all_games: participants = game.participants() for player in players_info: if get_nickname(player) in participants: players_info[player]['games'].append(game) performances = [ PlayerPerformance(player, info['games'], PLAYING_AIs) for player, info in players_info.items() ] performances.sort(key=lambda perf: perf.winrate, reverse=True) perf_strings = [performances[0].competitors_header() ] + [str(perf) for perf in performances] fields = [perf.split() for perf in perf_strings] print(column_t(fields))