diff --git a/aima/agents.py b/aima/agents.py index d159466cc..28af8d5f6 100644 --- a/aima/agents.py +++ b/aima/agents.py @@ -806,8 +806,7 @@ class TrivialVacuumEnvironment(Environment): def __init__(self): super().__init__() - self.status = {loc_A: random.choice(['Clean', 'Dirty']), - loc_B: random.choice(['Clean', 'Dirty'])} + self.status = {loc: random.choice(['Clean', 'Dirty']) for loc in locations} def thing_classes(self): """Return the Thing/Agent classes that may populate this vacuum world.""" diff --git a/aima/notebook_utils.py b/aima/notebook_utils.py index 7b881d29c..6da04b8a0 100644 --- a/aima/notebook_utils.py +++ b/aima/notebook_utils.py @@ -50,7 +50,7 @@ def psource(*functions): from pygments.lexers import PythonLexer from pygments import highlight - display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True)))) + display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(noclasses=True, style='monokai')))) except ImportError: print(source_code) diff --git a/notebooks/vacuum_world.ipynb b/notebooks/vacuum_world.ipynb index 8fa52dffc..fa3b2af2b 100644 --- a/notebooks/vacuum_world.ipynb +++ b/notebooks/vacuum_world.ipynb @@ -2,9 +2,18 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/iain/.venvs/AIMA/lib/python3.13/site-packages/nbformat/validator.py:434: MissingIDFieldWarning: Cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.\n", + " _validate(nbdict, ref, version, version_minor, relax_add_props)\n" + ] + } + ], "source": [ "%run bootstrap.ipynb" ] @@ -82,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -99,138 +108,49 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "\n", - "\n", - "\n", - "\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "

\n", + "
class TrivialVacuumEnvironment(Environment):\n",
+       "    \"\"\"This environment has two locations, A and B. Each can be Dirty\n",
+       "    or Clean. The agent perceives its location and the location's\n",
+       "    status. This serves as an example of how to implement a simple\n",
+       "    Environment.\"\"\"\n",
        "\n",
-       "
class TrivialVacuumEnvironment(Environment):\n",
+       "    def __init__(self):\n",
+       "        super().__init__()\n",
+       "        self.status = {loc_A: random.choice(['Clean', 'Dirty']),\n",
+       "                       loc_B: random.choice(['Clean', 'Dirty'])}\n",
        "\n",
-       "    """This environment has two locations, A and B. Each can be Dirty\n",
-       "    or Clean. The agent perceives its location and the location's\n",
-       "    status. This serves as an example of how to implement a simple\n",
-       "    Environment."""\n",
+       "    def thing_classes(self):\n",
+       "        \"\"\"Return the Thing/Agent classes that may populate this vacuum world.\"\"\"\n",
+       "        return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent, TableDrivenVacuumAgent, ModelBasedVacuumAgent]\n",
        "\n",
-       "    def __init__(self):\n",
-       "        super().__init__()\n",
-       "        self.status = {loc_A: random.choice(['Clean', 'Dirty']),\n",
-       "                       loc_B: random.choice(['Clean', 'Dirty'])}\n",
+       "    def percept(self, agent):\n",
+       "        \"\"\"Returns the agent's location, and the location status (Dirty/Clean).\"\"\"\n",
+       "        return agent.location, self.status[agent.location]\n",
        "\n",
-       "    def thing_classes(self):\n",
-       "        return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,\n",
-       "                TableDrivenVacuumAgent, ModelBasedVacuumAgent]\n",
+       "    def execute_action(self, agent, action):\n",
+       "        \"\"\"Change agent's location and/or location's status; track performance.\n",
+       "        Score 10 for each dirt cleaned; -1 for each move.\"\"\"\n",
+       "        if action == 'Right':\n",
+       "            agent.location = loc_B\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Left':\n",
+       "            agent.location = loc_A\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Suck':\n",
+       "            if self.status[agent.location] == 'Dirty':\n",
+       "                agent.performance += 10\n",
+       "            self.status[agent.location] = 'Clean'\n",
        "\n",
-       "    def percept(self, agent):\n",
-       "        """Returns the agent's location, and the location status (Dirty/Clean)."""\n",
-       "        return (agent.location, self.status[agent.location])\n",
-       "\n",
-       "    def execute_action(self, agent, action):\n",
-       "        """Change agent's location and/or location's status; track performance.\n",
-       "        Score 10 for each dirt cleaned; -1 for each move."""\n",
-       "        if action == 'Right':\n",
-       "            agent.location = loc_B\n",
-       "            agent.performance -= 1\n",
-       "        elif action == 'Left':\n",
-       "            agent.location = loc_A\n",
-       "            agent.performance -= 1\n",
-       "        elif action == 'Suck':\n",
-       "            if self.status[agent.location] == 'Dirty':\n",
-       "                agent.performance += 10\n",
-       "            self.status[agent.location] = 'Clean'\n",
-       "\n",
-       "    def default_location(self, thing):\n",
-       "        """Agents start in either location at random."""\n",
-       "        return random.choice([loc_A, loc_B])\n",
-       "
\n", - "\n", - "\n" + " def default_location(self, thing):\n", + " \"\"\"Agents start in either location at random.\"\"\"\n", + " return random.choice([loc_A, loc_B])\n", + "
\n" ], "text/plain": [ "" @@ -246,24 +166,25 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Dirty'}.\n" ] } ], "source": [ - "# These are the two locations for the two-state environment\n", - "loc_A, loc_B = (0, 0), (1, 0)\n", + "# These are the four locations for the four-state environment\n", + "loc_A, loc_B = (0, 0), (0, 1)\n", + "loc_C, loc_D = (1, 0), (1, 1)\n", + "locations = [loc_A, loc_B, loc_C, loc_D]\n", "\n", - "# Initialize the two-state environment\n", + "# Initialize the four-state environment\n", "trivial_vacuum_env = TrivialVacuumEnvironment()\n", - "\n", "# Check the initial state of the environment\n", "print(\"State of the Environment: {}.\".format(trivial_vacuum_env.status))" ] @@ -277,12 +198,12 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Create the random agent\n", - "random_agent = Agent(program=RandomAgentProgram(['Right', 'Left', 'Suck', 'NoOp']))" + "random_agent = Agent(program=RandomAgentProgram(['Right', 'Left', 'Up', 'Down', 'Suck', 'NoOp']))" ] }, { @@ -294,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -321,14 +242,14 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n", "RandomVacuumAgent is located at (1, 0).\n" ] } @@ -355,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -381,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -398,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -407,14 +328,14 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "TableDrivenVacuumAgent is located at (0, 0).\n" + "TableDrivenVacuumAgent is located at (1, 0).\n" ] } ], @@ -427,14 +348,14 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n", "TableDrivenVacuumAgent is located at (1, 0).\n" ] } @@ -471,7 +392,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -488,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -522,14 +443,14 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "SimpleReflexVacuumAgent is located at (0, 0).\n" ] } ], @@ -541,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -549,7 +470,7 @@ "output_type": "stream", "text": [ "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "SimpleReflexVacuumAgent is located at (0, 0).\n" ] } ], @@ -584,7 +505,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -601,20 +522,21 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "ModelBasedVacuumAgent is located at (0, 0).\n" + "ModelBasedVacuumAgent is located at (1, 0).\n" ] } ], "source": [ "# TODO: Implement this function for the two-dimensional environment\n", "def update_state(state, action, percept, model):\n", + " \n", " pass\n", "\n", "# Create a model-based reflex agent\n", @@ -628,7 +550,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -636,7 +558,7 @@ "output_type": "stream", "text": [ "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "ModelBasedVacuumAgent is located at (1, 0).\n" + "ModelBasedVacuumAgent is located at (0, 0).\n" ] } ], @@ -688,7 +610,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "AIMA (3.13.5)", "language": "python", "name": "python3" }, @@ -702,7 +624,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.13.5" } }, "nbformat": 4,