|
73 | 73 | },
|
74 | 74 | {
|
75 | 75 | "cell_type": "code",
|
76 |
| - "execution_count": 1, |
| 76 | + "execution_count": 38, |
77 | 77 | "metadata": {},
|
78 | 78 | "outputs": [],
|
79 | 79 | "source": [
|
|
90 | 90 | },
|
91 | 91 | {
|
92 | 92 | "cell_type": "code",
|
93 |
| - "execution_count": null, |
| 93 | + "execution_count": 39, |
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 | ]
|
99 | 237 | },
|
100 | 238 | {
|
101 | 239 | "cell_type": "code",
|
102 |
| - "execution_count": 3, |
| 240 | + "execution_count": 40, |
103 | 241 | "metadata": {},
|
104 | 242 | "outputs": [
|
105 | 243 | {
|
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 | ],
|
|
130 | 268 | },
|
131 | 269 | {
|
132 | 270 | "cell_type": "code",
|
133 |
| - "execution_count": 4, |
| 271 | + "execution_count": 41, |
134 | 272 | "metadata": {},
|
135 | 273 | "outputs": [],
|
136 | 274 | "source": [
|
|
147 | 285 | },
|
148 | 286 | {
|
149 | 287 | "cell_type": "code",
|
150 |
| - "execution_count": 5, |
| 288 | + "execution_count": 42, |
151 | 289 | "metadata": {},
|
152 | 290 | "outputs": [
|
153 | 291 | {
|
154 | 292 | "name": "stdout",
|
155 | 293 | "output_type": "stream",
|
156 | 294 | "text": [
|
157 |
| - "RandomVacuumAgent is located at (0, 0).\n" |
| 295 | + "RandomVacuumAgent is located at (1, 0).\n" |
158 | 296 | ]
|
159 | 297 | }
|
160 | 298 | ],
|
|
174 | 312 | },
|
175 | 313 | {
|
176 | 314 | "cell_type": "code",
|
177 |
| - "execution_count": 6, |
| 315 | + "execution_count": 43, |
178 | 316 | "metadata": {},
|
179 | 317 | "outputs": [
|
180 | 318 | {
|
181 | 319 | "name": "stdout",
|
182 | 320 | "output_type": "stream",
|
183 | 321 | "text": [
|
184 |
| - "State of the Environment: {(0, 0): 'Dirty', (1, 0): 'Clean'}.\n", |
185 |
| - "RandomVacuumAgent is located at (0, 0).\n" |
| 322 | + "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\n", |
| 323 | + "RandomVacuumAgent is located at (1, 0).\n" |
186 | 324 | ]
|
187 | 325 | }
|
188 | 326 | ],
|
|
208 | 346 | },
|
209 | 347 | {
|
210 | 348 | "cell_type": "code",
|
211 |
| - "execution_count": 7, |
| 349 | + "execution_count": 44, |
212 | 350 | "metadata": {},
|
213 | 351 | "outputs": [],
|
214 | 352 | "source": [
|
|
234 | 372 | },
|
235 | 373 | {
|
236 | 374 | "cell_type": "code",
|
237 |
| - "execution_count": 8, |
| 375 | + "execution_count": 45, |
238 | 376 | "metadata": {},
|
239 | 377 | "outputs": [],
|
240 | 378 | "source": [
|
|
251 | 389 | },
|
252 | 390 | {
|
253 | 391 | "cell_type": "code",
|
254 |
| - "execution_count": 9, |
| 392 | + "execution_count": 46, |
255 | 393 | "metadata": {},
|
256 | 394 | "outputs": [],
|
257 | 395 | "source": [
|
|
260 | 398 | },
|
261 | 399 | {
|
262 | 400 | "cell_type": "code",
|
263 |
| - "execution_count": 10, |
| 401 | + "execution_count": 47, |
264 | 402 | "metadata": {},
|
265 | 403 | "outputs": [
|
266 | 404 | {
|
|
280 | 418 | },
|
281 | 419 | {
|
282 | 420 | "cell_type": "code",
|
283 |
| - "execution_count": 11, |
| 421 | + "execution_count": 48, |
284 | 422 | "metadata": {},
|
285 | 423 | "outputs": [
|
286 | 424 | {
|
287 | 425 | "name": "stdout",
|
288 | 426 | "output_type": "stream",
|
289 | 427 | "text": [
|
290 |
| - "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n", |
291 |
| - "TableDrivenVacuumAgent is located at (0, 0).\n" |
| 428 | + "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Dirty'}.\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 | {
|
|
324 | 462 | },
|
325 | 463 | {
|
326 | 464 | "cell_type": "code",
|
327 |
| - "execution_count": 12, |
| 465 | + "execution_count": 49, |
328 | 466 | "metadata": {},
|
329 | 467 | "outputs": [],
|
330 | 468 | "source": [
|
|
341 | 479 | },
|
342 | 480 | {
|
343 | 481 | "cell_type": "code",
|
344 |
| - "execution_count": 13, |
| 482 | + "execution_count": 50, |
345 | 483 | "metadata": {},
|
346 | 484 | "outputs": [],
|
347 | 485 | "source": [
|
348 |
| - "# TODO: Implement these functions for two-dimensional environment\n", |
349 |
| - "# Interpret-input function for the two-state environment\n", |
350 |
| - "def interpret_input(percept):\n", |
351 |
| - " pass\n", |
352 | 486 | "\n",
|
353 |
| - "rules = None\n", |
| 487 | + "loc_A = (0, 0)\n", |
| 488 | + "loc_B = (1, 0)\n", |
| 489 | + "\n", |
| 490 | + "\"\"\"We change the simpleReflexAgentProgram so that it doesn't make use of the Rule class\"\"\"\n", |
| 491 | + "def SimpleReflexAgentProgram():\n", |
| 492 | + " \"\"\"This agent takes action based solely on the percept. [Figure 2.10]\"\"\"\n", |
| 493 | + " \n", |
| 494 | + " def program(percept):\n", |
| 495 | + " loc, status = percept\n", |
| 496 | + " return ('Suck' if status == 'Dirty' \n", |
| 497 | + " else'Right' if loc == loc_A \n", |
| 498 | + " else'Left')\n", |
| 499 | + " return program\n", |
354 | 500 | "\n",
|
355 |
| - "# Rule-match function for the two-state environment\n", |
356 |
| - "def rule_match(state, rule):\n", |
357 |
| - " for rule in rules:\n", |
358 |
| - " if rule.matches(state):\n", |
359 |
| - " return rule \n", |
360 | 501 | " \n",
|
361 | 502 | "# Create a simple reflex agent the two-state environment\n",
|
362 |
| - "simple_reflex_agent = ReflexVacuumAgent()" |
| 503 | + "program = SimpleReflexAgentProgram()\n", |
| 504 | + "simple_reflex_agent = Agent(program)" |
363 | 505 | ]
|
364 | 506 | },
|
365 | 507 | {
|
|
371 | 513 | },
|
372 | 514 | {
|
373 | 515 | "cell_type": "code",
|
374 |
| - "execution_count": 14, |
| 516 | + "execution_count": 51, |
375 | 517 | "metadata": {},
|
376 | 518 | "outputs": [
|
377 | 519 | {
|
|
390 | 532 | },
|
391 | 533 | {
|
392 | 534 | "cell_type": "code",
|
393 |
| - "execution_count": 15, |
| 535 | + "execution_count": 52, |
394 | 536 | "metadata": {},
|
395 | 537 | "outputs": [
|
396 | 538 | {
|
397 | 539 | "name": "stdout",
|
398 | 540 | "output_type": "stream",
|
399 | 541 | "text": [
|
400 | 542 | "State of the Environment: {(0, 0): 'Clean', (1, 0): 'Clean'}.\n",
|
401 |
| - "SimpleReflexVacuumAgent is located at (0, 0).\n" |
| 543 | + "SimpleReflexVacuumAgent is located at (1, 0).\n" |
402 | 544 | ]
|
403 | 545 | }
|
404 | 546 | ],
|
|
551 | 693 | "name": "python",
|
552 | 694 | "nbconvert_exporter": "python",
|
553 | 695 | "pygments_lexer": "ipython3",
|
554 |
| - "version": "3.6.3" |
| 696 | + "version": "3.6.4" |
555 | 697 | }
|
556 | 698 | },
|
557 | 699 | "nbformat": 4,
|
|
0 commit comments