Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 74 additions & 60 deletions notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Get checkers.py"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"!wget \"https://raw.githubusercontent.com/adilmrk/checkers/main/checkers.py\"\n"
],
"metadata": {},
"outputs": [],
"metadata": {}
"source": [
"!wget \"https://raw.githubusercontent.com/Btsan/CheckersBot/master/checkers.py\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create get metrics function"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow import keras\n",
"from tensorflow.keras import layers\n",
"from keras.models import Sequential\n",
"from keras.layers.core import Dense\n",
"from keras import regularizers\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras import regularizers\n",
"import checkers\n",
"import numpy as np\n",
"import matplotlib.pyplot as plot\n",
Expand All @@ -58,20 +60,20 @@
"\t\treturn np.array([-1, capped, potential, men, kings, caps, semicaps, uncaps, mid, far, won])\n",
"\telse:\n",
"\t\treturn np.array([1, capped, potential, men, kings, caps, semicaps, uncaps, mid, far, won])\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create metrics_model & model heuristic metrics"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Metrics model, which only looks at heuristic scoring metrics used for labeling\n",
"metrics_model = Sequential()\n",
Expand Down Expand Up @@ -108,21 +110,21 @@
"\n",
"# fit the metrics model\n",
"history = metrics_model.fit(\n",
"\tmetrics, winning, epochs=32, batch_size=64, verbose=0)\n"
],
"outputs": [],
"metadata": {}
"\tmetrics, winning, epochs=32, batch_size=64, verbose=0, validation_split=0.33)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualize"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# History for accuracy\n",
"plot.plot(history.history['acc'])\n",
Expand All @@ -141,20 +143,20 @@
"plot.xlabel('epoch')\n",
"plot.legend(['train', 'validation'], loc='upper left')\n",
"plot.show()\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instnatiate board model and heuristic metrics"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Board model\n",
"board_model = Sequential()\n",
Expand All @@ -179,25 +181,25 @@
"metrics = np.zeros((0, 10))\n",
"winning = np.zeros((0, 1))\n",
"data = boards_list\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fit the model and save weights"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for board in data:\n",
"\ttemp = checkers.get_metrics(board)\n",
"\tmetrics = np.vstack((metrics, temp[1:]))\n",
"\twinning = np.zeros((0, 1))\n",
"\twinning = np.vstack((winning, temp[0]))\n",
"\n",
"# calculate probilistic (noisy) labels\n",
"probabilistic = metrics_model.predict_on_batch(metrics)\n",
Expand All @@ -206,7 +208,7 @@
"probabilistic = np.sign(probabilistic)\n",
"\n",
"# calculate confidence score for each probabilistic label using error between probabilistic and weak label\n",
"confidence = 1/(1 + np.absolute(winning - probabilistic[:, 0]))\n",
"confidence = 1/(1 + np.absolute(winning - probabilistic))\n",
"\n",
"# pass to the Board model\n",
"board_model.fit(data, probabilistic, epochs=32, batch_size=64,\n",
Expand All @@ -218,20 +220,20 @@
"board_model.save_weights('board_model.h5')\n",
"\n",
"print('Checkers Board Model saved to: board_model.json/h5')\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reinforcing the model"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"json_file = open('board_model.json', 'r')\n",
"board_json = json_file.read()\n",
Expand All @@ -248,7 +250,7 @@
"learning_rate = 0.5\n",
"discount_factor = 0.95\n",
"\n",
"for gen in range(0, 50):\n",
"for gen in range(0, 500):\n",
"\tfor game in range(0, 200):\n",
"\t\ttemp_data = np.zeros((1, 32))\n",
"\t\tboard = checkers.expand(checkers.np_board())\n",
Expand Down Expand Up @@ -312,32 +314,32 @@
"\treinforced_model.save_weights('reinforced_model.h5')\n",
"\n",
"print('Checkers Board Model updated by reinforcement learning & saved to: reinforced_model.json/h5')\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generations = range(0, 500)\n",
"print(\"Final win/draw rate : \" + str(winrates[499])+\"%\")\n",
"plot.plot(generations, winrates)\n",
"plot.show()\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the model"
],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def best_move(board):\n",
" compressed_board = checkers.compress(board)\n",
Expand All @@ -360,13 +362,13 @@
" caracter = \"| \"\n",
" print(str(caracter), end='')\n",
" print('|')\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"start_board = [1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0,\n",
" 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]\n",
Expand All @@ -378,24 +380,36 @@
"\n",
"print(\"\\nBest next move : \")\n",
"print_board(next_board)\n"
],
"outputs": [],
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"source": [],
"metadata": {},
"outputs": [],
"metadata": {}
"source": []
}
],
"metadata": {
"orig_nbformat": 4,
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
}