Skip to content

Commit 8ee4da2

Browse files
committed
Added direction class, suck, and move actions for Chap 2 exercises.
1 parent ada3954 commit 8ee4da2

File tree

1 file changed

+172
-30
lines changed

1 file changed

+172
-30
lines changed

adxyz_agents_chap2.ipynb

Lines changed: 172 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 204,
18+
"execution_count": 334,
1919
"metadata": {
2020
"collapsed": false,
2121
"scrolled": true
@@ -43,7 +43,7 @@
4343
},
4444
{
4545
"cell_type": "code",
46-
"execution_count": 205,
46+
"execution_count": 335,
4747
"metadata": {
4848
"collapsed": false
4949
},
@@ -75,7 +75,7 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 206,
78+
"execution_count": 336,
7979
"metadata": {
8080
"collapsed": false
8181
},
@@ -126,7 +126,7 @@
126126
},
127127
{
128128
"cell_type": "code",
129-
"execution_count": 207,
129+
"execution_count": 337,
130130
"metadata": {
131131
"collapsed": false
132132
},
@@ -195,7 +195,7 @@
195195
},
196196
{
197197
"cell_type": "code",
198-
"execution_count": 208,
198+
"execution_count": 338,
199199
"metadata": {
200200
"collapsed": false
201201
},
@@ -233,7 +233,7 @@
233233
},
234234
{
235235
"cell_type": "code",
236-
"execution_count": 209,
236+
"execution_count": 339,
237237
"metadata": {
238238
"collapsed": false
239239
},
@@ -261,7 +261,7 @@
261261
},
262262
{
263263
"cell_type": "code",
264-
"execution_count": 210,
264+
"execution_count": 340,
265265
"metadata": {
266266
"collapsed": true
267267
},
@@ -328,7 +328,7 @@
328328
},
329329
{
330330
"cell_type": "code",
331-
"execution_count": 211,
331+
"execution_count": 341,
332332
"metadata": {
333333
"collapsed": false
334334
},
@@ -346,36 +346,65 @@
346346
"# Need to override the percept method \n",
347347
" def percept(self, agent):\n",
348348
" '''By default, agent perceives things within a default radius.'''\n",
349-
" print (\"In override percept.\")\n",
349+
" print ()\n",
350+
" print (\"In adxyz_VacuumEnvironment - percept override:\")\n",
351+
" print (\"Self = \", self)\n",
352+
" print (\"Self.things = \", self.things)\n",
350353
" print (\"Agent ID = \", agent)\n",
351354
" print (\"Agent location = \", agent.location)\n",
352355
" print (\"Agent performance = \", agent.performance)\n",
353-
" print (\"Self = \", self)\n",
354-
" print (\"Self.things = \", self.things)\n",
356+
" \n",
355357
" for iThing in range(len(self.things)):\n",
356358
" if self.things[iThing].location==agent.location: #check location\n",
357359
" if self.things[iThing] != agent: # Don't return agent information\n",
358360
" if (isinstance(self.things[iThing], DirtClump)):\n",
359-
" print (\"A thing which is not agent, but dirt a clump = \", self.things[iThing] )\n",
361+
" print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing] )\n",
360362
" print (\"Location = \", self.things[iThing].location)\n",
361363
" return agent.location, \"DirtClump\"\n",
362364
" \n",
363365
" return agent.location, \"CleanSquare\" #Default, if we don't find a dirt clump.\n",
364366
" \n",
365367
"# Need to override the action method (and update performance measure.)\n",
366368
" def execute_action(self, agent, action):\n",
367-
" print(\"Execute_action:\")\n",
369+
" print ()\n",
370+
" print (\"In adxyz_VacuumEnvironment - execute_action override:\")\n",
368371
" print(\"self = \", self)\n",
369372
" print(\"agent = \", agent)\n",
370373
" print(\"action = \", action)\n",
371374
" print()\n",
372375
" if action==\"Suck\":\n",
373376
" print(\"Action-Suck\")\n",
374-
" print(\"Need to remove dirt clod\")\n",
377+
" print(\"Need to remove dirt clod at correct location\")\n",
378+
" for iThing in range(len(self.things)):\n",
379+
" if self.things[iThing].location==agent.location: #check location\n",
380+
" if (isinstance(self.things[iThing], DirtClump)): # Only clean dirt\n",
381+
" print (\"A thing which is not agent, but a dirt clump = \", self.things[iThing])\n",
382+
" print (\"Location of dirt clod = \", self.things[iThing].location)\n",
383+
" self.delete_thing(self.things[iThing])\n",
384+
" return\n",
385+
" \n",
375386
" elif action==\"MoveRight\":\n",
376387
" print(\"Action-MoveRight\")\n",
388+
" print(\"agent direction before MoveRight = \", agent.direction)\n",
389+
" print(\"agent location before MoveRight = \", agent.location)\n",
390+
" agent.bump = False\n",
391+
" agent.direction = agent.direction + Direction.R\n",
392+
" agent.direction = agent.direction + Direction.R\n",
393+
" agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))\n",
394+
" print(\"agent direction after MoveRight = \", agent.direction)\n",
395+
" print(\"agent location after MoveRight = \", agent.location)\n",
396+
" print()\n",
377397
" elif action==\"MoveLeft\":\n",
378398
" print(\"Action-MoveLeft\")\n",
399+
" print(\"agent direction before MoveLeft = \", agent.direction)\n",
400+
" print(\"agent location before MoveLeft = \", agent.location)\n",
401+
" agent.bump = False\n",
402+
" agent.direction = agent.direction + Direction.L\n",
403+
" agent.direction = agent.direction + Direction.L\n",
404+
" agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))\n",
405+
" print(\"agent direction after MoveLeft = \", agent.direction)\n",
406+
" print(\"agent location after MoveLeft = \", agent.location)\n",
407+
" print()\n",
379408
" elif action==\"DoNothing\":\n",
380409
" print(\"Action-DoNothing\")\n",
381410
" else:\n",
@@ -406,7 +435,7 @@
406435
},
407436
{
408437
"cell_type": "code",
409-
"execution_count": 212,
438+
"execution_count": 342,
410439
"metadata": {
411440
"collapsed": false
412441
},
@@ -423,9 +452,6 @@
423452
"# [(1,0),Dirty] -> Suck\n",
424453
"#\n",
425454
"\n",
426-
"#\n",
427-
"# Simple reflex cleaning program\n",
428-
"#\n",
429455
"def SimpleReflexClean(percept):\n",
430456
" print (\"SimpleReflexClean - percept = \", percept)\n",
431457
" print (\"percept[0] = \" , percept[0])\n",
@@ -444,12 +470,13 @@
444470
"\n",
445471
"# Instantiate a simple reflex vacuum agent\n",
446472
"class adxyz_SimpleReflexAgentVacuum(Agent):\n",
447-
" pass"
473+
" pass\n",
474+
"\n"
448475
]
449476
},
450477
{
451478
"cell_type": "code",
452-
"execution_count": 213,
479+
"execution_count": 343,
453480
"metadata": {
454481
"collapsed": false
455482
},
@@ -478,7 +505,7 @@
478505
},
479506
{
480507
"cell_type": "code",
481-
"execution_count": 214,
508+
"execution_count": 344,
482509
"metadata": {
483510
"collapsed": false
484511
},
@@ -500,23 +527,128 @@
500527
"currInitDirtLocation = (1, 0)\n",
501528
"\n",
502529
"Environment:\n",
503-
"[(<DirtClump>, 0), (<adxyz_SimpleReflexAgentVacuum>, 1)]\n",
530+
"[(<DirtClump>, 0), (<adxyz_SimpleReflexAgentVacuum>, 0)]\n",
531+
"\n",
532+
"step# = 0\n",
533+
"\n",
534+
"In adxyz_VacuumEnvironment - percept override:\n",
535+
"Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
536+
"Self.things = [<DirtClump>, <adxyz_SimpleReflexAgentVacuum>]\n",
537+
"Agent ID = <adxyz_SimpleReflexAgentVacuum>\n",
538+
"Agent location = (1, 0)\n",
539+
"Agent performance = 0\n",
540+
"A thing which is not agent, but a dirt clump = <DirtClump>\n",
541+
"Location = (1, 0)\n",
542+
"SimpleReflexClean - percept = ((1, 0), 'DirtClump')\n",
543+
"percept[0] = (1, 0)\n",
544+
"percept[1] = DirtClump\n",
545+
"\n",
546+
"In adxyz_VacuumEnvironment - execute_action override:\n",
547+
"self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
548+
"agent = <adxyz_SimpleReflexAgentVacuum>\n",
549+
"action = Suck\n",
550+
"\n",
551+
"Action-Suck\n",
552+
"Need to remove dirt clod at correct location\n",
553+
"A thing which is not agent, but a dirt clump = <DirtClump>\n",
554+
"Location of dirt clod = (1, 0)\n",
555+
"------\n",
556+
"------\n",
557+
"\n",
558+
"step# = 1\n",
559+
"\n",
560+
"In adxyz_VacuumEnvironment - percept override:\n",
561+
"Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
562+
"Self.things = [<adxyz_SimpleReflexAgentVacuum>]\n",
563+
"Agent ID = <adxyz_SimpleReflexAgentVacuum>\n",
564+
"Agent location = (1, 0)\n",
565+
"Agent performance = 0\n",
566+
"SimpleReflexClean - percept = ((1, 0), 'CleanSquare')\n",
567+
"percept[0] = (1, 0)\n",
568+
"percept[1] = CleanSquare\n",
569+
"\n",
570+
"In adxyz_VacuumEnvironment - execute_action override:\n",
571+
"self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
572+
"agent = <adxyz_SimpleReflexAgentVacuum>\n",
573+
"action = MoveLeft\n",
504574
"\n",
505-
"In override percept.\n",
575+
"Action-MoveLeft\n",
576+
"agent direction before MoveLeft = <agents.Direction object at 0x1051c87f0>\n",
577+
"agent location before MoveLeft = (1, 0)\n",
578+
"agent direction after MoveLeft = <agents.Direction object at 0x105304828>\n",
579+
"agent location after MoveLeft = (0, 0)\n",
580+
"\n",
581+
"------\n",
582+
"------\n",
583+
"\n",
584+
"step# = 2\n",
585+
"\n",
586+
"In adxyz_VacuumEnvironment - percept override:\n",
587+
"Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
588+
"Self.things = [<adxyz_SimpleReflexAgentVacuum>]\n",
506589
"Agent ID = <adxyz_SimpleReflexAgentVacuum>\n",
507590
"Agent location = (0, 0)\n",
508591
"Agent performance = 0\n",
509-
"Self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n",
510-
"Self.things = [<DirtClump>, <adxyz_SimpleReflexAgentVacuum>]\n",
511592
"SimpleReflexClean - percept = ((0, 0), 'CleanSquare')\n",
512593
"percept[0] = (0, 0)\n",
513594
"percept[1] = CleanSquare\n",
514-
"Execute_action:\n",
515-
"self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n",
595+
"\n",
596+
"In adxyz_VacuumEnvironment - execute_action override:\n",
597+
"self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
516598
"agent = <adxyz_SimpleReflexAgentVacuum>\n",
517599
"action = MoveRight\n",
518600
"\n",
519-
"Action-MoveRight\n"
601+
"Action-MoveRight\n",
602+
"agent direction before MoveRight = <agents.Direction object at 0x105304828>\n",
603+
"agent location before MoveRight = (0, 0)\n",
604+
"agent direction after MoveRight = <agents.Direction object at 0x105304208>\n",
605+
"agent location after MoveRight = (0, -1)\n",
606+
"\n",
607+
"------\n",
608+
"------\n",
609+
"\n",
610+
"step# = 3\n",
611+
"\n",
612+
"In adxyz_VacuumEnvironment - percept override:\n",
613+
"Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
614+
"Self.things = [<adxyz_SimpleReflexAgentVacuum>]\n",
615+
"Agent ID = <adxyz_SimpleReflexAgentVacuum>\n",
616+
"Agent location = (0, -1)\n",
617+
"Agent performance = 0\n",
618+
"SimpleReflexClean - percept = ((0, -1), 'CleanSquare')\n",
619+
"percept[0] = (0, -1)\n",
620+
"percept[1] = CleanSquare\n",
621+
"\n",
622+
"In adxyz_VacuumEnvironment - execute_action override:\n",
623+
"self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
624+
"agent = <adxyz_SimpleReflexAgentVacuum>\n",
625+
"action = DoNothing\n",
626+
"\n",
627+
"Action-DoNothing\n",
628+
"------\n",
629+
"------\n",
630+
"\n",
631+
"step# = 4\n",
632+
"\n",
633+
"In adxyz_VacuumEnvironment - percept override:\n",
634+
"Self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
635+
"Self.things = [<adxyz_SimpleReflexAgentVacuum>]\n",
636+
"Agent ID = <adxyz_SimpleReflexAgentVacuum>\n",
637+
"Agent location = (0, -1)\n",
638+
"Agent performance = 0\n",
639+
"SimpleReflexClean - percept = ((0, -1), 'CleanSquare')\n",
640+
"percept[0] = (0, -1)\n",
641+
"percept[1] = CleanSquare\n",
642+
"\n",
643+
"In adxyz_VacuumEnvironment - execute_action override:\n",
644+
"self = <__main__.adxyz_VacuumEnvironment object at 0x1051c8208>\n",
645+
"agent = <adxyz_SimpleReflexAgentVacuum>\n",
646+
"action = DoNothing\n",
647+
"\n",
648+
"Action-DoNothing\n",
649+
"------\n",
650+
"------\n",
651+
"\n"
520652
]
521653
}
522654
],
@@ -553,7 +685,13 @@
553685
"#\n",
554686
" myAgent=adxyz_SimpleReflexAgentVacuum()\n",
555687
" myAgent.program=SimpleReflexClean #Place the agent program here\n",
556-
" myVacEnv.add_thing(myAgent,location=(0,0))\n",
688+
"\n",
689+
"# Instantiate a direction object for 2D generality\n",
690+
" myAgent.direction = Direction(\"right\")\n",
691+
"### mySimpleReflexAgentVacuumDirection = Direction(myAgent.direction)\n",
692+
" \n",
693+
"# Add agent to environment\n",
694+
" myVacEnv.add_thing(myAgent,location=(1,0))\n",
557695
" print()\n",
558696
" print(\"Environment:\")\n",
559697
" print(myVacEnv.things_near(location=(0,0)))\n",
@@ -562,9 +700,13 @@
562700
"#\n",
563701
"# Now step the environment clock\n",
564702
"#\n",
565-
" numSteps = 1\n",
703+
" numSteps = 5\n",
566704
" for iStep in range(numSteps):\n",
705+
" print(\"step# = \", iStep)\n",
567706
" myVacEnv.step()\n",
707+
" print(\"------\")\n",
708+
" print(\"------\")\n",
709+
" print()\n",
568710
" \n",
569711
"#\n",
570712
"# End of script\n",

0 commit comments

Comments
 (0)