def test(self, config: Config) -> None: # agents black: Agent = self.black white: Agent = config.white # set train mode if isinstance(white, TrainableAgent): white.train_mode = False # reset agents black.reset() white.reset() # print agents print(f'\nTESTING\n\t{black}\n\t{white}\n') for episode in tqdm(range(1, config.num_episodes + 1)): # create new game game: Game = Game(self.board_size, self.black, config, episode) # play game game.play() # print score ties: int = config.num_episodes - black.num_games_won - white.num_games_won win_ratio: float = black.num_games_won / config.num_episodes * 100 print( f'({black.num_games_won:>4}|{white.num_games_won:>4}|{ties:>4}) / {config.num_episodes:>4} -> win ratio: {win_ratio:>6.2f} %') # set train mode back if isinstance(white, TrainableAgent): white.train_mode = config.train_white
def human(self, config: Config) -> None: assert isinstance(config.white, HumanAgent) # agents black = self.black white = config.white # reset agents black.reset() white.reset() print(f'\nHUMAN\n\t{black}\n\t{white}\n') for episode in tqdm(range(1, config.num_episodes + 1)): # create new game game: Game = Game(self.board_size, self.black, config, episode) # let black begin # get legal actions legal_actions: Actions = game.board.get_legal_actions(game.agent.color) location, legal_directions = game.agent.next_action(game.board, legal_actions) game.board.take_action(location, legal_directions, game.agent.color) # create GUI controller controller: Controller = Controller(game, self.black) # play game controller.start() # print score ties: int = config.num_episodes - black.num_games_won - white.num_games_won win_ratio: float = black.num_games_won / config.num_episodes * 100 print( f'({black.num_games_won:>4}|{white.num_games_won:>4}|{ties:>4}) / {config.num_episodes:>4} -> win ratio: {win_ratio:>6.2f} %')
def setUp(self): """ Before performing the tests on the Game class: - create a player, a map and a game. """ # Create a mock Map. self.game_map = MagicMock() self.game_map.grid = [list(row) for row in parameters.test_grid] self.game_map.width = 8 self.game_map.height = 3 # Create a Game object with: # - The mock map created above # - A server user that anwers '1' once, then ''. self.game = Game(self.game_map, DeafInteractor(['1']))
def eval(self, config: Config) -> float: assert isinstance(self.black, TrainableAgent) # agents black: TrainableAgent = self.black white: Agent = config.white # set train mode if isinstance(white, TrainableAgent): white.train_mode = False # reset agents black.reset() white.reset() for episode in range(1, config.num_episodes + 1): # create new game game: Game = Game(self.board_size, self.black, config, episode) # play game game.play() # print score ties: int = config.num_episodes - black.num_games_won - white.num_games_won win_ratio: float = black.num_games_won / config.num_episodes * 100 print( f'({black.num_games_won:>4}|{white.num_games_won:>4}|{ties:>4}) / {config.num_episodes:>4} -> win ratio: {win_ratio:>6.2f} %') # set train mode back if isinstance(white, TrainableAgent): white.train_mode = config.train_white return win_ratio
def play_human(ai_v={}): ai1 = IA("1", 0, 0, epsilon=0) ai1.V = ai_v p1 = Player("2", BOARD_SIZE_X - 1, BOARD_SIZE_Y - 1) players = [ai1, p1] game = Game(BOARD_SIZE_X, BOARD_SIZE_Y, players) while True: play_game(game, train=False)
def train_eval(self, config: Config) -> None: assert isinstance(self.black, TrainableAgent) # agents black: TrainableAgent = self.black white: Agent = config.white if isinstance(white, TrainableAgent): white.train_mode = config.train_white # initialize train policy if isinstance(black.train_policy, AnnealingTrainablePolicy): black.train_policy.num_episodes = config.num_episodes if isinstance(config.white, TrainableAgent) and isinstance(white.train_policy, AnnealingTrainablePolicy): white.train_policy.num_episodes = config.num_episodes # print agents print(f'\nTRAINING\n\t{black}\n\t{white}\n') for episode in tqdm(range(1, config.num_episodes + 1)): # update policies if isinstance(black.train_policy, AnnealingTrainablePolicy): black.train_policy.update(episode) if isinstance(white, TrainableAgent) and isinstance(black.train_policy, AnnealingTrainablePolicy): white.train_policy.update(episode) # evaluate every 10 % of number of episodes if (episode - 1) % ceil(config.num_episodes / 10) == 0: # set train mode black.train_mode = False # evaluate print(f'\nEVALUATING episode {episode - 1:>5}') self.evals() # plot win ratio self.plot.update(self.total_episodes + episode - 1, self.scores) # set train mode back black.train_mode = True # play new game Game(self.board_size, self.black, config, episode, random_start=True).play() # set train mode one last time black.train_mode = False # evaluate one last time print(f'EVALUATING episode {config.num_episodes:>5}') self.evals() # plot win ratio one last time self.plot.update(self.total_episodes + config.num_episodes, self.scores) # set train mode back one last time black.train_mode = True # save models black.save_weights() if isinstance(white, TrainableAgent) and white.train_mode: white.save_weights()
def playing(): game = Game(phrases) game.start_game() last_input = "_" Printer.show_elements(game.lives_player, get_phrase_value(game.active_phrase)) while game.state != GameState.TURN_OFF: character = Printer.input_user() try: was_bad = game.check_user_guess(character) if was_bad == None: character = last_input else: last_input = character except: Printer.report_exceptions() Printer.show_elements(game.lives_player, get_phrase_value(game.active_phrase), character) check_if_game_over(game)
def main(): # initialize colors init() # check global variables assert 1 <= num_episodes <= 10000, f'Invalid number of episodes: num_episodes should be between 1 and 10000, but got {num_episodes}' assert black.num_games_won == 0, f'Invalid black agent' assert white.num_games_won == 0, f'Invalid white agent' print(f'\nAgents:\n\t{black}\n\t{white}\n') for episode in range(1, num_episodes + 1): if isinstance(black, TrainableAgent): black.episode_rewards = [] black.training_errors = [] if isinstance(white, TrainableAgent): white.episode_rewards = [] white.training_errors = [] # create new game game: Game = Game(episode, black, white, board_size, verbose) # play game game.play() print() ties: int = num_episodes - black.num_games_won - white.num_games_won if black.num_games_won > white.num_games_won: print( colored( f'BLACK {black.num_games_won:>5}/{num_episodes:>5} ({black.num_games_won:>5}|{white.num_games_won:>5}|{ties:>5})', 'red')) elif black.num_games_won < white.num_games_won: print( colored( f'WHITE {white.num_games_won:>5}/{num_episodes:>5} ({black.num_games_won:>5}|{white.num_games_won:>5}|{ties:>5})', 'green')) elif black.num_games_won == white.num_games_won: print( colored( f'DRAW {black.num_games_won:>5}/{num_episodes:>5} ({black.num_games_won:>5}|{white.num_games_won:>5}|{ties:>5})', 'cyan')) else: raise Exception('The scores were miscalculated') print()
def training(ai1_v={}, ai2_v={}, n=10000): ai1 = IA("1", 0, 0) ai2 = IA("2", BOARD_SIZE_X - 1, BOARD_SIZE_Y - 1) ai1.V = ai1_v ai2.V = ai2_v players_g1 = [ai1, ai2] game = Game(BOARD_SIZE_X, BOARD_SIZE_Y, players_g1) for i in range(n): if i % 1000 == 0: print("%s - %d " % (datetime.now(tz=None), i)) if (i % 1000 == 0): players_g1[0]._eps = max(players_g1[0]._eps * 0.996, 0.05) players_g1[1]._eps = max(players_g1[1]._eps * 0.996, 0.05) play_game(game) return ai1.V
def choose_game(self): """ Prompts the user to choose between the possible maps. """ self.current_game = None for player in self.connected_players: player.send("En attente du choix d'un labyrinthe côté serveur.") # Valid inputs are the maps big enough for all connected users to play. rg = range(1, len(self.maps) + 1) player_nb = len(self.connected_players) maps_max = [m.max_players for m in self.maps] valid_inputs = [str(n) for n in rg if maps_max[n - 1] >= player_nb] if len(self.maps) != 0: self.print_maps() # Ask the server to choose a maps. prompt = "Veuillez saisir le labyrinthe de votre choix: " map_number = "" while map_number not in valid_inputs + ['0']: map_number = self.get(prompt).upper() if map_number not in valid_inputs + ['0']: choices = ", ".join(valid_inputs) message = "Saisies autorisées: {}.".format(choices) self.print(message) if map_number == '0': # The user wants to close the session. self.close() else: current_map = self.maps[int(map_number) - 1] message = "Labyrinthe choisi: {}.\n".format(int(map_number)) self.print(message) self.current_game = Game(current_map, self.interactor) else: self.print("Aucune carte n'est disponible.\n")
class TestGame(unittest.TestCase): """ TestCase for functions of the 'game' module. We call the two methods called from outside the class: - play - add_player This ensures entire coverage of the Game methods. """ def setUp(self): """ Before performing the tests on the Game class: - create a player, a map and a game. """ # Create a mock Map. self.game_map = MagicMock() self.game_map.grid = [list(row) for row in parameters.test_grid] self.game_map.width = 8 self.game_map.height = 3 # Create a Game object with: # - The mock map created above # - A server user that anwers '1' once, then ''. self.game = Game(self.game_map, DeafInteractor(['1'])) def test_available_positions(self): """ Check that the game has correctly found 6 available positions in the test_grid. """ # The method has already been called in the constructor of Game. # There is no need to call it again. self.assertEqual(len(self.game.available_positions), 6) def test_add_player(self): """ Test if the method add_player from the Game class: - Adds a player to the game - Chooses an available position for this player. """ # Add a mock player to the game. player = MagicMock() self.game.add_player(player) # Check the number of players of the game has been incremented self.assertEqual(self.game.player_number, 1) # Check that the number of available positions has been decremented. self.assertEqual(len(self.game.available_positions), 5) # Check that the mock player's parameters have been correctly updated. self.assertTrue(player.game is self.game) self.assertTrue(player.game_map is self.game_map) def test_play(self): """ Tests one run of the game with two players. """ # Add two mock players to the game. # Both of them only play 'E' all the time. # The game should be won by the first player. # For each mock player: # - The two first calls of has_won return False. # - The third call returns True. for i in range(0, 2): player = MagicMock() player.recv.return_value = 'E' player.has_left = False # has_won returns twice False, and True the third time. player.has_won.side_effect = [False, False, True] player.check_input.return_value = True self.game.add_player(player) # The game should thus be won by the first player on the 5th round: # Round 1: player1.has_won = False # Round 2: player2.has_won = False # Round 3: player1.has_won = False # Round 4: player2.has_won = False # Round 5: player1.has_won = True => wins game # We play the game. self.game.play() # We check that the game is over. self.assertTrue(self.game.finished) # We check that player one has won the game. self.assertEqual(self.game.winner, self.game.players[0]) # We check there were 5 rounds in the game self.assertEqual(self.game.how_many_rounds, 5)