FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fixed grabbing behaviour in agent by omar-3 · Pull Request #1148 · aimacode/aima-python · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
45 changes: 13 additions & 32 deletions agents.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
"""

# TODO
# Implement grabbing correctly.
# When an object is grabbed, does it still have a location?
# What if it is released?
# What if the grabbed or the grabber is deleted?
# What if the grabber moves?
# Speed control in GUI does not have any effect -- fix it.

from utils import distance_squared, turn_heading
Expand Down Expand Up @@ -510,14 +505,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 +567,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 @@ -964,24 +959,10 @@ def execute_action(self, agent, action):

if isinstance(agent, Explorer) and self.in_danger(agent):
return

agent.bump = False
if action == 'TurnRight':
agent.direction += Direction.R
agent.performance -= 1
elif action == 'TurnLeft':
agent.direction += Direction.L
agent.performance -= 1
elif action == 'Forward':
agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location))
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])
if action in ['TurnRight', 'TurnLeft', 'Forward', 'Grab']:
super().execute_action(agent, action)
agent.performance -= 1
elif action == 'Climb':
if agent.location == (1, 1): # Agent can only climb out of (1,1)
Expand Down

Back | FazBrowse Home | New Git URL