{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Install kaggle-environments" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: kaggle-environments in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (1.7.6)\n", "Requirement already satisfied: jsonschema>=3.0.1 in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from kaggle-environments) (3.2.0)\n", "Requirement already satisfied: six>=1.11.0 in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from jsonschema>=3.0.1->kaggle-environments) (1.15.0)\n", "Requirement already satisfied: setuptools in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from jsonschema>=3.0.1->kaggle-environments) (51.0.0.post20201207)\n", "Requirement already satisfied: attrs>=17.4.0 in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from jsonschema>=3.0.1->kaggle-environments) (20.3.0)\n", "Requirement already satisfied: pyrsistent>=0.14.0 in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from jsonschema>=3.0.1->kaggle-environments) (0.17.3)\n", "Collecting gfootball\n", " Downloading gfootball-2.8.tar.gz (8.7 MB)\n", "\u001b[K |████████████████████████████████| 8.7 MB 1.5 MB/s eta 0:00:01 |████████████████████████▏ | 6.6 MB 1.9 MB/s eta 0:00:02\n", "\u001b[?25hRequirement already satisfied: scipy in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from gfootball) (1.5.4)\n", "Requirement already satisfied: gym>=0.11.0 in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from gfootball) (0.18.0)\n", "Requirement already satisfied: absl-py in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from gfootball) (0.11.0)\n", "Requirement already satisfied: wheel in /home/josh/anaconda3/envs/muzero-general/lib/python3.8/site-packages (from gfootball) (0.36.2)\n", "Collecting pygame==1.9.6\n", " Downloading pygame-1.9.6.tar.gz (3.2 MB)\n", "\u001b[K |████████████████████████████████| 3.2 MB 852 kB/s eta 0:00:01\n", "\u001b[31m ERROR: Command errored out with exit status 1:\n", " command: /home/josh/anaconda3/envs/muzero-general/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '\"'\"'/tmp/pip-install-z_nwsjyx/pygame_d54f0305be8f4d84bc572907b4852505/setup.py'\"'\"'; __file__='\"'\"'/tmp/pip-install-z_nwsjyx/pygame_d54f0305be8f4d84bc572907b4852505/setup.py'\"'\"';f=getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__);code=f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' egg_info --egg-base /tmp/pip-pip-egg-info-2d2vpvhf\n", " cwd: /tmp/pip-install-z_nwsjyx/pygame_d54f0305be8f4d84bc572907b4852505/\n", " Complete output (25 lines):\n", " \n", " \n", " WARNING, No \"Setup\" File Exists, Running \"buildconfig/config.py\"\n", " Using UNIX configuration...\n", " \n", " /bin/sh: 1: sdl-config: not found\n", " /bin/sh: 1: sdl-config: not found\n", " /bin/sh: 1: sdl-config: not found\n", " Package freetype2 was not found in the pkg-config search path.\n", " Perhaps you should add the directory containing `freetype2.pc'\n", " to the PKG_CONFIG_PATH environment variable\n", " No package 'freetype2' found\n", " Package freetype2 was not found in the pkg-config search path.\n", " Perhaps you should add the directory containing `freetype2.pc'\n", " to the PKG_CONFIG_PATH environment variable\n", " No package 'freetype2' found\n", " Package freetype2 was not found in the pkg-config search path.\n", " Perhaps you should add the directory containing `freetype2.pc'\n", " to the PKG_CONFIG_PATH environment variable\n", " No package 'freetype2' found\n", " \n", " Hunting dependencies...\n", " WARNING: \"sdl-config\" failed!\n", " WARNING: \"pkg-config freetype2\" failed!\n", " Unable to run \"sdl-config\". Please make sure a development version of SDL is installed.\n", " ----------------------------------------\u001b[0m\n", "\u001b[31mERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "# 1. Enable Internet in the Kernel (Settings side pane)\n", "\n", "# 2. Curl cache may need purged if v0.1.4 cannot be found (uncomment if needed). \n", "# !curl -X PURGE https://pypi.org/simple/kaggle-environments\n", "\n", "# ConnectX environment was defined in v0.1.4\n", "#!pip install 'kaggle-environments>=0.1.4'\n", "!pip install 'kaggle-environments'\n", "#!pip install 'gfootball'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create ConnectX Environment" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0", "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a" }, "outputs": [], "source": [ "from kaggle_environments import evaluate, make\n", "\n", "env = make(\"connectx\", debug=True)\n", "env.render()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading environment football failed: No module named 'gfootball'\n" ] } ], "source": [ "import kaggle_environments\n", "\n", "env = kaggle_environments.make(\"connectx\", debug=True)\n", "env.render()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from kaggle_environments import make\n", "\n", "# Setup a tictactoe environment.\n", "env = make(\"tictactoe\")\n", "\n", "# Basic agent which marks the first available cell.\n", "def my_agent(obs):\n", " return [c for c in range(len(obs.board)) if obs.board[c] == 0][0]\n", "\n", "# Run the basic agent against a default agent which chooses a \"random\" move.\n", "env.run([my_agent, \"random\"])\n", "\n", "# Render an html ipython replay of the tictactoe game.\n", "env.render(mode=\"ipython\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import kaggle_environments\n", "\n", "env = kaggle_environments.make(\"connectx\")\n", "\n", "# Basic agent which marks the first available cell.\n", "def my_agent(obs):\n", " return [c for c in range(len(obs.board)) if obs.board[c] == 0][0]\n", "\n", "# Run the basic agent against a default agent which chooses a \"random\" move.\n", "env.run([my_agent, \"random\"])\n", "\n", "\n", "env.render(mode=\"ipython\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import kaggle_environments\n", "\n", "env = kaggle_environments.make(\"connectx\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create an Agent\n", "\n", "To create the submission, an agent function should be fully encapsulated (no external dependencies). \n", "\n", "When your agent is being evaluated against others, it will not have access to the Kaggle docker image. Only the following can be imported: Python Standard Library Modules, gym, numpy, scipy (more may be added later). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This agent random chooses a non-empty column.\n", "def my_agent(observation, configuration):\n", " from random import choice\n", " return choice([c for c in range(configuration.columns) if observation.board[c] == 0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def random_agent(observation, configuration):\n", " from random import choice\n", " return choice([c for c in range(configuration.columns) if observation.board[c] == 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test your Agent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "env.reset()\n", "# Play as the first agent against default \"random\" agent.\n", "env.run([my_agent, \"random\"])\n", "env.render(mode=\"ipython\", width=500, height=450)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Debug/Train your Agent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Play as first position against random agent.\n", "trainer = env.train([None, \"random\"])\n", "\n", "observation = trainer.reset()\n", "\n", "while not env.done:\n", " my_action = my_agent(observation, env.configuration)\n", " print(\"My Action\", my_action)\n", " observation, reward, done, info = trainer.step(my_action)\n", " # env.render(mode=\"ipython\", width=100, height=90, header=False, controls=False)\n", "env.render()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Evaluate your Agent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def mean_reward(rewards):\n", " return sum(r[0] for r in rewards) / sum(r[0] + r[1] for r in rewards)\n", "\n", "# Run multiple episodes to estimate it's performance.\n", "print(\"My Agent vs Random Agent:\", mean_reward(evaluate(\"connectx\", [my_agent, \"random\"], num_episodes=10)))\n", "print(\"My Agent vs Negamax Agent:\", mean_reward(evaluate(\"connectx\", [my_agent, \"negamax\"], num_episodes=10)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Write Submission File\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import inspect\n", "import os\n", "\n", "def write_agent_to_file(function, file):\n", " with open(file, \"a\" if os.path.exists(file) else \"w\") as f:\n", " f.write(inspect.getsource(function))\n", " print(function, \"written to\", file)\n", "\n", "write_agent_to_file(my_agent, \"submission.py\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Submit to Competition\n", "\n", "1. Commit this kernel.\n", "2. View the commited version.\n", "3. Go to \"Data\" section and find submission.py file.\n", "4. Click \"Submit to Competition\"\n", "5. Go to [My Submissions](https://kaggle.com/c/connectx/submissions) to view your score and episodes being played." ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }