in data/envs/babyai/bot_agent.py [0:0]
def _find_obj_pos(self, obj_desc, adjacent=False):
"""Find the position of the closest visible object matching a given description."""
assert len(obj_desc.obj_set) > 0
best_distance_to_obj = 999
best_pos = None
best_obj = None
for i in range(len(obj_desc.obj_set)):
if obj_desc.obj_set[i].type == "wall":
continue
try:
if obj_desc.obj_set[i] == self.mission.unwrapped.carrying:
continue
obj_pos = obj_desc.obj_poss[i]
if self.vis_mask[obj_pos]:
shortest_path_to_obj, _, with_blockers = self._shortest_path(
lambda pos, cell: pos == obj_pos, try_with_blockers=True
)
assert shortest_path_to_obj is not None
distance_to_obj = len(shortest_path_to_obj)
if with_blockers:
# The distance should take into account the steps necessary
# to unblock the way. Instead of computing it exactly,
# we can use a lower bound on this number of steps
# which is 4 when the agent is not holding anything
# (pick, turn, drop, turn back
# and 7 if the agent is carrying something
# (turn, drop, turn back, pick,
# turn to other direction, drop, turn back)
distance_to_obj = len(shortest_path_to_obj) + (7 if self.mission.unwrapped.carrying else 4)
# If we looking for a door and we are currently in that cell
# that contains the door, it will take us at least 2
# (3 if `adjacent == True`) steps to reach the goal.`
if distance_to_obj == 0:
distance_to_obj = 3 if adjacent else 2
# If what we want is to face a location that is adjacent to an object,
# and if we are already right next to this object,
# then we should not prefer this object to those at distance 2
if adjacent and distance_to_obj == 1:
distance_to_obj = 3
if distance_to_obj < best_distance_to_obj:
best_distance_to_obj = distance_to_obj
best_pos = obj_pos
best_obj = obj_desc.obj_set[i]
except IndexError:
# Suppose we are tracking red keys, and we just used a red key to open a door,
# then for the last i, accessing obj_desc.obj_poss[i] will raise an IndexError
# -> Solution: Not care about that red key we used to open the door
pass
return best_obj, best_pos