Skip to content
Merged
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
214 changes: 178 additions & 36 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": 38,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -90,23 +90,161 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"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": 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"
]
}
],
Expand All @@ -130,7 +268,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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"
]
}
],
Expand All @@ -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"
]
}
],
Expand All @@ -208,7 +346,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -234,7 +372,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -251,7 +389,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -260,7 +398,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 47,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
]
}
],
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": 49,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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)"
]
},
{
Expand All @@ -371,7 +513,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 51,
"metadata": {},
"outputs": [
{
Expand All @@ -390,15 +532,15 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 52,
"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"
"SimpleReflexVacuumAgent is located at (1, 0).\n"
]
}
],
Expand Down Expand Up @@ -551,7 +693,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.4"
}
},
"nbformat": 4,
Expand Down