Exemple #1
0
    def train_from_directory(self, directory, learning_rate, max_epochs):
        """
        Train a neural network based on a set of game logs located in a single directory

        @param directory: the path to the directory containing the 2048 log files
        @type directory: str
        @param learning_rate: The learning rate (between 0 and 1)
        @type learning_rate: float
        @param max_epochs: The maximum number of epochs (cycles)
        @type max_epochs: int
        """
        all_x = None
        all_y = None
        for filename in os.listdir(directory):
            if filename.endswith(".log"):
                print("OK - File parsed: {}".format(
                    os.path.join(directory, filename)))
                game = Game.load_game(os.path.join(directory, filename),
                                      display_grid=False)
                x, y = self.parse_inputs_outputs_for_neural_net(game)
                if all_x is None:
                    all_x = x
                    all_y = y
                else:
                    all_x = np.concatenate((all_x, x))
                    all_y = np.concatenate((all_y, y))
            else:
                print("NOK - File not parsed: {}".format(
                    os.path.join(directory, filename)))
        self.train(all_x, all_y, learning_rate, max_epochs)
Exemple #2
0
 def replay_game(self):
     """
     Method to visualize a 2048 game from log file
     """
     self.mode = Modes.MODE_REPLAY
     self.mode_text.set("REPLAY")
     self.mode_label.configure(bg="red")
     filepath = filedialog.askopenfilename(initialdir=".", title="Select file",
                                           filetypes=(("2048 replay files", "*.log"), ("all files", "*.*")))
     if filepath != '':
         self.game = Game.load_game(filepath)
         t_str_state = self.game.history.grid_history[0]
         try:
             self.grid.grid = Grid.from_string(t_str_state, self.nb_rows, self.nb_columns)
             self.update_grid()
         except ValueError:
             print("Incorrect matrix dimensions!")
     else:
         self.start_new_game()