├── Chess Ai Trainer v4.ipynb ├── Chess4096.ipynb ├── LICENSE ├── Naive Bayes Chess AI.ipynb ├── README.md └── chess_normalized.csv /Chess Ai Trainer v4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2020-08-24T15:06:45.571115Z", 9 | "start_time": "2020-08-24T15:01:54.482389Z" 10 | } 11 | }, 12 | "outputs": [ 13 | { 14 | "ename": "FileNotFoundError", 15 | "evalue": "[WinError 3] The system cannot find the path specified: '/Users/elizabethtai/Desktop/Files/Data/Normalized'", 16 | "output_type": "error", 17 | "traceback": [ 18 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 19 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 20 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[0mflatten\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0ml\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mitem\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0msublist\u001b[0m \u001b[1;32min\u001b[0m \u001b[0ml\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msublist\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'/Users/elizabethtai/Desktop/Files/Data/Normalized'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 19\u001b[0m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'chess_normalized.csv'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'moves'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtolist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 21 | "\u001b[1;31mFileNotFoundError\u001b[0m: [WinError 3] The system cannot find the path specified: '/Users/elizabethtai/Desktop/Files/Data/Normalized'" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import os\n", 27 | "import chess\n", 28 | "import numpy as np\n", 29 | "import pandas as pd\n", 30 | "import keras\n", 31 | "import time\n", 32 | "\n", 33 | "from keras import callbacks, optimizers\n", 34 | "from keras.layers import (LSTM, BatchNormalization, Dense, Dropout, Flatten,\n", 35 | " TimeDistributed)\n", 36 | "from keras.layers.convolutional import Conv2D, MaxPooling2D\n", 37 | "from keras.models import Sequential, load_model, model_from_json\n", 38 | "from IPython.display import clear_output\n", 39 | "from matplotlib import pyplot as plt\n", 40 | "\n", 41 | "flatten = lambda l: [item for sublist in l for item in sublist]\n", 42 | "\n", 43 | "os.chdir('/Users/elizabethtai/Desktop/Files/Data/Normalized')\n", 44 | "df = pd.read_csv('chess_normalized.csv')\n", 45 | "data = df['moves'].tolist()\n", 46 | "\n", 47 | "lendata= len(data)\n", 48 | "data = data[:100]\n", 49 | "\n", 50 | "chess_dict = {\n", 51 | " 'p' : [1,0,0,0,0,0,0,0,0,0,0,0],\n", 52 | " 'P' : [0,0,0,0,0,0,1,0,0,0,0,0],\n", 53 | " 'n' : [0,1,0,0,0,0,0,0,0,0,0,0],\n", 54 | " 'N' : [0,0,0,0,0,0,0,1,0,0,0,0],\n", 55 | " 'b' : [0,0,1,0,0,0,0,0,0,0,0,0],\n", 56 | " 'B' : [0,0,0,0,0,0,0,0,1,0,0,0],\n", 57 | " 'r' : [0,0,0,1,0,0,0,0,0,0,0,0],\n", 58 | " 'R' : [0,0,0,0,0,0,0,0,0,1,0,0],\n", 59 | " 'q' : [0,0,0,0,1,0,0,0,0,0,0,0],\n", 60 | " 'Q' : [0,0,0,0,0,0,0,0,0,0,1,0],\n", 61 | " 'k' : [0,0,0,0,0,1,0,0,0,0,0,0],\n", 62 | " 'K' : [0,0,0,0,0,0,0,0,0,0,0,1],\n", 63 | " '.' : [0,0,0,0,0,0,0,0,0,0,0,0],\n", 64 | "}\n", 65 | "\n", 66 | "def make_matrix(board): \n", 67 | " pgn = board.epd()\n", 68 | " foo = [] \n", 69 | " pieces = pgn.split(\" \", 1)[0]\n", 70 | " rows = pieces.split(\"/\")\n", 71 | " for row in rows:\n", 72 | " foo2 = [] \n", 73 | " for thing in row:\n", 74 | " if thing.isdigit():\n", 75 | " for i in range(0, int(thing)):\n", 76 | " foo2.append('.')\n", 77 | " else:\n", 78 | " foo2.append(thing)\n", 79 | " foo.append(foo2)\n", 80 | " return foo\n", 81 | "\n", 82 | "def translate(matrix,chess_dict):\n", 83 | " rows = []\n", 84 | " for row in matrix:\n", 85 | " terms = []\n", 86 | " for term in row:\n", 87 | " terms.append(chess_dict[term])\n", 88 | " rows.append(terms)\n", 89 | " return rows\n", 90 | "\n", 91 | "def data_setup():\n", 92 | " X = []\n", 93 | " y = []\n", 94 | " for game in range(len(data)):\n", 95 | " data[game] = data[game].split()\n", 96 | " board = chess.Board()\n", 97 | " for move in range(len(data[game])):\n", 98 | " legal_moves = str(board.legal_moves)[36:-2].replace(',','').split()\n", 99 | " matrix = make_matrix(board.copy())\n", 100 | " translated = translate(matrix,chess_dict)\n", 101 | " X.append(translated)\n", 102 | " board.push_san(data[game][move])\n", 103 | " y.append(legal_moves.index(data[game][move])/len(legal_moves))\n", 104 | " return X,y\n", 105 | "\n", 106 | "def initialize_network():\n", 107 | " model = Sequential()\n", 108 | " model.add(Conv2D(filters=10, kernel_size=1, activation='relu', input_shape=(8,8,12)))\n", 109 | " model.add(MaxPooling2D(pool_size=2, strides=None))\n", 110 | " model.add(Conv2D(filters=10, kernel_size=1, activation='relu'))\n", 111 | " model.add(MaxPooling2D(pool_size=2, strides=None))\n", 112 | " model.add(Conv2D(filters=10, kernel_size=1, activation='relu'))\n", 113 | " model.add(MaxPooling2D(pool_size=2, strides=None))\n", 114 | " model.add(Conv2D(filters=10, kernel_size=1, activation='relu'))\n", 115 | " model.add(Flatten())\n", 116 | " model.add(BatchNormalization())\n", 117 | " model.add(Dense(1,activation = 'sigmoid'))\n", 118 | " return model\n", 119 | "\n", 120 | "print('Preparing Data...')\n", 121 | "start = time.time()\n", 122 | "X,y = data_setup()\n", 123 | "one_game_time = time.time()-start\n", 124 | "X = np.array(X)\n", 125 | "X_test = X[:int(len(X)*0.1)]\n", 126 | "X_train = X[int(len(X)*0.1):]\n", 127 | "y_test = y[:int(len(X)*0.1)]\n", 128 | "y_train = y[int(len(X)*0.1):]\n", 129 | "print('Initalizing Network...')\n", 130 | "model = initialize_network()\n", 131 | "print('Compiling Network...')\n", 132 | "\n", 133 | "model.compile(optimizer='Nadam', loss='mse')\n", 134 | "dirx = '//Users/elizabethtai/Desktop/Files/Programs/ML/Best Models Stocks'\n", 135 | "os.chdir(dirx)\n", 136 | "h5 = 'chess' + '_best_model' + '.h5'\n", 137 | "checkpoint = callbacks.ModelCheckpoint(h5,\n", 138 | " monitor='val_loss',\n", 139 | " verbose=0,\n", 140 | " save_best_only=True,\n", 141 | " save_weights_only=True,\n", 142 | " mode='auto',\n", 143 | " period=1)\n", 144 | "es = callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5000/10)\n", 145 | "callback = [checkpoint,es]\n", 146 | "json = 'chess' + '_best_model' + '.json'\n", 147 | "model_json = model.to_json()\n", 148 | "with open(json, \"w\") as json_file:\n", 149 | " json_file.write(model_json)\n", 150 | "print('Training Network...')\n", 151 | "history = model.fit(X_train,y_train,epochs = 1000,verbose = 2,validation_data = (X_test,y_test),callbacks = callback)\n", 152 | "plt.plot(history.history['loss'])\n", 153 | "plt.plot(history.history['val_loss'])" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 17, 159 | "metadata": { 160 | "ExecuteTime": { 161 | "end_time": "2020-08-24T15:08:49.137805Z", 162 | "start_time": "2020-08-24T15:08:48.783493Z" 163 | } 164 | }, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "image/svg+xml": [ 169 | "" 170 | ], 171 | "text/plain": [ 172 | "Board('r1bqk1nr/ppppbp1p/4p3/5RN1/2B3P1/n3P3/PP1P1P1P/RNB2K2 w kq - 74 51')" 173 | ] 174 | }, 175 | "execution_count": 17, 176 | "metadata": {}, 177 | "output_type": "execute_result" 178 | } 179 | ], 180 | "source": [ 181 | "chess_board = chess.Board()\n", 182 | "for i in range(100):\n", 183 | " matrix = make_matrix(chess_board)\n", 184 | " board = translate(matrix,chess_dict)\n", 185 | " board = np.array(board)\n", 186 | " board = np.reshape(board,(1,8,8,12))\n", 187 | " pred = model.predict(board)\n", 188 | " legal_moves = str(chess_board.legal_moves)[36:-2].replace(',','').split()\n", 189 | " index = int(round((len(legal_moves)*pred)[0][0]))\n", 190 | " chess_board.push_san(legal_moves[index])\n", 191 | " if chess_board.is_checkmate():\n", 192 | " break\n", 193 | "chess_board" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": { 200 | "ExecuteTime": { 201 | "end_time": "2020-08-01T12:20:00.150107Z", 202 | "start_time": "2020-08-01T12:19:59.491787Z" 203 | } 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "from matplotlib import pyplot as plt\n", 208 | "plt.plot(history.history['loss'])" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "ExecuteTime": { 216 | "end_time": "2020-08-01T11:49:28.249307Z", 217 | "start_time": "2020-08-01T11:49:28.223526Z" 218 | } 219 | }, 220 | "outputs": [], 221 | "source": [ 222 | "data = df['moves'].tolist()\n", 223 | "data.index(sorted(data,key=lambda point: len(point),reverse = True)[0])" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": null, 229 | "metadata": { 230 | "ExecuteTime": { 231 | "end_time": "2020-08-01T14:33:21.126592Z", 232 | "start_time": "2020-08-01T14:33:21.094435Z" 233 | } 234 | }, 235 | "outputs": [], 236 | "source": [ 237 | "model.weights" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": { 244 | "ExecuteTime": { 245 | "end_time": "2020-08-02T04:12:09.869514Z", 246 | "start_time": "2020-08-02T04:12:09.857261Z" 247 | } 248 | }, 249 | "outputs": [], 250 | "source": [ 251 | "X[1]" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 79, 257 | "metadata": { 258 | "ExecuteTime": { 259 | "end_time": "2020-12-20T12:57:13.442101Z", 260 | "start_time": "2020-12-20T12:57:13.422157Z" 261 | } 262 | }, 263 | "outputs": [], 264 | "source": [ 265 | "import chess\n", 266 | "board = chess.Board()\n", 267 | "def make_matrix(board): \n", 268 | " pgn = board.epd()\n", 269 | " foo = [] \n", 270 | " pieces = pgn.split(\" \", 1)[0]\n", 271 | " rows = pieces.split(\"/\")\n", 272 | " for row in rows:\n", 273 | " foo2 = [] \n", 274 | " for thing in row:\n", 275 | " if thing.isdigit():\n", 276 | " for i in range(0, int(thing)):\n", 277 | " foo2.append('.')\n", 278 | " else:\n", 279 | " foo2.append(thing)\n", 280 | " foo.append(foo2)\n", 281 | " return foo" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 81, 287 | "metadata": { 288 | "ExecuteTime": { 289 | "end_time": "2020-12-20T12:58:53.828421Z", 290 | "start_time": "2020-12-20T12:58:53.821434Z" 291 | } 292 | }, 293 | "outputs": [], 294 | "source": [ 295 | "import os\n", 296 | "os.getcwd()\n", 297 | "os.chdir('C:\\\\Users\\\\v_sim\\\\Desktop\\\\Files\\\\Data')" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 82, 303 | "metadata": { 304 | "ExecuteTime": { 305 | "end_time": "2020-12-20T12:58:55.460235Z", 306 | "start_time": "2020-12-20T12:58:54.234671Z" 307 | } 308 | }, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "100" 314 | ] 315 | }, 316 | "execution_count": 82, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "import chess.pgn\n", 323 | "pgn = open(\"Carlsen.pgn\")\n", 324 | "sides = []\n", 325 | "games = []\n", 326 | "length = 100\n", 327 | "for i in range(length):\n", 328 | " try:\n", 329 | " if chess.pgn.read_game(pgn).mainline_moves():\n", 330 | " games.append(chess.pgn.read_game(pgn).mainline_moves())\n", 331 | " sides.append(chess.pgn.read_game(pgn).headers[\"White\"])\n", 332 | " except:\n", 333 | " print(i,chess.pgn.read_game(pgn))\n", 334 | " pass\n", 335 | "len(games)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 97, 341 | "metadata": { 342 | "ExecuteTime": { 343 | "end_time": "2020-12-20T13:04:38.790848Z", 344 | "start_time": "2020-12-20T13:04:37.136417Z" 345 | } 346 | }, 347 | "outputs": [ 348 | { 349 | "data": { 350 | "image/svg+xml": [ 351 | "" 352 | ], 353 | "text/plain": [ 354 | "Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')" 355 | ] 356 | }, 357 | "execution_count": 97, 358 | "metadata": {}, 359 | "output_type": "execute_result" 360 | } 361 | ], 362 | "source": [ 363 | "X = []\n", 364 | "y = []\n", 365 | "counter2 = 0\n", 366 | "for game in games:\n", 367 | " board = chess.Board()\n", 368 | " white = sides[counter2]\n", 369 | " if white == 'Carlsen,Magnus':\n", 370 | " remainder = 0\n", 371 | " else:\n", 372 | " remainder = 1\n", 373 | " counter = 0\n", 374 | " for move in game:\n", 375 | " if counter % 2 == remainder:\n", 376 | " X.append(board.copy())\n", 377 | " board.push(move)\n", 378 | " if counter % 2 == remainder:\n", 379 | " y.append(board.copy())\n", 380 | " counter += 1\n", 381 | " counter2 += 1\n", 382 | "X[0]" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 101, 388 | "metadata": { 389 | "ExecuteTime": { 390 | "end_time": "2020-12-20T13:05:42.034348Z", 391 | "start_time": "2020-12-20T13:05:42.016408Z" 392 | } 393 | }, 394 | "outputs": [], 395 | "source": [ 396 | "chess_dict = {\n", 397 | " 'p' : [1,0,0,0,0,0,0,0,0,0,0,0,0],\n", 398 | " 'P' : [0,0,0,0,0,0,1,0,0,0,0,0,0],\n", 399 | " 'n' : [0,1,0,0,0,0,0,0,0,0,0,0,0],\n", 400 | " 'N' : [0,0,0,0,0,0,0,1,0,0,0,0,0],\n", 401 | " 'b' : [0,0,1,0,0,0,0,0,0,0,0,0,0],\n", 402 | " 'B' : [0,0,0,0,0,0,0,0,1,0,0,0,0],\n", 403 | " 'r' : [0,0,0,1,0,0,0,0,0,0,0,0,0],\n", 404 | " 'R' : [0,0,0,0,0,0,0,0,0,1,0,0,0],\n", 405 | " 'q' : [0,0,0,0,1,0,0,0,0,0,0,0,0],\n", 406 | " 'Q' : [0,0,0,0,0,0,0,0,0,0,1,0,0],\n", 407 | " 'k' : [0,0,0,0,0,1,0,0,0,0,0,0,0],\n", 408 | " 'K' : [0,0,0,0,0,0,0,0,0,0,0,1,0],\n", 409 | " '.' : [0,0,0,0,0,0,0,0,0,0,0,0,1],\n", 410 | "}\n", 411 | "\n", 412 | "def make_matrix(board): \n", 413 | " pgn = board.epd()\n", 414 | " foo = [] \n", 415 | " pieces = pgn.split(\" \", 1)[0]\n", 416 | " rows = pieces.split(\"/\")\n", 417 | " for row in rows:\n", 418 | " foo2 = [] \n", 419 | " for thing in row:\n", 420 | " if thing.isdigit():\n", 421 | " for i in range(0, int(thing)):\n", 422 | " foo2.append('.')\n", 423 | " else:\n", 424 | " foo2.append(thing)\n", 425 | " foo.append(foo2)\n", 426 | " return foo\n", 427 | "\n", 428 | "def translate(matrix,chess_dict):\n", 429 | " rows = []\n", 430 | " for row in matrix:\n", 431 | " terms = []\n", 432 | " for term in row:\n", 433 | " terms.append(chess_dict[term])\n", 434 | " rows.append(terms)\n", 435 | " return rows" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 102, 441 | "metadata": { 442 | "ExecuteTime": { 443 | "end_time": "2020-12-20T13:05:43.990704Z", 444 | "start_time": "2020-12-20T13:05:42.348275Z" 445 | } 446 | }, 447 | "outputs": [], 448 | "source": [ 449 | "import numpy as np\n", 450 | "for i in range(len(X)):\n", 451 | " X[i] = translate(make_matrix(X[i]),chess_dict)\n", 452 | "for i in range(len(y)):\n", 453 | " y[i] = translate(make_matrix(y[i]),chess_dict)\n", 454 | "X = np.array(X)\n", 455 | "y = np.array(y)" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 124, 461 | "metadata": { 462 | "ExecuteTime": { 463 | "end_time": "2020-12-20T13:27:52.023518Z", 464 | "start_time": "2020-12-20T13:27:52.002636Z" 465 | } 466 | }, 467 | "outputs": [ 468 | { 469 | "data": { 470 | "image/svg+xml": [ 471 | "" 472 | ], 473 | "text/plain": [ 474 | "Board('r1bqk1nr/5pbp/p1ppp1p1/2p1P3/5P2/2N2N2/PPPPQ1PP/R1B2RK1 w - - 0 1')" 475 | ] 476 | }, 477 | "execution_count": 124, 478 | "metadata": {}, 479 | "output_type": "execute_result" 480 | } 481 | ], 482 | "source": [ 483 | "import random\n", 484 | "flatten = lambda l: [item for sublist in l for item in sublist]\n", 485 | "def retranslate(action):\n", 486 | " board = []\n", 487 | " flatten_action = flatten(action)\n", 488 | " for i in range(len(flatten_action)):\n", 489 | " new_set = np.zeros((13,))\n", 490 | " max_index = list(flatten_action[i]).index(max(flatten_action[i]))\n", 491 | " new_set[max_index] = 1\n", 492 | " board.append(new_set)\n", 493 | " for i in range(len(board)):\n", 494 | " board[i] = new_chess_dict[tuple(board[i])]\n", 495 | " board = np.array(board).reshape(8,8)\n", 496 | " return board\n", 497 | "\n", 498 | "def matrix2board(matrix):\n", 499 | " fen = []\n", 500 | " for row in matrix:\n", 501 | " foo2 = str()\n", 502 | " counter = 0\n", 503 | " counter2 = 0\n", 504 | " for thing in row:\n", 505 | " counter2+=1\n", 506 | " if thing == '.':\n", 507 | " counter+=1\n", 508 | " if counter2 == len(row) and thing == '.':\n", 509 | " foo2+=str(counter)\n", 510 | " if thing != '.':\n", 511 | " if counter > 0:\n", 512 | " foo2+=str(counter)\n", 513 | " counter = 0\n", 514 | " foo2+=thing\n", 515 | " fen.append(foo2)\n", 516 | "\n", 517 | " final_fentasy = str()\n", 518 | " for i in range(len(fen)):\n", 519 | " final_fentasy += fen[i]\n", 520 | " if i != len(fen)-1:\n", 521 | " final_fentasy += '/'\n", 522 | " return chess.Board(final_fentasy)\n", 523 | "\n", 524 | "new_chess_dict = {}\n", 525 | "for term in chess_dict:\n", 526 | " definition = tuple(chess_dict[term])\n", 527 | " new_chess_dict[definition] = term\n", 528 | " new_chess_dict[term] = definition\n", 529 | "\n", 530 | "matrix = retranslate(random.choice(X))\n", 531 | "board = matrix2board(matrix)\n", 532 | "board" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": null, 538 | "metadata": {}, 539 | "outputs": [], 540 | "source": [] 541 | } 542 | ], 543 | "metadata": { 544 | "hide_input": false, 545 | "kernelspec": { 546 | "display_name": "Python 3", 547 | "language": "python", 548 | "name": "python3" 549 | }, 550 | "language_info": { 551 | "codemirror_mode": { 552 | "name": "ipython", 553 | "version": 3 554 | }, 555 | "file_extension": ".py", 556 | "mimetype": "text/x-python", 557 | "name": "python", 558 | "nbconvert_exporter": "python", 559 | "pygments_lexer": "ipython3", 560 | "version": "3.8.3" 561 | }, 562 | "toc": { 563 | "base_numbering": 1, 564 | "nav_menu": {}, 565 | "number_sections": true, 566 | "sideBar": true, 567 | "skip_h1_title": false, 568 | "title_cell": "Table of Contents", 569 | "title_sidebar": "Contents", 570 | "toc_cell": false, 571 | "toc_position": {}, 572 | "toc_section_display": true, 573 | "toc_window_display": false 574 | }, 575 | "varInspector": { 576 | "cols": { 577 | "lenName": 16, 578 | "lenType": 16, 579 | "lenVar": 40 580 | }, 581 | "kernels_config": { 582 | "python": { 583 | "delete_cmd_postfix": "", 584 | "delete_cmd_prefix": "del ", 585 | "library": "var_list.py", 586 | "varRefreshCmd": "print(var_dic_list())" 587 | }, 588 | "r": { 589 | "delete_cmd_postfix": ") ", 590 | "delete_cmd_prefix": "rm(", 591 | "library": "var_list.r", 592 | "varRefreshCmd": "cat(var_dic_list()) " 593 | } 594 | }, 595 | "types_to_exclude": [ 596 | "module", 597 | "function", 598 | "builtin_function_or_method", 599 | "instance", 600 | "_Feature" 601 | ], 602 | "window_display": false 603 | } 604 | }, 605 | "nbformat": 4, 606 | "nbformat_minor": 4 607 | } 608 | -------------------------------------------------------------------------------- /Chess4096.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 42, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2021-02-12T02:26:59.146115Z", 9 | "start_time": "2021-02-12T02:26:59.132162Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "import os\n", 15 | "os.getcwd()\n", 16 | "os.chdir('C:\\\\Users\\\\v_sim\\\\Desktop\\\\Files\\\\Data')" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 43, 22 | "metadata": { 23 | "ExecuteTime": { 24 | "end_time": "2021-02-12T02:27:00.968971Z", 25 | "start_time": "2021-02-12T02:27:00.091712Z" 26 | } 27 | }, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "100" 33 | ] 34 | }, 35 | "execution_count": 43, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "import chess.pgn\n", 42 | "pgn = open(\"Carlsen.pgn\")\n", 43 | "sides = []\n", 44 | "games = []\n", 45 | "length = 100\n", 46 | "for i in range(length):\n", 47 | " try:\n", 48 | " if chess.pgn.read_game(pgn).mainline_moves():\n", 49 | " games.append(chess.pgn.read_game(pgn).mainline_moves())\n", 50 | " sides.append(chess.pgn.read_game(pgn).headers[\"White\"])\n", 51 | " except:\n", 52 | " pass\n", 53 | "len(games)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 44, 59 | "metadata": { 60 | "ExecuteTime": { 61 | "end_time": "2021-02-12T02:27:01.681409Z", 62 | "start_time": "2021-02-12T02:27:00.970916Z" 63 | } 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "X = []\n", 68 | "y = []\n", 69 | "counter2 = 0\n", 70 | "for game in games:\n", 71 | " board = chess.Board()\n", 72 | " white = sides[counter2]\n", 73 | " if white == 'Carlsen,Magnus':\n", 74 | " remainder = 0\n", 75 | " else:\n", 76 | " remainder = 1\n", 77 | " counter = 0\n", 78 | " for move in game:\n", 79 | " if counter % 2 == remainder:\n", 80 | " X.append(board.copy())\n", 81 | " y.append(move)\n", 82 | " board.push(move)\n", 83 | " counter += 1\n", 84 | " counter2 += 1" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 45, 90 | "metadata": { 91 | "ExecuteTime": { 92 | "end_time": "2021-02-12T02:27:01.699357Z", 93 | "start_time": "2021-02-12T02:27:01.683406Z" 94 | } 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "chess_dict = {\n", 99 | " 'p' : [1,0,0,0,0,0,0,0,0,0,0,0,0],\n", 100 | " 'P' : [0,0,0,0,0,0,1,0,0,0,0,0,0],\n", 101 | " 'n' : [0,1,0,0,0,0,0,0,0,0,0,0,0],\n", 102 | " 'N' : [0,0,0,0,0,0,0,1,0,0,0,0,0],\n", 103 | " 'b' : [0,0,1,0,0,0,0,0,0,0,0,0,0],\n", 104 | " 'B' : [0,0,0,0,0,0,0,0,1,0,0,0,0],\n", 105 | " 'r' : [0,0,0,1,0,0,0,0,0,0,0,0,0],\n", 106 | " 'R' : [0,0,0,0,0,0,0,0,0,1,0,0,0],\n", 107 | " 'q' : [0,0,0,0,1,0,0,0,0,0,0,0,0],\n", 108 | " 'Q' : [0,0,0,0,0,0,0,0,0,0,1,0,0],\n", 109 | " 'k' : [0,0,0,0,0,1,0,0,0,0,0,0,0],\n", 110 | " 'K' : [0,0,0,0,0,0,0,0,0,0,0,1,0],\n", 111 | " '.' : [0,0,0,0,0,0,0,0,0,0,0,0,1],\n", 112 | "}\n", 113 | "\n", 114 | "def make_matrix(board): \n", 115 | " pgn = board.epd()\n", 116 | " foo = [] \n", 117 | " pieces = pgn.split(\" \", 1)[0]\n", 118 | " rows = pieces.split(\"/\")\n", 119 | " for row in rows:\n", 120 | " foo2 = [] \n", 121 | " for thing in row:\n", 122 | " if thing.isdigit():\n", 123 | " for i in range(0, int(thing)):\n", 124 | " foo2.append('.')\n", 125 | " else:\n", 126 | " foo2.append(thing)\n", 127 | " foo.append(foo2)\n", 128 | " return foo\n", 129 | "\n", 130 | "def translate(matrix,chess_dict):\n", 131 | " rows = []\n", 132 | " for row in matrix:\n", 133 | " terms = []\n", 134 | " for term in row:\n", 135 | " terms.append(chess_dict[term])\n", 136 | " rows.append(terms)\n", 137 | " return rows" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 46, 143 | "metadata": { 144 | "ExecuteTime": { 145 | "end_time": "2021-02-12T02:27:01.715362Z", 146 | "start_time": "2021-02-12T02:27:01.701351Z" 147 | } 148 | }, 149 | "outputs": [], 150 | "source": [ 151 | "new_chess_dict = {}\n", 152 | "for term in chess_dict:\n", 153 | " definition = tuple(chess_dict[term])\n", 154 | " new_chess_dict[definition] = term\n", 155 | " new_chess_dict[term] = definition" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 47, 161 | "metadata": { 162 | "ExecuteTime": { 163 | "end_time": "2021-02-12T02:27:02.423518Z", 164 | "start_time": "2021-02-12T02:27:02.052379Z" 165 | } 166 | }, 167 | "outputs": [], 168 | "source": [ 169 | "import numpy as np\n", 170 | "for i in range(len(X)):\n", 171 | " X[i] = translate(make_matrix(X[i]),chess_dict)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 48, 177 | "metadata": { 178 | "ExecuteTime": { 179 | "end_time": "2021-02-12T02:27:02.672155Z", 180 | "start_time": "2021-02-12T02:27:02.653214Z" 181 | } 182 | }, 183 | "outputs": [], 184 | "source": [ 185 | "def move2array(move):\n", 186 | " from_square = move.from_square\n", 187 | " to_square = move.to_square\n", 188 | " array = np.zeros((64,64))\n", 189 | " array[from_square,to_square] = 1\n", 190 | " return array\n", 191 | "\n", 192 | "def array2move(array):\n", 193 | " from_square = chess.SQUARE_NAMES[int(np.where(array == 1)[0])]\n", 194 | " to_square = chess.SQUARE_NAMES[int(np.where(array == 1)[1])]\n", 195 | " move = chess.Move.from_uci(from_square+to_square)\n", 196 | " return move\n", 197 | " \n", 198 | "array = move2array(y[0]).reshape(64,64)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 49, 204 | "metadata": { 205 | "ExecuteTime": { 206 | "end_time": "2021-02-12T02:27:03.907311Z", 207 | "start_time": "2021-02-12T02:27:03.832437Z" 208 | } 209 | }, 210 | "outputs": [], 211 | "source": [ 212 | "for i in range(len(y)):\n", 213 | " y[i] = move2array(y[i]).reshape(64,64)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 50, 219 | "metadata": { 220 | "ExecuteTime": { 221 | "end_time": "2021-02-12T02:27:05.182637Z", 222 | "start_time": "2021-02-12T02:27:04.741649Z" 223 | } 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "X = np.array(X)\n", 228 | "y = np.array(y)" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 51, 234 | "metadata": { 235 | "ExecuteTime": { 236 | "end_time": "2021-02-12T02:27:05.526689Z", 237 | "start_time": "2021-02-12T02:27:05.501676Z" 238 | } 239 | }, 240 | "outputs": [ 241 | { 242 | "data": { 243 | "text/plain": [ 244 | "((3810, 8, 8, 13), (3810, 64, 64))" 245 | ] 246 | }, 247 | "execution_count": 51, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "X.shape,y.shape" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 55, 259 | "metadata": { 260 | "ExecuteTime": { 261 | "end_time": "2021-02-12T02:27:25.062676Z", 262 | "start_time": "2021-02-12T02:27:24.763478Z" 263 | } 264 | }, 265 | "outputs": [], 266 | "source": [ 267 | "from keras.models import Sequential\n", 268 | "from keras.layers import Dense,BatchNormalization,Dropout,Reshape,Flatten\n", 269 | "\n", 270 | "model = Sequential()\n", 271 | "model.add(Dense(32,input_shape = (8,8,13)))\n", 272 | "model.add(Dense(64))\n", 273 | "model.add(Dense(128))\n", 274 | "model.add(Flatten())\n", 275 | "model.add(Dense(4096,activation = 'softmax'))\n", 276 | "model.add(Reshape((64,64)))\n", 277 | "\n", 278 | "model.compile(optimizer = 'adam',loss = 'mse')" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 56, 284 | "metadata": { 285 | "ExecuteTime": { 286 | "end_time": "2021-02-12T02:27:38.901074Z", 287 | "start_time": "2021-02-12T02:27:25.471795Z" 288 | } 289 | }, 290 | "outputs": [ 291 | { 292 | "name": "stdout", 293 | "output_type": "stream", 294 | "text": [ 295 | "120/120 [==============================] - 13s 106ms/step - loss: 2.4408e-04\n" 296 | ] 297 | }, 298 | { 299 | "data": { 300 | "text/plain": [ 301 | "" 302 | ] 303 | }, 304 | "execution_count": 56, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "model.fit(X,y)" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 61, 316 | "metadata": { 317 | "ExecuteTime": { 318 | "end_time": "2021-02-12T08:48:05.768682Z", 319 | "start_time": "2021-02-12T08:47:51.950202Z" 320 | } 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "Requirement already satisfied: python-chess in c:\\users\\v_sim\\miniconda3\\lib\\site-packages (0.31.4)Note: you may need to restart the kernel to use updated packages.\n", 328 | "\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "pip install python-chess" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": null, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [] 342 | } 343 | ], 344 | "metadata": { 345 | "kernelspec": { 346 | "display_name": "Python 3", 347 | "language": "python", 348 | "name": "python3" 349 | }, 350 | "language_info": { 351 | "codemirror_mode": { 352 | "name": "ipython", 353 | "version": 3 354 | }, 355 | "file_extension": ".py", 356 | "mimetype": "text/x-python", 357 | "name": "python", 358 | "nbconvert_exporter": "python", 359 | "pygments_lexer": "ipython3", 360 | "version": "3.8.3" 361 | } 362 | }, 363 | "nbformat": 4, 364 | "nbformat_minor": 4 365 | } 366 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 victorsimrbt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Naive Bayes Chess AI.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2020-08-24T13:16:53.986680Z", 9 | "start_time": "2020-08-24T13:16:35.453896Z" 10 | } 11 | }, 12 | "outputs": [ 13 | { 14 | "ename": "ValueError", 15 | "evalue": "invalid san: '(Kxf7'", 16 | "output_type": "error", 17 | "traceback": [ 18 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 19 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 20 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 86\u001b[0m \u001b[0mside\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mchess\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mWHITE\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 87\u001b[0m \u001b[0mwhite_moves\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mblack_moves\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmove\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcalculate_probability\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mside\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mboard\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mwhite\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mblack\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mwhite_moves\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mblack_moves\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 88\u001b[1;33m \u001b[0mboard\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpush_san\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmove\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 89\u001b[0m \u001b[0mboard\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 21 | "\u001b[1;32m~\\Miniconda3\\lib\\site-packages\\chess\\__init__.py\u001b[0m in \u001b[0;36mpush_san\u001b[1;34m(self, san)\u001b[0m\n\u001b[0;32m 2843\u001b[0m \u001b[1;33m:\u001b[0m\u001b[0mraises\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m:\u001b[0m\u001b[0mexc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mValueError\u001b[0m\u001b[0;31m`\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mneither\u001b[0m \u001b[0mlegal\u001b[0m \u001b[0mnor\u001b[0m \u001b[0ma\u001b[0m \u001b[0mnull\u001b[0m \u001b[0mmove\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2844\u001b[0m \"\"\"\n\u001b[1;32m-> 2845\u001b[1;33m \u001b[0mmove\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparse_san\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msan\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2846\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpush\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmove\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2847\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mmove\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 22 | "\u001b[1;32m~\\Miniconda3\\lib\\site-packages\\chess\\__init__.py\u001b[0m in \u001b[0;36mparse_san\u001b[1;34m(self, san)\u001b[0m\n\u001b[0;32m 2793\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mMove\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2794\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2795\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"invalid san: {san!r}\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2796\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2797\u001b[0m \u001b[1;31m# Get target square.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 23 | "\u001b[1;31mValueError\u001b[0m: invalid san: '(Kxf7'" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "from pandas import read_csv\n", 29 | "import chess\n", 30 | "csv = read_csv('C:\\\\Users\\\\v_sim\\\\Desktop\\\\Files\\\\Data\\\\chess.csv')\n", 31 | "outcomes = list(csv['winner'])\n", 32 | "moves = list(csv['moves'])\n", 33 | "for i in range(len(moves)):\n", 34 | " moves[i] = moves[i].split()\n", 35 | "white_moves = 0\n", 36 | "black_moves = 0\n", 37 | "\n", 38 | "def initialize_dictionaries():\n", 39 | " white = {}\n", 40 | " black = {}\n", 41 | " all_moves = []\n", 42 | " for game in moves:\n", 43 | " for move in game:\n", 44 | " if not(move in all_moves):\n", 45 | " all_moves.append(move)\n", 46 | " for move in all_moves:\n", 47 | " white[move] = 1\n", 48 | " black[move] = 1\n", 49 | " return white,black\n", 50 | "\n", 51 | "def initialize_data(white,black):\n", 52 | " white_moves = 0\n", 53 | " black_moves = 0\n", 54 | " for outcome in outcomes:\n", 55 | " index = outcomes.index(outcome)\n", 56 | " if outcome == 'white':\n", 57 | " work_dict = white\n", 58 | " white_moves += len(moves[index])\n", 59 | " ip2 = 0\n", 60 | " elif outcome == 'black':\n", 61 | " work_dict = black\n", 62 | " black_moves += len(moves[index])\n", 63 | " ip2 = 1\n", 64 | " for move in moves[index]:\n", 65 | " if moves[index].index(move) % 2 == ip2:\n", 66 | " if move in work_dict:\n", 67 | " work_dict[move] += 1\n", 68 | " return white,black,white_moves,black_moves\n", 69 | "\n", 70 | "def calculate_probability(side,board,white,black,white_moves,black_moves):\n", 71 | " len_white = white_moves\n", 72 | " len_black = black_moves\n", 73 | " if side == True:\n", 74 | " color = 'white'\n", 75 | " else:\n", 76 | " color = 'black'\n", 77 | " legal_moves = str(board.legal_moves)[36:-2].replace(',','').split()\n", 78 | " probs = []\n", 79 | " for move in legal_moves:\n", 80 | " if not(move in white):\n", 81 | " len_white += 1\n", 82 | " white[move] = 1/len_white\n", 83 | " PA = outcomes.count('white')/len(outcomes)\n", 84 | " PB = 1\n", 85 | " PBgiveA = white[move]/outcomes.count('white')\n", 86 | " whitePAgiveB = (PBgiveA * PA) / PB\n", 87 | " if not(move in black):\n", 88 | " len_black += 1\n", 89 | " black[move] = 1/len_black\n", 90 | " PA = outcomes.count('black')/len(outcomes)\n", 91 | " PB = 1\n", 92 | " PBgiveA = black[move]/outcomes.count('black')\n", 93 | " blackPAgiveB = (PBgiveA * PA) / PB\n", 94 | " if color == 'white':\n", 95 | " probs.append(whitePAgiveB-blackPAgiveB)\n", 96 | " elif color == 'black':\n", 97 | " probs.append(blackPAgiveB-whitePAgiveB)\n", 98 | " final_move = legal_moves[legal_moves.index(max(legal_moves))]\n", 99 | " return white_moves,black_moves,final_move\n", 100 | " \n", 101 | "def to_probability(dictionary,length):\n", 102 | " for term in dictionary:\n", 103 | " dictionary[term] = dictionary[term]/length\n", 104 | " return dictionary\n", 105 | "\n", 106 | "\n", 107 | "white,black = initialize_dictionaries()\n", 108 | "white,black,white_moves,black_moves = initialize_data(white,black)\n", 109 | "white = to_probability(white,white_moves)\n", 110 | "black = to_probability(black,black_moves)\n", 111 | "board = chess.Board()\n", 112 | "for i in range(15):\n", 113 | " side = chess.WHITE\n", 114 | " white_moves,black_moves,move = calculate_probability(side,board,white,black,white_moves,black_moves)\n", 115 | " board.push_san(move)\n", 116 | "board" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 19, 122 | "metadata": { 123 | "ExecuteTime": { 124 | "end_time": "2020-08-24T13:24:07.521102Z", 125 | "start_time": "2020-08-24T13:24:06.827615Z" 126 | } 127 | }, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "MEAT\n", 134 | "MEAT\n" 135 | ] 136 | }, 137 | { 138 | "data": { 139 | "image/svg+xml": [ 140 | "" 141 | ], 142 | "text/plain": [ 143 | "Board('rnbq1bn1/pppppk1r/8/8/8/5P2/PPPP4/RNBQKBN1 b Q - 0 8')" 144 | ] 145 | }, 146 | "execution_count": 19, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "board = chess.Board()\n", 153 | "for i in range(15):\n", 154 | " side = chess.WHITE\n", 155 | " white_moves,black_moves,move = calculate_probability(side,board,white,black,white_moves,black_moves)\n", 156 | " board.push_san(move)\n", 157 | "board" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "metadata": { 164 | "ExecuteTime": { 165 | "end_time": "2020-08-23T18:23:32.034630Z", 166 | "start_time": "2020-08-23T18:23:31.910085Z" 167 | } 168 | }, 169 | "outputs": [], 170 | "source": [ 171 | "all_moves = np.array(np.array(moves)[0]).flatten()" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": { 178 | "ExecuteTime": { 179 | "end_time": "2020-08-23T18:25:22.417922Z", 180 | "start_time": "2020-08-23T18:25:13.002071Z" 181 | } 182 | }, 183 | "outputs": [], 184 | "source": [ 185 | "all_moves = []\n", 186 | "for game in moves:\n", 187 | " for move in game:\n", 188 | " if not(move in all_moves):\n", 189 | " all_moves.append(move)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": { 196 | "ExecuteTime": { 197 | "end_time": "2020-08-23T18:25:29.484313Z", 198 | "start_time": "2020-08-23T18:25:29.474775Z" 199 | } 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "len(all_moves)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [] 212 | } 213 | ], 214 | "metadata": { 215 | "hide_input": false, 216 | "kernelspec": { 217 | "display_name": "Python 3", 218 | "language": "python", 219 | "name": "python3" 220 | }, 221 | "language_info": { 222 | "codemirror_mode": { 223 | "name": "ipython", 224 | "version": 3 225 | }, 226 | "file_extension": ".py", 227 | "mimetype": "text/x-python", 228 | "name": "python", 229 | "nbconvert_exporter": "python", 230 | "pygments_lexer": "ipython3", 231 | "version": "3.8.3" 232 | }, 233 | "toc": { 234 | "base_numbering": 1, 235 | "nav_menu": {}, 236 | "number_sections": true, 237 | "sideBar": true, 238 | "skip_h1_title": false, 239 | "title_cell": "Table of Contents", 240 | "title_sidebar": "Contents", 241 | "toc_cell": false, 242 | "toc_position": {}, 243 | "toc_section_display": true, 244 | "toc_window_display": false 245 | }, 246 | "varInspector": { 247 | "cols": { 248 | "lenName": 16, 249 | "lenType": 16, 250 | "lenVar": 40 251 | }, 252 | "kernels_config": { 253 | "python": { 254 | "delete_cmd_postfix": "", 255 | "delete_cmd_prefix": "del ", 256 | "library": "var_list.py", 257 | "varRefreshCmd": "print(var_dic_list())" 258 | }, 259 | "r": { 260 | "delete_cmd_postfix": ") ", 261 | "delete_cmd_prefix": "rm(", 262 | "library": "var_list.r", 263 | "varRefreshCmd": "cat(var_dic_list()) " 264 | } 265 | }, 266 | "types_to_exclude": [ 267 | "module", 268 | "function", 269 | "builtin_function_or_method", 270 | "instance", 271 | "_Feature" 272 | ], 273 | "window_display": false 274 | } 275 | }, 276 | "nbformat": 4, 277 | "nbformat_minor": 4 278 | } 279 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chess-Deep-Learning --------------------------------------------------------------------------------