From 6939b876fd86405cb8575aae3e1978ff47558c97 Mon Sep 17 00:00:00 2001
From: MLErik <baerenjesus@gmail.com>
Date: Thu, 31 Oct 2019 11:59:30 -0400
Subject: [PATCH] fixed comment by christian. Added function to draw breaking
 agent

---
 flatland/envs/rail_env.py | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/flatland/envs/rail_env.py b/flatland/envs/rail_env.py
index f284f3ac..fc73cd6c 100644
--- a/flatland/envs/rail_env.py
+++ b/flatland/envs/rail_env.py
@@ -421,26 +421,27 @@ class RailEnv(Environment):
                 agent.moving = agent.malfunction_data['moving_before_malfunction']
                 continue
 
+    def _draw_malfunctioning_agent(self):
+        # Select only from active agents
+        breaking_agent_idx = self.np_random.choice(self.active_agents)
+        breaking_agent = self.agents[breaking_agent_idx]
+        return breaking_agent
+
     def _malfunction(self, rate):
         """
         Malfunction generator that breaks agents at a given rate. It does randomly chose agent to break during the run
 
         """
         if self.np_random.rand() < self._malfunction_prob(rate, len(self.active_agents)):
-            # Select only from agents that are not done yet
-            breaking_agent_idx = self.np_random.choice(self.active_agents)
-            breaking_agent = self.agents[breaking_agent_idx]
 
+            breaking_agent = self._draw_malfunctioning_agent()
             # We assume that less then half of the active agents should be broken at MOST.
             # Therefore we only try that many times before ignoring the malfunction
-
             tries = 0
             max_tries = 0.5 * len(self.active_agents)
-
             # Look for a functioning active agent
             while breaking_agent.malfunction_data['malfunction'] > 0 and tries < max_tries:
-                breaking_agent_idx = self.np_random.choice(self.active_agents)
-                breaking_agent = self.agents[breaking_agent_idx]
+                breaking_agent = self._draw_malfunctioning_agent()
                 tries += 1
 
             # If we did not manage to find a functioning agent among the active ones skip this malfunction
-- 
GitLab