Ejemplo n.º 1
0
    def write_add_format_advanced(moves, args, file=Params.STANDARD_GAME_FILE):
        """
        Advanced format should contain informations like the board used, the model of the NN, the setup and time for
        the UTC...
        Current format : [player1],[player2],[winner]:[",".join([moves])]
        """
        Params.prt("hex_game_manager.py", "Moves : " + str(moves))
        Params.prt("hex_game_manager.py", "Args : " + str(args))

        with open(file, "a") as mf:
            a = args["player1"] + "," + args["player2"] + "," + str(
                args["winner"]) + ":"
            ms = []
            for m in moves:
                ms.append(positions_letter[m[1]] + positions_letter[m[2]])
            a += ",".join(ms) + '\n'
            mf.write(a)

        if Params.GAME_SET_METHOD == "maximum":
            lines = []
            with open(file, "r") as mf:
                lines = mf.readlines()

            if len(lines) > Params.MAXIMUM_GAMES_BATCH:
                lines.pop(0)

            with open(file, "w") as mf:
                for l in lines:
                    mf.write(l)
Ejemplo n.º 2
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")