Exemplo n.º 1
0
 def load_checkpoint(self,
                     folder=Params.NN_CHECKPOINT_FOLDER,
                     filename=Params.WORKING_CHECKPOINT_FILENAME):
     filepath = os.path.join(folder, filename)
     if not os.path.exists(filepath):
         Params.log("hex_ai.py", "No model in path {}".format(filepath))
     else:
         self.nnet.model.load_weights(filepath)
Exemplo n.º 2
0
 def play_move(self, move):
     if self.board_size > move[1] >= 0 and self.board_size > move[2] >= 0:
         if self.matrix[move[1]][move[2]] == 0:
             self.matrix[move[1]][move[2]] = move[0]
             self.moves_list.append(move)
         else:
             Params.log("hex_board.py", "Illegal move : " + str(move))
             raise IllegalMove
     else:
         Params.log("hex_board.py", "Illegal move : " + str(move))
         raise IllegalMove
Exemplo n.º 3
0
    def save_checkpoint(self,
                        folder=Params.NN_CHECKPOINT_FOLDER,
                        filename=Params.WORKING_CHECKPOINT_FILENAME):
        filepath = os.path.join(folder, filename)

        if not os.path.exists(folder):
            Params.log(
                "hex_ai.py",
                "Checkpoint Directory does not exist! Making directory {}".
                format(folder))
            os.mkdir(folder)

        self.nnet.model.save_weights(filepath)
Exemplo n.º 4
0
    def trainAI(self, checkpoint=Params.TAKE_FROM_CHECKPOINT):
        j = 0
        if checkpoint:
            try:
                infos = self.get_last_valid_checkpoint_name()
                if infos is not None:
                    self.ai.load_checkpoint(filename=infos["full"])
                    self.training_calls = infos["iters"]
                Params.prt("hex_coach.py",
                           "Checkpoint Loaded : " + infos["full"])

            except:
                Params.log("hex_coach.py", "Unable to open the checkpoint")

        while True:
            try:
                if Params.GAME_SET_METHOD is "reset":
                    if j % Params.RESET_GAMES_AFTER_BATCH is 0:
                        import os
                        if os.path.isfile(Params.STANDARD_GAME_FILE):
                            os.remove(Params.STANDARD_GAME_FILE)
                        Params.prt("hex_coach.py", "Games removed")

            except Exception:
                traceback.print_exc()
                Params.log("hex_coach.py",
                           "Impossible to remove previous games")

            try:
                self.add_batch_file()
            except Exception:
                traceback.print_exc()
                Params.log("hex_coach.py", "Impossible to add Files")

            try:
                self.launch_train()
            except Exception:
                traceback.print_exc()
                Params.log("hex_coach.py",
                           "Impossible to train the neural network")

            try:
                self.check_infos_size_and_save()
            except Exception:
                traceback.print_exc()
                Params.log("hex_coach.py", "Impossible to check the infos")

            j += 1

            try:
                Params.log(
                    "hex_coach.py", "Round " + str(j + 1) + " (round " +
                    str(j % Params.RESET_GAMES_AFTER_BATCH + 1) + "/" +
                    str(Params.RESET_GAMES_AFTER_BATCH) +
                    ", average winner : " + str(HexCoach.average_winner[-1]) +
                    ", number of moves : " +
                    str(HexCoach.average_number_moves[-1]) +
                    ", number of learning iter : " + str(self.training_calls) +
                    ", rii : " + str(HexCoach.rii) + ")")

                HexCoach.riis.append(HexCoach.rii)

            except:
                traceback.print_exc()
                Params.log("hex_coach.py", "Impossible to view round work")
Exemplo n.º 5
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