From d1c6a97564e087236b5b7b643090b093d72e231b Mon Sep 17 00:00:00 2001
From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch>
Date: Thu, 23 May 2019 17:14:46 +0200
Subject: [PATCH] editor support agent rotate

---
 env-data/railway/example_network_000.pkl | Bin 180 -> 180 bytes
 env-data/railway/example_network_001.pkl | Bin 218 -> 218 bytes
 env-data/railway/example_network_002.pkl | Bin 282 -> 282 bytes
 examples/demo.py                         |  13 ++++---------
 flatland/utils/editor.py                 |  13 +++++++++++--
 notebooks/Editor2.ipynb                  |   4 ++--
 6 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/env-data/railway/example_network_000.pkl b/env-data/railway/example_network_000.pkl
index 280688c2629331621ab2ea80b4b096226464e653..ab7764b1ea250633e5a0cd2ec9b93a4e07479c9f 100644
GIT binary patch
delta 19
bcmdnOxP@`T3LeHutendd(^K<GiYEX7Ls|z2

delta 19
bcmdnOxP@`T3Lb_@tendd(^K<GiYEX7Lrw<=

diff --git a/env-data/railway/example_network_001.pkl b/env-data/railway/example_network_001.pkl
index 801f95149dec6eb4d47fd14e36d30f2541480188..af9dc43246449562065be64c750a06c75c3e7571 100644
GIT binary patch
delta 19
bcmcb`c#CnuIUdGI91P16(^K<GiYEX7N|Oh4

delta 19
bcmcb`c#CnuIUa^d91P16(^K<GiYEX7N{0t?

diff --git a/env-data/railway/example_network_002.pkl b/env-data/railway/example_network_002.pkl
index 898d54ebeb823e48790d4661ffe75a6940cd0712..d39e44798066e0a0b753006775c5727326fc58da 100644
GIT binary patch
delta 20
bcmbQmG>d5iA0rR*BnF0MiRr0%CB+i}I^qUV

delta 20
bcmbQmG>d5iA0rRLBnF0MiRr0%CB+i}I=%)@

diff --git a/examples/demo.py b/examples/demo.py
index 7e3725bc..a53370aa 100644
--- a/examples/demo.py
+++ b/examples/demo.py
@@ -6,7 +6,6 @@ import time
 import numpy as np
 import torch
 
-from flatland.baselines.dueling_double_dqn import Agent
 from flatland.envs.generators import complex_rail_generator
 # from flatland.envs.generators import rail_from_list_of_saved_GridTransitionMap_generator
 from flatland.envs.generators import random_rail_generator
@@ -126,13 +125,7 @@ class Demo:
     def __init__(self, env):
         self.env = env
         self.create_renderer()
-        self.load_agent()
-
-    def load_agent(self):
-        self.state_size = 105 * 2
         self.action_size = 4
-        self.agent = Agent(self.state_size, self.action_size, "FC", 0)
-        self.agent.qnetwork_local.load_state_dict(torch.load('./flatland/baselines/Nets/avoid_checkpoint15000.pth'))
 
     def create_renderer(self):
         self.renderer = RenderTool(self.env, gl="QTSVG")
@@ -170,10 +163,12 @@ class Demo:
             # print(step)
             # Action
             for a in range(self.env.get_num_agents()):
-                action = self.agent.act(agent_obs[a])
+                action = np.random.choice(self.action_size) #self.agent.act(agent_obs[a])
                 action_prob[action] += 1
                 action_dict.update({a: action})
 
+            print(action_dict)
+
             self.renderer.renderEnv(show=True,action_dict=action_dict)
 
             # Environment step
@@ -196,7 +191,7 @@ class Demo:
                 break
 
 
-if True:
+if False:
     demo_000 = Demo(Scenario_Generator.generate_random_scenario())
     demo_000.run_demo()
     demo_000 = None
diff --git a/flatland/utils/editor.py b/flatland/utils/editor.py
index 91d81faa..59a55db5 100644
--- a/flatland/utils/editor.py
+++ b/flatland/utils/editor.py
@@ -132,6 +132,7 @@ class View(object):
             dict(name="Clear", method=self.controller.clear, tip="Clear rails and agents"),
             dict(name="Reset", method=self.controller.reset,
                  tip="Standard env reset, including regen rail + agents"),
+            dict(name="Rotate Agent", method=self.controller.rotate_agent, tip="Rotate selected agent"),
             dict(name="Restart Agents", method=self.controller.restartAgents,
                  tip="Move agents back to start positions"),
             dict(name="Regenerate", method=self.controller.regenerate,
@@ -325,6 +326,15 @@ class Controller(object):
         self.model.reset(replace_agents=self.view.wReplaceAgents.value,
                          nAgents=self.view.wRegenNAgents.value)
 
+    def rotate_agent(self,event):
+        self.log("Rotate Agent:", self.model.iSelectedAgent)
+        if self.model.iSelectedAgent is not None:
+            for iAgent, agent in enumerate(self.model.env.agents_static):
+                if agent is None:
+                    continue
+                agent.direction = (agent.direction + 1) % 4
+        self.model.redraw()
+
     def restartAgents(self, event):
         self.log("Restart Agents - nAgents:", self.view.wRegenNAgents.value)
         self.model.restartAgents()
@@ -695,8 +705,7 @@ class EditorModel(object):
             # No
             if self.iSelectedAgent is None:
                 # Create a new agent and select it.
-                ## ADRIAN
-                agent_static = EnvAgentStatic(rcCell, np.random.choice(4), rcCell)
+                agent_static = EnvAgentStatic(rcCell,0, rcCell)
                 self.iSelectedAgent = self.env.add_agent_static(agent_static)
                 self.player = None  # will need to start a new player
             else:
diff --git a/notebooks/Editor2.ipynb b/notebooks/Editor2.ipynb
index 8305d115..20286e88 100644
--- a/notebooks/Editor2.ipynb
+++ b/notebooks/Editor2.ipynb
@@ -105,7 +105,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "83640a5c6059421d92e0d69049ad232f",
+       "model_id": "7b66ea9348c9477f881ff27456987363",
        "version_major": 2,
        "version_minor": 0
       },
@@ -131,7 +131,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "dc7691bf5f804c5c95604cb551dbb335",
+       "model_id": "ffa0f869fe8a4921a7415384b75c1ded",
        "version_major": 2,
        "version_minor": 0
       },
-- 
GitLab