From 276ec269e0dd68b7672b5a7da8ec81949ceca0c9 Mon Sep 17 00:00:00 2001 From: Noumanmufc1 Date: Thu, 8 Mar 2018 04:40:02 +0500 Subject: [PATCH 1/3] Added simpleReflexAgentProgram to vacuumworld.ipynb --- vacuum_world.ipynb | 219 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 186 insertions(+), 33 deletions(-) diff --git a/vacuum_world.ipynb b/vacuum_world.ipynb index 2c18e4185..0017593dc 100644 --- a/vacuum_world.ipynb +++ b/vacuum_world.ipynb @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -90,23 +90,161 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " Codestin Search App\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": 4, "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": 5, "metadata": {}, "outputs": [], "source": [ @@ -147,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -174,15 +312,15 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "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": 8, "metadata": {}, "outputs": [], "source": [ @@ -234,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -251,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -260,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -280,15 +418,15 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "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": 13, "metadata": {}, "outputs": [], "source": [ @@ -336,21 +474,35 @@ "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": 17, "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", + "# 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", + "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 +511,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 +524,26 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "SimpleReflexVacuumAgent is located at (1, 0).\n" + "SimpleReflexAgent is located at (0, 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": 19, "metadata": {}, "outputs": [ { @@ -398,18 +551,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 +704,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.4" } }, "nbformat": 4, From e92df12f4e3f1dcdb20232e42fb63cf954322766 Mon Sep 17 00:00:00 2001 From: Noumanmufc1 Date: Thu, 8 Mar 2018 04:46:30 +0500 Subject: [PATCH 2/3] fixed a styling issue --- vacuum_world.ipynb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vacuum_world.ipynb b/vacuum_world.ipynb index 0017593dc..e0047d0a3 100644 --- a/vacuum_world.ipynb +++ b/vacuum_world.ipynb @@ -484,7 +484,7 @@ "outputs": [], "source": [ "# TODO: Implement these functions for two-dimensional environment\n", - "# Interpret-input function for the two-state environment\n", + "\n", "# Rule class\n", "class Rule:\n", " def __init__(self, state, action):\n", @@ -493,7 +493,8 @@ " \n", " def matches(self, state):\n", " return self.__state == state\n", - " \n", + "\n", + "# Interpret-input function for the two-state environment\n", "def interpret_input(state):\n", " return state\n", " \n", From 7d392e83b7d2fa63814ade363ebbb5a0bb3a789e Mon Sep 17 00:00:00 2001 From: Noumanmufc1 Date: Thu, 8 Mar 2018 16:52:25 +0500 Subject: [PATCH 3/3] Fixed a type --- vacuum_world.ipynb | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/vacuum_world.ipynb b/vacuum_world.ipynb index e0047d0a3..f089cf1c1 100644 --- a/vacuum_world.ipynb +++ b/vacuum_world.ipynb @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -237,14 +237,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 22, "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" ] } ], @@ -268,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -285,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -312,15 +312,15 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 25, "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'}.\n", + "RandomVacuumAgent is located at (0, 0).\n" ] } ], @@ -346,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -372,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -389,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -398,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -418,7 +418,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -426,7 +426,7 @@ "output_type": "stream", "text": [ "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", - "TableDrivenVacuumAgent is located at (1, 0).\n" + "TableDrivenVacuumAgent is located at (0, 0).\n" ] } ], @@ -462,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -479,7 +479,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -501,7 +501,7 @@ "loc_A = (0, 0)\n", "loc_B = (1, 0)\n", "\n", - "# create rules for a two state Vacuum Environment\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", @@ -525,14 +525,14 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "SimpleReflexAgent is located at (0, 0).\n" + "SimpleReflexAgent is located at (1, 0).\n" ] } ], @@ -544,7 +544,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 34, "metadata": {}, "outputs": [ {