Skip to content
Snippets Groups Projects
Commit 3f6990ac authored by u214892's avatar u214892
Browse files

Merge branch 'master' of gitlab.aicrowd.com:flatland/flatland into 62-increase-unit-test-coverage

parents d2a71e19 1154e8da
No related branches found
No related tags found
No related merge requests found
...@@ -96,6 +96,7 @@ class Demo: ...@@ -96,6 +96,7 @@ class Demo:
for iAgent in range(self.env.get_num_agents()): for iAgent in range(self.env.get_num_agents()):
# allways walk straight forward # allways walk straight forward
action = 2 action = 2
action = np.random.choice([0, 1, 2, 3], 1, p=[0.0, 0.5, 0.5, 0.0])[0]
# update the actions # update the actions
action_dict.update({iAgent: action}) action_dict.update({iAgent: action})
......
...@@ -9,7 +9,6 @@ from ipywidgets import IntSlider, VBox, HBox, Checkbox, Output, Text, RadioButto ...@@ -9,7 +9,6 @@ from ipywidgets import IntSlider, VBox, HBox, Checkbox, Output, Text, RadioButto
from numpy import array from numpy import array
import flatland.utils.rendertools as rt import flatland.utils.rendertools as rt
from examples.play_model import Player
from flatland.envs.agent_utils import EnvAgent, EnvAgentStatic from flatland.envs.agent_utils import EnvAgent, EnvAgentStatic
from flatland.envs.env_utils import mirror from flatland.envs.env_utils import mirror
from flatland.envs.generators import complex_rail_generator, empty_rail_generator from flatland.envs.generators import complex_rail_generator, empty_rail_generator
...@@ -75,10 +74,6 @@ class View(object): ...@@ -75,10 +74,6 @@ class View(object):
self.wDebug_move = Checkbox(description="Debug mouse move") self.wDebug_move = Checkbox(description="Debug mouse move")
self.wDebug_move.observe(self.controller.setDebugMove, names="value") self.wDebug_move.observe(self.controller.setDebugMove, names="value")
# Checkbox for rendering observations
self.wShowObs = Checkbox(description="Show Agent Observations")
self.wShowObs.observe(self.controller.refresh, names="value")
# This is like a cell widget where loggin goes # This is like a cell widget where loggin goes
self.wOutput = Output() self.wOutput = Output()
...@@ -109,13 +104,9 @@ class View(object): ...@@ -109,13 +104,9 @@ class View(object):
for i, title in enumerate(tab_contents): for i, title in enumerate(tab_contents):
self.wTab.set_title(i, title) self.wTab.set_title(i, title)
self.wTab.children = [ self.wTab.children = [
VBox([self.wRegenSizeWidth, self.wRegenSizeHeight, self.wRegenNAgents]), VBox([self.wRegenSizeWidth, self.wRegenSizeHeight, self.wRegenNAgents])
VBox([self.wShowObs]),
] ]
# Progress bar intended for stepping in the background (not yet working)
self.wProg_steps = ipywidgets.IntProgress(value=0, min=0, max=20, step=1, description="Step")
# 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"),
...@@ -128,7 +119,6 @@ class View(object): ...@@ -128,7 +119,6 @@ class View(object):
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)
] ]
self.lwButtons = [] self.lwButtons = []
...@@ -166,7 +156,7 @@ class View(object): ...@@ -166,7 +156,7 @@ class View(object):
self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", agents=True, self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", agents=True,
show=False, iSelectedAgent=self.model.iSelectedAgent, show=False, iSelectedAgent=self.model.iSelectedAgent,
show_observations=self.show_observations()) show_observations=False)
img = self.oRT.getImage() img = self.oRT.getImage()
self.wImage.data = img self.wImage.data = img
...@@ -201,13 +191,6 @@ class View(object): ...@@ -201,13 +191,6 @@ class View(object):
else: else:
print(*args, **kwargs) print(*args, **kwargs)
def show_observations(self):
''' returns whether to show observations - boolean '''
if self.wShowObs.value:
return True
else:
return False
class Controller(object): class Controller(object):
""" """
...@@ -360,7 +343,6 @@ class Controller(object): ...@@ -360,7 +343,6 @@ class Controller(object):
self.model.init_agents_static] self.model.init_agents_static]
self.model.env.agents = None self.model.env.agents = None
self.model.init_agents_static = None self.model.init_agents_static = None
self.player = None
self.model.env.restart_agents() self.model.env.restart_agents()
self.model.env.reset(False, False) self.model.env.reset(False, False)
self.refresh(event) self.refresh(event)
...@@ -413,7 +395,6 @@ class EditorModel(object): ...@@ -413,7 +395,6 @@ class EditorModel(object):
self.env_filename = "temp.pkl" self.env_filename = "temp.pkl"
self.set_env(env) self.set_env(env)
self.iSelectedAgent = None self.iSelectedAgent = None
self.player = None
self.init_agents_static = None self.init_agents_static = None
self.thread = None self.thread = None
...@@ -616,7 +597,6 @@ class EditorModel(object): ...@@ -616,7 +597,6 @@ class EditorModel(object):
self.env.rail.grid[:, :] = 0 self.env.rail.grid[:, :] = 0
self.env.agents = [] self.env.agents = []
self.env.agents_static = [] self.env.agents_static = []
self.player = None
self.redraw() self.redraw()
...@@ -631,7 +611,6 @@ class EditorModel(object): ...@@ -631,7 +611,6 @@ class EditorModel(object):
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 = None
self.redraw() self.redraw()
def setFilename(self, filename): def setFilename(self, filename):
...@@ -650,6 +629,7 @@ class EditorModel(object): ...@@ -650,6 +629,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.init_agents_static = None
self.view.oRT.update_background()
self.fix_env() self.fix_env()
self.set_env(self.env) self.set_env(self.env)
self.redraw() self.redraw()
...@@ -688,7 +668,6 @@ class EditorModel(object): ...@@ -688,7 +668,6 @@ 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 = None
self.view.new_env() self.view.new_env()
self.redraw() self.redraw()
...@@ -721,7 +700,7 @@ class EditorModel(object): ...@@ -721,7 +700,7 @@ class EditorModel(object):
# Create a new agent and select it. # Create a new agent and select it.
agent_static = EnvAgentStatic(rcCell, 0, rcCell, moving=False) agent_static = EnvAgentStatic(rcCell, 0, rcCell, moving=False)
self.iSelectedAgent = self.env.add_agent_static(agent_static) self.iSelectedAgent = self.env.add_agent_static(agent_static)
self.player = None # will need to start a new player self.view.oRT.update_background()
else: else:
# Move the selected agent to this cell # Move the selected agent to this cell
agent_static = self.env.agents_static[self.iSelectedAgent] agent_static = self.env.agents_static[self.iSelectedAgent]
...@@ -745,26 +724,9 @@ class EditorModel(object): ...@@ -745,26 +724,9 @@ class EditorModel(object):
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.init_agents_static = None
self.view.oRT.update_background()
self.redraw() 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 bg_updater(self, wProg_steps):
try:
for i in range(20):
self.step()
time.sleep(0.2)
wProg_steps.value = i + 1 # indicate progress on bar
finally:
self.thread = None
def fix_env(self): def fix_env(self):
self.env.width = self.env.rail.width self.env.width = self.env.rail.width
self.env.height = self.env.rail.height self.env.height = self.env.rail.height
......
...@@ -358,17 +358,17 @@ class PILSVG(PILGL): ...@@ -358,17 +358,17 @@ class PILSVG(PILGL):
"NN SS": "Bahnhof_#d50000_Gleis_vertikal.svg"} "NN SS": "Bahnhof_#d50000_Gleis_vertikal.svg"}
# Dict of rail cell images indexed by binary transitions # Dict of rail cell images indexed by binary transitions
self.dPilRail = self.loadSVGs(dRailFiles, rotate=True, backgroundImage="Background_rail.svg", dPilRailFiles = self.loadSVGs(dRailFiles, rotate=True, backgroundImage="Background_rail.svg",
whitefilter="Background_white_filter.svg") whitefilter="Background_white_filter.svg")
# Load the target files (which have rails and transitions of their own) # Load the target files (which have rails and transitions of their own)
# They are indexed by (binTrans, iAgent), ie a tuple of the binary transition and the agent index # They are indexed by (binTrans, iAgent), ie a tuple of the binary transition and the agent index
dPilRail2 = self.loadSVGs(dTargetFiles, rotate=False, agent_colors=self.ltAgentColors, dPilTargetFiles = self.loadSVGs(dTargetFiles, rotate=False, agent_colors=self.ltAgentColors,
backgroundImage="Background_rail.svg", backgroundImage="Background_rail.svg",
whitefilter="Background_white_filter.svg") whitefilter="Background_white_filter.svg")
# Merge them with the regular rails. # Merge them with the regular rails.
# https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression # https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
self.dPilRail = {**self.dPilRail, **dPilRail2} self.dPilRail = {**dPilRailFiles, **dPilTargetFiles}
def loadSVGs(self, dDirFile, rotate=False, agent_colors=False, backgroundImage=None, whitefilter=None): def loadSVGs(self, dDirFile, rotate=False, agent_colors=False, backgroundImage=None, whitefilter=None):
dPil = {} dPil = {}
...@@ -445,13 +445,13 @@ class PILSVG(PILGL): ...@@ -445,13 +445,13 @@ class PILSVG(PILGL):
self.drawImageRC(pilTrack, (row, col)) self.drawImageRC(pilTrack, (row, col))
else: else:
print("Illegal rail:", row, col, format(binTrans, "#018b")[2:]) print("Illegal rail:", row, col, format(binTrans, "#018b")[2:], binTrans)
else: else:
if (binTrans, iTarget) in self.dPilRail: if (binTrans, iTarget) in self.dPilRail:
pilTrack = self.dPilRail[(binTrans, iTarget)] pilTrack = self.dPilRail[(binTrans, iTarget)]
self.drawImageRC(pilTrack, (row, col)) self.drawImageRC(pilTrack, (row, col))
else: else:
print("Illegal target rail:", row, col, format(binTrans, "#018b")[2:]) print("Illegal target rail:", row, col, format(binTrans, "#018b")[2:], (binTrans, iTarget))
if isSelected: if isSelected:
svgBG = self.pilFromSvgFile("svg", "Selected_Target.svg") svgBG = self.pilFromSvgFile("svg", "Selected_Target.svg")
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Railway Scene Editor # Railway Scene Editor
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
import numpy as np
from numpy import array
import ipywidgets
import IPython
from IPython.core.display import display, HTML from IPython.core.display import display, HTML
```
%% Cell type:code id: tags:
``` 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 from flatland.utils.editor import EditorMVC
```
%% Cell type:code id: tags:
``` python
mvc = EditorMVC(sGL="PILSVG" ) mvc = EditorMVC(sGL="PILSVG" )
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Instructions ## Instructions
- Drag to draw (improved dead-ends) - Drag to draw (improved dead-ends)
- ctrl-click to add agent or select agent - ctrl-click to add agent or select agent
- if agent is selected: - if agent is selected:
- ctrl-click to move agent position - ctrl-click to move agent position
- use rotate agent to rotate 90° - use rotate agent to rotate 90°
- ctrl-shift-click to set target for selected agent - ctrl-shift-click to set target for selected 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
Demo Scene: complex_scene.pkl Demo Scene: complex_scene.pkl
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
mvc.view.display() mvc.view.display()
``` ```
%% Output %% Output
......
ffmpeg -y -framerate 12 -i frame_%04d.bmp -hide_banner -c:v libx264 -pix_fmt yuv420p test.mp4 ffmpeg -y -framerate 12 -i frame_%04d.bmp -hide_banner -c:v libx264 -pix_fmt yuv420p test.mp4
ffmpeg -i test.mp4 test.gif
pause pause
\ No newline at end of file
...@@ -98,7 +98,9 @@ deps = ...@@ -98,7 +98,9 @@ deps =
commands = commands =
; run tests from subfolder to ensure that resources are accessed via resources and not via relative paths ; run tests from subfolder to ensure that resources are accessed via resources and not via relative paths
sh -c 'mkdir -p {envtmpdir}/6f59bc68108c3895b1828abdd04b9a06' sh -c 'mkdir -p {envtmpdir}/6f59bc68108c3895b1828abdd04b9a06'
sh -c 'jupyter nbextension install --py --sys-prefix widgetsnbextension'
sh -c 'jupyter nbextension enable --py --sys-prefix widgetsnbextension' sh -c 'jupyter nbextension enable --py --sys-prefix widgetsnbextension'
sh -c 'jupyter nbextension install --py --sys-prefix jpy_canvas'
sh -c 'jupyter nbextension enable --py --sys-prefix jpy_canvas' sh -c 'jupyter nbextension enable --py --sys-prefix jpy_canvas'
; https://stackoverflow.com/questions/35545402/how-to-run-an-ipynb-jupyter-notebook-from-terminal/35545463 ; https://stackoverflow.com/questions/35545402/how-to-run-an-ipynb-jupyter-notebook-from-terminal/35545463
sh -c 'ls notebooks/*.ipynb | xargs -n 1 jupyter nbconvert --to python' sh -c 'ls notebooks/*.ipynb | xargs -n 1 jupyter nbconvert --to python'
......
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