Example #1
0
class PlayABunchOfGames():
    def __init__(self, players, loop=100):
        self.players = players
        self.loop = loop
        self.all_games_moves = [
        ]  # each element is [ q_moves, [ [board1, move1], [board2, move2] ] ]

    def start(self):
        for iteration in range(self.loop):
            print('\tplaying game {}/{}'.format(iteration, self.loop))
            # Make a game
            self.game = Game(players=self.players, print_board_state=False)
            self.game.start()
            if self.game.winner:
                winner_moves = self.game.get_winner_moves()
                self.all_games_moves.append([
                    self.game.move_number,
                    [[move['board_state'], move['move']]
                     for move in winner_moves]
                ])

    def get_top_moves(self, top_percentage=1.0):
        """
        Returns the moves of the games that were won in less than the average number of moves
        Arguments:
        - top_percentage (float): games that finished in less than average_moves * top_percentage moves will be returned.
        """
        q_moves_list = [iteration[0] for iteration in self.all_games_moves]
        if len(q_moves_list) == 0:
            print(
                'This is wrong. This should not happen. But guess what? It happened!'
            )
        q_moves_average = sum(q_moves_list) / len(q_moves_list)
        threshold = np.ceil(q_moves_average * top_percentage)
        top_moves = []
        for game_moves in self.all_games_moves:
            if game_moves[0] < threshold:
                top_moves += game_moves[1]
        print('Average number of moves:', q_moves_average)
        print('\tThreshold: {}\tQ top moves: {}', threshold, len(top_moves))
        return top_moves
Example #2
0

randomness, probablilistic = calculate_randomness(latest_generation_trained)

# Make players
agent_1 = Agent(  # current gen
    player_id=1,
    player_colour='white',
    model='models/model_{}.h5'.format(latest_generation_trained)
    if latest_generation_trained > 1 else None)
agent_1.set_randomness_level(randomness)
agent_1.set_probabilistic_level(probablilistic)
agent_2 = Agent(  # previous gen
    player_id=2,
    player_colour='red',
    model='models/model_{}.h5'.format(latest_generation_trained - 1)
    if latest_generation_trained > 1 else None)
agent_2.set_randomness_level(randomness)
agent_2.set_probabilistic_level(probablilistic)

players = [agent_1, agent_2]

# Make game
game = Game(players=players,
            print_board_state=True,
            print_board_delay=1,
            clear_screen_before_printing_board=True)
game.start()
if game.winner:
    print('Player {} won'.format(game.winner))
    print('Q moves: {}'.format(game.move_number))