Commit e61019c4 authored by MasterScrat's avatar MasterScrat
Browse files

Added deadlock check, not loading any checkpoint for now

parent ba096011
......@@ -10,6 +10,7 @@ from flatland.evaluators.client import FlatlandRemoteClient
from flatland.envs.predictions import ShortestPathPredictorForRailEnv
from reinforcement_learning.observations import TreeObsForRailEnv
from utils.deadlock_check import check_if_all_blocked
base_dir = Path(__file__).resolve().parent.parent
sys.path.append(str(base_dir))
......@@ -65,7 +66,7 @@ if __name__ == "__main__":
action_size = 5
policy = DDDQNPolicy(state_size, action_size, Namespace(**parameters), evaluation_mode=True)
policy.qnetwork_local = torch.load(checkpoint)
#policy.qnetwork_local = torch.load(checkpoint)
# Controller
pool = Pool()
......@@ -112,15 +113,19 @@ if __name__ == "__main__":
#####################################################################
time_start = time.time()
action = rl_controller(observation, number_of_agents)
time_taken = time.time() - time_start
print(steps, time_taken)
time_taken_by_controller.append(time_taken)
agent_time = time.time() - time_start
time_taken_by_controller.append(agent_time)
time_start = time.time()
observation, all_rewards, done, info = remote_client.env_step(action)
steps += 1
time_taken = time.time() - time_start
time_taken_per_step.append(time_taken)
step_time = time.time() - time_start
time_taken_per_step.append(step_time)
print("Step {}\t Agent time {:.3f}\t Step time {:.3f}".format(str(steps).zfill(3), agent_time, step_time))
if check_if_all_blocked(local_env):
print("DEADLOCKED!!")
if done['__all__']:
print("Reward : ", sum(list(all_rewards.values())))
......
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.
FIXME still experimental!
: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
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment