|
15 | 15 | }, |
16 | 16 | { |
17 | 17 | "cell_type": "code", |
18 | | - "execution_count": 204, |
| 18 | + "execution_count": 334, |
19 | 19 | "metadata": { |
20 | 20 | "collapsed": false, |
21 | 21 | "scrolled": true |
|
43 | 43 | }, |
44 | 44 | { |
45 | 45 | "cell_type": "code", |
46 | | - "execution_count": 205, |
| 46 | + "execution_count": 335, |
47 | 47 | "metadata": { |
48 | 48 | "collapsed": false |
49 | 49 | }, |
|
75 | 75 | }, |
76 | 76 | { |
77 | 77 | "cell_type": "code", |
78 | | - "execution_count": 206, |
| 78 | + "execution_count": 336, |
79 | 79 | "metadata": { |
80 | 80 | "collapsed": false |
81 | 81 | }, |
|
126 | 126 | }, |
127 | 127 | { |
128 | 128 | "cell_type": "code", |
129 | | - "execution_count": 207, |
| 129 | + "execution_count": 337, |
130 | 130 | "metadata": { |
131 | 131 | "collapsed": false |
132 | 132 | }, |
|
195 | 195 | }, |
196 | 196 | { |
197 | 197 | "cell_type": "code", |
198 | | - "execution_count": 208, |
| 198 | + "execution_count": 338, |
199 | 199 | "metadata": { |
200 | 200 | "collapsed": false |
201 | 201 | }, |
|
233 | 233 | }, |
234 | 234 | { |
235 | 235 | "cell_type": "code", |
236 | | - "execution_count": 209, |
| 236 | + "execution_count": 339, |
237 | 237 | "metadata": { |
238 | 238 | "collapsed": false |
239 | 239 | }, |
|
261 | 261 | }, |
262 | 262 | { |
263 | 263 | "cell_type": "code", |
264 | | - "execution_count": 210, |
| 264 | + "execution_count": 340, |
265 | 265 | "metadata": { |
266 | 266 | "collapsed": true |
267 | 267 | }, |
|
328 | 328 | }, |
329 | 329 | { |
330 | 330 | "cell_type": "code", |
331 | | - "execution_count": 211, |
| 331 | + "execution_count": 341, |
332 | 332 | "metadata": { |
333 | 333 | "collapsed": false |
334 | 334 | }, |
|
346 | 346 | "# Need to override the percept method \n", |
347 | 347 | " def percept(self, agent):\n", |
348 | 348 | " '''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", |
350 | 353 | " print (\"Agent ID = \", agent)\n", |
351 | 354 | " print (\"Agent location = \", agent.location)\n", |
352 | 355 | " print (\"Agent performance = \", agent.performance)\n", |
353 | | - " print (\"Self = \", self)\n", |
354 | | - " print (\"Self.things = \", self.things)\n", |
| 356 | + " \n", |
355 | 357 | " for iThing in range(len(self.things)):\n", |
356 | 358 | " if self.things[iThing].location==agent.location: #check location\n", |
357 | 359 | " if self.things[iThing] != agent: # Don't return agent information\n", |
358 | 360 | " 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", |
360 | 362 | " print (\"Location = \", self.things[iThing].location)\n", |
361 | 363 | " return agent.location, \"DirtClump\"\n", |
362 | 364 | " \n", |
363 | 365 | " return agent.location, \"CleanSquare\" #Default, if we don't find a dirt clump.\n", |
364 | 366 | " \n", |
365 | 367 | "# Need to override the action method (and update performance measure.)\n", |
366 | 368 | " def execute_action(self, agent, action):\n", |
367 | | - " print(\"Execute_action:\")\n", |
| 369 | + " print ()\n", |
| 370 | + " print (\"In adxyz_VacuumEnvironment - execute_action override:\")\n", |
368 | 371 | " print(\"self = \", self)\n", |
369 | 372 | " print(\"agent = \", agent)\n", |
370 | 373 | " print(\"action = \", action)\n", |
371 | 374 | " print()\n", |
372 | 375 | " if action==\"Suck\":\n", |
373 | 376 | " 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", |
375 | 386 | " elif action==\"MoveRight\":\n", |
376 | 387 | " 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", |
377 | 397 | " elif action==\"MoveLeft\":\n", |
378 | 398 | " 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", |
379 | 408 | " elif action==\"DoNothing\":\n", |
380 | 409 | " print(\"Action-DoNothing\")\n", |
381 | 410 | " else:\n", |
|
406 | 435 | }, |
407 | 436 | { |
408 | 437 | "cell_type": "code", |
409 | | - "execution_count": 212, |
| 438 | + "execution_count": 342, |
410 | 439 | "metadata": { |
411 | 440 | "collapsed": false |
412 | 441 | }, |
|
423 | 452 | "# [(1,0),Dirty] -> Suck\n", |
424 | 453 | "#\n", |
425 | 454 | "\n", |
426 | | - "#\n", |
427 | | - "# Simple reflex cleaning program\n", |
428 | | - "#\n", |
429 | 455 | "def SimpleReflexClean(percept):\n", |
430 | 456 | " print (\"SimpleReflexClean - percept = \", percept)\n", |
431 | 457 | " print (\"percept[0] = \" , percept[0])\n", |
|
444 | 470 | "\n", |
445 | 471 | "# Instantiate a simple reflex vacuum agent\n", |
446 | 472 | "class adxyz_SimpleReflexAgentVacuum(Agent):\n", |
447 | | - " pass" |
| 473 | + " pass\n", |
| 474 | + "\n" |
448 | 475 | ] |
449 | 476 | }, |
450 | 477 | { |
451 | 478 | "cell_type": "code", |
452 | | - "execution_count": 213, |
| 479 | + "execution_count": 343, |
453 | 480 | "metadata": { |
454 | 481 | "collapsed": false |
455 | 482 | }, |
|
478 | 505 | }, |
479 | 506 | { |
480 | 507 | "cell_type": "code", |
481 | | - "execution_count": 214, |
| 508 | + "execution_count": 344, |
482 | 509 | "metadata": { |
483 | 510 | "collapsed": false |
484 | 511 | }, |
|
500 | 527 | "currInitDirtLocation = (1, 0)\n", |
501 | 528 | "\n", |
502 | 529 | "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", |
504 | 574 | "\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", |
506 | 589 | "Agent ID = <adxyz_SimpleReflexAgentVacuum>\n", |
507 | 590 | "Agent location = (0, 0)\n", |
508 | 591 | "Agent performance = 0\n", |
509 | | - "Self = <__main__.adxyz_VacuumEnvironment object at 0x10531f2e8>\n", |
510 | | - "Self.things = [<DirtClump>, <adxyz_SimpleReflexAgentVacuum>]\n", |
511 | 592 | "SimpleReflexClean - percept = ((0, 0), 'CleanSquare')\n", |
512 | 593 | "percept[0] = (0, 0)\n", |
513 | 594 | "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", |
516 | 598 | "agent = <adxyz_SimpleReflexAgentVacuum>\n", |
517 | 599 | "action = MoveRight\n", |
518 | 600 | "\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" |
520 | 652 | ] |
521 | 653 | } |
522 | 654 | ], |
|
553 | 685 | "#\n", |
554 | 686 | " myAgent=adxyz_SimpleReflexAgentVacuum()\n", |
555 | 687 | " 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", |
557 | 695 | " print()\n", |
558 | 696 | " print(\"Environment:\")\n", |
559 | 697 | " print(myVacEnv.things_near(location=(0,0)))\n", |
|
562 | 700 | "#\n", |
563 | 701 | "# Now step the environment clock\n", |
564 | 702 | "#\n", |
565 | | - " numSteps = 1\n", |
| 703 | + " numSteps = 5\n", |
566 | 704 | " for iStep in range(numSteps):\n", |
| 705 | + " print(\"step# = \", iStep)\n", |
567 | 706 | " myVacEnv.step()\n", |
| 707 | + " print(\"------\")\n", |
| 708 | + " print(\"------\")\n", |
| 709 | + " print()\n", |
568 | 710 | " \n", |
569 | 711 | "#\n", |
570 | 712 | "# End of script\n", |
|
0 commit comments