diff --git a/vacuum_world.ipynb b/vacuum_world.ipynb index 2c18e4185..f089cf1c1 100644 --- a/vacuum_world.ipynb +++ b/vacuum_world.ipynb @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -90,23 +90,161 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "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",
+       "\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",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(TrivialVacuumEnvironment)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n" + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Dirty'}.\n" ] } ], @@ -130,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -147,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -174,14 +312,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n", + "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Dirty'}.\n", "RandomVacuumAgent is located at (0, 0).\n" ] } @@ -208,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -234,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -251,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -260,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -280,14 +418,14 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 30, "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): 'Clean', (1, 0): 'Dirty'}.\n", "TableDrivenVacuumAgent is located at (0, 0).\n" ] } @@ -312,7 +450,7 @@ "\n", "The schematic diagram shown in **Figure 2.9** of the book will make this more clear:\n", "\n", - "" + "![simple reflex agent](images/simple_reflex_agent.jpg)" ] }, { @@ -324,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -336,21 +474,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To create our agent, we need two functions: INTERPRET-INPUT function, which generates an abstracted description of the current state from the percerpt and the RULE-MATCH function, which returns the first rule in the set of rules that matches the given state description." + "To create our agent, we need two functions: INTERPRET-INPUT function, which generates an abstracted description of the current state from the percerpt and the RULE-MATCH function, which returns the first rule in the set of rules that matches the given state description. For rules, we will implement a `Rule` class." ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# TODO: Implement these functions for two-dimensional environment\n", + "\n", + "# Rule class\n", + "class Rule:\n", + " def __init__(self, state, action):\n", + " self.__state = state\n", + " self.action = action\n", + " \n", + " def matches(self, state):\n", + " return self.__state == state\n", + "\n", "# Interpret-input function for the two-state environment\n", - "def interpret_input(percept):\n", - " pass\n", + "def interpret_input(state):\n", + " return state\n", + " \n", + "loc_A = (0, 0)\n", + "loc_B = (1, 0)\n", "\n", - "rules = None\n", + "# create rules for a two-state Vacuum Environment\n", + "rules = [Rule((loc_A, \"Dirty\"), \"Suck\"), Rule((loc_A, \"Clean\"), \"Right\"),\n", + " Rule((loc_B, \"Dirty\"), \"Suck\"), Rule((loc_B, \"Clean\"), \"Left\")]\n", "\n", "# Rule-match function for the two-state environment\n", "def rule_match(state, rule):\n", @@ -359,7 +512,8 @@ " return rule \n", " \n", "# Create a simple reflex agent the two-state environment\n", - "simple_reflex_agent = ReflexVacuumAgent()" + "program = SimpleReflexAgentProgram(rules, interpret_input)\n", + "simple_reflex_agent = Agent(program)" ] }, { @@ -371,26 +525,26 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "SimpleReflexAgent is located at (1, 0).\n" ] } ], "source": [ "trivial_vacuum_env.add_thing(simple_reflex_agent)\n", "\n", - "print(\"SimpleReflexVacuumAgent is located at {}.\".format(simple_reflex_agent.location))" + "print(\"SimpleReflexAgent is located at {}.\".format(simple_reflex_agent.location))" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -398,18 +552,18 @@ "output_type": "stream", "text": [ "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "SimpleReflexVacuumAgent is located at (0, 0).\n" + "SimpleReflexAgent is located at (0, 0).\n" ] } ], "source": [ "# Run the environment\n", - "trivial_vacuum_env.step()\n", + "trivial_vacuum_env.run()\n", "\n", "# Check the current state of the environment\n", "print(\"State of the Environment: {}.\".format(trivial_vacuum_env.status))\n", "\n", - "print(\"SimpleReflexVacuumAgent is located at {}.\".format(simple_reflex_agent.location))" + "print(\"SimpleReflexAgent is located at {}.\".format(simple_reflex_agent.location))" ] }, { @@ -551,7 +705,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.4" } }, "nbformat": 4, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy