Esempio n. 1
0
    def add_batch_file(self):
        player = Params.FIRST_PLAYER

        i = 0
        error_count = 0
        while i < Params.NUMBER_GAMES_BATCH:
            try:
                b = HexBoard()
                moves = []
                w = 0
                j = 0
                expansions = []
                rollouts = []
                ended = []

                start = time.time()
                while w == 0:
                    m, infos = self.uct.next_turn(b, player)
                    expansions.append(infos["expansions"])
                    rollouts.append(infos["rollouts"])
                    ended.append(infos["ended"])
                    player = Params.get_next_player(player)
                    moves.append(m)
                    b.play_move(m)
                    b.find_if_winner(m)
                    w = b.winner()
                    j += 1
                    Params.ongoing()
                end = time.time()

                Params.end_ongoing()
                Params.log(
                    "hex_coach.py", "Match : " + str(i + 1) + "/" +
                    str(Params.NUMBER_GAMES_BATCH) + " - " + str(end - start) +
                    " sec")
                Params.log("hex_coach.py", "Winner : " + str(w))
                Params.log("hex_coach.py",
                           "Moves (" + str(len(moves)) + ") : " + str(moves))
                Params.log("hex_coach.py", "Expansions : " + str(expansions))
                Params.log("hex_coach.py", "Rollouts : " + str(rollouts))
                Params.log("hex_coach.py", "Ended : " + str(ended))
                Params.log("hex_coach.py",
                           "Matrix : \n" + str(b.get_copy_matrix()))
                args = {"player1": "cnn", "player2": "cnn", "winner": str(w)}
                HexGameManager.write_add_format_advanced(
                    moves, args, Params.STANDARD_GAME_FILE)
                i += 1
                HexCoach.rii = Params.RII_PARAMETER * HexCoach.rii + (
                    1 - Params.RII_PARAMETER) * w

            except Exception:
                traceback.print_exc()
                time.sleep(0.1)
                Params.log("hex_coach.py", "Failure when creating game")

                error_count += 1
                if error_count >= Params.SAVING_FROM_CONVERGENCE_TO_ERROR:
                    raise ConvNetUnableToProduceGame