Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added simpleReflexAgentProgram to vacuumworld.ipynb #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 185 additions & 31 deletions vacuum_world.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -90,23 +90,161 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n",
" \"http://www.w3.org/TR/html4/strict.dtd\">\n",
"\n",
"<html>\n",
"<head>\n",
" <title></title>\n",
" <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n",
" <style type=\"text/css\">\n",
"td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n",
"span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n",
"pre { line-height: 125%; }\n",
"body .hll { background-color: #ffffcc }\n",
"body { background: #f8f8f8; }\n",
"body .c { color: #408080; font-style: italic } /* Comment */\n",
"body .err { border: 1px solid #FF0000 } /* Error */\n",
"body .k { color: #008000; font-weight: bold } /* Keyword */\n",
"body .o { color: #666666 } /* Operator */\n",
"body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n",
"body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
"body .cp { color: #BC7A00 } /* Comment.Preproc */\n",
"body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n",
"body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
"body .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
"body .gd { color: #A00000 } /* Generic.Deleted */\n",
"body .ge { font-style: italic } /* Generic.Emph */\n",
"body .gr { color: #FF0000 } /* Generic.Error */\n",
"body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
"body .gi { color: #00A000 } /* Generic.Inserted */\n",
"body .go { color: #888888 } /* Generic.Output */\n",
"body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
"body .gs { font-weight: bold } /* Generic.Strong */\n",
"body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
"body .gt { color: #0044DD } /* Generic.Traceback */\n",
"body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
"body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
"body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
"body .kp { color: #008000 } /* Keyword.Pseudo */\n",
"body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
"body .kt { color: #B00040 } /* Keyword.Type */\n",
"body .m { color: #666666 } /* Literal.Number */\n",
"body .s { color: #BA2121 } /* Literal.String */\n",
"body .na { color: #7D9029 } /* Name.Attribute */\n",
"body .nb { color: #008000 } /* Name.Builtin */\n",
"body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
"body .no { color: #880000 } /* Name.Constant */\n",
"body .nd { color: #AA22FF } /* Name.Decorator */\n",
"body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
"body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
"body .nf { color: #0000FF } /* Name.Function */\n",
"body .nl { color: #A0A000 } /* Name.Label */\n",
"body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
"body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
"body .nv { color: #19177C } /* Name.Variable */\n",
"body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
"body .w { color: #bbbbbb } /* Text.Whitespace */\n",
"body .mb { color: #666666 } /* Literal.Number.Bin */\n",
"body .mf { color: #666666 } /* Literal.Number.Float */\n",
"body .mh { color: #666666 } /* Literal.Number.Hex */\n",
"body .mi { color: #666666 } /* Literal.Number.Integer */\n",
"body .mo { color: #666666 } /* Literal.Number.Oct */\n",
"body .sa { color: #BA2121 } /* Literal.String.Affix */\n",
"body .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
"body .sc { color: #BA2121 } /* Literal.String.Char */\n",
"body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n",
"body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
"body .s2 { color: #BA2121 } /* Literal.String.Double */\n",
"body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
"body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
"body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
"body .sx { color: #008000 } /* Literal.String.Other */\n",
"body .sr { color: #BB6688 } /* Literal.String.Regex */\n",
"body .s1 { color: #BA2121 } /* Literal.String.Single */\n",
"body .ss { color: #19177C } /* Literal.String.Symbol */\n",
"body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
"body .fm { color: #0000FF } /* Name.Function.Magic */\n",
"body .vc { color: #19177C } /* Name.Variable.Class */\n",
"body .vg { color: #19177C } /* Name.Variable.Global */\n",
"body .vi { color: #19177C } /* Name.Variable.Instance */\n",
"body .vm { color: #19177C } /* Name.Variable.Magic */\n",
"body .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"\n",
" </style>\n",
"</head>\n",
"<body>\n",
"<h2></h2>\n",
"\n",
"<div class=\"highlight\"><pre><span></span><span class=\"k\">class</span> <span class=\"nc\">TrivialVacuumEnvironment</span><span class=\"p\">(</span><span class=\"n\">Environment</span><span class=\"p\">):</span>\n",
"\n",
" <span class=\"sd\">&quot;&quot;&quot;This environment has two locations, A and B. Each can be Dirty</span>\n",
"<span class=\"sd\"> or Clean. The agent perceives its location and the location&#39;s</span>\n",
"<span class=\"sd\"> status. This serves as an example of how to implement a simple</span>\n",
"<span class=\"sd\"> Environment.&quot;&quot;&quot;</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n",
" <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">()</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">status</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"n\">loc_A</span><span class=\"p\">:</span> <span class=\"n\">random</span><span class=\"o\">.</span><span class=\"n\">choice</span><span class=\"p\">([</span><span class=\"s1\">&#39;Clean&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Dirty&#39;</span><span class=\"p\">]),</span>\n",
" <span class=\"n\">loc_B</span><span class=\"p\">:</span> <span class=\"n\">random</span><span class=\"o\">.</span><span class=\"n\">choice</span><span class=\"p\">([</span><span class=\"s1\">&#39;Clean&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Dirty&#39;</span><span class=\"p\">])}</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">thing_classes</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n",
" <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">Wall</span><span class=\"p\">,</span> <span class=\"n\">Dirt</span><span class=\"p\">,</span> <span class=\"n\">ReflexVacuumAgent</span><span class=\"p\">,</span> <span class=\"n\">RandomVacuumAgent</span><span class=\"p\">,</span>\n",
" <span class=\"n\">TableDrivenVacuumAgent</span><span class=\"p\">,</span> <span class=\"n\">ModelBasedVacuumAgent</span><span class=\"p\">]</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">percept</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">agent</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">&quot;&quot;&quot;Returns the agent&#39;s location, and the location status (Dirty/Clean).&quot;&quot;&quot;</span>\n",
" <span class=\"k\">return</span> <span class=\"p\">(</span><span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">location</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">status</span><span class=\"p\">[</span><span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">location</span><span class=\"p\">])</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">execute_action</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">agent</span><span class=\"p\">,</span> <span class=\"n\">action</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">&quot;&quot;&quot;Change agent&#39;s location and/or location&#39;s status; track performance.</span>\n",
"<span class=\"sd\"> Score 10 for each dirt cleaned; -1 for each move.&quot;&quot;&quot;</span>\n",
" <span class=\"k\">if</span> <span class=\"n\">action</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;Right&#39;</span><span class=\"p\">:</span>\n",
" <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">location</span> <span class=\"o\">=</span> <span class=\"n\">loc_B</span>\n",
" <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">performance</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>\n",
" <span class=\"k\">elif</span> <span class=\"n\">action</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;Left&#39;</span><span class=\"p\">:</span>\n",
" <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">location</span> <span class=\"o\">=</span> <span class=\"n\">loc_A</span>\n",
" <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">performance</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>\n",
" <span class=\"k\">elif</span> <span class=\"n\">action</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;Suck&#39;</span><span class=\"p\">:</span>\n",
" <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">status</span><span class=\"p\">[</span><span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">location</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;Dirty&#39;</span><span class=\"p\">:</span>\n",
" <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">performance</span> <span class=\"o\">+=</span> <span class=\"mi\">10</span>\n",
" <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">status</span><span class=\"p\">[</span><span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">location</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;Clean&#39;</span>\n",
"\n",
" <span class=\"k\">def</span> <span class=\"nf\">default_location</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">thing</span><span class=\"p\">):</span>\n",
" <span class=\"sd\">&quot;&quot;&quot;Agents start in either location at random.&quot;&quot;&quot;</span>\n",
" <span class=\"k\">return</span> <span class=\"n\">random</span><span class=\"o\">.</span><span class=\"n\">choice</span><span class=\"p\">([</span><span class=\"n\">loc_A</span><span class=\"p\">,</span> <span class=\"n\">loc_B</span><span class=\"p\">])</span>\n",
"</pre></div>\n",
"</body>\n",
"</html>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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"
]
}
],
Expand All @@ -130,7 +268,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -147,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
]
}
Expand All @@ -208,7 +346,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -234,7 +372,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -251,7 +389,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -260,7 +398,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 29,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
]
}
Expand All @@ -312,7 +450,7 @@
"\n",
"The schematic diagram shown in **Figure 2.9** of the book will make this more clear:\n",
"\n",
"<img src=\"/files/images/simple_reflex_agent.jpg\">"
"![simple reflex agent](images/simple_reflex_agent.jpg)"
]
},
{
Expand All @@ -324,7 +462,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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",
Expand All @@ -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)"
]
},
{
Expand All @@ -371,45 +525,45 @@
},
{
"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": [
{
"name": "stdout",
"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))"
]
},
{
Expand Down Expand Up @@ -551,7 +705,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.4"
}
},
"nbformat": 4,
Expand Down