Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc7f01d

Browse files
committed
Merge pull request aimacode#169 from tolusalako/master
Multiple updates to agents.py (WumpusEnvironment and XYEnvironment)
2 parents 81e3fea + 474fe87 commit cc7f01d

File tree

3 files changed

+470
-47
lines changed

3 files changed

+470
-47
lines changed

agents.ipynb

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 29,
18+
"execution_count": 1,
1919
"metadata": {
2020
"collapsed": false,
2121
"scrolled": true
@@ -43,7 +43,7 @@
4343
},
4444
{
4545
"cell_type": "code",
46-
"execution_count": 30,
46+
"execution_count": 2,
4747
"metadata": {
4848
"collapsed": false
4949
},
@@ -82,7 +82,7 @@
8282
},
8383
{
8484
"cell_type": "code",
85-
"execution_count": 43,
85+
"execution_count": 3,
8686
"metadata": {
8787
"collapsed": false
8888
},
@@ -95,8 +95,8 @@
9595
" pass\n",
9696
"\n",
9797
"class Park(Environment):\n",
98-
" '''prints & return a list of things that are in our dog's location'''\n",
9998
" def percept(self, agent):\n",
99+
" '''prints & return a list of things that are in our agent's location'''\n",
100100
" things = self.list_things_at(agent.location)\n",
101101
" print(things)\n",
102102
" return things\n",
@@ -151,7 +151,7 @@
151151
},
152152
{
153153
"cell_type": "code",
154-
"execution_count": 44,
154+
"execution_count": 4,
155155
"metadata": {
156156
"collapsed": false
157157
},
@@ -191,7 +191,7 @@
191191
},
192192
{
193193
"cell_type": "code",
194-
"execution_count": 45,
194+
"execution_count": 5,
195195
"metadata": {
196196
"collapsed": false
197197
},
@@ -230,17 +230,48 @@
230230
"cell_type": "markdown",
231231
"metadata": {},
232232
"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."
234236
]
235237
},
236238
{
237239
"cell_type": "code",
238-
"execution_count": null,
240+
"execution_count": 6,
239241
"metadata": {
240242
"collapsed": true
241243
},
242244
"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+
]
244275
}
245276
],
246277
"metadata": {

0 commit comments

Comments
 (0)