diff --git a/vacuum_world.ipynb b/vacuum_world.ipynb index 2c18e4185..48ff4e655 100644 --- a/vacuum_world.ipynb +++ b/vacuum_world.ipynb @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -90,23 +90,161 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "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": 40, "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): 'Clean', (1, 0): 'Dirty'}.\n" ] } ], @@ -130,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -147,14 +285,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "RandomVacuumAgent is located at (0, 0).\n" + "RandomVacuumAgent is located at (1, 0).\n" ] } ], @@ -174,15 +312,15 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n", - "RandomVacuumAgent is located at (0, 0).\n" + "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", + "RandomVacuumAgent is located at (1, 0).\n" ] } ], @@ -208,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -234,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -251,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ @@ -260,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 47, "metadata": {}, "outputs": [ { @@ -280,15 +418,15 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "TableDrivenVacuumAgent is located at (0, 0).\n" + "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", + "TableDrivenVacuumAgent is located at (1, 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": 49, "metadata": {}, "outputs": [], "source": [ @@ -341,25 +479,29 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ - "# TODO: Implement these functions for two-dimensional environment\n", - "# Interpret-input function for the two-state environment\n", - "def interpret_input(percept):\n", - " pass\n", "\n", - "rules = None\n", + "loc_A = (0, 0)\n", + "loc_B = (1, 0)\n", + "\n", + "\"\"\"We change the simpleReflexAgentProgram so that it doesn't make use of the Rule class\"\"\"\n", + "def SimpleReflexAgentProgram():\n", + " \"\"\"This agent takes action based solely on the percept. [Figure 2.10]\"\"\"\n", + " \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", + " return program\n", "\n", - "# Rule-match function for the two-state environment\n", - "def rule_match(state, rule):\n", - " for rule in rules:\n", - " if rule.matches(state):\n", - " return rule \n", " \n", "# Create a simple reflex agent the two-state environment\n", - "simple_reflex_agent = ReflexVacuumAgent()" + "program = SimpleReflexAgentProgram()\n", + "simple_reflex_agent = Agent(program)" ] }, { @@ -371,7 +513,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 51, "metadata": {}, "outputs": [ { @@ -390,7 +532,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -398,7 +540,7 @@ "output_type": "stream", "text": [ "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", - "SimpleReflexVacuumAgent is located at (0, 0).\n" + "SimpleReflexVacuumAgent is located at (1, 0).\n" ] } ], @@ -551,7 +693,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.4" } }, "nbformat": 4,