Ejemplo n.º 1
0
    def query(self, input_state):
        if self.number_hidden_layers == 0:
            inputs = f.copy_board(input_state)

            final_inputs = f.matrix_multiplication(self.wio, inputs)
            final_outputs = self.activation_function(final_inputs)

            return final_outputs
        elif self.number_hidden_layers == 1:
            inputs = f.copy_board(input_state)

            hidden_inputs = f.matrix_multiplication(self.wih, inputs)
            hidden_outputs = self.activation_function(hidden_inputs)

            final_inputs = f.matrix_multiplication(self.who, hidden_outputs)
            final_outputs = self.activation_function(final_inputs)

            return final_outputs
        elif self.number_hidden_layers == 2:
            inputs = f.copy_board(input_state)

            hidden_1_inputs = f.matrix_multiplication(self.wih1, inputs)
            hidden_1_outputs = self.activation_function(hidden_1_inputs)

            hidden_2_inputs = f.matrix_multiplication(self.wh1h2,
                                                      hidden_1_outputs)
            hidden_2_outputs = self.activation_function(hidden_2_inputs)

            final_inputs = f.matrix_multiplication(self.wh2o, hidden_2_outputs)
            final_outputs = self.activation_function(final_inputs)

            return final_outputs
    def update_board(self):
        # Update the board with the move and check if the game ends

        self.board_next = f.copy_board(self.board)

        self.board_next[0][self.move] = 0.99
        self.board_next[2][self.move] = 0.01

        self.check_board()
    def change_board(self):
        # After every turn has the board to be changed for the other player
        # This is because for both player the same stone is their
        self.board = f.copy_board(self.board_next)

        for i in range(len(self.board[0])):
            if self.board[0][i] == 0.01 and self.board[1][i] == 0.99:
                self.board[0][i] = 0.99
                self.board[1][i] = 0.01
            elif self.board[0][i] == 0.99 and self.board[1][i] == 0.01:
                self.board[0][i] = 0.01
                self.board[1][i] = 0.99
    def set_up_game(self, player_1, player_2):
        # Before every game, to set the variables to 0 and chose who goes first

        self.board = f.copy_board(self.board_empty)
        self.reward = 0
        self.reward_opponent = 0

        if random.randint(0, 1) == 0:
            self.player_x_turn, self.not_player_x_turn = player_1, player_2
        else:
            self.player_x_turn, self.not_player_x_turn = player_2, player_1

        self.game_playing = True
Ejemplo n.º 5
0
    def check_move(self, board, x, y):

        board_test = f.copy_board(board)

        board_test[1][y] = 0.99
        board_test[2][y] = 0.01

        for a, b, c in self.win_combinations:
            if board_test[1][a] == board_test[1][b] == 0.99 and board_test[2][c] == 0.99:
                return y
            elif board_test[1][a] == board_test[1][c] == 0.99 and board_test[2][b] == 0.99:
                return y
            elif board_test[1][b] == board_test[1][c] == 0.99 and board_test[2][a] == 0.99:
                return y

        return x
Ejemplo n.º 6
0
    def train(self, input_state, targets):
        # Training for NN with 0 hidden layers
        if self.number_hidden_layers == 0:
            inputs = f.copy_board(input_state)

            final_inputs = f.matrix_multiplication(self.wio, inputs)
            final_outputs = self.activation_function(final_inputs)

            output_errors = f.subtraction(targets, final_outputs)

            self.wio = f.addition(
                self.wio,
                f.multiplication(
                    self.lr,
                    f.matrix_multiplication(
                        f.multiplication(output_errors,
                                         self.derivation(final_outputs)),
                        f.matrix_transposition(inputs))))

        elif self.number_hidden_layers == 1:
            # Training for NN with 1 hidden layers
            inputs = f.copy_board(input_state)

            hidden_inputs = f.matrix_multiplication(self.wih, inputs)
            hidden_outputs = self.activation_function(hidden_inputs)

            final_inputs = f.matrix_multiplication(self.who, hidden_outputs)
            final_outputs = self.activation_function(final_inputs)

            output_errors = f.subtraction(targets, final_outputs)

            hidden_errors = f.matrix_multiplication(
                f.matrix_transposition(self.who), output_errors)

            self.who = f.addition(
                self.who,
                f.multiplication(
                    self.lr,
                    f.matrix_multiplication(
                        f.multiplication(output_errors,
                                         self.derivation(final_outputs)),
                        f.matrix_transposition(hidden_outputs))))
            self.wih = f.addition(
                self.wih,
                f.multiplication(
                    self.lr,
                    f.matrix_multiplication(
                        f.multiplication(hidden_errors,
                                         self.derivation(hidden_outputs)),
                        f.matrix_transposition(inputs))))

        elif self.number_hidden_layers == 2:
            # Training for NN with 2 hidden layers
            inputs = f.copy_board(input_state)

            hidden_1_inputs = f.matrix_multiplication(self.wih1, inputs)
            hidden_1_outputs = self.activation_function(hidden_1_inputs)

            hidden_2_inputs = f.matrix_multiplication(self.wh1h2,
                                                      hidden_1_outputs)
            hidden_2_outputs = self.activation_function(hidden_2_inputs)

            final_inputs = f.matrix_multiplication(self.wh2o, hidden_2_outputs)
            final_outputs = self.activation_function(final_inputs)

            output_errors = f.subtraction(targets, final_outputs)

            hidden_errors_2 = f.matrix_multiplication(
                f.matrix_transposition(self.wh2o), output_errors)
            hidden_errors_1 = f.matrix_multiplication(
                f.matrix_transposition(self.wh1h2), hidden_errors_2)

            self.wh2o = f.addition(
                self.wh2o,
                f.multiplication(
                    self.lr,
                    f.matrix_multiplication(
                        f.multiplication(output_errors,
                                         self.derivation(final_outputs)),
                        f.matrix_transposition(hidden_2_outputs))))
            self.wh1h2 = f.addition(
                self.wh1h2,
                f.multiplication(
                    self.lr,
                    f.matrix_multiplication(
                        f.multiplication(hidden_errors_2,
                                         self.derivation(hidden_2_outputs)),
                        f.matrix_transposition(hidden_1_outputs))))
            self.wih1 = f.addition(
                self.wih1,
                f.multiplication(
                    self.lr,
                    f.matrix_multiplication(
                        f.multiplication(hidden_errors_1,
                                         self.derivation(hidden_1_outputs)),
                        f.matrix_transposition(inputs))))