From 1aeeb02ad9d329dd508a5268532642c8aeff2804 Mon Sep 17 00:00:00 2001
From: u214892 <u214892@sbb.ch>
Date: Tue, 17 Sep 2019 11:06:47 +0200
Subject: [PATCH] #175 no sleep when running examples as benchmark or for
 profiling

---
 benchmarks/run_all_examples.py                      |  2 +-
 ...y => custom_observation_example_01_SimpleObs.py} |  0
 ...ervation_example_02_SingleAgentNavigationObs.py} | 10 +++++-----
 ...om_observation_example_03_ObservePredictions.py} | 13 ++++++-------
 4 files changed, 12 insertions(+), 13 deletions(-)
 rename examples/{custom_observation_example_01.py => custom_observation_example_01_SimpleObs.py} (100%)
 rename examples/{custom_observation_example_02.py => custom_observation_example_02_SingleAgentNavigationObs.py} (94%)
 rename examples/{custom_observation_example_03.py => custom_observation_example_03_ObservePredictions.py} (95%)

diff --git a/benchmarks/run_all_examples.py b/benchmarks/run_all_examples.py
index b30a7658..509e2324 100644
--- a/benchmarks/run_all_examples.py
+++ b/benchmarks/run_all_examples.py
@@ -24,5 +24,5 @@ for entry in [entry for entry in importlib_resources.contents('examples') if
         print("*****************************************************************")
         with swap_attr(sys, "stdin", StringIO("q")):
             runpy.run_path(file_in, run_name="__main__", init_globals={
-                'argv': ['--no-sleep']
+                'argv': ['--sleep-for-animation=False']
             })
diff --git a/examples/custom_observation_example_01.py b/examples/custom_observation_example_01_SimpleObs.py
similarity index 100%
rename from examples/custom_observation_example_01.py
rename to examples/custom_observation_example_01_SimpleObs.py
diff --git a/examples/custom_observation_example_02.py b/examples/custom_observation_example_02_SingleAgentNavigationObs.py
similarity index 94%
rename from examples/custom_observation_example_02.py
rename to examples/custom_observation_example_02_SingleAgentNavigationObs.py
index 3927f038..b16a4e3e 100644
--- a/examples/custom_observation_example_02.py
+++ b/examples/custom_observation_example_02_SingleAgentNavigationObs.py
@@ -62,14 +62,14 @@ class SingleAgentNavigationObs(TreeObsForRailEnv):
 
 def main(args):
     try:
-        opts, args = getopt.getopt(args, "", ["no-sleep", ""])
+        opts, args = getopt.getopt(args, "", ["sleep-for-animation=", ""])
     except getopt.GetoptError as err:
         print(str(err))  # will print something like "option -a not recognized"
         sys.exit(2)
-    no_sleep = False
+    sleep_for_animation = True
     for o, a in opts:
-        if o in ("--no-sleep"):
-            no_sleep = True
+        if o in ("--sleep-for-animation"):
+            sleep_for_animation = bool(a)
         else:
             assert False, "unhandled option"
 
@@ -89,7 +89,7 @@ def main(args):
         obs, all_rewards, done, _ = env.step({0: action})
         print("Rewards: ", all_rewards, "  [done=", done, "]")
         env_renderer.render_env(show=True, frames=True, show_observations=True)
-        if not no_sleep:
+        if sleep_for_animation:
             time.sleep(0.1)
         if done["__all__"]:
             break
diff --git a/examples/custom_observation_example_03.py b/examples/custom_observation_example_03_ObservePredictions.py
similarity index 95%
rename from examples/custom_observation_example_03.py
rename to examples/custom_observation_example_03_ObservePredictions.py
index efe99763..00a3d625 100644
--- a/examples/custom_observation_example_03.py
+++ b/examples/custom_observation_example_03_ObservePredictions.py
@@ -100,20 +100,19 @@ class ObservePredictions(TreeObsForRailEnv):
         return observation
 
 
-def main(argv):
+def main(args):
     try:
-        opts, args = getopt.getopt(argv, "", ["no-sleep", ""])
+        opts, args = getopt.getopt(args, "", ["sleep-for-animation=", ""])
     except getopt.GetoptError as err:
         print(str(err))  # will print something like "option -a not recognized"
         sys.exit(2)
-    no_sleep = False
+    sleep_for_animation = True
     for o, a in opts:
-        if o in ("--no-sleep"):
-            no_sleep = True
+        if o in ("--sleep-for-animation"):
+            sleep_for_animation = bool(a)
         else:
             assert False, "unhandled option"
 
-
     # Initiate the Predictor
     custom_predictor = ShortestPathPredictorForRailEnv(10)
 
@@ -143,7 +142,7 @@ def main(argv):
         obs, all_rewards, done, _ = env.step(action_dict)
         print("Rewards: ", all_rewards, "  [done=", done, "]")
         env_renderer.render_env(show=True, frames=True, show_observations=True, show_predictions=False)
-        if not no_sleep:
+        if sleep_for_animation:
             time.sleep(0.5)
 
 
-- 
GitLab