|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
| 7 | + "\n", |
7 | 8 | "# AGENT #\n", |
8 | 9 | "\n", |
9 | 10 | "An agent, as defined in 2.1 is anything that can perceive its <b>environment</b> through sensors, and act upon that environment through actuators based on its <b>agent program</b>. This can be a dog, robot, or even you. As long as you can perceive the environment and act on it, you are an agent. This notebook will explain how to implement a simple agent, create an environment, and create a program that helps the agent act on the environment based on its percepts.\n", |
|
17 | 18 | "cell_type": "code", |
18 | 19 | "execution_count": 1, |
19 | 20 | "metadata": { |
| 21 | + "collapsed": true, |
20 | 22 | "scrolled": true |
21 | 23 | }, |
22 | 24 | "outputs": [], |
|
80 | 82 | { |
81 | 83 | "cell_type": "code", |
82 | 84 | "execution_count": 3, |
83 | | - "metadata": {}, |
| 85 | + "metadata": { |
| 86 | + "collapsed": true |
| 87 | + }, |
84 | 88 | "outputs": [], |
85 | 89 | "source": [ |
86 | 90 | "class Food(Thing):\n", |
|
151 | 155 | { |
152 | 156 | "cell_type": "code", |
153 | 157 | "execution_count": 4, |
154 | | - "metadata": {}, |
| 158 | + "metadata": { |
| 159 | + "collapsed": true |
| 160 | + }, |
155 | 161 | "outputs": [], |
156 | 162 | "source": [ |
157 | 163 | "class BlindDog(Agent):\n", |
|
163 | 169 | " def eat(self, thing):\n", |
164 | 170 | " '''returns True upon success or False otherwise'''\n", |
165 | 171 | " if isinstance(thing, Food):\n", |
166 | | - " #print(\"Dog: Ate food at {}.\".format(self.location))\n", |
167 | 172 | " return True\n", |
168 | 173 | " return False\n", |
169 | 174 | " \n", |
170 | 175 | " def drink(self, thing):\n", |
171 | 176 | " ''' returns True upon success or False otherwise'''\n", |
172 | 177 | " if isinstance(thing, Water):\n", |
173 | | - " #print(\"Dog: Drank water at {}.\".format(self.location))\n", |
174 | 178 | " return True\n", |
175 | 179 | " return False\n", |
176 | 180 | " \n", |
|
456 | 460 | { |
457 | 461 | "cell_type": "code", |
458 | 462 | "execution_count": 10, |
459 | | - "metadata": {}, |
| 463 | + "metadata": { |
| 464 | + "collapsed": true |
| 465 | + }, |
460 | 466 | "outputs": [], |
461 | 467 | "source": [ |
462 | 468 | "from random import choice\n", |
|
487 | 493 | " def eat(self, thing):\n", |
488 | 494 | " '''returns True upon success or False otherwise'''\n", |
489 | 495 | " if isinstance(thing, Food):\n", |
490 | | - " #print(\"Dog: Ate food at {}.\".format(self.location))\n", |
491 | 496 | " return True\n", |
492 | 497 | " return False\n", |
493 | 498 | " \n", |
494 | 499 | " def drink(self, thing):\n", |
495 | 500 | " ''' returns True upon success or False otherwise'''\n", |
496 | 501 | " if isinstance(thing, Water):\n", |
497 | | - " #print(\"Dog: Drank water at {}.\".format(self.location))\n", |
498 | 502 | " return True\n", |
499 | 503 | " return False\n", |
500 | 504 | " \n", |
|
546 | 550 | " if action == 'turnright':\n", |
547 | 551 | " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", |
548 | 552 | " agent.turn(Direction.R)\n", |
549 | | - " #print('now facing {}'.format(agent.direction.direction))\n", |
550 | 553 | " elif action == 'turnleft':\n", |
551 | 554 | " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", |
552 | 555 | " agent.turn(Direction.L)\n", |
553 | | - " #print('now facing {}'.format(agent.direction.direction))\n", |
554 | 556 | " elif action == 'moveforward':\n", |
555 | 557 | " loc = copy.deepcopy(agent.location) # find out the target location\n", |
556 | 558 | " if agent.direction.direction == Direction.R:\n", |
|
561 | 563 | " loc[1] += 1\n", |
562 | 564 | " elif agent.direction.direction == Direction.U:\n", |
563 | 565 | " loc[1] -= 1\n", |
564 | | - " #print('{} at {} facing {}'.format(agent, loc, agent.direction.direction))\n", |
565 | 566 | " if self.is_inbounds(loc):# move only if the target is a valid location\n", |
566 | 567 | " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", |
567 | 568 | " agent.moveforward()\n", |
|
664 | 665 | " if action == 'turnright':\n", |
665 | 666 | " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", |
666 | 667 | " agent.turn(Direction.R)\n", |
667 | | - " #print('now facing {}'.format(agent.direction.direction))\n", |
668 | 668 | " elif action == 'turnleft':\n", |
669 | 669 | " print('{} decided to {} at location: {}'.format(str(agent)[1:-1], action, agent.location))\n", |
670 | 670 | " agent.turn(Direction.L)\n", |
671 | | - " #print('now facing {}'.format(agent.direction.direction))\n", |
672 | 671 | " elif action == 'moveforward':\n", |
673 | 672 | " loc = copy.deepcopy(agent.location) # find out the target location\n", |
674 | 673 | " if agent.direction.direction == Direction.R:\n", |
|
679 | 678 | " loc[1] += 1\n", |
680 | 679 | " elif agent.direction.direction == Direction.U:\n", |
681 | 680 | " loc[1] -= 1\n", |
682 | | - " #print('{} at {} facing {}'.format(agent, loc, agent.direction.direction))\n", |
683 | 681 | " if self.is_inbounds(loc):# move only if the target is a valid location\n", |
684 | 682 | " print('{} decided to move {}wards at location: {}'.format(str(agent)[1:-1], agent.direction.direction, agent.location))\n", |
685 | 683 | " agent.moveforward()\n", |
|
1157 | 1155 | { |
1158 | 1156 | "cell_type": "code", |
1159 | 1157 | "execution_count": 4, |
1160 | | - "metadata": {}, |
| 1158 | + "metadata": { |
| 1159 | + "collapsed": true |
| 1160 | + }, |
1161 | 1161 | "outputs": [], |
1162 | 1162 | "source": [ |
1163 | 1163 | "from ipythonblocks import BlockGrid\n", |
|
1252 | 1252 | "name": "python", |
1253 | 1253 | "nbconvert_exporter": "python", |
1254 | 1254 | "pygments_lexer": "ipython3", |
1255 | | - "version": "3.5.4rc1" |
| 1255 | + "version": "3.6.4" |
1256 | 1256 | } |
1257 | 1257 | }, |
1258 | 1258 | "nbformat": 4, |
|
0 commit comments