Skip to content
Snippets Groups Projects
Commit d05a34e6 authored by Egli Adrian (IT-SCI-API-PFI)'s avatar Egli Adrian (IT-SCI-API-PFI)
Browse files

restart agents

parent 96bf75d0
No related branches found
No related tags found
No related merge requests found
...@@ -134,19 +134,19 @@ class View(object): ...@@ -134,19 +134,19 @@ class View(object):
# abbreviated description of buttons and the methods they call # abbreviated description of buttons and the methods they call
ldButtons = [ ldButtons = [
dict(name="Refresh", method=self.controller.refresh, tip="Redraw only"), dict(name="Refresh", method=self.controller.refresh, tip="Redraw only"),
dict(name="Clear", method=self.controller.clear, tip="Clear rails and agents"), dict(name="Clear", method=self.controller.clear, tip="Clear rails and agents"),
dict(name="Reset", method=self.controller.reset, dict(name="Reset", method=self.controller.reset,
tip="Standard env reset, including regen rail + agents"), tip="Standard env reset, including regen rail + agents"),
dict(name="Rotate Agent", method=self.controller.rotate_agent, tip="Rotate selected agent"), dict(name="Rotate Agent", method=self.controller.rotate_agent, tip="Rotate selected agent"),
dict(name="Restart Agents", method=self.controller.restartAgents, dict(name="Restart Agents", method=self.controller.restartAgents,
tip="Move agents back to start positions"), tip="Move agents back to start positions"),
dict(name="Regenerate", method=self.controller.regenerate, dict(name="Regenerate", method=self.controller.regenerate,
tip="Regenerate the rails using the method selected below"), tip="Regenerate the rails using the method selected below"),
dict(name="Load", method=self.controller.load), dict(name="Load", method=self.controller.load),
dict(name="Save", method=self.controller.save), dict(name="Save", method=self.controller.save),
dict(name="Step", method=self.controller.step), dict(name="Step", method=self.controller.step)
dict(name="Run Steps", method=self.controller.start_run)] ]
self.lwButtons = [] self.lwButtons = []
for dButton in ldButtons: for dButton in ldButtons:
...@@ -361,7 +361,17 @@ class Controller(object): ...@@ -361,7 +361,17 @@ class Controller(object):
def restartAgents(self, event): def restartAgents(self, event):
self.log("Restart Agents - nAgents:", self.view.wRegenNAgents.value) 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): def regenerate(self, event):
method = self.view.wRegenMethod.value method = self.view.wRegenMethod.value
...@@ -383,8 +393,6 @@ class Controller(object): ...@@ -383,8 +393,6 @@ class Controller(object):
def step(self, event): def step(self, event):
self.model.step() self.model.step()
def start_run(self, event):
self.model.start_run()
def log(self, *args, **kwargs): def log(self, *args, **kwargs):
if self.view is None: if self.view is None:
...@@ -415,6 +423,7 @@ class EditorModel(object): ...@@ -415,6 +423,7 @@ class EditorModel(object):
self.set_env(env) self.set_env(env)
self.iSelectedAgent = None self.iSelectedAgent = None
self.player = None self.player = None
self.init_agents_static = None
self.thread = None self.thread = None
def set_env(self, env): def set_env(self, env):
...@@ -641,12 +650,12 @@ class EditorModel(object): ...@@ -641,12 +650,12 @@ class EditorModel(object):
# if replace_agents: # if replace_agents:
# self.env.agents_handles = range(nAgents) # self.env.agents_handles = range(nAgents)
self.env.reset(regen_rail=True, replace_agents=replace_agents) self.env.reset(regen_rail=True, replace_agents=replace_agents)
self.player = Player(self.env) self.player = None
self.redraw() self.redraw()
def restartAgents(self): def restartAgents(self):
self.env.agents = EnvAgent.list_from_static(self.env.agents_static) self.env.agents = EnvAgent.list_from_static(self.env.agents_static)
self.player = Player(self.env) self.player = None
self.redraw() self.redraw()
def setFilename(self, filename): def setFilename(self, filename):
...@@ -666,7 +675,7 @@ class EditorModel(object): ...@@ -666,7 +675,7 @@ class EditorModel(object):
self.env.restart_agents() self.env.restart_agents()
self.env.reset(False,False) self.env.reset(False,False)
self.init_agents_static = None
self.fix_env() self.fix_env()
self.set_env(self.env) self.set_env(self.env)
self.redraw() self.redraw()
...@@ -707,7 +716,7 @@ class EditorModel(object): ...@@ -707,7 +716,7 @@ class EditorModel(object):
self.env.reset(regen_rail=True) self.env.reset(regen_rail=True)
self.fix_env() self.fix_env()
self.set_env(self.env) self.set_env(self.env)
self.player = Player(self.env) self.player = None
self.view.new_env() self.view.new_env()
# self.view.init_canvas() # Can't do init_canvas - need to keep the same canvas widget! # self.view.init_canvas() # Can't do init_canvas - need to keep the same canvas widget!
self.redraw() self.redraw()
...@@ -761,21 +770,18 @@ class EditorModel(object): ...@@ -761,21 +770,18 @@ class EditorModel(object):
def add_target(self, rcCell): def add_target(self, rcCell):
if self.iSelectedAgent is not None: if self.iSelectedAgent is not None:
self.env.agents_static[self.iSelectedAgent].target = rcCell self.env.agents_static[self.iSelectedAgent].target = rcCell
self.init_agents_static = None
self.redraw() self.redraw()
def step(self): 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: if self.player is None:
self.player = Player(self.env) self.player = Player(self.env)
self.env.reset(regen_rail=False, replace_agents=False) self.env.reset(regen_rail=False, replace_agents=False)
self.player.step() self.player.step()
self.redraw() 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): def bg_updater(self, wProg_steps):
try: try:
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Rail Editor v0.2 # Rail Editor v0.2
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%load_ext autoreload %load_ext autoreload
%autoreload 2 %autoreload 2
``` ```
%% Output
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
from numpy import array from numpy import array
import ipywidgets import ipywidgets
import IPython import IPython
from IPython.core.display import display, HTML from IPython.core.display import display, HTML
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
display(HTML("<style>.container { width:95% !important; }</style>")) display(HTML("<style>.container { width:95% !important; }</style>"))
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from flatland.utils.editor import EditorMVC, EditorModel, View, Controller from flatland.utils.editor import EditorMVC, EditorModel, View, Controller
``` ```
%% Output
cairo installed: OK
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
mvc = EditorMVC(sGL="PILSVG" ) # sGL="PIL") mvc = EditorMVC(sGL="PILSVG" ) # sGL="PIL")
``` ```
%% Output %% Output
<flatland.utils.graphics_pil.PILSVG object at 0x0000020D4CDD2E10> <class 'flatland.utils.graphics_pil.PILSVG'> <flatland.utils.graphics_pil.PILSVG object at 0x0000021AB01F0198> <class 'flatland.utils.graphics_pil.PILSVG'>
<super: <class 'PILSVG'>, <PILSVG object>> <class 'super'> <super: <class 'PILSVG'>, <PILSVG object>> <class 'super'>
0000000000000000 Background_#91D1DD.svg
WE 0000000000000100 Gleis_Deadend.svg
WW EE NN SS 1000010000100001 Gleis_Diamond_Crossing.svg
WW EE 0000010000000001 Gleis_horizontal.svg
EN SW 0000100000010000 Gleis_Kurve_oben_links.svg
WN SE 0000000001001000 Gleis_Kurve_oben_rechts.svg
ES NW 0001001000000000 Gleis_Kurve_unten_links.svg
NE WS 0100000000000010 Gleis_Kurve_unten_rechts.svg
NN SS 1000000000100000 Gleis_vertikal.svg
NN SS EE WW ES NW SE WN 1001011001101001 Weiche_Double_Slip.svg
EE WW EN SW 0000110000010001 Weiche_horizontal_oben_links.svg
EE WW SE WN 0000010001001001 Weiche_horizontal_oben_rechts.svg
EE WW ES NW 0001011000000001 Weiche_horizontal_unten_links.svg
EE WW NE WS 0100010000000011 Weiche_horizontal_unten_rechts.svg
NN SS EE WW NW ES 1001011000100001 Weiche_Single_Slip.svg
NE NW ES WS 0101001000000010 Weiche_Symetrical.svg
NN SS EN SW 1000100000110000 Weiche_vertikal_oben_links.svg
NN SS SE WN 1000000001101000 Weiche_vertikal_oben_rechts.svg
NN SS NW ES 1001001000100000 Weiche_vertikal_unten_links.svg
NN SS NE WS 1100000000100010 Weiche_vertikal_unten_rechts.svg
EW 0000000100000000 Bahnhof_#d50000_Deadend_links.svg
NS 0010000000000000 Bahnhof_#d50000_Deadend_oben.svg
WE 0000000000000100 Bahnhof_#d50000_Deadend_rechts.svg
SN 0000000010000000 Bahnhof_#d50000_Deadend_unten.svg
EE WW 0000010000000001 Bahnhof_#d50000_Gleis_horizontal.svg
NN SS 1000000000100000 Bahnhof_#d50000_Gleis_vertikal.svg
Clear rails Clear rails
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Instructions ## Instructions
- Drag to draw (improved dead-ends) - Drag to draw (improved dead-ends)
- Shift-Drag to erase rails (erasing dead ends not yet automated - drag right across them) - Shift-Drag to erase rails (erasing dead ends not yet automated - drag right across them)
- ctrl-click to add agent - ctrl-click to add agent
- direction chosen randomly to fit rail - direction chosen randomly to fit rail
- ctrl-shift-click to add target for last agent - ctrl-shift-click to add target for last agent
- target can be moved by repeating - target can be moved by repeating
- to Resize the env (cannot preserve work): - to Resize the env (cannot preserve work):
- select "Regen" tab, set regen size slider, click regenerate. - select "Regen" tab, set regen size slider, click regenerate.
- alt-click remove all rails from cell - alt-click remove all rails from cell
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
mvc.view.display() mvc.view.display()
``` ```
%% Output %% Output
<flatland.utils.graphics_pil.PILSVG object at 0x0000020D4CF49D30> <class 'flatland.utils.graphics_pil.PILSVG'> ---------------------------------------------------------------------------
<super: <class 'PILSVG'>, <PILSVG object>> <class 'super'> AttributeError Traceback (most recent call last)
0000000000000000 Background_#91D1DD.svg c:\users\u216993\appdata\local\programs\python\python36\lib\site-packages\flatland_rl-0.1.1-py3.6.egg\flatland\utils\editor.py in step(self, event)
WE 0000000000000100 Gleis_Deadend.svg 386
WW EE NN SS 1000010000100001 Gleis_Diamond_Crossing.svg 387 def step(self, event):
WW EE 0000010000000001 Gleis_horizontal.svg --> 388 self.model.step()
EN SW 0000100000010000 Gleis_Kurve_oben_links.svg 389
WN SE 0000000001001000 Gleis_Kurve_oben_rechts.svg 390 def start_run(self, event):
ES NW 0001001000000000 Gleis_Kurve_unten_links.svg c:\users\u216993\appdata\local\programs\python\python36\lib\site-packages\flatland_rl-0.1.1-py3.6.egg\flatland\utils\editor.py in step(self)
NE WS 0100000000000010 Gleis_Kurve_unten_rechts.svg 771 def step(self):
NN SS 1000000000100000 Gleis_vertikal.svg 772 if self.player is None:
NN SS EE WW ES NW SE WN 1001011001101001 Weiche_Double_Slip.svg --> 773 self.model.init_agents_static = self.env.agents_static.copy()
EE WW EN SW 0000110000010001 Weiche_horizontal_oben_links.svg 774 self.player = Player(self.env)
EE WW SE WN 0000010001001001 Weiche_horizontal_oben_rechts.svg 775 self.env.reset(regen_rail=False, replace_agents=False)
EE WW ES NW 0001011000000001 Weiche_horizontal_unten_links.svg AttributeError: 'EditorModel' object has no attribute 'model'
EE WW NE WS 0100010000000011 Weiche_horizontal_unten_rechts.svg
NN SS EE WW NW ES 1001011000100001 Weiche_Single_Slip.svg
NE NW ES WS 0101001000000010 Weiche_Symetrical.svg
NN SS EN SW 1000100000110000 Weiche_vertikal_oben_links.svg
NN SS SE WN 1000000001101000 Weiche_vertikal_oben_rechts.svg
NN SS NW ES 1001001000100000 Weiche_vertikal_unten_links.svg
NN SS NE WS 1100000000100010 Weiche_vertikal_unten_rechts.svg
EW 0000000100000000 Bahnhof_#d50000_Deadend_links.svg
NS 0010000000000000 Bahnhof_#d50000_Deadend_oben.svg
WE 0000000000000100 Bahnhof_#d50000_Deadend_rechts.svg
SN 0000000010000000 Bahnhof_#d50000_Deadend_unten.svg
EE WW 0000010000000001 Bahnhof_#d50000_Gleis_horizontal.svg
NN SS 1000000000100000 Bahnhof_#d50000_Gleis_vertikal.svg
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
mvc.view.wOutput.clear_output() mvc.view.wOutput.clear_output()
mvc.view.wOutput mvc.view.wOutput
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(mvc.editor.env.agents), len(mvc.editor.env.agents_static) len(mvc.editor.env.agents), len(mvc.editor.env.agents_static)
``` ```
%% Output %% Output
(0, 0) (0, 0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment