Something went wrong on our end
Forked from
adrian_egli / neurips2020-flatland-starter-kit
60 commits behind the upstream repository.
-
MasterScrat authoredMasterScrat authored
deadlock_check.py 1.61 KiB
from flatland.core.grid.grid4_utils import get_new_position
from flatland.envs.agent_utils import RailAgentStatus
def check_if_all_blocked(env):
"""
Checks whether all the agents are blocked (full deadlock situation).
In that case it is pointless to keep running inference as no agent will be able to move.
:param env: current environment
:return:
"""
# First build a map of agents in each position
location_has_agent = {}
for agent in env.agents:
if agent.status in [RailAgentStatus.ACTIVE, RailAgentStatus.DONE] and agent.position:
location_has_agent[tuple(agent.position)] = 1
# Looks for any agent that can still move
for handle in env.get_agent_handles():
agent = env.agents[handle]
if agent.status == RailAgentStatus.READY_TO_DEPART:
agent_virtual_position = agent.initial_position
elif agent.status == RailAgentStatus.ACTIVE:
agent_virtual_position = agent.position
elif agent.status == RailAgentStatus.DONE:
agent_virtual_position = agent.target
else:
continue
possible_transitions = env.rail.get_transitions(*agent_virtual_position, agent.direction)
orientation = agent.direction
for branch_direction in [(orientation + i) % 4 for i in range(-1, 3)]:
if possible_transitions[branch_direction]:
new_position = get_new_position(agent_virtual_position, branch_direction)
if new_position not in location_has_agent:
return False
# No agent can move at all: full deadlock!
return True