Skip to content

Commit 92710c5

Browse files
nouman-10norvig
authored andcommitted
Removed all Rule mechanism (aimacode#859)
1 parent 954f50c commit 92710c5

File tree

1 file changed

+178
-36
lines changed

1 file changed

+178
-36
lines changed

vacuum_world.ipynb

Lines changed: 178 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"cell_type": "code",
76-
"execution_count": 1,
76+
"execution_count": 38,
7777
"metadata": {},
7878
"outputs": [],
7979
"source": [
@@ -90,23 +90,161 @@
9090
},
9191
{
9292
"cell_type": "code",
93-
"execution_count": null,
93+
"execution_count": 39,
9494
"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\">&quot;&quot;&quot;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&#39;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.&quot;&quot;&quot;</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\">&#39;Clean&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Dirty&#39;</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\">&#39;Clean&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Dirty&#39;</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\">&quot;&quot;&quot;Returns the agent&#39;s location, and the location status (Dirty/Clean).&quot;&quot;&quot;</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\">&quot;&quot;&quot;Change agent&#39;s location and/or location&#39;s status; track performance.</span>\n",
207+
"<span class=\"sd\"> Score 10 for each dirt cleaned; -1 for each move.&quot;&quot;&quot;</span>\n",
208+
" <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",
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\">&#39;Left&#39;</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\">&#39;Suck&#39;</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\">&#39;Dirty&#39;</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\">&#39;Clean&#39;</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\">&quot;&quot;&quot;Agents start in either location at random.&quot;&quot;&quot;</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+
],
96234
"source": [
97235
"psource(TrivialVacuumEnvironment)"
98236
]
99237
},
100238
{
101239
"cell_type": "code",
102-
"execution_count": 3,
240+
"execution_count": 40,
103241
"metadata": {},
104242
"outputs": [
105243
{
106244
"name": "stdout",
107245
"output_type": "stream",
108246
"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"
110248
]
111249
}
112250
],
@@ -130,7 +268,7 @@
130268
},
131269
{
132270
"cell_type": "code",
133-
"execution_count": 4,
271+
"execution_count": 41,
134272
"metadata": {},
135273
"outputs": [],
136274
"source": [
@@ -147,14 +285,14 @@
147285
},
148286
{
149287
"cell_type": "code",
150-
"execution_count": 5,
288+
"execution_count": 42,
151289
"metadata": {},
152290
"outputs": [
153291
{
154292
"name": "stdout",
155293
"output_type": "stream",
156294
"text": [
157-
"RandomVacuumAgent is located at (0, 0).\n"
295+
"RandomVacuumAgent is located at (1, 0).\n"
158296
]
159297
}
160298
],
@@ -174,15 +312,15 @@
174312
},
175313
{
176314
"cell_type": "code",
177-
"execution_count": 6,
315+
"execution_count": 43,
178316
"metadata": {},
179317
"outputs": [
180318
{
181319
"name": "stdout",
182320
"output_type": "stream",
183321
"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"
186324
]
187325
}
188326
],
@@ -208,7 +346,7 @@
208346
},
209347
{
210348
"cell_type": "code",
211-
"execution_count": 7,
349+
"execution_count": 44,
212350
"metadata": {},
213351
"outputs": [],
214352
"source": [
@@ -234,7 +372,7 @@
234372
},
235373
{
236374
"cell_type": "code",
237-
"execution_count": 8,
375+
"execution_count": 45,
238376
"metadata": {},
239377
"outputs": [],
240378
"source": [
@@ -251,7 +389,7 @@
251389
},
252390
{
253391
"cell_type": "code",
254-
"execution_count": 9,
392+
"execution_count": 46,
255393
"metadata": {},
256394
"outputs": [],
257395
"source": [
@@ -260,7 +398,7 @@
260398
},
261399
{
262400
"cell_type": "code",
263-
"execution_count": 10,
401+
"execution_count": 47,
264402
"metadata": {},
265403
"outputs": [
266404
{
@@ -280,15 +418,15 @@
280418
},
281419
{
282420
"cell_type": "code",
283-
"execution_count": 11,
421+
"execution_count": 48,
284422
"metadata": {},
285423
"outputs": [
286424
{
287425
"name": "stdout",
288426
"output_type": "stream",
289427
"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"
292430
]
293431
}
294432
],
@@ -312,7 +450,7 @@
312450
"\n",
313451
"The schematic diagram shown in **Figure 2.9** of the book will make this more clear:\n",
314452
"\n",
315-
"<img src=\"/files/images/simple_reflex_agent.jpg\">"
453+
"\"![simple reflex agent](images/simple_reflex_agent.jpg)\""
316454
]
317455
},
318456
{
@@ -324,7 +462,7 @@
324462
},
325463
{
326464
"cell_type": "code",
327-
"execution_count": 12,
465+
"execution_count": 49,
328466
"metadata": {},
329467
"outputs": [],
330468
"source": [
@@ -341,25 +479,29 @@
341479
},
342480
{
343481
"cell_type": "code",
344-
"execution_count": 13,
482+
"execution_count": 50,
345483
"metadata": {},
346484
"outputs": [],
347485
"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",
352486
"\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",
354500
"\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",
360501
" \n",
361502
"# 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)"
363505
]
364506
},
365507
{
@@ -371,7 +513,7 @@
371513
},
372514
{
373515
"cell_type": "code",
374-
"execution_count": 14,
516+
"execution_count": 51,
375517
"metadata": {},
376518
"outputs": [
377519
{
@@ -390,15 +532,15 @@
390532
},
391533
{
392534
"cell_type": "code",
393-
"execution_count": 15,
535+
"execution_count": 52,
394536
"metadata": {},
395537
"outputs": [
396538
{
397539
"name": "stdout",
398540
"output_type": "stream",
399541
"text": [
400542
"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"
402544
]
403545
}
404546
],
@@ -551,7 +693,7 @@
551693
"name": "python",
552694
"nbconvert_exporter": "python",
553695
"pygments_lexer": "ipython3",
554-
"version": "3.6.3"
696+
"version": "3.6.4"
555697
}
556698
},
557699
"nbformat": 4,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy