diff --git a/kaggle_environments/envs/connectx/connectx.ipynb b/kaggle_environments/envs/connectx/connectx.ipynb index 77f6387b..a3188196 100644 --- a/kaggle_environments/envs/connectx/connectx.ipynb +++ b/kaggle_environments/envs/connectx/connectx.ipynb @@ -16,7 +16,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "connectx 1.0.0\n", + "connectx 1.0.1\n", "Default Agents: random negamax\n" ] } @@ -37,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -75,11 +75,6 @@ " font-family: sans-serif;\n", " }\n", " canvas {\n", - " position: absolute;\n", - " top: 0;\n", - " left: 0;\n", - " width: 100%;\n", - " height: 100%;\n", " /* image-rendering: -moz-crisp-edges;\n", " image-rendering: -webkit-crisp-edges;\n", " image-rendering: pixelated;\n", @@ -98,7 +93,6 @@ " };\n", " \n", " \n", - " \n", " \n", " \n", " \n", + " \n", + " \n", + "\n", + "\" width=\"600\" height=\"500\" frameborder=\"0\"> " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "def agent(observation, configuration):\n", + " board = observation.board\n", + " columns = configuration.columns\n", + " return [c for c in range(columns) if board[c] == 0][0]\n", + "\n", + "env = make(\"connectx\", debug=True)\n", + "# play agent above vs default random agent.\n", + "env.run([agent, \"random\"])\n", + "env.render(mode=\"ipython\", width=600, height=500, header=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Specification" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Configuration: {\n", + " \"actTimeout\": {\n", + " \"default\": 2,\n", + " \"description\": \"Maximum runtime (seconds) to obtain an action from an agent.\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " },\n", + " \"agentExec\": {\n", + " \"default\": \"PROCESS\",\n", + " \"description\": \"How the agent is executed alongside the running envionment.\",\n", + " \"enum\": [\n", + " \"LOCAL\",\n", + " \"PROCESS\"\n", + " ],\n", + " \"type\": \"string\"\n", + " },\n", + " \"agentTimeout\": {\n", + " \"default\": 10,\n", + " \"description\": \"Maximum runtime (seconds) to initialize an agent.\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " },\n", + " \"columns\": {\n", + " \"default\": 7,\n", + " \"description\": \"The number of columns on the board\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " },\n", + " \"episodeSteps\": {\n", + " \"default\": 1000,\n", + " \"description\": \"Maximum number of steps in the episode.\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " },\n", + " \"inarow\": {\n", + " \"default\": 4,\n", + " \"description\": \"The number of checkers in a row required to win.\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " },\n", + " \"rows\": {\n", + " \"default\": 6,\n", + " \"description\": \"The number of rows on the board\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " },\n", + " \"runTimeout\": {\n", + " \"default\": 600,\n", + " \"description\": \"Maximum runtime (seconds) of an episode (not necessarily DONE).\",\n", + " \"minimum\": 1,\n", + " \"type\": \"integer\"\n", + " }\n", + "}\n", + "Observation: {\n", + " \"board\": {\n", + " \"default\": [],\n", + " \"description\": \"Serialized grid (rows x columns). 0 = Empty, 1 = P1, 2 = P2\",\n", + " \"shared\": true,\n", + " \"type\": \"array\"\n", + " },\n", + " \"mark\": {\n", + " \"defaults\": [\n", + " 1,\n", + " 2\n", + " ],\n", + " \"description\": \"Which checkers are the agents.\",\n", + " \"enum\": [\n", + " 1,\n", + " 2\n", + " ]\n", + " }\n", + "}\n", + "Action: {\n", + " \"default\": 0,\n", + " \"description\": \"Column to drop a checker onto the board.\",\n", + " \"minimum\": 0,\n", + " \"type\": \"integer\"\n", + "}\n" + ] + } + ], + "source": [ + "import json\n", + "print(\"Configuration:\", json.dumps(env.specification.configuration, indent=4, sort_keys=True))\n", + "print(\"Observation:\", json.dumps(env.specification.observation, indent=4, sort_keys=True))\n", + "print(\"Action:\", json.dumps(env.specification.action, indent=4, sort_keys=True))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Manual Play" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + " " + "\" width=\"800\" height=\"600\" frameborder=\"0\"> " ], "text/plain": [ "" @@ -1814,94 +3074,9 @@ } ], "source": [ - "def agent(observation, configuration):\n", - " board = observation.board\n", - " columns = configuration.columns\n", - " return [c for c in range(columns) if board[c] == 0][0]\n", - "\n", - "env = make(\"connectx\", debug=True)\n", - "# play agent above vs default random agent.\n", - "env.run([agent, \"random\"])\n", - "env.render(mode=\"ipython\", width=600, height=500, header=False)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Specification" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Configuration: {\n", - " \"columns\": {\n", - " \"default\": 7,\n", - " \"description\": \"The number of columns on the board\",\n", - " \"minimum\": 1,\n", - " \"type\": \"integer\"\n", - " },\n", - " \"inarow\": {\n", - " \"default\": 4,\n", - " \"description\": \"The number of checkers in a row required to win.\",\n", - " \"minimum\": 1,\n", - " \"type\": \"integer\"\n", - " },\n", - " \"rows\": {\n", - " \"default\": 6,\n", - " \"description\": \"The number of rows on the board\",\n", - " \"minimum\": 1,\n", - " \"type\": \"integer\"\n", - " },\n", - " \"steps\": {\n", - " \"default\": 1000,\n", - " \"description\": \"Maximum number of steps the environment can run.\",\n", - " \"minimum\": 1,\n", - " \"type\": \"integer\"\n", - " },\n", - " \"timeout\": {\n", - " \"default\": 2,\n", - " \"description\": \"Seconds an agent can run before timing out.\",\n", - " \"minimum\": 1,\n", - " \"type\": \"integer\"\n", - " }\n", - "}\n", - "Observation: {\n", - " \"board\": {\n", - " \"default\": [],\n", - " \"description\": \"Serialized grid (rows x columns). 0 = Empty, 1 = P1, 2 = P2\",\n", - " \"type\": \"array\"\n", - " },\n", - " \"mark\": {\n", - " \"default\": 0,\n", - " \"description\": \"Which checkers are the agents.\",\n", - " \"enum\": [\n", - " 1,\n", - " 2\n", - " ]\n", - " }\n", - "}\n", - "Action: {\n", - " \"default\": 0,\n", - " \"description\": \"Column to drop a checker onto the board.\",\n", - " \"minimum\": 0,\n", - " \"type\": \"integer\"\n", - "}\n" - ] - } - ], - "source": [ - "import json\n", - "print(\"Configuration:\", json.dumps(env.specification.configuration, indent=4, sort_keys=True))\n", - "print(\"Observation:\", json.dumps(env.specification.observation, indent=4, sort_keys=True))\n", - "print(\"Action:\", json.dumps(env.specification.action, indent=4, sort_keys=True))" + "env = make(\"battlegeese\", configuration={\"agentExec\": \"LOCAL\"})\n", + "# Play against 3 default shortest agents.\n", + "env.play([None, \"negamax\"], width=800, height=600)" ] }, { @@ -1913,25 +3088,153 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, + "execution_count": 5, + "metadata": { + "scrolled": false + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+---+---+---+---+---+---+---+\n", - "| 2 | 1 | 1 | 0 | 0 | 0 | 0 |\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "+---+---+---+---+---+---+---+\n", - "| 1 | 1 | 2 | 0 | 0 | 0 | 0 |\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "+---+---+---+---+---+---+---+\n", - "| 2 | 2 | 1 | 0 | 0 | 0 | 0 |\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "+---+---+---+---+---+---+---+\n", - "| 1 | 1 | 2 | 1 | 0 | 0 | 0 |\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "+---+---+---+---+---+---+---+\n", - "| 2 | 2 | 1 | 2 | 0 | 0 | 0 |\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "+---+---+---+---+---+---+---+\n", - "| 1 | 1 | 2 | 2 | 2 | 0 | 0 |\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 2 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 2 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 2 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 2 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 2 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 2 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 2 | 2 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 1 | 0 | 2 | 0 | 0 | 2 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", + "+---+---+---+---+---+---+---+\n", + "| 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "+---+---+---+---+---+---+---+\n", "\n" ] @@ -1939,38 +3242,19 @@ ], "source": [ "from kaggle_environments import make\n", - "import gym\n", "\n", - "class ConnectX(gym.Env):\n", - "\n", - " def __init__(self):\n", - " self.env = make(\"connectx\", debug=True)\n", - " self.trainer = self.env.train([None, \"random\"])\n", - " \n", - " # Define required gym fields (examples):\n", - " config = self.env.configuration\n", - " self.action_space = gym.spaces.Discrete(config.columns)\n", - " self.observation_space = gym.spaces.Discrete(config.columns * config.rows)\n", + "env = make(\"connectx\", debug=True)\n", "\n", - " def step(self, action):\n", - " return self.trainer.step(action)\n", - " \n", - " def reset(self):\n", - " return self.trainer.reset()\n", - " \n", - " def render(self, **kwargs):\n", - " return self.env.render(**kwargs)\n", - " \n", - " \n", - "env = ConnectX()\n", + "# Training agent in first position (player 1) against the default random agent.\n", + "trainer = env.train([None, \"random\"])\n", "\n", - "done = False\n", - "obs = env.reset()\n", - "while not done:\n", - " # Choose first available empty column as the action.\n", - " action = [i for i in range(len(obs.board)) if obs.board[i] == 0][0]\n", - " obs, reward, done, info = env.step(action)\n", - "env.render()" + "obs = trainer.reset()\n", + "for _ in range(10):\n", + " env.render()\n", + " action = 0 # Action for the agent being trained.\n", + " obs, reward, done, info = trainer.step(action)\n", + " if done:\n", + " obs = trainer.reset()" ] } ],