in data/envs/babyai/bot_agent.py [0:0]
def replan_before_action(self):
# Find the closest unseen position
_, unseen_pos, with_blockers = self.bot._shortest_path(
lambda pos, cell: not self.bot.vis_mask[pos], try_with_blockers=True
)
if unseen_pos:
self.bot.stack.append(GoNextToSubgoal(self.bot, unseen_pos, reason="Explore"))
return None
# Find the closest unlocked unopened door
def unopened_unlocked_door(pos, cell):
return cell and cell.type == "door" and not cell.is_locked and not cell.is_open
# Find the closest unopened door
def unopened_door(pos, cell):
return cell and cell.type == "door" and not cell.is_open
# Try to find an unlocked door first.
# We do this because otherwise, opening a locked door as
# a subgoal may try to open the same door for exploration,
# resulting in an infinite loop.
_, door_pos, _ = self.bot._shortest_path(unopened_unlocked_door, try_with_blockers=True)
if not door_pos:
# Try to find a locker door if an unlocked one is not available.
_, door_pos, _ = self.bot._shortest_path(unopened_door, try_with_blockers=True)
# Open the door
if door_pos:
door_obj = self.bot.mission.unwrapped.grid.get(*door_pos)
# If we are going to a locked door, there are two cases:
# - we already have the key, then we should not drop it
# - we don't have the key, in which case eventually we should drop it
got_the_key = self.carrying and self.carrying.type == "key" and self.carrying.color == door_obj.color
open_reason = "KeepKey" if door_obj.is_locked and got_the_key else None
self.bot.stack.pop()
self.bot.stack.append(OpenSubgoal(self.bot, reason=open_reason))
self.bot.stack.append(GoNextToSubgoal(self.bot, door_obj, reason="Open"))
return
assert False, "0nothing left to explore"