<commit_before>import keras
import numpy as np
from os.path import abspath, dirname

import sys

import os

from src.play.model.Move import Move


class SimplestNNBot:

    def __init__(self):
        project_dir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))
        # try to make this path relative instead of absolute to project_dir,
        # otherwise it might make troubles when packing it into an executable TODO
        filepath = os.path.join(project_dir, 'src\learn\simplest_move_prediction\model.h5')
        self.model = keras.models.load_model(filepath)

    def genmove(self, color, game) -> Move:
        # We're still interested in the playable locations
        playable_locations = game.get_playable_locations(color)

        # Format the board and make predictions
        board = game.board.tolist()
        board = np.array([[entry for row in board for entry in row]])
        pred_moves = self.model.predict(board)
        # print(pred_moves)

        pred_moves = pred_moves.reshape(9, 9)
        # print(pred_moves)
        # print(playable_locations)
        dummy_value = -10 if color == 'b' else 10
        potential_moves = np.array([[dummy_value]*9]*9, dtype=float)
        for move in playable_locations:
            # print(move)
            if move.is_pass:
                continue
            loc = move.to_matrix_location()
            # print(loc)
            potential_moves[loc[0]][loc[1]] = pred_moves[loc[0]][loc[1]]
        # print([i for row in potential_moves for i in row])
        # print(self.color)
        if color == 'b':
            row, col = np.unravel_index(
                potential_moves.argmax(),
                potential_moves.shape)
        elif color == 'w':
            row, col = np.unravel_index(
                potential_moves.argmin(),
                potential_moves.shape)
        # random_choice = rn.choice(playable_locations)<commit_msg>Update on the NN! WIP, this commit is mostly because I want to pull<commit_after>import keras
import numpy as np
from os.path import abspath, dirname
from src.play.model.Move import Move
from src.play.model.Board import WHITE, BLACK, EMPTY

import sys

import os

from src.play.model.Move import Move


class SimplestNNBot:

    def __init__(self):
        project_dir = dirname(dirname(dirname(dirname(dirname(
            abspath(__file__))))))
        # try to make this path relative instead of absolute to project_dir,
        # otherwise it might make troubles when packing it into
        # an executable TODO
        filepath = os.path.join(
            project_dir, 'src\learn\simplest_move_prediction\model.h5')
        self.model = keras.models.load_model(filepath)

    def board_to_input(self):
        b = self.game.board
        if self.color == 'b':
            me = BLACK
            other = WHITE
        else:
            me = WHITE
            other = BLACK
        my_board = (b == me) * 2 - 1
        other_board = (b == other) * 2 - 1

        my_board_vect = my_board.reshape(
            1, my_board.shape[0]*my_board.shape[1])
        other_board_vect = other_board.reshape(
            1, other_board.shape[0]*other_board.shape[1])
        return np.append(my_board_vect, other_board_vect)

    def genmove(self):
        print('NN generating move')
        # We're still interested in the playable locations
        playable_locations = self.game.get_playable_locations(self.color)

        # Format the board and make predictions
        inp = self.board_to_input()
        pred_moves = self.model.predict(inp)
        # print(pred_moves)

        pred_moves = pred_moves.reshape(9, 9)
        # print(pred_moves)
        # print(playable_locations)
        dummy_value = -10 if self.color == 'b' else 10
        potential_moves = np.array([[dummy_value]*9]*9, dtype=float)
        for move in playable_locations:
            # print(move)
            if move.is_pass:
                continue
            loc = move.to_matrix_location()
            # print(loc)
            potential_moves[loc[0]][loc[1]] = pred_moves[loc[0]][loc[1]]
        # print([i for row in potential_moves for i in row])
        # print(self.color)
        if self.color == 'b':
            row, col = np.unravel_index(
                potential_moves.argmax(),
                potential_moves.shape)
        elif self.color == 'w':
            row, col = np.unravel_index(
                potential_moves.argmin(),
                potential_moves.shape)
        # random_choice = rn.choice(playable_locations)
{'fraction_non_alphanumeric': 0.09555261999119331, 'fraction_numerical': 0.007926023778071334, 'mean_word_length': 2.231152204836415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5616995', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1357, 'n_words': 339}