Skip to content
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

fixed grabbing behaviour in agent #1148

Merged
merged 5 commits into from
Mar 18, 2020
Merged
Changes from 2 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
33 changes: 14 additions & 19 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,17 @@ 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(thing)]
if things:
agent.holding.append(things[0])
print("Grabbing ", things[0].__class__.__name__)
self.delete_thing(things[0])
elif action == 'Release':
if agent.holding:
agent.holding.pop()
dropped = agent.holding.pop()
print("Dropping ", dropped.__class__.__name__)
self.add_thing(dropped, location=agent.location)

def default_location(self, thing):
location = self.random_location_inbounds()
Expand Down Expand Up @@ -569,10 +572,7 @@ 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)
del thing.holding

super().delete_thing(thing)
for obs in self.observers:
Expand Down Expand Up @@ -967,21 +967,16 @@ def execute_action(self, agent, action):

agent.bump = False
if action == 'TurnRight':
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the if statement could be streamlined if we rewrote it as:

if action in ['TurnRight', 'TurnLeft', 'Forward']:
            super().execute_action(agent,action)
            agent.performance -= 1
elif action == 'Grab':
            super().execute_action(agent,action)

I think the above looks a bit better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had that in mind, but I thought to myself that being more articulate and reasonably redundant in code would help the student retaining the information that is my case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But actually seeing it, it really didn't help much.

agent.direction += Direction.R
super().execute_action(agent,action)
agent.performance -= 1
elif action == 'TurnLeft':
agent.direction += Direction.L
super().execute_action(agent,action)
agent.performance -= 1
elif action == 'Forward':
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
super().execute_action(agent,action)
agent.performance -= 1
elif action == 'Grab':
things = [thing for thing in self.list_things_at(agent.location)
if agent.can_grab(thing)]
if len(things):
print("Grabbing", things[0].__class__.__name__)
if len(things):
agent.holding.append(things[0])
super().execute_action(agent,action)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You need a space after the comma, to comply with the overall project style.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

agent.performance -= 1
elif action == 'Climb':
if agent.location == (1, 1): # Agent can only climb out of (1,1)
Expand Down