|
90 | 90 | },
|
91 | 91 | {
|
92 | 92 | "cell_type": "code",
|
93 |
| - "execution_count": null, |
| 93 | + "execution_count": 2, |
94 | 94 | "metadata": {},
|
95 |
| - "outputs": [], |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "data": { |
| 98 | + "text/html": [ |
| 99 | + "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n", |
| 100 | + " \"http://www.w3.org/TR/html4/strict.dtd\">\n", |
| 101 | + "\n", |
| 102 | + "<html>\n", |
| 103 | + "<head>\n", |
| 104 | + " <title></title>\n", |
| 105 | + " <meta http-equiv=\"content-type\" content=\"text/html; charset=None\">\n", |
| 106 | + " <style type=\"text/css\">\n", |
| 107 | + "td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n", |
| 108 | + "span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n", |
| 109 | + "pre { line-height: 125%; }\n", |
| 110 | + "body .hll { background-color: #ffffcc }\n", |
| 111 | + "body { background: #f8f8f8; }\n", |
| 112 | + "body .c { color: #408080; font-style: italic } /* Comment */\n", |
| 113 | + "body .err { border: 1px solid #FF0000 } /* Error */\n", |
| 114 | + "body .k { color: #008000; font-weight: bold } /* Keyword */\n", |
| 115 | + "body .o { color: #666666 } /* Operator */\n", |
| 116 | + "body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */\n", |
| 117 | + "body .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n", |
| 118 | + "body .cp { color: #BC7A00 } /* Comment.Preproc */\n", |
| 119 | + "body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */\n", |
| 120 | + "body .c1 { color: #408080; font-style: italic } /* Comment.Single */\n", |
| 121 | + "body .cs { color: #408080; font-style: italic } /* Comment.Special */\n", |
| 122 | + "body .gd { color: #A00000 } /* Generic.Deleted */\n", |
| 123 | + "body .ge { font-style: italic } /* Generic.Emph */\n", |
| 124 | + "body .gr { color: #FF0000 } /* Generic.Error */\n", |
| 125 | + "body .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n", |
| 126 | + "body .gi { color: #00A000 } /* Generic.Inserted */\n", |
| 127 | + "body .go { color: #888888 } /* Generic.Output */\n", |
| 128 | + "body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n", |
| 129 | + "body .gs { font-weight: bold } /* Generic.Strong */\n", |
| 130 | + "body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n", |
| 131 | + "body .gt { color: #0044DD } /* Generic.Traceback */\n", |
| 132 | + "body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n", |
| 133 | + "body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n", |
| 134 | + "body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n", |
| 135 | + "body .kp { color: #008000 } /* Keyword.Pseudo */\n", |
| 136 | + "body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n", |
| 137 | + "body .kt { color: #B00040 } /* Keyword.Type */\n", |
| 138 | + "body .m { color: #666666 } /* Literal.Number */\n", |
| 139 | + "body .s { color: #BA2121 } /* Literal.String */\n", |
| 140 | + "body .na { color: #7D9029 } /* Name.Attribute */\n", |
| 141 | + "body .nb { color: #008000 } /* Name.Builtin */\n", |
| 142 | + "body .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n", |
| 143 | + "body .no { color: #880000 } /* Name.Constant */\n", |
| 144 | + "body .nd { color: #AA22FF } /* Name.Decorator */\n", |
| 145 | + "body .ni { color: #999999; font-weight: bold } /* Name.Entity */\n", |
| 146 | + "body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n", |
| 147 | + "body .nf { color: #0000FF } /* Name.Function */\n", |
| 148 | + "body .nl { color: #A0A000 } /* Name.Label */\n", |
| 149 | + "body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n", |
| 150 | + "body .nt { color: #008000; font-weight: bold } /* Name.Tag */\n", |
| 151 | + "body .nv { color: #19177C } /* Name.Variable */\n", |
| 152 | + "body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n", |
| 153 | + "body .w { color: #bbbbbb } /* Text.Whitespace */\n", |
| 154 | + "body .mb { color: #666666 } /* Literal.Number.Bin */\n", |
| 155 | + "body .mf { color: #666666 } /* Literal.Number.Float */\n", |
| 156 | + "body .mh { color: #666666 } /* Literal.Number.Hex */\n", |
| 157 | + "body .mi { color: #666666 } /* Literal.Number.Integer */\n", |
| 158 | + "body .mo { color: #666666 } /* Literal.Number.Oct */\n", |
| 159 | + "body .sa { color: #BA2121 } /* Literal.String.Affix */\n", |
| 160 | + "body .sb { color: #BA2121 } /* Literal.String.Backtick */\n", |
| 161 | + "body .sc { color: #BA2121 } /* Literal.String.Char */\n", |
| 162 | + "body .dl { color: #BA2121 } /* Literal.String.Delimiter */\n", |
| 163 | + "body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n", |
| 164 | + "body .s2 { color: #BA2121 } /* Literal.String.Double */\n", |
| 165 | + "body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n", |
| 166 | + "body .sh { color: #BA2121 } /* Literal.String.Heredoc */\n", |
| 167 | + "body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n", |
| 168 | + "body .sx { color: #008000 } /* Literal.String.Other */\n", |
| 169 | + "body .sr { color: #BB6688 } /* Literal.String.Regex */\n", |
| 170 | + "body .s1 { color: #BA2121 } /* Literal.String.Single */\n", |
| 171 | + "body .ss { color: #19177C } /* Literal.String.Symbol */\n", |
| 172 | + "body .bp { color: #008000 } /* Name.Builtin.Pseudo */\n", |
| 173 | + "body .fm { color: #0000FF } /* Name.Function.Magic */\n", |
| 174 | + "body .vc { color: #19177C } /* Name.Variable.Class */\n", |
| 175 | + "body .vg { color: #19177C } /* Name.Variable.Global */\n", |
| 176 | + "body .vi { color: #19177C } /* Name.Variable.Instance */\n", |
| 177 | + "body .vm { color: #19177C } /* Name.Variable.Magic */\n", |
| 178 | + "body .il { color: #666666 } /* Literal.Number.Integer.Long */\n", |
| 179 | + "\n", |
| 180 | + " </style>\n", |
| 181 | + "</head>\n", |
| 182 | + "<body>\n", |
| 183 | + "<h2></h2>\n", |
| 184 | + "\n", |
| 185 | + "<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", |
| 186 | + "\n", |
| 187 | + " <span class=\"sd\">"""This environment has two locations, A and B. Each can be Dirty</span>\n", |
| 188 | + "<span class=\"sd\"> or Clean. The agent perceives its location and the location's</span>\n", |
| 189 | + "<span class=\"sd\"> status. This serves as an example of how to implement a simple</span>\n", |
| 190 | + "<span class=\"sd\"> Environment."""</span>\n", |
| 191 | + "\n", |
| 192 | + " <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n", |
| 193 | + " <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">()</span>\n", |
| 194 | + " <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\">'Clean'</span><span class=\"p\">,</span> <span class=\"s1\">'Dirty'</span><span class=\"p\">]),</span>\n", |
| 195 | + " <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\">'Clean'</span><span class=\"p\">,</span> <span class=\"s1\">'Dirty'</span><span class=\"p\">])}</span>\n", |
| 196 | + "\n", |
| 197 | + " <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", |
| 198 | + " <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", |
| 199 | + " <span class=\"n\">TableDrivenVacuumAgent</span><span class=\"p\">,</span> <span class=\"n\">ModelBasedVacuumAgent</span><span class=\"p\">]</span>\n", |
| 200 | + "\n", |
| 201 | + " <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", |
| 202 | + " <span class=\"sd\">"""Returns the agent's location, and the location status (Dirty/Clean)."""</span>\n", |
| 203 | + " <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", |
| 204 | + "\n", |
| 205 | + " <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", |
| 206 | + " <span class=\"sd\">"""Change agent's location and/or location's status; track performance.</span>\n", |
| 207 | + "<span class=\"sd\"> Score 10 for each dirt cleaned; -1 for each move."""</span>\n", |
| 208 | + " <span class=\"k\">if</span> <span class=\"n\">action</span> <span class=\"o\">==</span> <span class=\"s1\">'Right'</span><span class=\"p\">:</span>\n", |
| 209 | + " <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", |
| 210 | + " <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">performance</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>\n", |
| 211 | + " <span class=\"k\">elif</span> <span class=\"n\">action</span> <span class=\"o\">==</span> <span class=\"s1\">'Left'</span><span class=\"p\">:</span>\n", |
| 212 | + " <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", |
| 213 | + " <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">performance</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>\n", |
| 214 | + " <span class=\"k\">elif</span> <span class=\"n\">action</span> <span class=\"o\">==</span> <span class=\"s1\">'Suck'</span><span class=\"p\">:</span>\n", |
| 215 | + " <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\">'Dirty'</span><span class=\"p\">:</span>\n", |
| 216 | + " <span class=\"n\">agent</span><span class=\"o\">.</span><span class=\"n\">performance</span> <span class=\"o\">+=</span> <span class=\"mi\">10</span>\n", |
| 217 | + " <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\">'Clean'</span>\n", |
| 218 | + "\n", |
| 219 | + " <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", |
| 220 | + " <span class=\"sd\">"""Agents start in either location at random."""</span>\n", |
| 221 | + " <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", |
| 222 | + "</pre></div>\n", |
| 223 | + "</body>\n", |
| 224 | + "</html>\n" |
| 225 | + ], |
| 226 | + "text/plain": [ |
| 227 | + "<IPython.core.display.HTML object>" |
| 228 | + ] |
| 229 | + }, |
| 230 | + "metadata": {}, |
| 231 | + "output_type": "display_data" |
| 232 | + } |
| 233 | + ], |
96 | 234 | "source": [
|
97 | 235 | "psource(TrivialVacuumEnvironment)"
|
98 | 236 | ]
|
|
106 | 244 | "name": "stdout",
|
107 | 245 | "output_type": "stream",
|
108 | 246 | "text": [
|
109 |
| - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n" |
| 247 | + "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n" |
110 | 248 | ]
|
111 | 249 | }
|
112 | 250 | ],
|
|
181 | 319 | "name": "stdout",
|
182 | 320 | "output_type": "stream",
|
183 | 321 | "text": [
|
184 |
| - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n", |
| 322 | + "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", |
185 | 323 | "RandomVacuumAgent is located at (0, 0).\n"
|
186 | 324 | ]
|
187 | 325 | }
|
|
267 | 405 | "name": "stdout",
|
268 | 406 | "output_type": "stream",
|
269 | 407 | "text": [
|
270 |
| - "TableDrivenVacuumAgent is located at (0, 0).\n" |
| 408 | + "TableDrivenVacuumAgent is located at (1, 0).\n" |
271 | 409 | ]
|
272 | 410 | }
|
273 | 411 | ],
|
|
288 | 426 | "output_type": "stream",
|
289 | 427 | "text": [
|
290 | 428 | "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n",
|
291 |
| - "TableDrivenVacuumAgent is located at (0, 0).\n" |
| 429 | + "TableDrivenVacuumAgent is located at (1, 0).\n" |
292 | 430 | ]
|
293 | 431 | }
|
294 | 432 | ],
|
|
312 | 450 | "\n",
|
313 | 451 | "The schematic diagram shown in **Figure 2.9** of the book will make this more clear:\n",
|
314 | 452 | "\n",
|
315 |
| - "<img src=\"/files/images/simple_reflex_agent.jpg\">" |
| 453 | + "\"\"" |
316 | 454 | ]
|
317 | 455 | },
|
318 | 456 | {
|
|
341 | 479 | },
|
342 | 480 | {
|
343 | 481 | "cell_type": "code",
|
344 |
| - "execution_count": 13, |
| 482 | + "execution_count": 21, |
345 | 483 | "metadata": {},
|
346 | 484 | "outputs": [],
|
347 | 485 | "source": [
|
348 | 486 | "# TODO: Implement these functions for two-dimensional environment\n",
|
349 | 487 | "# Interpret-input function for the two-state environment\n",
|
350 |
| - "def interpret_input(percept):\n", |
351 |
| - " pass\n", |
352 |
| - "\n", |
353 |
| - "rules = None\n", |
| 488 | + "def interpret_input(state):\n", |
| 489 | + " return state\n", |
354 | 490 | "\n",
|
355 | 491 | "# Rule-match function for the two-state environment\n",
|
356 | 492 | "def rule_match(state, rule):\n",
|
357 | 493 | " for rule in rules:\n",
|
358 |
| - " if rule.matches(state):\n", |
359 |
| - " return rule \n", |
| 494 | + " if matches(rule, state):\n", |
| 495 | + " return rule \n", |
| 496 | + "# matches function used in Rule-match function\n", |
| 497 | + "def matches(rule, state):\n", |
| 498 | + " if(rule[0] == state):\n", |
| 499 | + " return True\n", |
| 500 | + " else:\n", |
| 501 | + " return False\n", |
| 502 | + "\n", |
| 503 | + "\"\"\"We also change the simpleReflexAgentProgram so that it doesn't make \n", |
| 504 | + "use of the Rule class\"\"\"\n", |
| 505 | + "def SimpleReflexAgentProgram(rules, interpret_input):\n", |
| 506 | + " \"\"\"This agent takes action based solely on the percept. [Figure 2.10]\"\"\"\n", |
| 507 | + " def program(percept):\n", |
| 508 | + " state = interpret_input(percept)\n", |
| 509 | + " rule = rule_match(state, rules)\n", |
| 510 | + " action = rule[1]\n", |
| 511 | + " return action\n", |
| 512 | + " return program\n", |
| 513 | + "\n", |
| 514 | + "# create rules for a two-state Vacuum Environment\n", |
| 515 | + "rules = [((loc_A, \"Dirty\"), \"Suck\"), ((loc_A, \"Clean\"), \"Right\"),\n", |
| 516 | + " ((loc_B, \"Dirty\"), \"Suck\"), ((loc_B, \"Clean\"), \"Left\")]\n", |
360 | 517 | " \n",
|
361 | 518 | "# Create a simple reflex agent the two-state environment\n",
|
362 |
| - "simple_reflex_agent = ReflexVacuumAgent()" |
| 519 | + "program = SimpleReflexAgentProgram(rules, interpret_input)\n", |
| 520 | + "simple_reflex_agent = Agent(program)" |
363 | 521 | ]
|
364 | 522 | },
|
365 | 523 | {
|
|
371 | 529 | },
|
372 | 530 | {
|
373 | 531 | "cell_type": "code",
|
374 |
| - "execution_count": 14, |
| 532 | + "execution_count": 22, |
375 | 533 | "metadata": {},
|
376 | 534 | "outputs": [
|
377 | 535 | {
|
|
390 | 548 | },
|
391 | 549 | {
|
392 | 550 | "cell_type": "code",
|
393 |
| - "execution_count": 15, |
| 551 | + "execution_count": 23, |
394 | 552 | "metadata": {},
|
395 | 553 | "outputs": [
|
396 | 554 | {
|
|
551 | 709 | "name": "python",
|
552 | 710 | "nbconvert_exporter": "python",
|
553 | 711 | "pygments_lexer": "ipython3",
|
554 |
| - "version": "3.6.3" |
| 712 | + "version": "3.6.4" |
555 | 713 | }
|
556 | 714 | },
|
557 | 715 | "nbformat": 4,
|
|
0 commit comments