Esempio n. 1
0
    def run_against_random(self, num_games=25, game_num=0):
        wrong_moves = 0
        total_moves = 0
        games_won = 0
        for _ in range(num_games):
            game = Hex(5, 1)
            while len(game.get_moves()) > 0:
                if game.player == Hex.PLAYER_TOP:  # model player
                    next_move = ANET.predict(get_feature(
                        game.get_state(), game.player == Hex.PLAYER_LEFT),
                                             model=self.model)
                    next_move = (
                        next_move[1], next_move[0]
                    ) if game.player == Hex.PLAYER_LEFT else next_move
                    if next_move not in game.get_moves():
                        wrong_moves += 1
                        next_move = choice(game.get_moves())
                else:
                    next_move = choice(game.get_moves())
                game.do_move(next_move)
                total_moves += 1

            res_model = game.get_result(Hex.PLAYER_TOP)
            res_random = game.get_result(Hex.PLAYER_LEFT)
            if res_model == 0 and res_random == 0:
                print("Draw")
                continue
            games_won += res_model

        win_rate = games_won / num_games
        wrong_moves_rate = wrong_moves / total_moves
        pp.pprint("Game {}    : {}".format(game_num, win_rate))
        pp.pprint("Wrong moves: {}".format(wrong_moves_rate))
        return win_rate, wrong_moves_rate
    def run_tournament(self, path="topp"):
        self.fetch_game_models(path)
        games = self.get_games()
        wrong_moves = 0
        total_moves = 0
        for p1, p2 in games:
            for i in range(self.num_games):
                game = Hex(5, 1)
                while len(game.get_moves()) > 0:
                    if self.random and game.player == Hex.PLAYER_LEFT:
                        next_move = choice(game.get_moves())
                    else:
                        model = self.models[p1 if game.player ==
                                            Hex.PLAYER_TOP else p2]
                        next_move = ANET.predict(get_feature(
                            game.get_state(), game.player == Hex.PLAYER_LEFT),
                                                 model=model)
                        next_move = (
                            next_move[1], next_move[0]
                        ) if game.player == Hex.PLAYER_LEFT else next_move
                        if next_move not in game.get_moves():
                            wrong_moves += 1
                            next_move = choice(game.get_moves())
                    total_moves += 1
                    game.do_move(next_move)
                res_p1 = game.get_result(Hex.PLAYER_TOP)
                res_p2 = game.get_result(Hex.PLAYER_LEFT)
                if res_p1 == 0 and res_p2 == 0:
                    raise Exception
                if res_p1 == 1 and res_p2 == 1:
                    raise Exception

                winning_model = p1 if res_p1 == 1 else p2
                loosing_model = p2 if res_p2 == 0 else p1
                if self.results.get(
                    (winning_model, loosing_model)) is not None:
                    self.results[(winning_model, loosing_model)] += 1
                else:
                    self.results[(winning_model, loosing_model)] = 1
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(self.results)
        if total_moves > 0:
            pp.pprint("Wrong moves (%): " + str(wrong_moves / total_moves))