From 16accddcbd3f7c163c15ef50df8bd3f2f1ba675d Mon Sep 17 00:00:00 2001 From: Iain Ross Date: Wed, 2 Sep 2026 15:33:11 -0600 Subject: [PATCH 1/4] added basic locations for further use --- aima/notebook_utils.py | 2 +- notebooks/vacuum_world.ipynb | 234 ++++++++++++----------------------- 2 files changed, 79 insertions(+), 157 deletions(-) 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, From 3d66524b8979feb6af1391ed3ec9056cc2f583d5 Mon Sep 17 00:00:00 2001 From: Iain Ross Date: Wed, 2 Sep 2026 15:36:17 -0600 Subject: [PATCH 2/4] init now uses all four locations --- aima/agents.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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.""" From 0e9c1afc2c557145ba099ff95da8ef6f11f5a347 Mon Sep 17 00:00:00 2001 From: Fairestmars <131192190+Fairestmars@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:16:19 -0600 Subject: [PATCH 3/4] update env, execute agent, some of the table agent, and the simple reflex agent --- aima/agents.py | 21 ++- aima/notebook_utils.py | 10 +- notebooks/agents.ipynb | 293 +++++++++++++++++++++++++++++++---- notebooks/vacuum_world.ipynb | 238 ++++++++++------------------ 4 files changed, 367 insertions(+), 195 deletions(-) diff --git a/aima/agents.py b/aima/agents.py index d159466cc..e3ac190fe 100644 --- a/aima/agents.py +++ b/aima/agents.py @@ -191,7 +191,7 @@ def rule_match(state, rules): # ______________________________________________________________________________ -loc_A, loc_B = (0, 0), (1, 0) # The two locations for the Vacuum world +loc_A, loc_B, loc_C, loc_D = (0, 0), (1, 0), (0, 1), (1, 1) # The four locations for the Vacuum world def RandomVacuumAgent(): @@ -203,7 +203,7 @@ def RandomVacuumAgent(): >>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'} True """ - return Agent(RandomAgentProgram(['Right', 'Left', 'Suck', 'NoOp'])) + return Agent(RandomAgentProgram(['Right', 'Left','Up','Down', 'Suck', 'NoOp'])) def TableDrivenVacuumAgent(): @@ -807,7 +807,9 @@ class TrivialVacuumEnvironment(Environment): def __init__(self): super().__init__() self.status = {loc_A: random.choice(['Clean', 'Dirty']), - loc_B: random.choice(['Clean', 'Dirty'])} + loc_B: random.choice(['Clean', 'Dirty']), + loc_C: random.choice(['Clean', 'Dirty']), + loc_D: random.choice(['Clean', 'Dirty'])} def thing_classes(self): """Return the Thing/Agent classes that may populate this vacuum world.""" @@ -820,11 +822,18 @@ def percept(self, agent): def execute_action(self, agent, action): """Change agent's location and/or location's status; track performance. Score 10 for each dirt cleaned; -1 for each move.""" + a, b = agent.location if action == 'Right': - agent.location = loc_B + agent.location = (a + 1, b) agent.performance -= 1 elif action == 'Left': - agent.location = loc_A + agent.location = (a - 1, b) + agent.performance -= 1 + elif action == 'Up': + agent.location = (a, b + 1) + agent.performance -= 1 + elif action == 'Down': + agent.location = (a, b - 1) agent.performance -= 1 elif action == 'Suck': if self.status[agent.location] == 'Dirty': @@ -833,7 +842,7 @@ def execute_action(self, agent, action): def default_location(self, thing): """Agents start in either location at random.""" - return random.choice([loc_A, loc_B]) + return random.choice([loc_A, loc_B, loc_C, loc_D]) # ______________________________________________________________________________ diff --git a/aima/notebook_utils.py b/aima/notebook_utils.py index 7b881d29c..e98b79391 100644 --- a/aima/notebook_utils.py +++ b/aima/notebook_utils.py @@ -50,7 +50,15 @@ def psource(*functions): from pygments.lexers import PythonLexer from pygments import highlight - display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True)))) + # Render an HTML fragment with inline token colors. ``full=True`` emits + # a complete document whose global ``body`` CSS leaks into VS Code's + # shared notebook webview and can make every Markdown cell unreadable. + highlighted = highlight(source_code, PythonLexer(), + HtmlFormatter(noclasses=True, style='dracula', + nobackground=True)) + # Give unstyled tokens a readable foreground instead of inheriting + # palette instead of inheriting a color from the notebook theme. + display(HTML('
{}
'.format(highlighted))) except ImportError: print(source_code) diff --git a/notebooks/agents.ipynb b/notebooks/agents.ipynb index 6cff727ff..f851618ed 100644 --- a/notebooks/agents.ipynb +++ b/notebooks/agents.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -52,9 +52,53 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
class Agent(Thing):\n",
+       "    """An Agent is a subclass of Thing with one required instance attribute \n",
+       "    (aka slot), .program, which should hold a function that takes one argument,\n",
+       "    the percept, and returns an action. (What counts as a percept or action \n",
+       "    will depend on the specific environment in which the agent exists.)\n",
+       "    Note that 'program' is a slot, not a method. If it were a method, then the\n",
+       "    program could 'cheat' and look at aspects of the agent. It's not supposed\n",
+       "    to do that: the program can only look at the percepts. An agent program\n",
+       "    that needs a model of the world (and of the agent itself) will have to\n",
+       "    build and maintain its own model. There is an optional slot, .performance,\n",
+       "    which is a number giving the performance measure of the agent in its\n",
+       "    environment."""\n",
+       "\n",
+       "    def __init__(self, program=None):\n",
+       "        self.alive = True\n",
+       "        self.bump = False\n",
+       "        self.holding = []\n",
+       "        self.performance = 0\n",
+       "        if program is None or not isinstance(program, collections.abc.Callable):\n",
+       "            print("Can't find a valid program for {}, falling back to default.".format(self.__class__.__name__))\n",
+       "\n",
+       "            def program(percept):\n",
+       "                return eval(input('Percept={}; action? '.format(percept)))\n",
+       "\n",
+       "        self.program = program\n",
+       "\n",
+       "    def can_grab(self, thing):\n",
+       "        """Return True if this agent can grab this thing.\n",
+       "        Override for appropriate subclasses of Agent and Thing."""\n",
+       "        return False\n",
+       "
\n", + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(Agent)" ] @@ -84,9 +128,126 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
class Environment:\n",
+       "    """Abstract class representing an Environment. 'Real' Environment classes\n",
+       "    inherit from this. Your Environment will typically need to implement::\n",
+       "\n",
+       "        percept:         Define the percept that an agent sees.\n",
+       "        execute_action:  Define the effects of executing an action;\n",
+       "                         also update the agent.performance slot.\n",
+       "\n",
+       "    The environment keeps a list of .things and .agents (which is a subset\n",
+       "    of .things). Each agent has a .performance slot, initialized to 0.\n",
+       "    Each thing has a .location slot, even though some environments may not\n",
+       "    need this."""\n",
+       "\n",
+       "    def __init__(self):\n",
+       "        self.things = []\n",
+       "        self.agents = []\n",
+       "\n",
+       "    def thing_classes(self):\n",
+       "        """Return the list of Thing subclasses that may appear in this environment."""\n",
+       "        return []  # List of classes that can go into environment\n",
+       "\n",
+       "    def percept(self, agent):\n",
+       "        """Return the percept that the agent sees at this point. (Implement this.)"""\n",
+       "        raise NotImplementedError\n",
+       "\n",
+       "    def execute_action(self, agent, action):\n",
+       "        """Change the world to reflect this action. (Implement this.)"""\n",
+       "        raise NotImplementedError\n",
+       "\n",
+       "    def default_location(self, thing):\n",
+       "        """Default location to place a new thing with unspecified location."""\n",
+       "        return None\n",
+       "\n",
+       "    def exogenous_change(self):\n",
+       "        """If there is spontaneous change in the world, override this."""\n",
+       "        pass\n",
+       "\n",
+       "    def is_done(self):\n",
+       "        """By default, we're done when we can't find a live agent."""\n",
+       "        return not any(agent.is_alive() for agent in self.agents)\n",
+       "\n",
+       "    def step(self):\n",
+       "        """Run the environment for one time step. If the\n",
+       "        actions and exogenous changes are independent, this method will\n",
+       "        do. If there are interactions between them, you'll need to\n",
+       "        override this method."""\n",
+       "        if not self.is_done():\n",
+       "            actions = []\n",
+       "            for agent in self.agents:\n",
+       "                if agent.alive:\n",
+       "                    actions.append(agent.program(self.percept(agent)))\n",
+       "                else:\n",
+       "                    actions.append("")\n",
+       "            for (agent, action) in zip(self.agents, actions):\n",
+       "                self.execute_action(agent, action)\n",
+       "            self.exogenous_change()\n",
+       "\n",
+       "    def run(self, steps=1000):\n",
+       "        """Run the Environment for given number of time steps."""\n",
+       "        for step in range(steps):\n",
+       "            if self.is_done():\n",
+       "                return\n",
+       "            self.step()\n",
+       "\n",
+       "    def list_things_at(self, location, tclass=Thing):\n",
+       "        """Return all things exactly at a given location."""\n",
+       "        if isinstance(location, numbers.Number):\n",
+       "            return [thing for thing in self.things\n",
+       "                    if thing.location == location and isinstance(thing, tclass)]\n",
+       "        return [thing for thing in self.things\n",
+       "                if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)]\n",
+       "\n",
+       "    def some_things_at(self, location, tclass=Thing):\n",
+       "        """Return true if at least one of the things at location\n",
+       "        is an instance of class tclass (or a subclass)."""\n",
+       "        return self.list_things_at(location, tclass) != []\n",
+       "\n",
+       "    def add_thing(self, thing, location=None):\n",
+       "        """Add a thing to the environment, setting its location. For\n",
+       "        convenience, if thing is an agent program we make a new agent\n",
+       "        for it. (Shouldn't need to override this.)"""\n",
+       "        if not isinstance(thing, Thing):\n",
+       "            thing = Agent(thing)\n",
+       "        if thing in self.things:\n",
+       "            print("Can't add the same thing twice")\n",
+       "        else:\n",
+       "            thing.location = location if location is not None else self.default_location(thing)\n",
+       "            self.things.append(thing)\n",
+       "            if isinstance(thing, Agent):\n",
+       "                thing.performance = 0\n",
+       "                self.agents.append(thing)\n",
+       "\n",
+       "    def delete_thing(self, thing):\n",
+       "        """Remove a thing from the environment."""\n",
+       "        try:\n",
+       "            self.things.remove(thing)\n",
+       "        except ValueError as e:\n",
+       "            print(e)\n",
+       "            print("  in Environment delete_thing")\n",
+       "            print("  Thing to be removed: {} at {}".format(thing, thing.location))\n",
+       "            print("  from list: {}".format([(thing, thing.location) for thing in self.things]))\n",
+       "        if thing in self.agents:\n",
+       "            self.agents.remove(thing)\n",
+       "
\n", + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(Environment)" ] @@ -123,9 +284,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Can't find a valid program for BlindDog, falling back to default.\n" + ] + } + ], "source": [ "class BlindDog(Agent):\n", " def eat(self, thing):\n", @@ -146,9 +315,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "print(dog.alive)" ] @@ -172,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -226,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -273,7 +450,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -296,9 +473,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "BlindDog decided to move down at location: 1\n", + "BlindDog decided to move down at location: 2\n", + "BlindDog decided to move down at location: 3\n", + "BlindDog decided to move down at location: 4\n", + "BlindDog ate Food at location: 5\n" + ] + } + ], "source": [ "park = Park()\n", "dog = BlindDog(program)\n", @@ -322,9 +511,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "BlindDog decided to move down at location: 5\n", + "BlindDog decided to move down at location: 6\n", + "BlindDog drank Water at location: 7\n" + ] + } + ], "source": [ "park.run(5)" ] @@ -366,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -432,9 +631,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "park = Park2D(5,20, color={'BlindDog': (200,0,0), 'Water': (0, 200, 200), 'Food': (230, 115, 40)}) # park width is set to 5, and height to 20\n", "dog = BlindDog(program)\n", @@ -491,7 +703,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -562,7 +774,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -627,9 +839,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "park = Park2D(5,5, color={'EnergeticBlindDog': (200,0,0), 'Water': (0, 200, 200), 'Food': (230, 115, 40)})\n", "dog = EnergeticBlindDog(program)\n", @@ -663,7 +888,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -736,7 +961,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.11.5" } }, "nbformat": 4, diff --git a/notebooks/vacuum_world.ipynb b/notebooks/vacuum_world.ipynb index 8fa52dffc..e04257a91 100644 --- a/notebooks/vacuum_world.ipynb +++ b/notebooks/vacuum_world.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -99,138 +99,59 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "\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",
-       "\n",
-       "\n",
-       "  \n",
-       "  \n",
-       "  \n",
-       "\n",
-       "\n",
-       "

\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", - "
class TrivialVacuumEnvironment(Environment):\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",
-       "    """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 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",
+       "        a, b = agent.location\n",
+       "        if action == 'Right':\n",
+       "            agent.location = (a + 1, b)\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Left':\n",
+       "            agent.location = (a - 1, b)\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Up':\n",
+       "            agent.location = (a, b + 1)\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Down':\n",
+       "            agent.location = (a, b - 1)\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 __init__(self):\n",
-       "        super().__init__()\n",
-       "        self.status = {loc_A: random.choice(['Clean', 'Dirty']),\n",
-       "                       loc_B: random.choice(['Clean', 'Dirty'])}\n",
-       "\n",
-       "    def thing_classes(self):\n",
-       "        return [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,\n",
-       "                TableDrivenVacuumAgent, ModelBasedVacuumAgent]\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",
+       "    def default_location(self, thing):\n",
+       "        """Agents start in either location at random."""\n",
+       "        return random.choice([loc_A, loc_B, loc_C, loc_D])\n",
        "
\n", - "\n", - "\n" + "
" ], "text/plain": [ "" @@ -246,22 +167,22 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 5, "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', (0, 1): 'Clean', (1, 1): 'Clean'}.\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, loc_C, loc_D = (0, 0), (1, 0), (0, 1), (1, 1)\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", @@ -277,12 +198,12 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 6, "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,14 +215,14 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "RandomVacuumAgent is located at (1, 0).\n" + "RandomVacuumAgent is located at (0, 0).\n" ] } ], @@ -321,15 +242,15 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", - "RandomVacuumAgent is located at (1, 0).\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", + "RandomVacuumAgent is located at (0, 1).\n" ] } ], @@ -355,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -363,6 +284,10 @@ " ((loc_A, 'Dirty'),): 'Suck',\n", " ((loc_B, 'Clean'),): 'Left',\n", " ((loc_B, 'Dirty'),): 'Suck',\n", + " ((loc_C, 'Clean'),): 'Down',\n", + " ((loc_C, 'Dirty'),): 'Suck',\n", + " ((loc_D, 'Clean'),): 'Up',\n", + " ((loc_D, 'Dirty'),): 'Suck',\n", " ((loc_A, 'Dirty'), (loc_A, 'Clean')): 'Right',\n", " ((loc_A, 'Clean'), (loc_B, 'Dirty')): 'Suck',\n", " ((loc_B, 'Clean'), (loc_A, 'Dirty')): 'Suck',\n", @@ -381,7 +306,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -407,14 +332,14 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "TableDrivenVacuumAgent is located at (0, 0).\n" + "TableDrivenVacuumAgent is located at (0, 1).\n" ] } ], @@ -427,15 +352,15 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", - "TableDrivenVacuumAgent is located at (1, 0).\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", + "TableDrivenVacuumAgent is located at (0, 0).\n" ] } ], @@ -471,7 +396,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -488,13 +413,15 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "\n", "loc_A = (0, 0)\n", "loc_B = (1, 0)\n", + "loc_C = (0, 1)\n", + "loc_D = (1, 1)\n", "\n", "\"\"\"We change the simpleReflexAgentProgram so that it doesn't make use of the Rule class\"\"\"\n", "def SimpleReflexAgentProgram():\n", @@ -502,9 +429,12 @@ " \n", " def program(percept):\n", " loc, status = percept\n", - " return ('Suck' if status == 'Dirty' \n", - " else'Right' if loc == loc_A \n", - " else'Left')\n", + " if status == 'Dirty':\n", + " return 'Suck'\n", + " return ('Right' if loc == loc_A else\n", + " 'Left' if loc == loc_B else\n", + " 'Down' if loc == loc_C else\n", + " 'Up')\n", " return program\n", "\n", " \n", @@ -522,14 +452,14 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "SimpleReflexVacuumAgent is located at (0, 1).\n" ] } ], @@ -541,15 +471,15 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", + "SimpleReflexVacuumAgent is located at (0, 0).\n" ] } ], @@ -702,9 +632,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.11.5" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } From 2a6146163f4d1e75666719df509b26c668227570 Mon Sep 17 00:00:00 2001 From: Nicole Nageli Date: Fri, 4 Sep 2026 03:55:51 -0600 Subject: [PATCH 4/4] q4 --- aima/agents.py | 15 ++-- notebooks/vacuum_world.ipynb | 146 +++++++++++++++++------------------ 2 files changed, 82 insertions(+), 79 deletions(-) diff --git a/aima/agents.py b/aima/agents.py index 7a6267f68..7e7ecd038 100644 --- a/aima/agents.py +++ b/aima/agents.py @@ -192,7 +192,7 @@ def rule_match(state, rules): loc_A, loc_B, loc_C, loc_D = (0, 0), (1, 0), (0, 1), (1, 1) # The four locations for the Vacuum world - +locations = [loc_A, loc_B, loc_C, loc_D] def RandomVacuumAgent(): """Randomly choose one of the actions from the vacuum environment. @@ -261,13 +261,14 @@ def ModelBasedVacuumAgent(): >>> environment.status == {(1,0):'Clean' , (0,0) : 'Clean'} True """ - model = {loc_A: None, loc_B: None} + model = {loc_A: None, loc_B: None, loc_C: None, loc_D: None} def program(percept): """Same as ReflexVacuumAgent, except if everything is clean, do NoOp.""" - location, status = percept - model[location] = status # Update the model here - if model[loc_A] == model[loc_B] == 'Clean': + # location, status = percept + # model[location] = status # Update the model here + state = update_state(state, action, percept, model) + if model[loc_A] == model[loc_B] == model[loc_C] == model[loc_D] == 'Clean': return 'NoOp' elif status == 'Dirty': return 'Suck' @@ -275,6 +276,10 @@ def program(percept): return 'Right' elif location == loc_B: return 'Left' + elif location == loc_C: + return 'Down' + elif location == loc_D: + return 'Up' return Agent(program) diff --git a/notebooks/vacuum_world.ipynb b/notebooks/vacuum_world.ipynb index 624bb9eaa..3292df633 100644 --- a/notebooks/vacuum_world.ipynb +++ b/notebooks/vacuum_world.ipynb @@ -2,14 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "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", + "/s/bach/d/under/C838051824/.local/lib/python3.11/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" ] } @@ -91,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -108,59 +108,55 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
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",
+       "
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",
-       "    def __init__(self):\n",
-       "        super().__init__()\n",
-       "        self.status = {loc_A: random.choice(['Clean', 'Dirty']),\n",
-       "                       loc_B: random.choice(['Clean', 'Dirty']),\n",
-       "                       loc_C: random.choice(['Clean', 'Dirty']),\n",
-       "                       loc_D: random.choice(['Clean', 'Dirty'])}\n",
+       "    def __init__(self):\n",
+       "        super().__init__()\n",
+       "        self.status = {loc: random.choice(['Clean', 'Dirty']) for loc in locations}\n",
        "\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",
+       "    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 percept(self, agent):\n",
-       "        """Returns the agent's location, and the location status (Dirty/Clean)."""\n",
-       "        return agent.location, self.status[agent.location]\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",
-       "        a, b = agent.location\n",
-       "        if action == 'Right':\n",
-       "            agent.location = (a + 1, b)\n",
-       "            agent.performance -= 1\n",
-       "        elif action == 'Left':\n",
-       "            agent.location = (a - 1, b)\n",
-       "            agent.performance -= 1\n",
-       "        elif action == 'Up':\n",
-       "            agent.location = (a, b + 1)\n",
-       "            agent.performance -= 1\n",
-       "        elif action == 'Down':\n",
-       "            agent.location = (a, b - 1)\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",
+       "    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",
+       "        a, b = agent.location\n",
+       "        if action == 'Right':\n",
+       "            agent.location = (a + 1, b)\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Left':\n",
+       "            agent.location = (a - 1, b)\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Up':\n",
+       "            agent.location = (a, b + 1)\n",
+       "            agent.performance -= 1\n",
+       "        elif action == 'Down':\n",
+       "            agent.location = (a, b - 1)\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, loc_C, loc_D])\n",
-       "
\n", - "
" + " def default_location(self, thing):\n", + " \"\"\"Agents start in either location at random.\"\"\"\n", + " return random.choice([loc_A, loc_B, loc_C, loc_D])\n", + "
\n" ], "text/plain": [ "" @@ -176,14 +172,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Dirty', (0, 1): 'Clean', (1, 1): 'Dirty'}.\n" ] } ], @@ -208,7 +204,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -225,14 +221,14 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "RandomVacuumAgent is located at (0, 0).\n" + "RandomVacuumAgent is located at (1, 1).\n" ] } ], @@ -259,8 +255,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", - "RandomVacuumAgent is located at (0, 1).\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Dirty', (0, 1): 'Clean', (1, 1): 'Dirty'}.\n", + "RandomVacuumAgent is located at (1, 2).\n" ] } ], @@ -286,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -316,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -333,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -342,14 +338,14 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "TableDrivenVacuumAgent is located at (0, 1).\n" + "TableDrivenVacuumAgent is located at (1, 0).\n" ] } ], @@ -362,15 +358,15 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", - "TableDrivenVacuumAgent is located at (0, 0).\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Dirty'}.\n", + "TableDrivenVacuumAgent is located at (1, 0).\n" ] } ], @@ -406,7 +402,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -423,7 +419,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -462,7 +458,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -481,14 +477,14 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Dirty'}.\n", "SimpleReflexVacuumAgent is located at (0, 0).\n" ] } @@ -524,7 +520,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -541,7 +537,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -555,8 +551,10 @@ "source": [ "# TODO: Implement this function for the two-dimensional environment\n", "def update_state(state, action, percept, model):\n", - " \n", - " pass\n", + " location, status = percept\n", + " model[location] = status\n", + " state = percept\n", + " return state\n", "\n", "# Create a model-based reflex agent\n", "model_based_reflex_agent = ModelBasedVacuumAgent()\n", @@ -569,14 +567,14 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean', (0, 1): 'Clean', (1, 1): 'Clean'}.\n", "ModelBasedVacuumAgent is located at (0, 0).\n" ] } @@ -629,7 +627,7 @@ ], "metadata": { "kernelspec": { - "display_name": "AIMA (3.13.5)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -643,7 +641,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.13" } }, "nbformat": 4,