|
15 | 15 | }, |
16 | 16 | { |
17 | 17 | "cell_type": "code", |
18 | | - "execution_count": 29, |
| 18 | + "execution_count": 1, |
19 | 19 | "metadata": { |
20 | 20 | "collapsed": false, |
21 | 21 | "scrolled": true |
|
43 | 43 | }, |
44 | 44 | { |
45 | 45 | "cell_type": "code", |
46 | | - "execution_count": 30, |
| 46 | + "execution_count": 2, |
47 | 47 | "metadata": { |
48 | 48 | "collapsed": false |
49 | 49 | }, |
|
82 | 82 | }, |
83 | 83 | { |
84 | 84 | "cell_type": "code", |
85 | | - "execution_count": 43, |
| 85 | + "execution_count": 3, |
86 | 86 | "metadata": { |
87 | 87 | "collapsed": false |
88 | 88 | }, |
|
95 | 95 | " pass\n", |
96 | 96 | "\n", |
97 | 97 | "class Park(Environment):\n", |
98 | | - " '''prints & return a list of things that are in our dog's location'''\n", |
99 | 98 | " def percept(self, agent):\n", |
| 99 | + " '''prints & return a list of things that are in our agent's location'''\n", |
100 | 100 | " things = self.list_things_at(agent.location)\n", |
101 | 101 | " print(things)\n", |
102 | 102 | " return things\n", |
|
151 | 151 | }, |
152 | 152 | { |
153 | 153 | "cell_type": "code", |
154 | | - "execution_count": 44, |
| 154 | + "execution_count": 4, |
155 | 155 | "metadata": { |
156 | 156 | "collapsed": false |
157 | 157 | }, |
|
191 | 191 | }, |
192 | 192 | { |
193 | 193 | "cell_type": "code", |
194 | | - "execution_count": 45, |
| 194 | + "execution_count": 5, |
195 | 195 | "metadata": { |
196 | 196 | "collapsed": false |
197 | 197 | }, |
|
230 | 230 | "cell_type": "markdown", |
231 | 231 | "metadata": {}, |
232 | 232 | "source": [ |
233 | | - "That's how easy it is to implement an agent, its program, and environment. But that was a very simple case. What if our environment was 2-Dimentional instead of 1? And what if we had multiple agents?" |
| 233 | + "That's how easy it is to implement an agent, its program, and environment. But that was a very simple case. What if our environment was 2-Dimentional instead of 1? And what if we had multiple agents?\n", |
| 234 | + "\n", |
| 235 | + "To make our Park 2D, we will need to make it a subclass of <b>XYEnvironment</b> instead of Environment. Also, let's add a person to play fetch with the dog." |
234 | 236 | ] |
235 | 237 | }, |
236 | 238 | { |
237 | 239 | "cell_type": "code", |
238 | | - "execution_count": null, |
| 240 | + "execution_count": 6, |
239 | 241 | "metadata": { |
240 | 242 | "collapsed": true |
241 | 243 | }, |
242 | 244 | "outputs": [], |
243 | | - "source": [] |
| 245 | + "source": [ |
| 246 | + "class Park(XYEnvironment):\n", |
| 247 | + " def percept(self, agent):\n", |
| 248 | + " '''prints & return a list of things that are in our agent's location'''\n", |
| 249 | + " things = self.list_things_at(agent.location)\n", |
| 250 | + " print(things)\n", |
| 251 | + " return things\n", |
| 252 | + " \n", |
| 253 | + " def execute_action(self, agent, action):\n", |
| 254 | + " '''changes the state of the environment based on what the agent does.'''\n", |
| 255 | + " if action == \"move down\":\n", |
| 256 | + " agent.movedown()\n", |
| 257 | + " elif action == \"eat\":\n", |
| 258 | + " items = self.list_things_at(agent.location, tclass=Food)\n", |
| 259 | + " if len(items) != 0:\n", |
| 260 | + " if agent.eat(items[0]): #Have the dog pick eat the first item\n", |
| 261 | + " self.delete_thing(items[0]) #Delete it from the Park after.\n", |
| 262 | + " elif action == \"drink\":\n", |
| 263 | + " items = self.list_things_at(agent.location, tclass=Water)\n", |
| 264 | + " if len(items) != 0:\n", |
| 265 | + " if agent.drink(items[0]): #Have the dog drink the first item\n", |
| 266 | + " self.delete_thing(items[0]) #Delete it from the Park after.\n", |
| 267 | + " \n", |
| 268 | + " def is_done(self):\n", |
| 269 | + " '''By default, we're done when we can't find a live agent, \n", |
| 270 | + " but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n", |
| 271 | + " no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n", |
| 272 | + " dead_agents = not any(agent.is_alive() for agent in self.agents)\n", |
| 273 | + " return dead_agents or no_edibles" |
| 274 | + ] |
244 | 275 | } |
245 | 276 | ], |
246 | 277 | "metadata": { |
|
0 commit comments