1113 lines
52 KiB
Plaintext
1113 lines
52 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Deep Otello AI\n",
|
|
"\n",
|
|
"The game reversi is a very good game to apply deep learning methods to.\n",
|
|
"\n",
|
|
"Othello also known as reversi is a board game first published in 1883 by eiter Lewis Waterman or John W. Mollet in England (each one was denouncing the other as fraud).\n",
|
|
"It is a strickt turn based zero-sum game with a clear Markov chain and now hidden states like in card games with an unknown distribution of cards or unknown player allegiance.\n",
|
|
"There is like for the game go only one set of stones with two colors which is much easier to abstract than chess with its 6 unique pieces.\n",
|
|
"The game has a symmetrical game board wich allows to play with rotating the state around an axis to allow for a breaking of sequences or interesting ANN architectures, quadruple the data generation by simulation or interesting test cases where a symetry in turns should be observable if the AI reaches an \"objective\" policy."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"\n",
|
|
"## Content\n",
|
|
"\n",
|
|
"* [The game rules](#the-game-rules) A short overview over the rules of the game.\n",
|
|
"* [Some common Otello strategies](#some-common-otello-strategies) introduces some easy approaches to a classic Otello AI and defines some behavioral expectations.\n",
|
|
"* [Initial design decisions](#initial-design-decisions) an explanation about some initial design decision and assumptions\n",
|
|
"* [Imports and dependencies](#imports-and-dependencies) explains what libraries where used"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"\n",
|
|
"## The game rules\n",
|
|
"\n",
|
|
"Othello is played on a board with 8 x 8 fields for two player.\n",
|
|
"The board geometry is equal to a chess game.\n",
|
|
"The game is played with game stones that are black on one siede and white on the other.\n",
|
|
"\n",
|
|
"The player take turns.\n",
|
|
"A player places a stone with his or her color up on the game board.\n",
|
|
"The player can only place stones when he surrounds a number of stones with the opponents color with the new stone and already placed stones of his color.\n",
|
|
"Those surrounded stones can either be horizontally, vertically and/or diagonally be placed.\n",
|
|
"All stones thus surrounded will be flipped to be of the players color.\n",
|
|
"Turns are only possible if the player is also changing the color of the opponents stones. If a player can't act he is skipped.\n",
|
|
"The game ends if both players can't act. The player with the most stones wins.\n",
|
|
"If the score is counted in detail unclaimed fields go to the player with more stones of his or her color on the board.\n",
|
|
"The game begins with four stones places in the center of the game. Each player gets two. They are placed diagonally to each other.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Some common Othello strategies\n",
|
|
"\n",
|
|
"As can be easily understood the placement of stones and on the bord is always a careful balance of attack and defence.\n",
|
|
"If the player occupies huge homogenous stretches on the board it can be attacked easier.\n",
|
|
"The boards corners provide safety from wich occupied territory is impossible to loos but since it is only possible to reach the corners if the enemy is forced to allow this or calculates the cost of giving a stable base to the enemy it is difficult to obtain.\n",
|
|
"There are some text on otello computer strategies which implement greedy algorithms for reversi based on a modified score to each field.\n",
|
|
"Those different values are score modifiers for a traditional greedy algorithm.\n",
|
|
"If a players stone has captured such a filed the score reached is multiplied by the modifier.\n",
|
|
"The total score is the score reached by the player subtracted with the score of the enemy.\n",
|
|
"The scores change in the course of the game and converges against one. This gives some indications of what to expect from an Othello AI.\n",
|
|
"\n",
|
|
"\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Initial design decisions\n",
|
|
"\n",
|
|
"At the beginning of this project I made some design decisions.\n",
|
|
"The first onw was that I do not want to use a gym library because it limits the data formats accessible.\n",
|
|
"I choose to implement the hole game as entry in a stack in numpy arrays to be able to accommodate interfacing with a neural network easier and to use scipy pattern recognition tools to implement some game mechanics for a fast simulation cycle.\n",
|
|
"I chose to ignore player colors as far as I could instead a player perspective was used. Which allowed to change the perspective with a flipping of the sign. (multiplying with -1).\n",
|
|
"The array format should also allow for data multiplication or the breaking of strikt sequences by flipping the game along one the for axis, (horizontal, vertical, transpose along both diagonals).\n",
|
|
"\n",
|
|
"I wanted to implement different agents as classes that act on those game stacks.\n",
|
|
"\n",
|
|
"Since computation time is critical all computational have results are saved.\n",
|
|
"The analysis of those is then repeated in real time. If a recalculation of such a section is required the save file can be deleted and the code should be executed again."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n",
|
|
"%load_ext blackcellmagic"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Imports and dependencies\n",
|
|
"\n",
|
|
"The following direct dependencies where used for this project:\n",
|
|
"```toml\n",
|
|
"jupyter = \"^1.0.0\"\n",
|
|
"matplotlib = \"^3.6.3\"\n",
|
|
"numpy = \"^1.24.1\"\n",
|
|
"pytest = \"^7.2.1\"\n",
|
|
"python = \"3.10.*\"\n",
|
|
"scipy = \"^1.10.0\"\n",
|
|
"tqdm = \"^4.64.1\"\n",
|
|
"jupyterlab = \"^3.6.1\"\n",
|
|
"torchvision = \"^0.14.1\"\n",
|
|
"torchaudio = \"^0.13.1\"\n",
|
|
"```\n",
|
|
"* `Jupyter` and `jupyterlab` on pycharm was used as a IDE / Ipython was used to implement this code.\n",
|
|
"* `matplotlib` was used for visualisation and statistics.\n",
|
|
"* `numpy` was used for array support and mathematical functions\n",
|
|
"* `tqdm` was used for progress bars\n",
|
|
"* `scipy` contains fast pattern recognition tools for images. It was used to make an initial estimation about where possible turns should be.\n",
|
|
"* `torch` supplied the ANN functionalities."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import itertools\n",
|
|
"import numpy as np\n",
|
|
"import abc\n",
|
|
"from typing import Final\n",
|
|
"from scipy.ndimage import binary_dilation\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"from abc import ABC"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Constants\n",
|
|
"\n",
|
|
"Some general constants needed to be defined. Such as board game size and Player and Enemy representations. Also, directional offsets and the initial placement of blocks."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"BOARD_SIZE: Final[int] = 8 # defines the board side length as 8\n",
|
|
"PLAYER: Final[int] = 1 # defines the number symbolising the player as 1\n",
|
|
"ENEMY: Final[int] = -1 # defines the number symbolising the enemy as -1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"The directions array contains all the numerical offsets needed to move along one of the 8 directions in a 2 dimensional grid. This will allow an iteration over the game board.\n",
|
|
""
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "array([[-1, -1],\n [-1, 0],\n [-1, 1],\n [ 0, -1],\n [ 0, 1],\n [ 1, -1],\n [ 1, 0],\n [ 1, 1]])"
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"DIRECTIONS: Final[np.ndarray] = np.array(\n",
|
|
" [[i, j] for i in range(-1, 2) for j in range(-1, 2) if j != 0 or i != 0],\n",
|
|
" dtype=int,\n",
|
|
")\n",
|
|
"DIRECTIONS.setflags(write=False)\n",
|
|
"DIRECTIONS"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"Another constant needed is the initial start square at the center of the board."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "array([[-1, 1],\n [ 1, -1]])"
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"START_SQUARE: Final[np.ndarray] = np.array(\n",
|
|
" [[ENEMY, PLAYER], [PLAYER, ENEMY]], dtype=int\n",
|
|
")\n",
|
|
"START_SQUARE.setflags(write=False)\n",
|
|
"START_SQUARE"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Creating new boards\n",
|
|
"\n",
|
|
"The first function implemented and tested is a function to generate the starting environment as a stack of games.\n",
|
|
"As described above I simply placed a 2 by 2 square in the center of an empty stack of boards."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, -1, 1, 0, 0, 0],\n [ 0, 0, 0, 1, -1, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0],\n [ 0, 0, 0, 0, 0, 0, 0, 0]])"
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"def get_new_games(number_of_games: int) -> np.ndarray:\n",
|
|
" \"\"\"Generates a stack of initialised game boards.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" number_of_games: The size of the board stack.\n",
|
|
"\n",
|
|
" Returns: The generates stack of games as a stack n x 8 x 8.\n",
|
|
"\n",
|
|
" \"\"\"\n",
|
|
" empty = np.zeros([number_of_games, BOARD_SIZE, BOARD_SIZE], dtype=int)\n",
|
|
" empty[:, 3:5, 3:5] = START_SQUARE\n",
|
|
" return empty\n",
|
|
"\n",
|
|
"\n",
|
|
"get_new_games(1)[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"test_number_of_games = 3\n",
|
|
"assert get_new_games(test_number_of_games).shape == (\n",
|
|
" test_number_of_games,\n",
|
|
" BOARD_SIZE,\n",
|
|
" BOARD_SIZE,\n",
|
|
")\n",
|
|
"np.testing.assert_equal(\n",
|
|
" get_new_games(test_number_of_games).sum(axis=1),\n",
|
|
" np.zeros(\n",
|
|
" [\n",
|
|
" test_number_of_games,\n",
|
|
" 8,\n",
|
|
" ]\n",
|
|
" ),\n",
|
|
")\n",
|
|
"np.testing.assert_equal(\n",
|
|
" get_new_games(test_number_of_games).sum(axis=2),\n",
|
|
" np.zeros(\n",
|
|
" [\n",
|
|
" test_number_of_games,\n",
|
|
" 8,\n",
|
|
" ]\n",
|
|
" ),\n",
|
|
")\n",
|
|
"assert np.all(get_new_games(test_number_of_games)[:, 3:4, 3:4] != 0)\n",
|
|
"del test_number_of_games"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Visualisation tools\n",
|
|
"\n",
|
|
"In this section a visualisation help was implemented for debugging of the game and a proper display of the results.\n",
|
|
"For this visualisation ChatGPT was used as a prompted code generator that was later reviewed and refactored by hand to integrate seamlessly into the project as a whole.\n",
|
|
"White stones represent the player, black stones the enemy. A single plot can be used as a subplot when the `ax` argument is used."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "<Figure size 300x300 with 1 Axes>",
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASIAAAEiCAYAAABdvt+2AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb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MjT27l8vLy5BEjRjT62qFDh2QA8vHjxzWrP3HiRBnK8uMyADk+Pl4eMGCA/Pe//12zmrerqKiQCwoK5O7du8t33XWXfP/998s//vGP5T179mhat37fTSaTnJiYKD/55JPyypUrZa/Xq2nt2+vX33r16qV5bX/90aNH61LrdhUVFfKLL74op6SkyDExMXJcXJz8yCOPyG+//bZcU1OjWd1gfd6zZ48MQL5+/bomdbkMCBEJ1+rPERFR+8cgIiLhGEREJByDiIiEYxARkXAMIiISjkFERMIxiIhIOAYREQnHICIi4RhERCQcg4iIhPt/kWo4zMTZT44AAAAASUVORK5CYII=\n"
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"def plot_othello_board(board, ax=None) -> None:\n",
|
|
" \"\"\"Plots a single otello board.\n",
|
|
"\n",
|
|
" If a matplot axis object is given the board will be plotted into that axis. If not an axis object will be generated.\n",
|
|
" The image generated will be shown directly.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" board: The bord that should be plotted. Only a single games is allowed. A numpy array of the form 8x8 is expected.\n",
|
|
" ax: If needed a matplotlib axis object can be defined that is used to place the board as a sublot into a bigger context.\n",
|
|
" \"\"\"\n",
|
|
" assert board.shape == (8, 8)\n",
|
|
" plot_all = False\n",
|
|
" if ax is None:\n",
|
|
" fig_size = 3\n",
|
|
" plot_all = True\n",
|
|
" fig, ax = plt.subplots(figsize=(fig_size, fig_size))\n",
|
|
"\n",
|
|
" ax.set_facecolor(\"#66FF00\")\n",
|
|
" for x_pos, y_pos in itertools.product(range(BOARD_SIZE), range(BOARD_SIZE)):\n",
|
|
" if board[x_pos, y_pos] == -1:\n",
|
|
" color = \"white\"\n",
|
|
" elif board[x_pos, y_pos] == 1:\n",
|
|
" color = \"black\"\n",
|
|
" else:\n",
|
|
" continue\n",
|
|
" ax.scatter(y_pos, x_pos, s=300 if plot_all else 150, c=color)\n",
|
|
" for x_pos in range(-1, 8):\n",
|
|
" ax.axhline(x_pos + 0.5, color=\"black\", lw=2)\n",
|
|
" ax.axvline(x_pos + 0.5, color=\"black\", lw=2)\n",
|
|
" ax.set_xlim(-0.5, 7.5)\n",
|
|
" ax.set_ylim(7.5, -0.5)\n",
|
|
" ax.set_xticks(np.arange(8))\n",
|
|
" ax.set_xticklabels(list(\"ABCDEFGH\"))\n",
|
|
" ax.set_yticks(np.arange(8))\n",
|
|
" ax.set_yticklabels(list(\"12345678\"))\n",
|
|
" if plot_all:\n",
|
|
" plt.tight_layout()\n",
|
|
" plt.show()\n",
|
|
"\n",
|
|
"\n",
|
|
"plot_othello_board(get_new_games(1)[0])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def plot_othello_boards(boards: np.ndarray) -> None:\n",
|
|
" \"\"\"Plots multiple boards into subplots.\n",
|
|
"\n",
|
|
" The plots are shown directly.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" boards: Plots the boards given into subplots. The maximum number of boards accepted is 70.\n",
|
|
" \"\"\"\n",
|
|
" assert len(boards.shape) == 3\n",
|
|
" assert boards.shape[1:] == (BOARD_SIZE, BOARD_SIZE)\n",
|
|
" assert boards.shape[0] < 70\n",
|
|
"\n",
|
|
" plots_per_row = 4\n",
|
|
" rows = int(np.ceil(boards.shape[0] / plots_per_row))\n",
|
|
" fig, axs = plt.subplots(rows, plots_per_row, figsize=(12, 3 * rows))\n",
|
|
" for game_index, ax in enumerate(axs.flatten()):\n",
|
|
" if game_index >= boards.shape[0]:\n",
|
|
" fig.delaxes(ax)\n",
|
|
" else:\n",
|
|
" plot_othello_board(boards[game_index], ax)\n",
|
|
" plt.tight_layout()\n",
|
|
" plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Find possible actions to take\n",
|
|
"\n",
|
|
"The frist step in the implementation of an AI like this is to get an overview over the possible actions that can be taken in a situation.\n",
|
|
"Here was the design choice taken to first find fields that are empty and have at least one neighbouring enemy stone.\n",
|
|
"This was implemented with element wise check for a stone and a binary dilation marking all fields neighboring an enemy stone.\n",
|
|
"For that the `SURROUNDING` mask was used. Both aries are then element wise combined using and.\n",
|
|
"The resulting array contains all filed where a turn could potentially be made. Those are then check in detail.\n",
|
|
"The previous element wise operations on the numpy array increase the spead for this operation dramatically.\n",
|
|
"\n",
|
|
"The check for a possible turn is done in detail by following each direction step by step as long as there are enemy stones in that direction.\n",
|
|
"If the board end is reached or en empty filed before reaching a field occupied by the player that direction does not surround enemy stones.\n",
|
|
"If one direction surrounds enemy stone a turn is possible.\n",
|
|
"This detailed step is implemented as a recursion and need to go at leas one step to return True."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "array([[[1, 1, 1],\n [1, 0, 1],\n [1, 1, 1]]])"
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"SURROUNDING: Final = np.array(\n",
|
|
" [[[1, 1, 1], [1, 0, 1], [1, 1, 1]]]\n",
|
|
") # defines the binary dilation mask to check if a field is next to an enemy stones\n",
|
|
"SURROUNDING"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"8.78 ms ± 868 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
|
|
"82.7 ms ± 585 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": "array([[[False, False, False, False, False, False, False, False],\n [False, False, False, False, False, False, False, False],\n [False, False, False, True, False, False, False, False],\n [False, False, True, False, False, False, False, False],\n [False, False, False, False, False, True, False, False],\n [False, False, False, False, True, False, False, False],\n [False, False, False, False, False, False, False, False],\n [False, False, False, False, False, False, False, False]]])"
|
|
},
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"def _recursive_steps(board, rec_direction, rec_position, step_one=True) -> bool:\n",
|
|
" \"\"\"Check if a player can place a stone on the board specified in the direction specified and direction specified.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" board: The board that should be checked for a playable action.\n",
|
|
" rec_direction: The direction that should be checked.\n",
|
|
" rec_position: The position that should be checked.\n",
|
|
" step_one: Defines if the call of this function is the firs or not. Should be kept to the default value for proper functionality.\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" True if a turn is possible for possition and direction on the board defined.\n",
|
|
" \"\"\"\n",
|
|
" rec_position = rec_position + rec_direction\n",
|
|
" if np.any((rec_position >= BOARD_SIZE) | (rec_position < 0)):\n",
|
|
" return False\n",
|
|
" next_field = board[tuple(rec_position.tolist())]\n",
|
|
" if next_field == 0:\n",
|
|
" return False\n",
|
|
" if next_field == -1:\n",
|
|
" return _recursive_steps(board, rec_direction, rec_position, step_one=False)\n",
|
|
" if next_field == 1:\n",
|
|
" return not step_one\n",
|
|
"\n",
|
|
"\n",
|
|
"def get_possible_turns(boards: np.ndarray) -> np.ndarray:\n",
|
|
" \"\"\"Analyses a stack of boards.\n",
|
|
"\n",
|
|
" Args:\n",
|
|
" boards: A stack of boards to check.\n",
|
|
"\n",
|
|
" Returns:\n",
|
|
" A stack of game boards containing boolean values showing where turns are possible for the player.\n",
|
|
" \"\"\"\n",
|
|
" assert len(boards.shape) == 3\n",
|
|
" assert boards.shape[:2] == (BOARD_SIZE, BOARD_SIZE)\n",
|
|
"\n",
|
|
" _poss_turns = boards == 0 # checks where fields are empty.\n",
|
|
" _poss_turns &= binary_dilation(\n",
|
|
" boards == -1, SURROUNDING\n",
|
|
" ) # checks where fields are next to an enemy filed an empty\n",
|
|
" for game, idx, idy in itertools.product(\n",
|
|
" range(boards.shape[0]), range(BOARD_SIZE), range(BOARD_SIZE)\n",
|
|
" ):\n",
|
|
" position = idx, idy\n",
|
|
" if _poss_turns[game, idx, idy]:\n",
|
|
" _poss_turns[game, idx, idy] = any(\n",
|
|
" _recursive_steps(boards[game, :, :], direction, position)\n",
|
|
" for direction in DIRECTIONS\n",
|
|
" )\n",
|
|
" return _poss_turns\n",
|
|
"\n",
|
|
"\n",
|
|
"%timeit get_possible_turns(get_new_games(10)) # checks turn possibility evaluation time for 10 initial games\n",
|
|
"%timeit get_possible_turns(get_new_games(100)) # check turn possibility evaluation time for 100 initial games\n",
|
|
"get_possible_turns(get_new_games(3))[:1] # shows a singe game"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def board_evaluation_final(boards: np.ndarray) -> tuple[np.ndarray, np.ndarray]:\n",
|
|
" score1, score2 = np.sum(boards == 1, axis=(1, 2)), np.sum(boards == -1, axis=(1, 2))\n",
|
|
" player_1_won = score1 > score2\n",
|
|
" player_2_won = score1 < score2\n",
|
|
" score1_final = 64 - score2[player_1_won]\n",
|
|
" score2_final = 64 - score1[player_2_won]\n",
|
|
" score1[player_1_won] = score1_final\n",
|
|
" score2[player_2_won] = score2_final\n",
|
|
" return score1, score2\n",
|
|
"\n",
|
|
"\n",
|
|
"def board_evaluation(boards: np.ndarray) -> tuple[np.ndarray, np.ndarray]:\n",
|
|
" score1, score2 = np.sum(boards == 1, axis=(1, 2)), np.sum(boards == -1, axis=(1, 2))\n",
|
|
" return score1, score2\n",
|
|
"\n",
|
|
"\n",
|
|
"def board_score(boards: np.ndarray) -> tuple[np.ndarray]:\n",
|
|
" return np.sign(np.sum(boards, axis=(1, 2)))\n",
|
|
"\n",
|
|
"\n",
|
|
"board_evaluation(get_new_games(3))\n",
|
|
"board_evaluation_final(get_new_games(3))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def move_possible(board: np.ndarray, move: np.ndarray) -> bool:\n",
|
|
" if np.all(move == -1):\n",
|
|
" return not np.any(get_possible_turns(np.reshape(board, (1, 8, 8))))\n",
|
|
" return any(\n",
|
|
" _recursive_steps(board[:, :], direction, move) for direction in DIRECTIONS\n",
|
|
" )\n",
|
|
"\n",
|
|
"\n",
|
|
"assert move_possible(get_new_games(1)[0], np.array([2, 3])) is True\n",
|
|
"assert move_possible(get_new_games(1)[0], np.array([3, 2])) is True\n",
|
|
"assert move_possible(get_new_games(1)[0], np.array([2, 2])) is False\n",
|
|
"assert move_possible(np.zeros((8, 8)), np.array([3, 2])) is False\n",
|
|
"assert move_possible(np.ones((8, 8)) * 1, np.array([-1, -1])) is True\n",
|
|
"assert move_possible(np.ones((8, 8)) * -1, np.array([-1, -1])) is True\n",
|
|
"assert move_possible(np.ones((8, 8)) * 0, np.array([-1, -1])) is True"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def moves_possible(boards: np.ndarray, moves: np.ndarray) -> np.ndarray:\n",
|
|
" arr_moves_possible = np.zeros(boards.shape[0], dtype=bool)\n",
|
|
" for game in range(boards.shape[0]):\n",
|
|
" if np.all(moves[game] == -1):\n",
|
|
" arr_moves_possible[game] = not np.any(\n",
|
|
" get_possible_turns(np.reshape(boards[game], (1, 8, 8)))\n",
|
|
" )\n",
|
|
" else:\n",
|
|
" arr_moves_possible[game] = any(\n",
|
|
" _recursive_steps(boards[game, :, :], direction, moves[game])\n",
|
|
" for direction in DIRECTIONS\n",
|
|
" )\n",
|
|
" return arr_moves_possible\n",
|
|
"\n",
|
|
"\n",
|
|
"np.testing.assert_array_equal(\n",
|
|
" moves_possible(np.ones((3, 8, 8)) * 1, np.array([[-1, -1]] * 3)),\n",
|
|
" np.array([True] * 3),\n",
|
|
")\n",
|
|
"\n",
|
|
"np.testing.assert_array_equal(\n",
|
|
" moves_possible(get_new_games(3), np.array([[2, 3], [3, 2], [3, 2]])),\n",
|
|
" np.array([True] * 3),\n",
|
|
")\n",
|
|
"np.testing.assert_array_equal(\n",
|
|
" moves_possible(get_new_games(3), np.array([[2, 2], [1, 1], [0, 0]])),\n",
|
|
" np.array([False] * 3),\n",
|
|
")\n",
|
|
"np.testing.assert_array_equal(\n",
|
|
" moves_possible(np.ones((3, 8, 8)) * -1, np.array([[-1, -1]] * 3)),\n",
|
|
" np.array([True] * 3),\n",
|
|
")\n",
|
|
"np.testing.assert_array_equal(\n",
|
|
" moves_possible(np.zeros((3, 8, 8)), np.array([[-1, -1]] * 3)),\n",
|
|
" np.array([True] * 3),\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class InvalidTurn(ValueError):\n",
|
|
" pass\n",
|
|
"\n",
|
|
"\n",
|
|
"def do_moves(boards: np.ndarray, moves: np.ndarray) -> np.ndarray:\n",
|
|
" def _do_directional_move(\n",
|
|
" board: np.ndarray, rec_move: np.ndarray, rev_direction, step_one=True\n",
|
|
" ) -> bool:\n",
|
|
" rec_position = rec_move + rev_direction\n",
|
|
" if np.any((rec_position >= 8) | (rec_position < 0)):\n",
|
|
" return False\n",
|
|
" next_field = board[tuple(rec_position.tolist())]\n",
|
|
" if next_field == 0:\n",
|
|
" return False\n",
|
|
" if next_field == 1:\n",
|
|
" return not step_one\n",
|
|
" if next_field == -1:\n",
|
|
" if _do_directional_move(board, rec_position, rev_direction, step_one=False):\n",
|
|
" board[tuple(rec_position.tolist())] = 1\n",
|
|
" return True\n",
|
|
" return False\n",
|
|
"\n",
|
|
" def _do_move(_board: np.ndarray, move: np.ndarray) -> None:\n",
|
|
" if np.all(move == -1):\n",
|
|
" return\n",
|
|
" if _board[tuple(move.tolist())] != 0:\n",
|
|
" raise InvalidTurn\n",
|
|
" action = False\n",
|
|
" for direction in DIRECTIONS:\n",
|
|
" if _do_directional_move(_board, move, direction):\n",
|
|
" action = True\n",
|
|
" if not action:\n",
|
|
" raise InvalidTurn()\n",
|
|
" _board[tuple(move.tolist())] = 1\n",
|
|
"\n",
|
|
" boards = boards.copy()\n",
|
|
" for game in range(boards.shape[0]):\n",
|
|
" _do_move(boards[game], moves[game])\n",
|
|
" return boards\n",
|
|
"\n",
|
|
"\n",
|
|
"do_moves(get_new_games(10), np.array([[2, 3]] * 10))[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class GamePolicy(ABC):\n",
|
|
"\n",
|
|
" IMPOSSIBLE: np.ndarray = np.array([-1, -1], dtype=int)\n",
|
|
"\n",
|
|
" @property\n",
|
|
" @abc.abstractmethod\n",
|
|
" def policy_name(self) -> str:\n",
|
|
" raise NotImplementedError()\n",
|
|
"\n",
|
|
" @abc.abstractmethod\n",
|
|
" def internal_policy(self, boards: np.ndarray) -> np.ndarray:\n",
|
|
" raise NotImplementedError()\n",
|
|
"\n",
|
|
" def get_policy(self, boards: np.ndarray) -> np.ndarray:\n",
|
|
" policies = self.internal_policy(boards)\n",
|
|
" possible_turns = get_possible_turns(boards)\n",
|
|
" policies[possible_turns == False] = -1.0\n",
|
|
" max_indices = [\n",
|
|
" np.unravel_index(policy.argmax(), policy.shape) for policy in policies\n",
|
|
" ]\n",
|
|
" policy_vector = np.array(max_indices)\n",
|
|
"\n",
|
|
" no_turn_possible = np.all(policy_vector == 0, 1) & (policies[:, 0, 0] == -1.0)\n",
|
|
"\n",
|
|
" policy_vector[no_turn_possible] = GamePolicy.IMPOSSIBLE\n",
|
|
" return policy_vector"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class RandomPolicy(GamePolicy):\n",
|
|
" @property\n",
|
|
" def policy_name(self) -> str:\n",
|
|
" return \"random\"\n",
|
|
"\n",
|
|
" def internal_policy(self, boards: np.ndarray) -> np.ndarray:\n",
|
|
" random_values = np.random.rand(*boards.shape)\n",
|
|
" return random_values\n",
|
|
" # return np.argmax(random_values, (1, 2))\n",
|
|
"\n",
|
|
"\n",
|
|
"rnd_policy = RandomPolicy()\n",
|
|
"assert rnd_policy.policy_name == \"random\"\n",
|
|
"rnd_policy_result = rnd_policy.get_policy(get_new_games(1))\n",
|
|
"assert np.any((5 >= rnd_policy_result) & (rnd_policy_result >= 3))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def single_turn(\n",
|
|
" current_boards: np, policy: GamePolicy\n",
|
|
") -> tuple[np.ndarray, np.ndarray]:\n",
|
|
" policy_results = policy.get_policy(current_boards)\n",
|
|
"\n",
|
|
" assert np.all(moves_possible(current_boards, policy_results)), (\n",
|
|
" current_boards[(moves_possible(current_boards, policy_results) == False)],\n",
|
|
" policy_results[(moves_possible(current_boards, policy_results) == False)],\n",
|
|
" np.where(moves_possible(current_boards, policy_results) == False),\n",
|
|
" )\n",
|
|
"\n",
|
|
" return do_moves(current_boards, policy_results), policy_results\n",
|
|
"\n",
|
|
"\n",
|
|
"%timeit single_turn(get_new_games(100), RandomPolicy())\n",
|
|
"single_turn(get_new_games(100), RandomPolicy())[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"SIMULATE_TURNS = 70\n",
|
|
"\n",
|
|
"\n",
|
|
"def simulate_game(\n",
|
|
" nr_of_games: int,\n",
|
|
" policies: tuple[GamePolicy, GamePolicy],\n",
|
|
") -> tuple[np.ndarray, np.ndarray]:\n",
|
|
"\n",
|
|
" board_history_stack = np.zeros((SIMULATE_TURNS, nr_of_games, 8, 8))\n",
|
|
" action_history_stack = np.zeros((SIMULATE_TURNS, nr_of_games, 2))\n",
|
|
" current_boards = get_new_games(nr_of_games)\n",
|
|
" for turn_index in range(SIMULATE_TURNS):\n",
|
|
" policy_index = turn_index % 2\n",
|
|
" policy = policies[policy_index]\n",
|
|
" board_history_stack[turn_index] = current_boards\n",
|
|
" if policy_index == 0:\n",
|
|
" current_boards = current_boards * -1\n",
|
|
" current_boards, action_taken = single_turn(current_boards, policy)\n",
|
|
" action_history_stack[turn_index] = action_taken\n",
|
|
"\n",
|
|
" if policy_index == 0:\n",
|
|
" current_boards = current_boards * -1\n",
|
|
"\n",
|
|
" return board_history_stack, action_history_stack\n",
|
|
"\n",
|
|
"\n",
|
|
"%timeit simulate_game(100, (RandomPolicy(), RandomPolicy()))\n",
|
|
"simulate_game(10, (RandomPolicy(), RandomPolicy()))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"\n",
|
|
"def create_test_game():\n",
|
|
" test_array = [\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 1, 2, 0, 0, 0],\n",
|
|
" [0, 0, 0, 2, 1, 0, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
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0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 1, 2, 2, 0, 0],\n",
|
|
" [2, 2, 2, 2, 2, 2, 0, 0],\n",
|
|
" [0, 2, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 1, 1, 1, 1, 0],\n",
|
|
" [2, 2, 2, 2, 2, 2, 0, 0],\n",
|
|
" [0, 2, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 2, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 1, 1, 1, 1, 0],\n",
|
|
" [2, 2, 2, 1, 2, 2, 0, 0],\n",
|
|
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 2, 1, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 2, 2, 2, 0, 0],\n",
|
|
" [0, 0, 0, 2, 2, 1, 1, 0],\n",
|
|
" [2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 0, 0],\n",
|
|
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
|
|
" [0, 0, 0, 2, 1, 2, 2, 0],\n",
|
|
" [0, 0, 0, 2, 2, 1, 2, 0],\n",
|
|
" [2, 2, 2, 2, 2, 2, 2, 2],\n",
|
|
" [0, 2, 0, 1, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 2, 1, 0, 1, 0, 0],\n",
|
|
" [0, 0, 0, 2, 1, 2, 2, 0],\n",
|
|
" [0, 0, 0, 2, 1, 1, 2, 0],\n",
|
|
" [2, 2, 2, 2, 1, 2, 2, 2],\n",
|
|
" [0, 2, 0, 1, 1, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" [\n",
|
|
" [0, 0, 0, 0, 2, 0, 0, 0],\n",
|
|
" [0, 0, 2, 2, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 2, 1, 2, 2, 0],\n",
|
|
" [0, 0, 0, 2, 1, 1, 2, 0],\n",
|
|
" [2, 2, 2, 2, 1, 2, 2, 2],\n",
|
|
" [0, 2, 0, 1, 1, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 2, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" ],\n",
|
|
" ]\n",
|
|
" test_array = np.array(test_array)\n",
|
|
"\n",
|
|
" # swapp 2 by one. 2 was only there for homogenous formating and easier readability while coading.\n",
|
|
" test_array[test_array == 2] = -1\n",
|
|
" assert np.all(\n",
|
|
" np.count_nonzero(test_array, axis=(1, 2))\n",
|
|
" == np.arange(4, 4 + test_array.shape[0])\n",
|
|
" )\n",
|
|
"\n",
|
|
" # validated that only one stone is added per turn\n",
|
|
" zero_array = test_array == 0\n",
|
|
" diff = zero_array != np.roll(zero_array, 1, axis=0)\n",
|
|
" turns = np.where(diff[1:])\n",
|
|
" arr = np.array(turns)[0]\n",
|
|
" assert len(arr) == len(set(arr))\n",
|
|
"\n",
|
|
" return test_array"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"plot_othello_boards(create_test_game()[-3:])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"array = create_test_game()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Sources\n",
|
|
"\n",
|
|
"* Game rules and example board images [https://en.wikipedia.org/wiki/Reversi](https://en.wikipedia.org/wiki/Reversi)\n",
|
|
"* Game rules and example game images [https://de.wikipedia.org/wiki/Othello_(Spiel)](https://de.wikipedia.org/wiki/Othello_(Spiel))\n",
|
|
"* Game strategy examples [https://de.wikipedia.org/wiki/Computer-Othello](https://de.wikipedia.org/wiki/Computer-Othello)\n",
|
|
"* Image for 8 directions [https://www.researchgate.net/journal/EURASIP-Journal-on-Image-and-Video-Processing-1687-5281](https://www.researchgate.net/journal/EURASIP-Journal-on-Image-and-Video-Processing-1687-5281)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|