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

Skip to content

Multiple updates to agents.py (WumpusEnvironment and XYEnvironment) #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions agents.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 1,
"metadata": {
"collapsed": false,
"scrolled": true
Expand Down Expand Up @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -82,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 3,
"metadata": {
"collapsed": false
},
Expand All @@ -95,8 +95,8 @@
" pass\n",
"\n",
"class Park(Environment):\n",
" '''prints & return a list of things that are in our dog's location'''\n",
" def percept(self, agent):\n",
" '''prints & return a list of things that are in our agent's location'''\n",
" things = self.list_things_at(agent.location)\n",
" print(things)\n",
" return things\n",
Expand Down Expand Up @@ -151,7 +151,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 4,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -191,7 +191,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 5,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -230,17 +230,48 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"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?"
"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",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"class Park(XYEnvironment):\n",
" def percept(self, agent):\n",
" '''prints & return a list of things that are in our agent's location'''\n",
" things = self.list_things_at(agent.location)\n",
" print(things)\n",
" return things\n",
" \n",
" def execute_action(self, agent, action):\n",
" '''changes the state of the environment based on what the agent does.'''\n",
" if action == \"move down\":\n",
" agent.movedown()\n",
" elif action == \"eat\":\n",
" items = self.list_things_at(agent.location, tclass=Food)\n",
" if len(items) != 0:\n",
" if agent.eat(items[0]): #Have the dog pick eat the first item\n",
" self.delete_thing(items[0]) #Delete it from the Park after.\n",
" elif action == \"drink\":\n",
" items = self.list_things_at(agent.location, tclass=Water)\n",
" if len(items) != 0:\n",
" if agent.drink(items[0]): #Have the dog drink the first item\n",
" self.delete_thing(items[0]) #Delete it from the Park after.\n",
" \n",
" def is_done(self):\n",
" '''By default, we're done when we can't find a live agent, \n",
" but to prevent killing our cute dog, we will or it with when there is no more food or water'''\n",
" no_edibles = not any(isinstance(thing, Food) or isinstance(thing, Water) for thing in self.things)\n",
" dead_agents = not any(agent.is_alive() for agent in self.agents)\n",
" return dead_agents or no_edibles"
]
}
],
"metadata": {
Expand Down
Loading