{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using gpu device 0: GeForce GTX 965M (CNMeM is disabled, CuDNN 4007)\n", "/usr/local/lib/python3.4/dist-packages/theano/tensor/signal/downsample.py:5: UserWarning: downsample module has been moved to the pool module.\n", " warnings.warn(\"downsample module has been moved to the pool module.\")\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Couldn't import dot_parser, loading of dot files will not be possible.\n" ] }, { "data": { "text/plain": [ "'float32'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import theano\n", "import theano.tensor as T\n", "%load_ext Cython\n", "import lasagne\n", "from lasagne.layers.dnn import Conv2DDNNLayer as conv2d\n", "from IPython.display import HTML, display\n", "from random import randint\n", "floatX = theano.config.floatX\n", "floatX" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cython\n", "# cython: infer_types=True, annotation_typing=True\n", "## cython: infer_types.verbose=True \n", "from IPython.display import HTML, display\n", "import numpy as np\n", "\n", "floatX = np.float32\n", "\n", "binary6 = np.array([ list(map(int,bin(2**6+i)[:2:-1])) for i in range(2**6)], dtype=floatX)\n", "height = np.array([-1]*65, dtype=np.int32)\n", "for __i in range(6):\n", " height[2**__i]=__i\n", "\n", "cdef class Connect4:\n", " cdef public:\n", " long turn\n", " long long[2] data\n", " cpdef long get_col_row(self, col: long, row: long):\n", " pos = col * 7 + row\n", " mask = (1) << pos \n", " if self.data[1] & mask:\n", " return 2\n", " return bool(self.data[0] & mask)\n", " \n", " cpdef long is_end(self):\n", " cdef long long mask\n", " bitboard = self.data[1-self.turn%2]\n", " bound = (1)<<48 # 49 = 7*(6+1) \n", " # horizontal: 0x204081 = 1|(1<<7)|(1<<14)|(1<<21)\n", " # vertical: 0xf = 1|(1<<1)|(1<<2)|(1<<3)\n", " # up-right: 0x1010101 = 1|(1<<8)|(1<<16)|(1<<24)\n", " # down-right: 0x208208 = (1<<3)|(1<<9)|(1<<15)|(1<<21)\n", " for mask in [0x204081, 0xf, 0x1010101, 0x208208]:\n", " while mask < bound:\n", " if mask & bitboard == mask:\n", " return True\n", " mask <<= 1\n", " return False\n", " \n", " def __init__(self, data=None, turn=0):\n", " if data is not None:\n", " self.data = data[:]\n", " else:\n", " self.data = [0, 0]\n", " self.turn = turn\n", " \n", " def _np_branch(self):\n", " c = self.turn%2 # who's turn\n", " base = np.zeros((3,7,6), dtype=floatX)\n", " base[2] = 1\n", " pos, moves = [], []\n", " red, yellow = self.data\n", " for i in range(7):\n", " mask = ((red|yellow) &0x3f) + 1\n", " p = height[mask]\n", " if p != -1:\n", " moves.append(i)\n", " pos.append(height[mask])\n", " base[c, i] = binary6[red&0x3f]\n", " base[1-c, i] = binary6[yellow&0x3f]\n", " red >>= 7\n", " yellow >>= 7\n", " boards = np.zeros( (len(moves), 3, 7, 6), dtype=floatX)\n", " for i in range(len(moves)):\n", " m, p = moves[i], pos[i]\n", " boards[i]=base\n", " boards[i, 0, m, p] = 1\n", " return moves, boards\n", " \n", " cpdef move(self, col:long, test=False):\n", " # assert 0<= col <7\n", " shift = col*7\n", " mask = (((self.data[0]|self.data[1]) >> shift) &0x3f) +1\n", " # print(\"mask=\", mask)\n", " if mask >= 64:\n", " return None\n", " if not test:\n", " self.data[self.turn%2] |= (mask<\"\n", " header = \"\"\"
\"\"\"\n", " header += \"\\n\".join(imgstr%('empty', pos(5-j), pos(i), 0) for i in range(7) for j in range(6))\n", " return header +\"\\n\".join(imgstr%('red_coin' if c==1 else 'yellow_coin', pos(5-j), pos(i), 2) for (i,j,c) in self.board_data()) +\"
\"\n", " \n", " def display(self):\n", " display(HTML(self._repr_html_()))\n", " \n", " def __repr__(self):\n", " row_str = lambda j: \"\".join(\".ox\"[self.get_col_row(i,j)] for i in range(7))\n", " return \"\\n\".join(row_str(j) for j in range(5,-1,-1))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "input_var = T.tensor4('inputs')\n", "target_var = T.vector('targets')\n", "l_in = lasagne.layers.InputLayer(shape=(None, 3, 7, 6), input_var=input_var)\n", "_ = conv2d(l_in, num_filters=400, filter_size = 5, pad='same')\n", "_ = conv2d(_, num_filters=200, filter_size = 3, pad='same')\n", "_ = conv2d(_, num_filters=100, filter_size = 3, pad='same')\n", "_ = conv2d(_, num_filters=50, filter_size = 3, pad='same')\n", "_ = conv2d(_, num_filters=25, filter_size = 3, pad='same')\n", "l_out = lasagne.layers.DenseLayer(_, num_units=1, nonlinearity=lasagne.nonlinearities.tanh, W=lasagne.init.GlorotUniform())\n", "\n", "prediction = lasagne.layers.get_output(l_out).flatten()\n", "V = theano.function([input_var], prediction)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# load\n", "import pickle\n", "values = pickle.load(open(\"c4-conv3.pkl\",\"rb\"))\n", "lasagne.layers.set_all_param_values(l_out, values)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0.99999511 0.30652273 1. 0.29646683 0.2878364 0.28512001]\n" ] } ], "source": [ "from IPython.display import clear_output\n", "game = Connect4()\n", "c = randint(0, 1)\n", "moves, boards = None, None\n", "while 1:\n", " clear_output()\n", " display(HTML(game._repr_html_()))\n", " if boards is not None:\n", " print(V(boards))\n", " if game.turn >= 42 or game.is_end():\n", " break\n", " if game.turn%2 == c:\n", " #game.move(MC_agent(game))\n", " #while game.move(randint(0,6)) is None: continue\n", " moves, boards = game._np_branch()\n", " game.move(moves[np.argmax(V(boards))])\n", " else:\n", " game.move(int(input(\"your turn\")))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }