Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • flatland/flatland
  • stefan_otte/flatland
  • jiaodaxiaozi/flatland
  • sfwatergit/flatland
  • utozx126/flatland
  • ChenKuanSun/flatland
  • ashivani/flatland
  • minhhoa/flatland
  • pranjal_dhole/flatland
  • darthgera123/flatland
  • rivesunder/flatland
  • thomaslecat/flatland
  • joel_joseph/flatland
  • kchour/flatland
  • alex_zharichenko/flatland
  • yoogottamk/flatland
  • troye_fang/flatland
  • elrichgro/flatland
  • jun_jin/flatland
  • nimishsantosh107/flatland
20 results
Show changes
Showing
with 715 additions and 1169 deletions
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
No preview for this file type
#!/usr/bin/env python
import os
import webbrowser
import subprocess
import webbrowser
from urllib.request import pathname2url
def browser(pathname):
webbrowser.open("file:" + pathname2url(os.path.abspath(pathname)))
subprocess.call(['coverage', 'run', '--source', 'flatland', '-m', 'pytest'])
subprocess.call(['coverage', 'report', '-m'])
subprocess.call(['coverage', 'html'])
......
#!/usr/bin/env python
import glob
import os
import webbrowser
import shutil
import subprocess
import webbrowser
from urllib.request import pathname2url
def browser(pathname):
webbrowser.open("file:" + pathname2url(os.path.abspath(pathname)))
def remove_exists(filename):
try:
os.remove(filename)
......@@ -16,14 +19,43 @@ def remove_exists(filename):
# clean docs config and html files, and rebuild everything
remove_exists('docs/flatland.rst')
# wildcards do not work under Windows
for image_file in glob.glob(r'./docs/flatland*.rst'):
remove_exists(image_file)
remove_exists('docs/modules.rst')
subprocess.call(['sphinx-apidoc', '-o', 'docs/', 'flatland'])
for md_file in glob.glob(r'./*.md') + glob.glob(r'./docs/specifications/*.md') + glob.glob(r'./docs/tutorials/*.md') + glob.glob(r'./docs/interface/*.md'):
from m2r import parse_from_file
os.environ["SPHINXPROJ"] = "flatland"
rst_content = parse_from_file(md_file)
rst_file = md_file.replace(".md", ".rst")
remove_exists(rst_file)
with open(rst_file, 'w') as out:
print("m2r {}->{}".format(md_file, rst_file))
out.write(rst_content)
out.flush()
img_dest = 'docs/images/'
if not os.path.exists(img_dest):
os.makedirs(img_dest)
for image_file in glob.glob(r'./images/*.png'):
shutil.copy(image_file, img_dest)
subprocess.call(['sphinx-apidoc', '--force', '-a', '-e', '-o', 'docs/', 'flatland', '-H', 'API Reference', '--tocfile',
'05_apidoc'])
os.environ["SPHINXPROJ"] = "Flatland"
os.chdir('docs')
subprocess.call(['python', '-msphinx', '-M', 'clean', '.', '_build'])
img_dest = '_build/html/img'
if not os.path.exists(img_dest):
os.makedirs(img_dest)
for image_file in glob.glob(r'./specifications/img/*'):
shutil.copy(image_file, img_dest)
subprocess.call(['python', '-msphinx', '-M', 'html', '.', '_build'])
# we do not currrently use pydeps, commented out https://gitlab.aicrowd.com/flatland/flatland/issues/149
# subprocess.call(['python', '-mpydeps', '../flatland', '-o', '_build/html/flatland.svg', '--no-config', '--noshow'])
browser('_build/html/index.html')
%% Cell type:markdown id: tags:
# Jupyter Canvas Widget - Rail Editor
From - https://github.com/Who8MyLunch/Jupyter_Canvas_Widget/blob/master/notebooks/example%20mouse%20events.ipynb
Follow his instructions to do a local dev install and enable the widget.
%% Cell type:markdown id: tags:
## You need to run all cells before trying to edit the rails!
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
import image_attendant as imat
import ipywidgets
import IPython
import jpy_canvas
import numpy as np
from numpy import array
import time
from collections import deque
from matplotlib import pyplot as plt
import io
from PIL import Image
```
%% Cell type:code id: tags:
``` python
from ipywidgets import IntSlider, link, VBox, RadioButtons, HBox, interact
```
%% Cell type:code id: tags:
``` python
import flatland.core.env
from flatland.envs.rail_env import RailEnv, random_rail_generator
from flatland.core.transitions import RailEnvTransitions
from flatland.core.env_observation_builder import TreeObsForRailEnv
import flatland.utils.rendertools as rt
from flatland.utils.editor import JupEditor
```
%% Output
cpu
%% Cell type:code id: tags:
``` python
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:90% !important; }</style>"))
```
%% Output
%% Cell type:code id: tags:
``` python
oEnv = RailEnv(width=20,
height=20,
rail_generator=random_rail_generator(cell_type_relative_proportion=[1,1] + [0.5] * 6),
number_of_agents=0,
obs_builder_object=TreeObsForRailEnv(max_depth=2))
obs = oEnv.reset()
```
%% Cell type:code id: tags:
``` python
sfEnv = "../flatland/env-data/tests/test1.npy"
if True:
oEnv.rail.load_transition_map(sfEnv)
oEnv.width = oEnv.rail.width
oEnv.height = oEnv.rail.height
```
%% Cell type:code id: tags:
``` python
oRT = rt.RenderTool(oEnv)
```
%% Cell type:code id: tags:
``` python
oEnv.width
```
%% Output
10
%% Cell type:code id: tags:
``` python
import sys
sys.path
```
%% Output
['/home/jeremy/projects/heating',
'/home/jeremy/projects/aicrowd/rl-trains/MAgent/python',
'/home/jeremy/ve367/lib/python36.zip',
'/home/jeremy/ve367/lib/python3.6',
'/home/jeremy/ve367/lib/python3.6/lib-dynload',
'/usr/lib/python3.6',
'',
'/home/jeremy/ve367/lib/python3.6/site-packages',
'/home/jeremy/projects/aicrowd/rl-trains/torch-rl/torch_rl',
'/home/jeremy/projects/aicrowd/rl-trains/gym-minigrid',
'/home/jeremy/projects/aicrowd/rl-trains/flatland',
'/home/jeremy/projects/aicrowd/rl-trains/Jupyter_Canvas_Widget',
'/home/jeremy/ve367/lib/python3.6/site-packages/IPython/extensions',
'/home/jeremy/.ipython']
%% Cell type:markdown id: tags:
### Clear the rails
%% Cell type:code id: tags:
``` python
oEnv.rail.grid[:,:] = 0
```
%% Cell type:markdown id: tags:
### Render the env in the usual way, and take an image snapshot as numpy array
If you have already edited the env in the cell below, these changes should be reflected here.
%% Cell type:code id: tags:
``` python
oFig = plt.figure(figsize=(10,10))
oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = oRT.getImage()
print(type(img))
plt.clf() # if you don't want the image to appear here
pass
wid_img = jpy_canvas.Canvas(img)
```
%% Output
<class 'numpy.ndarray'>
%% Cell type:markdown id: tags:
### Update the function - in case external code updated
%% Cell type:code id: tags:
``` python
wid_img.unregister_all()
oEditor = JupEditor(oEnv, wid_img)
wid_img.register_move(oEditor.event_handler)
wid_img.register_click(oEditor.on_click)
```
%% Output
Correct editor
%% Cell type:markdown id: tags:
### Some more widgets
%% Cell type:code id: tags:
``` python
wid_drawMode = ipywidgets.RadioButtons(options=["Draw", "Erase", "Origin", "Destination"])
wid_drawMode.observe(oEditor.setDrawMode, names="value")
wid_debug = ipywidgets.Checkbox(description = "Debug")
wid_debug.observe(oEditor.setDebug, names="value")
wid_debug_move = ipywidgets.Checkbox(description = "Debug mouse move")
wid_debug_move.observe(oEditor.setDebugMove, names="value")
wid_output = ipywidgets.Output()
oEditor.setOutput(wid_output)
wid_filename = ipywidgets.Text(description = "Filename")
wid_filename.value = sfEnv
oEditor.setFilename(sfEnv)
wid_filename.observe(oEditor.setFilename_event, names="value")
wid_size = ipywidgets.IntSlider(min=5, max=30, step=5, description="Regen Size")
wid_size.observe(oEditor.setRegenSize_event, names="value")
prog_steps = ipywidgets.IntProgress(value=0, min=0, max=20, step=1, description="Step")
#prog_steps.observe(oEditor.)
ldButtons = [
dict(name = "Refresh", method = oEditor.redraw_event),
dict(name = "Clear", method = oEditor.clear),
dict(name = "Regenerate", method = oEditor.regenerate_event),
dict(name = "Load", method = oEditor.load),
dict(name = "Save", method = oEditor.save),
dict(name = "Step", method = oEditor.step_event),
dict(name = "Run Steps", method = oEditor.start_run_event),
]
lwid_buttons = []
for dButton in ldButtons:
wid_button = ipywidgets.Button(description = dButton["name"])
wid_button.on_click(dButton["method"])
lwid_buttons.append(wid_button)
#wid_debug = interact(oEditor.setDebug, debug=False)
vbox_controls = VBox([wid_filename, wid_drawMode, *lwid_buttons, wid_size, wid_debug, wid_debug_move])
```
%% Cell type:markdown id: tags:
### Edit the map below here by dragging the mouse to create transitions
You can create a dead-end by dragging foward and backward, ie Cell A -> Adjacent Cell B -> back to Cell A
%% Cell type:code id: tags:
``` python
# wid_box
wid_main = HBox([wid_img, vbox_controls])
wid_output.clear_output()
wid_main
```
%% Output
%% Cell type:code id: tags:
``` python
wid_output.clear_output()
wid_output
```
%% Output
%% Cell type:markdown id: tags:
### Experimental
enable the thread.start below to try background stepping of the agents. It tends to make a mess.
%% Cell type:code id: tags:
``` python
import threading
def bgUpdate(editor):
for i in range(100):
editor.step_event()
time.sleep(0.2)
```
%% Cell type:code id: tags:
``` python
thread = threading.Thread(target=bgUpdate, args = (oEditor,))
```
%% Cell type:code id: tags:
``` python
if False:
thread.start()
```
%% Cell type:markdown id: tags:
### Save the image (change the filename...)
%% Cell type:code id: tags:
``` python
if False:
oEnv.rail.save_transition_map("../flatland/env-data/tests/test-editor.npy")
```
%% Cell type:markdown id: tags:
## Junk below here
%% Cell type:code id: tags:
``` python
def evListen(wid, ev):
x = ev["canvasX"]
y=ev["canvasY"]
yxBase = array([6, 21])
nPixCell = 35
rcCell = ((array([y, x]) - yxBase) / nPixCell).astype(int)
print(ev)
print(x, y, (x-21) / 35, (y-6) / 35, rcCell)
```
%% Cell type:code id: tags:
``` python
# wid_img.register_click(evListen)
#wid_img.register(evListen)
```
%% Cell type:code id: tags:
``` python
#wid_img.unregister_all()
```
%% Cell type:code id: tags:
``` python
ipywidgets.IntText(0)
```
%% Output
%% Cell type:code id: tags:
``` python
from examples.play_model import Player
```
%% Cell type:code id: tags:
``` python
oP = Player(oEnv)
```
%% Cell type:code id: tags:
``` python
oP.step()
```
%% Cell type:code id: tags:
``` python
import asyncio
def wait_for_change(widget, value):
future = asyncio.Future()
def getvalue(change):
# make the new value available
future.set_result(change.new)
widget.unobserve(getvalue, value)
widget.observe(getvalue, value)
return future
```
%% Cell type:code id: tags:
``` python
slider = ipywidgets.IntSlider()
async def f():
for i in range(10):
print('did work %s'%i)
x = await wait_for_change(slider, 'value')
print('async function continued with value %s'%x)
asyncio.ensure_future(f())
slider
```
%% Output
%% Cell type:code id: tags:
``` python
import threading
from IPython.display import display
import time
progress = ipywidgets.FloatProgress(value=0.0, min=0.0, max=1.0)
def work(progress):
total = 100
for i in range(total):
time.sleep(0.2)
progress.value = float(i+1)/total
thread = threading.Thread(target=work, args=(progress,))
display(progress)
thread.start()
```
%% Output
%% Cell type:code id: tags:
``` python
binTrans = format(32800, "#016b")
[binTrans[i+2:i+6] for i in range(0, len(binTrans)-2, 4)]
```
%% Output
['1000', '0000', '0010', '0000']
%% Cell type:code id: tags:
``` python
oEnv.rail
```
%% Output
<flatland.core.transition_map.GridTransitionMap at 0x7fc6dd6c9e48>
%% Cell type:code id: tags:
``` python
binDeadend = 0b0010000110000100
format(~binDeadend & 0xffff, "#018b")
```
%% Output
'0b1101111001111011'
%% Cell type:code id: tags:
``` python
oEnv.rail.cell_neighbours_valid((2,5))
```
%% Output
True
%% Cell type:code id: tags:
``` python
oEnv.rail.grid[2,6] & binDeadend
```
%% Output
0
%% Cell type:code id: tags:
``` python
oTrans = oEnv.rail.grid[2,5]
oTrans
oEnv.rail.transitions.repr(oTrans, 1)
```
%% Output
''
%% Cell type:code id: tags:
``` python
oEnv.rail.transitions.is_valid(oTrans)
```
%% Output
True
%% Cell type:code id: tags:
``` python
oEnv.rail.transitions.gDir2dRC
```
%% Output
array([[-1, 0],
[ 0, 1],
[ 1, 0],
[ 0, -1]])
%% Cell type:code id: tags:
``` python
binTrans = oEnv.rail.get_transitions((2,2))
sBinTrans = format(binTrans, "#018b")[2:]
print(sBinTrans)
lsBinTrans = [ sBinTrans[i:i+4] for i in range(0,16, 4)]
print(lsBinTrans)
```
%% Output
0000000000000000
['0000', '0000', '0000', '0000']
%% Cell type:code id: tags:
``` python
lnBinTrans = array([ binTrans >> 8, binTrans & 0xff ], dtype=np.uint8)
g2binTrans = np.unpackbits(lnBinTrans).reshape(4,4)
g2binTrans
```
%% Output
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=uint8)
%% Cell type:code id: tags:
``` python
gDirIn = g2binTrans.any(axis=1)
gDirOut = g2binTrans.any(axis=0)
print(gDirIn)
print(gDirOut)
```
%% Output
[False False False False]
[False False False False]
%% Cell type:code id: tags:
``` python
giDirOut = np.argwhere(gDirOut)[:,0]
giDirOut
```
%% Output
array([], dtype=int64)
%% Cell type:code id: tags:
``` python
any((0,0,0))
```
%% Output
False
%% Cell type:code id: tags:
``` python
oEnv.rail.transitions.gDir2dRC
```
%% Output
array([[-1, 0],
[ 0, 1],
[ 1, 0],
[ 0, -1]])
%% Cell type:code id: tags:
``` python
oEnv.rail.transitions.gDir2dRC[giDirOut,:]
```
%% Output
array([], shape=(0, 2), dtype=int64)
%% Cell type:code id: tags:
``` python
t4TransOut = oEnv.rail.get_transitions((2,2,3))
print (t4TransOut)
for iDir in np.argwhere(t4TransOut)[:,0]:
print(iDir, oEnv.rail.transitions.gDir2dRC[iDir])
```
%% Output
(0, 0, 0, 0)
%% Cell type:code id: tags:
``` python
lsRepr = []
sDirs = "NESW"
lsDirs = list(sDirs)
cell_transition = oTrans
sbinTrans = format(cell_transition, "#018b")[2:]
print(sbinTrans)
for iDirIn in range(0,4):
sDirTrans = sbinTrans[iDirIn*4:iDirIn*4+4]
if sDirTrans=="0000":
continue
sDirsOut = [
lsDirs[iDirOut]
for iDirOut in range(0,4)
if sDirTrans[iDirOut]=="1"
]
lsRepr.append(lsDirs[iDirIn] + ":" + "".join(sDirsOut))
", ".join(lsRepr)
```
%% Output
0000000000000000
''
%% Cell type:code id: tags:
``` python
s="1010"
ints = int(s,2)
gs = array(ints, dtype=np.uint8)
print(gs, np.unpackbits(gs)[4:8])
```
%% Output
10 [1 0 1 0]
%% Cell type:code id: tags:
``` python
oEnv.rail.transitions.repr(1025)
```
%% Output
'N:0000 E:0100 S:0000 W:0001'
File added
File added
File added
%% Cell type:markdown id: tags:
# Simple Animation Demo
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
import numpy as np
import time
from IPython import display
from ipycanvas import canvas
from flatland.utils.rendertools import RenderTool
from flatland.envs.rail_env import RailEnv
from flatland.envs.rail_env import RailEnvActions as rea
from flatland.envs.persistence import RailEnvPersister
```
%% Cell type:code id: tags:
``` python
env, env_dict = RailEnvPersister.load_new("complex_scene_2.pkl", load_from_package="env_data.railway")
_ = env.reset()
env._max_episode_steps = 100
```
%% Output
pickle failed to load file: complex_scene_2.pkl trying msgpack (deprecated)...
pickle failed to load file: complex_scene_2.pkl trying msgpack (deprecated)...
pickle failed to load file: complex_scene_2.pkl trying msgpack (deprecated)...
This env file has no max_episode_steps (deprecated) - setting to 100
%% Cell type:code id: tags:
``` python
oRT = RenderTool(env, gl="PILSVG", jupyter=False, show_debug=True)
image_arr = oRT.get_image()
oCanvas = canvas.Canvas()
oCanvas.put_image_data(image_arr[:,:,0:3])
display.display(oCanvas)
done={"__all__":False}
while not done["__all__"]:
actions = {}
for agent_handle, agents in enumerate(env.agents):
actions.update({agent_handle:rea.MOVE_FORWARD})
obs, rew, done, info = env.step(actions)
oRT.render_env(show_observations=False,show_predictions=False)
gIm = oRT.get_image()
oCanvas.put_image_data(gIm[:,:,0:3])
time.sleep(0.1)
```
%% Output
# list of notebooks to include in run-all-notebooks.py test
simple_example_manual_control.ipynb
simple_rendering_demo.ipynb
flatland_animate.ipynb
render_episode.ipynb
scene_editor.ipynb
test_saved_envs.ipynb
test_service.ipynb
source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id: tags:
# Render Episode
Render a stored episode. Env file needs to have "episode" and "action" keys.
- creates a moving gif file of the episode
- displays the episode in a widget with a slider for the time steps.
%% Cell type:markdown id: tags:
# Setup
%% Cell type:code id: tags:
``` python
#!apt -qq install graphviz libgraphviz-dev pkg-config
#!pip install -qq git+https://gitlab.aicrowd.com/flatland/flatland.git
```
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
from IPython import display
```
%% Cell type:code id: tags:
``` python
import os
import pandas as pd
import PIL
import imageio
```
%% Cell type:code id: tags:
``` python
from flatland.utils.rendertools import RenderTool
from flatland.envs.rail_env import RailEnv
from flatland.envs.rail_generators import sparse_rail_generator
from flatland.envs.line_generators import sparse_line_generator
from flatland.envs.malfunction_generators import malfunction_from_file, no_malfunction_generator
from flatland.envs.rail_generators import rail_from_file
from flatland.envs.rail_env import RailEnvActions
from flatland.envs.step_utils.states import TrainState
from flatland.envs.persistence import RailEnvPersister
```
%% Cell type:code id: tags:
``` python
def render_env(env_renderer):
ag0= env_renderer.env.agents[0]
#print("render_env ag0: ",ag0.position, ag0.direction)
aImage = env_renderer.render_env(show_rowcols=True, return_image=True)
pil_image = PIL.Image.fromarray(aImage)
return pil_image
```
%% Cell type:markdown id: tags:
# Experiments
This has been mostly changed to load envs using `importlib_resources`. It's getting them from the package "envdata.tests`
%% Cell type:code id: tags:
``` python
env, env_dict = RailEnvPersister.load_new("complex_scene_2.pkl", load_from_package="env_data.railway")
_ = env.reset()
env._max_episode_steps = 100
```
%% Output
pickle failed to load file: complex_scene_2.pkl trying msgpack (deprecated)...
pickle failed to load file: complex_scene_2.pkl trying msgpack (deprecated)...
pickle failed to load file: complex_scene_2.pkl trying msgpack (deprecated)...
This env file has no max_episode_steps (deprecated) - setting to 100
%% Cell type:code id: tags:
``` python
# the seed has to match that used to record the episode, in order for the malfunctions to match.
oRT = RenderTool(env, show_debug=True)
aImg = oRT.render_env(show_rowcols=True, return_image=True, show_inactive_agents=True)
print(env._max_episode_steps)
```
%% Cell type:code id: tags:
``` python
loAgs = env_dict["agents"]
lCols = "initial_direction,direction,initial_position,position".split(",")
pd.DataFrame([ [getattr(oAg, sCol) for sCol in lCols]
for oAg in loAgs], columns=lCols)
```
%% Cell type:code id: tags:
``` python
pd.DataFrame([ [getattr(oAg, sCol) for sCol in lCols]
for oAg in env.agents], columns=lCols)
```
%% Cell type:code id: tags:
``` python
pd.DataFrame([ vars(oAg) for oAg in env.agents])
```
%% Cell type:code id: tags:
``` python
# from persistence.py
def get_agent_state(env):
list_agents_state = []
for iAg, oAg in enumerate(env.agents):
# the int cast is to avoid numpy types which may cause problems with msgpack
# in env v2, agents may have position None, before starting
if oAg.position is None:
pos = (0, 0)
else:
pos = (int(oAg.position[0]), int(oAg.position[1]))
# print("pos:", pos, type(pos[0]))
list_agents_state.append(
[*pos, int(oAg.direction), oAg.malfunction_handler])
return list_agents_state
```
%% Cell type:code id: tags:
``` python
pd.DataFrame([ vars(oAg) for oAg in env.agents])
```
%% Cell type:code id: tags:
``` python
expert_actions = []
action = {}
```
%% Cell type:code id: tags:
``` python
env_renderer = RenderTool(env, gl="PGL", show_debug=True)
n_agents = env.get_num_agents()
x_dim, y_dim = env.width, env.height
max_steps = env._max_episode_steps
action_dict = {}
frames = []
# log everything in original state
statuses = []
for a in range(n_agents):
statuses.append(env.agents[a].state)
pilImg = render_env(env_renderer)
frames.append({
'image': pilImg,
'statuses': statuses
})
step = 0
all_done = False
failed_action_check = False
print("Processing episode steps:")
while not all_done:
print(step, end=", ")
for agent_handle, agent in enumerate(env.agents):
action_dict.update({agent_handle: RailEnvActions.MOVE_FORWARD})
next_obs, all_rewards, done, info = env.step(action_dict)
statuses = []
for a in range(n_agents):
statuses.append(env.agents[a].state)
#clear_output(wait=True)
pilImg = render_env(env_renderer)
frames.append({
'image': pilImg,
'statuses': statuses
})
#print("Replaying {}/{}".format(step, max_steps))
if done['__all__']:
all_done = True
max_steps = step + 1
print("done")
step += 1
```
%% Cell type:code id: tags:
``` python
assert failed_action_check == False, "Realised states did not match stored states."
```
%% Cell type:code id: tags:
``` python
from ipywidgets import interact, interactive, fixed, interact_manual, Play
import ipywidgets as widgets
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from IPython.display import HTML
display.display(HTML('<link rel="stylesheet" href="//stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>'))
def plot_func(frame_idx):
frame = frames[int(frame_idx)]
display.display(frame['image'])
#print(frame['statuses'])
slider = widgets.FloatSlider(value=0, min=0, max=max_steps, step=1)
interact(plot_func, frame_idx = slider)
play = Play(
max=max_steps,
value=0,
step=1,
interval=250
)
widgets.link((play, 'value'), (slider, 'value'))
widgets.VBox([play])
```
%% Cell type:code id: tags:
``` python
```
import shlex
import sys
from subprocess import Popen, PIPE
import importlib_resources
import pkg_resources
from importlib_resources import path
import importlib_resources as ir
from ipython_genutils.py3compat import string_types, bytes_to_str
# taken from https://github.com/jupyter/nbconvert/blob/master/nbconvert/tests/base.py
def run_python(parameters, ignore_return_code=False, stdin=None):
"""
Run python as a shell command, listening for both Errors and
non-zero return codes. Returns the tuple (stdout, stderr) of
output produced during the nbconvert run.
Parameters
----------
parameters : str, list(str)
List of parameters to pass to IPython.
ignore_return_code : optional bool (default False)
Throw an OSError if the return code
"""
cmd = [sys.executable]
if sys.platform == 'win32':
if isinstance(parameters, string_types):
cmd = ' '.join(cmd) + ' ' + parameters
else:
cmd = ' '.join(cmd + parameters)
else:
if isinstance(parameters, string_types):
parameters = shlex.split(parameters)
cmd += parameters
p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout, stderr = p.communicate(input=stdin)
if not (p.returncode == 0 or ignore_return_code):
raise OSError(bytes_to_str(stderr))
return stdout.decode('utf8', 'replace'), stderr.decode('utf8', 'replace')
def main():
# If the file notebooks-list exists, use it as a definitive list of notebooks to run
# This in effect ignores any local notebooks you might be working on, so you can run tox
# without them causing the notebooks task / testenv to fail.
if importlib_resources.is_resource("notebooks", "notebook-list"):
print("Using the notebooks-list file to designate which notebooks to run")
lsNB = [
sLine for sLine in ir.read_text("notebooks", "notebook-list").split("\n")
if len(sLine) > 3 and not sLine.startswith("#")
]
else:
lsNB = [
entry for entry in importlib_resources.contents('notebooks') if
not pkg_resources.resource_isdir('notebooks', entry)
and entry.endswith(".ipynb")
]
print("Running notebooks:", " ".join(lsNB))
for entry in lsNB:
print("*****************************************************************")
print("Converting and running {}".format(entry))
print("*****************************************************************")
with path('notebooks', entry) as file_in:
out, err = run_python(" -m jupyter nbconvert --ExecutePreprocessor.timeout=120 " +
"--execute --to notebook --inplace " + str(file_in))
sys.stderr.write(err)
sys.stderr.flush()
sys.stdout.write(out)
sys.stdout.flush()
if __name__ == "__main__":
main()
\ No newline at end of file
%% Cell type:markdown id: tags:
# Railway Scene Editor
%% Cell type:code id: tags:
``` python
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))
```
%% Cell type:code id: tags:
``` python
from flatland.utils.editor import EditorMVC
mvc = EditorMVC(sGL="PILSVG" )
```
%% Cell type:markdown id: tags:
## Instructions
- Drag to draw (improved dead-ends)
- ctrl-click to add agent or select agent
- if agent is selected:
- ctrl-click to move agent position
- use rotate agent to rotate 90°
- ctrl-shift-click to set target for selected agent
- target can be moved by repeating
- to Resize the env (cannot preserve work):
- select "Regen" tab, set regen size slider, click regenerate.
- alt-click remove all rails from cell
Demo Scene: complex_scene.pkl
%% Cell type:code id: tags:
``` python
mvc.view.display()
```
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
%% Cell type:markdown id: tags:
### Simple Example 3 - Manual Control
By default this runs a few "move forward" actions for two agents, in a separate window.
If you uncomment the "input" line below, it opens a text box in the Jupyter notebook, allowing basic manual control.
eg Enter `"0 2 s<enter>"` to tell agent 0 to move forward, and step the environment.
You should be able to see the red agent step forward, and get a reward from the env, looking like this:
`Rewards: {0: -1.0, 1: -1.0} [done= {0: False, 1: False, '__all__': False} ]`
Note that this example is set up to use the straightforward "PIL" renderer - without the special SBB artwork!
The agent observations are displayed as squares of varying sizes, with a paler version of the agent colour. The targets are half-size squares in the full agent colour.
You can switch to the "PILSVG" renderer which is prettier but currently renders the agents one step behind, because it needs to know which way the agent is turning. This can be confusing if you are debugging step-by-step.
The image below is what the separate window should look like.
%% Cell type:markdown id: tags:
![simple_example_3.png](simple_example_3.png)
%% Cell type:code id: tags:
``` python
import random
import numpy as np
import time
```
%% Cell type:code id: tags:
``` python
from IPython import display
from ipycanvas import canvas
```
%% Cell type:code id: tags:
``` python
from flatland.envs.rail_generators import sparse_rail_generator
from flatland.envs.line_generators import sparse_line_generator
from flatland.envs.observations import TreeObsForRailEnv
from flatland.envs.predictions import ShortestPathPredictorForRailEnv
from flatland.envs.rail_env import RailEnv
from flatland.utils.rendertools import RenderTool
```
%% Cell type:code id: tags:
``` python
random.seed(1)
np.random.seed(1)
```
%% Cell type:code id: tags:
``` python
nAgents = 3
n_cities = 2
max_rails_between_cities = 2
max_rails_in_city = 4
seed = 0
env = RailEnv(
width=20,
height=30,
rail_generator=sparse_rail_generator(
max_num_cities=n_cities,
seed=seed,
grid_mode=True,
max_rails_between_cities=max_rails_between_cities,
max_rail_pairs_in_city=max_rails_in_city
),
line_generator=sparse_line_generator(),
number_of_agents=nAgents,
obs_builder_object=TreeObsForRailEnv(max_depth=3, predictor=ShortestPathPredictorForRailEnv())
)
init_observation = env.reset()
```
%% Cell type:code id: tags:
``` python
# Print the observation vector for agent 0
obs, all_rewards, done, _ = env.step({0: 0})
for i in range(env.get_num_agents()):
env.obs_builder.util_print_obs_subtree(tree=obs[i])
env_renderer = RenderTool(env, gl="PIL")
env_renderer.render_env(show=True, frames=True)
print("Manual control: s=perform step, q=quit, [agent id] [1-2-3 action] \
(turnleft+move, move to front, turnright+move)")
```
%% Cell type:code id: tags:
``` python
image_arr = env_renderer.get_image()
oCanvas = canvas.Canvas()
oCanvas.put_image_data(image_arr[:,:,0:3])
display.display(oCanvas)
for step in range(10):
# This is an example command, setting agent 0's action to 2 (move forward), and agent 1's action to 2,
# then stepping the environment.
cmd = "0 2 1 2 s"
# uncomment this input statement if you want to try interactive manual commands
# cmd = input(">> ")
cmds = cmd.split(" ")
action_dict = {}
i = 0
while i < len(cmds):
if cmds[i] == 'q':
import sys
sys.exit()
elif cmds[i] == 's':
obs, all_rewards, done, _ = env.step(action_dict)
action_dict = {}
print("Rewards: ", all_rewards, " [done=", done, "]")
else:
agent_id = int(cmds[i])
action = int(cmds[i + 1])
action_dict[agent_id] = action
i = i + 1
i += 1
env_renderer.render_env(show=True, frames=True)
gIm = env_renderer.get_image()
oCanvas.put_image_data(gIm[:,:,0:3])
time.sleep(0.1)
```
%% Cell type:markdown id: tags:
# Plot the Env and some trees
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
```
%% Cell type:code id: tags:
``` python
import jpy_canvas
import random
import time
import sys
```
%% Cell type:code id: tags:
``` python
# in case you need to tweak your PYTHONPATH...
sys.path.append("../flatland")
```
%% Cell type:code id: tags:
``` python
import flatland.core.env
import flatland.utils.rendertools as rt
from flatland.envs.rail_env import RailEnv
from flatland.envs.rail_generators import sparse_rail_generator
from flatland.envs.line_generators import sparse_line_generator
from flatland.envs.observations import TreeObsForRailEnv
from flatland.envs.predictions import ShortestPathPredictorForRailEnv
```
%% Cell type:markdown id: tags:
# Generate
%% Cell type:code id: tags:
``` python
nAgents = 3
n_cities = 2
max_rails_between_cities = 2
max_rails_in_city = 4
seed = 0
env = RailEnv(
width=20,
height=30,
rail_generator=sparse_rail_generator(
max_num_cities=n_cities,
seed=seed,
grid_mode=True,
max_rails_between_cities=max_rails_between_cities,
max_rail_pairs_in_city=max_rails_in_city
),
line_generator=sparse_line_generator(),
number_of_agents=nAgents,
obs_builder_object=TreeObsForRailEnv(max_depth=3, predictor=ShortestPathPredictorForRailEnv())
)
init_observation = env.reset()
```
%% Cell type:markdown id: tags:
# Render
%% Cell type:code id: tags:
``` python
oRT = rt.RenderTool(env,gl="PILSVG")
oRT.render_env(show_observations=False,show_predictions=True)
img = oRT.get_image()
```
%% Cell type:code id: tags:
``` python
jpy_canvas.Canvas(img)
```
%% Cell type:code id: tags:
``` python
oRT = rt.RenderTool(env,gl="PIL")
oRT.render_env(show_observations=False,show_predictions=True)
img = oRT.get_image()
```
%% Cell type:code id: tags:
``` python
jpy_canvas.Canvas(img)
```