Skip to content

fixed grabbing behaviour in agent #1148

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 5 commits into from
Mar 18, 2020
Merged
Changes from 1 commit
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
23 changes: 13 additions & 10 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,16 @@ def execute_action(self, agent, action):
agent.direction += Direction.L
elif action == 'Forward':
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
# elif action == 'Grab':
# things = [thing for thing in self.list_things_at(agent.location)
# if agent.can_grab(thing)]
# if things:
# agent.holding.append(things[0])
elif action == 'Grab':
things = [thing for thing in self.list_things_at(agent.location)]
if agent.can_grab(things[0]):
if things:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line if things should be on the top otherwise things[0] (accessed in the line above) would raise an exception!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that is true

agent.holding.append(things[0])
self.delete_thing(things[0])
elif action == 'Release':
if agent.holding:
agent.holding.pop()
dropped = agent.holding.pop()
self.add_thing(dropped, location=agent.location)

def default_location(self, thing):
location = self.random_location_inbounds()
Expand Down Expand Up @@ -569,10 +571,11 @@ def random_location_inbounds(self, exclude=None):
def delete_thing(self, thing):
"""Deletes thing, and everything it is holding (if thing is an agent)"""
if isinstance(thing, Agent):
for obj in thing.holding:
super().delete_thing(obj)
for obs in self.observers:
obs.thing_deleted(obj)
# for obj in thing.holding:
# super().delete_thing(obj)
# for obs in self.observers:
# obs.thing_deleted(obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason to comment these lines out. Even though the second line is redundant, the observers of the environment still need to be informed about each thing being deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we delete all of the agent holdings with del thing.holding, and thanks to python memory management. All instants of the agent's holding would vanish from memory even in the observer list. Tell me what you think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observers of the environment need to be updated about all the things getting deleted along with the agent. I have not seen observers used anywhere, so it depends upon the maintainers of the project to keep or exclude it!

del thing.holding

super().delete_thing(thing)
for obs in self.observers:
Expand Down