diff --git a/examples/demo.py b/examples/demo.py index 6b48d6a535c3be307120a3e9c0705a28a99f3895..79eec82ba0fcc0d5d9703f8ccd43bb94e0072aa3 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -164,7 +164,12 @@ if False: demo_flatland_000.run_demo(60) demo_flatland_000 = None -demo_001 = Demo(Scenario_Generator.load_scenario('./env-data/railway/example_flatland_001.pkl')) -demo_001.set_record_frames('./rendering/frame_{:04d}.bmp') -demo_001.run_demo(60) +demo_001 = Demo(Scenario_Generator.load_scenario('./env-data/railway/temp.pkl')) +demo_001.run_demo(10) demo_001 = None + +if True: + demo_001 = Demo(Scenario_Generator.load_scenario('./env-data/railway/example_flatland_001.pkl')) + demo_001.set_record_frames('./rendering/frame_{:04d}.bmp') + demo_001.run_demo(60) + demo_001 = None diff --git a/flatland/utils/editor.py b/flatland/utils/editor.py index 58109b9beab85c56cc8004e0ce8f4274c8db40b6..a3dc120611649d3861f9e6f0795c0fe4d29a0fb1 100644 --- a/flatland/utils/editor.py +++ b/flatland/utils/editor.py @@ -134,19 +134,19 @@ class View(object): # abbreviated description of buttons and the methods they call ldButtons = [ - dict(name="Refresh", method=self.controller.refresh, tip="Redraw only"), - 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, - tip="Regenerate the rails using the method selected below"), - dict(name="Load", method=self.controller.load), - dict(name="Save", method=self.controller.save), - dict(name="Step", method=self.controller.step), - dict(name="Run Steps", method=self.controller.start_run)] + dict(name="Refresh", method=self.controller.refresh, tip="Redraw only"), + 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, + tip="Regenerate the rails using the method selected below"), + dict(name="Load", method=self.controller.load), + dict(name="Save", method=self.controller.save), + dict(name="Step", method=self.controller.step) + ] self.lwButtons = [] for dButton in ldButtons: @@ -210,7 +210,11 @@ class View(object): self.writableData[(y - 2):(y + 2), (x - 2):(x + 2), :3] = 0 def xy_to_rc(self, x, y): - rcCell = ((array([y, x]) - self.yxBase) / self.nPixCell).astype(int) + rcCell = ((array([y, x]) - self.yxBase)) + nX = np.floor((self.yxSize[0] - self.yxBase[0])/ self.model.env.height) + nY = np.floor((self.yxSize[1] - self.yxBase[1])/ self.model.env.width) + rcCell[0] = max(0,min(np.floor(rcCell[0]/nY),self.model.env.height-1)) + rcCell[1] = max(0,min(np.floor(rcCell[1]/nX),self.model.env.width-1)) return rcCell def log(self, *args, **kwargs): @@ -357,7 +361,17 @@ class Controller(object): def restartAgents(self, event): self.log("Restart Agents - nAgents:", self.view.wRegenNAgents.value) - self.model.restartAgents() + if self.model.init_agents_static is not None: + print("Restart Agents ...................") + print(self.model.env.agents_static) + self.model.env.agents_static = [EnvAgentStatic(d[0], d[1], d[2]) for d in self.model.init_agents_static ] + print(self.model.env.agents_static) + self.model.env.agents = None + self.model.init_agents_static = None + self.player = None + self.model.env.restart_agents() + self.model.env.reset(False,False) + self.refresh(event) def regenerate(self, event): method = self.view.wRegenMethod.value @@ -379,8 +393,6 @@ class Controller(object): def step(self, event): self.model.step() - def start_run(self, event): - self.model.start_run() def log(self, *args, **kwargs): if self.view is None: @@ -411,6 +423,7 @@ class EditorModel(object): self.set_env(env) self.iSelectedAgent = None self.player = None + self.init_agents_static = None self.thread = None def set_env(self, env): @@ -637,12 +650,12 @@ class EditorModel(object): # if replace_agents: # self.env.agents_handles = range(nAgents) self.env.reset(regen_rail=True, replace_agents=replace_agents) - self.player = Player(self.env) + self.player = None self.redraw() def restartAgents(self): self.env.agents = EnvAgent.list_from_static(self.env.agents_static) - self.player = Player(self.env) + self.player = None self.redraw() def setFilename(self, filename): @@ -654,13 +667,15 @@ class EditorModel(object): self.log("load file: ", self.env_filename) # self.env.rail.load_transition_map(self.env_filename, override_gridsize=True) self.env.load(self.env_filename) - - if not self.regen_size_height == self.env.height and not self.regen_size_width == self.env.width: + if not self.regen_size_height == self.env.height or not self.regen_size_width == self.env.width: self.regen_size_height = self.env.height self.regen_size_width = self.env.width self.regenerate(None, 0, self.env) self.env.load(self.env_filename) + self.env.restart_agents() + self.env.reset(False,False) + self.init_agents_static = None self.fix_env() self.set_env(self.env) self.redraw() @@ -670,7 +685,12 @@ class EditorModel(object): def save(self): self.log("save to ", self.env_filename, " working dir: ", os.getcwd()) # self.env.rail.save_transition_map(self.env_filename) + temp_store = self.env.agents + # clear agents before save , because we want the "init" position of the agent to expert + self.env.agents = [] self.env.save(self.env_filename) + # reset agents current (current position) + self.env.agents = temp_store def regenerate(self, method=None, nAgents=0, env=None): self.log("Regenerate size", @@ -696,7 +716,7 @@ class EditorModel(object): self.env.reset(regen_rail=True) self.fix_env() self.set_env(self.env) - self.player = Player(self.env) + self.player = None self.view.new_env() # self.view.init_canvas() # Can't do init_canvas - need to keep the same canvas widget! self.redraw() @@ -750,21 +770,18 @@ class EditorModel(object): def add_target(self, rcCell): if self.iSelectedAgent is not None: self.env.agents_static[self.iSelectedAgent].target = rcCell + self.init_agents_static = None self.redraw() def step(self): + if self.init_agents_static is None: + self.init_agents_static = [agent.to_list() for agent in self.env.agents_static] if self.player is None: self.player = Player(self.env) self.env.reset(regen_rail=False, replace_agents=False) self.player.step() self.redraw() - def start_run(self): - if self.thread is None: - self.thread = threading.Thread(target=self.bg_updater, args=(self.view.wProg_steps,)) - self.thread.start() - else: - self.log("thread already present") def bg_updater(self, wProg_steps): try: diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py index 5b81b039c8a439ece65547e58f66a49a1614f3d3..119f6dbfd6c85586007bc3c0532ddf1dad7dbd5d 100644 --- a/flatland/utils/graphics_pil.py +++ b/flatland/utils/graphics_pil.py @@ -344,7 +344,7 @@ class PILSVG(PILGL): lTrans16[iTrans] = "1" sTrans16 = "".join(lTrans16) binTrans = int(sTrans16, 2) - print(sTrans, sTrans16, sFile) + # print(sTrans, sTrans16, sFile) # Merge the transition svg image with the background colour. # This is a shortcut / hack and will need re-working. diff --git a/notebooks/Editor2.ipynb b/notebooks/Editor2.ipynb index 5218e6d9538238f56b6187cbc1d8f40e07954953..00250ea80c931d2bbaa1afcb3a3aa1fdebf27c23 100644 --- a/notebooks/Editor2.ipynb +++ b/notebooks/Editor2.ipynb @@ -78,34 +78,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "<flatland.utils.graphics_pil.PILSVG object at 0x000001C03C7A3080> <class 'flatland.utils.graphics_pil.PILSVG'>\n", + "<flatland.utils.graphics_pil.PILSVG object at 0x0000021AB01F0198> <class 'flatland.utils.graphics_pil.PILSVG'>\n", "<super: <class 'PILSVG'>, <PILSVG object>> <class 'super'>\n", - " 0000000000000000 Background_#91D1DD.svg\n", - "WE 0000000000000100 Gleis_Deadend.svg\n", - "WW EE NN SS 1000010000100001 Gleis_Diamond_Crossing.svg\n", - "WW EE 0000010000000001 Gleis_horizontal.svg\n", - "EN SW 0000100000010000 Gleis_Kurve_oben_links.svg\n", - "WN SE 0000000001001000 Gleis_Kurve_oben_rechts.svg\n", - "ES NW 0001001000000000 Gleis_Kurve_unten_links.svg\n", - "NE WS 0100000000000010 Gleis_Kurve_unten_rechts.svg\n", - "NN SS 1000000000100000 Gleis_vertikal.svg\n", - "NN SS EE WW ES NW SE WN 1001011001101001 Weiche_Double_Slip.svg\n", - "EE WW EN SW 0000110000010001 Weiche_horizontal_oben_links.svg\n", - "EE WW SE WN 0000010001001001 Weiche_horizontal_oben_rechts.svg\n", - "EE WW ES NW 0001011000000001 Weiche_horizontal_unten_links.svg\n", - "EE WW NE WS 0100010000000011 Weiche_horizontal_unten_rechts.svg\n", - "NN SS EE WW NW ES 1001011000100001 Weiche_Single_Slip.svg\n", - "NE NW ES WS 0101001000000010 Weiche_Symetrical.svg\n", - "NN SS EN SW 1000100000110000 Weiche_vertikal_oben_links.svg\n", - "NN SS SE WN 1000000001101000 Weiche_vertikal_oben_rechts.svg\n", - "NN SS NW ES 1001001000100000 Weiche_vertikal_unten_links.svg\n", - "NN SS NE WS 1100000000100010 Weiche_vertikal_unten_rechts.svg\n", - "EW 0000000100000000 Bahnhof_#d50000_Deadend_links.svg\n", - "NS 0010000000000000 Bahnhof_#d50000_Deadend_oben.svg\n", - "WE 0000000000000100 Bahnhof_#d50000_Deadend_rechts.svg\n", - "SN 0000000010000000 Bahnhof_#d50000_Deadend_unten.svg\n", - "EE WW 0000010000000001 Bahnhof_#d50000_Gleis_horizontal.svg\n", - "NN SS 1000000000100000 Bahnhof_#d50000_Gleis_vertikal.svg\n", "Clear rails\n" ] }, @@ -149,7 +123,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "82d6d69b02d14d51b773a3c2257844e6", + "model_id": "0f3ff6aff8954f3cb369b80b053f9f1a", "version_major": 2, "version_minor": 0 }, @@ -159,6 +133,18 @@ }, "metadata": {}, "output_type": "display_data" + }, + { + "ename": "AttributeError", + "evalue": "'EditorModel' object has no attribute 'model'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\users\\u216993\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\flatland_rl-0.1.1-py3.6.egg\\flatland\\utils\\editor.py\u001b[0m in \u001b[0;36mstep\u001b[1;34m(self, event)\u001b[0m\n\u001b[0;32m 386\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 387\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mevent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 388\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 389\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 390\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstart_run\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mevent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\u216993\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\flatland_rl-0.1.1-py3.6.egg\\flatland\\utils\\editor.py\u001b[0m in \u001b[0;36mstep\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 771\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 772\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplayer\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 773\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minit_agents_static\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0menv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0magents_static\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 774\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplayer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPlayer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0menv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 775\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0menv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mregen_rail\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreplace_agents\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAttributeError\u001b[0m: 'EditorModel' object has no attribute 'model'" + ] } ], "source": [ @@ -175,7 +161,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "047cd0c1a66e47ae9fcdbcef6eb6773d", + "model_id": "8df099a612d94e34bc64abd0263ae21f", "version_major": 2, "version_minor": 0 },