diff --git a/aima/agents.py b/aima/agents.py index d159466cc..7e7ecd038 100644 --- a/aima/agents.py +++ b/aima/agents.py @@ -191,8 +191,8 @@ 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 +locations = [loc_A, loc_B, loc_C, loc_D] def RandomVacuumAgent(): """Randomly choose one of the actions from the vacuum environment. @@ -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(): @@ -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) @@ -806,8 +811,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.""" @@ -820,11 +824,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 +844,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..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/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..3292df633 100644 --- a/notebooks/vacuum_world.ipynb +++ b/notebooks/vacuum_world.ipynb @@ -2,9 +2,18 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/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" + ] + } + ], "source": [ "%run bootstrap.ipynb" ] @@ -82,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -99,138 +108,55 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "\n", - "\n", - "\n", - "\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "

\n", - "\n", - "
class TrivialVacuumEnvironment(Environment):\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",
+       "
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",
+       "    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 [Wall, Dirt, ReflexVacuumAgent, RandomVacuumAgent,\n",
-       "                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",
-       "        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",
+       "    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])\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, loc_C, loc_D])\n", + "
\n" ], "text/plain": [ "" @@ -246,24 +172,25 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 6, "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', (0, 1): 'Clean', (1, 1): '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 +204,12 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 7, "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 +221,14 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "RandomVacuumAgent is located at (1, 0).\n" + "RandomVacuumAgent is located at (1, 1).\n" ] } ], @@ -321,15 +248,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): 'Dirty', (0, 1): 'Clean', (1, 1): 'Dirty'}.\n", + "RandomVacuumAgent is located at (1, 2).\n" ] } ], @@ -355,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -363,6 +290,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 +312,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -398,7 +329,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -407,14 +338,14 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "TableDrivenVacuumAgent is located at (0, 0).\n" + "TableDrivenVacuumAgent is located at (1, 0).\n" ] } ], @@ -427,14 +358,14 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 14, "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): 'Dirty'}.\n", "TableDrivenVacuumAgent is located at (1, 0).\n" ] } @@ -471,7 +402,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -488,13 +419,15 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 16, "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 +435,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 +458,14 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "SimpleReflexVacuumAgent is located at (0, 1).\n" ] } ], @@ -541,15 +477,15 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 18, "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): 'Dirty'}.\n", + "SimpleReflexVacuumAgent is located at (0, 0).\n" ] } ], @@ -584,7 +520,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -601,21 +537,24 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 24, "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", - " 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", @@ -628,15 +567,15 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "ModelBasedVacuumAgent is located at (1, 0).\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" ] } ], @@ -702,9 +641,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.11.13" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }