Commit 58b93bca authored by Erik Nygren's avatar Erik Nygren
Browse files

huge refactoring of render code

also added new prediction rendering
editor still broken with this update
parent 2c17b423
......@@ -99,7 +99,7 @@ Basic usage of the RailEnv environment used by the Flatland Challenge
_action = my_controller()
obs, all_rewards, done, _ = env.step(_action)
print("Rewards: {}, [done={}]".format( all_rewards, done))
env_renderer.renderEnv(show=True, frames=False, show_observations=False)
env_renderer.render_env(show=True, frames=False, show_observations=False)
time.sleep(0.3)
and **ideally** you should see something along the lines of
......
......@@ -43,5 +43,5 @@ Frequently Asked Questions (FAQs)
Renders the scene into a image (screenshot)
.. code-block:: python
renderer.gl.saveImage("filename.bmp")
renderer.gl.save_image("filename.bmp")
......@@ -79,7 +79,7 @@ Environments can be rendered using the utils.rendertools utilities, for example:
.. code-block:: python
env_renderer = RenderTool(env)
env_renderer.renderEnv(show=True)
env_renderer.render_env(show=True)
Finally, the environment can be run by supplying the environment step function
......
......@@ -37,6 +37,6 @@ env = RailEnv(width=6,
env.reset()
env_renderer = RenderTool(env)
env_renderer.renderEnv(show=True)
env_renderer.render_env(show=True)
input("Press Enter to continue...")
......@@ -102,7 +102,7 @@ class Demo:
action_dict.update({iAgent: action})
# render
self.renderer.renderEnv(show=True, show_observations=False)
self.renderer.render_env(show=True, show_observations=False)
# environment step (apply the actions to all agents)
next_obs, all_rewards, done, _ = self.env.step(action_dict)
......@@ -111,7 +111,7 @@ class Demo:
break
if self.record_frames is not None:
self.renderer.gl.saveImage(self.record_frames.format(step))
self.renderer.gl.save_image(self.record_frames.format(step))
self.renderer.close_window()
......
......@@ -117,7 +117,7 @@ def main(render=True, delay=0.0, n_trials=3, n_steps=50):
for step in range(n_steps):
oPlayer.step()
if render:
env_renderer.renderEnv(show=True, frames=True, iEpisode=trials, iStep=step)
env_renderer.render_env(show=True, frames=True, episode=trials, step=step)
if delay > 0:
time.sleep(delay)
......
......@@ -19,7 +19,7 @@ env = RailEnv(width=6,
env.reset()
env_renderer = RenderTool(env)
env_renderer.renderEnv(show=True)
env_renderer.renderEnv(show=True)
env_renderer.render_env(show=True)
env_renderer.render_env(show=True)
input("Press Enter to continue...")
......@@ -33,7 +33,7 @@ env = RailEnv(width=10,
env.reset()
env_renderer = RenderTool(env, gl="PIL")
env_renderer.renderEnv(show=True)
env_renderer.renderEnv(show=True)
env_renderer.render_env(show=True)
env_renderer.render_env(show=True)
input("Press Enter to continue...")
......@@ -22,8 +22,8 @@ for i in range(env.get_num_agents()):
env.obs_builder.util_print_obs_subtree(tree=obs[i])
env_renderer = RenderTool(env)
env_renderer.renderEnv(show=True, frames=True)
env_renderer.renderEnv(show=True, frames=True)
env_renderer.render_env(show=True, frames=True)
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)")
......@@ -50,4 +50,4 @@ for step in range(100):
i = i + 1
i += 1
env_renderer.renderEnv(show=True, frames=True)
env_renderer.render_env(show=True, frames=True)
......@@ -26,8 +26,8 @@ def tkmain(n_trials=2, n_steps=50, sGL="PIL"):
for step in range(n_steps):
oPlayer.step()
env_renderer.renderEnv(show=True, frames=True, iEpisode=trials, iStep=step,
action_dict=oPlayer.action_dict)
env_renderer.render_env(show=True, frames=True, episode=trials, step=step,
action_dict=oPlayer.action_dict)
env_renderer.close_window()
......
......@@ -122,13 +122,16 @@ class ShortestPathPredictorForRailEnv(PredictionBuilder):
_agent_initial_direction = agent.direction
prediction = np.zeros(shape=(self.max_depth + 1, 5))
prediction[0] = [0, *_agent_initial_position, _agent_initial_direction, 0]
visited = set()
for index in range(1, self.max_depth + 1):
# if we're at the target, stop moving...
if agent.position == agent.target:
prediction[index] = [index, *agent.target, agent.direction, RailEnvActions.STOP_MOVING]
visited.add((agent.position[0], agent.position[1], agent.direction))
continue
if not agent.moving:
prediction[index] = [index, *agent.position, agent.direction, RailEnvActions.STOP_MOVING]
visited.add((agent.position[0], agent.position[1], agent.direction))
continue
# Take shortest possible path
cell_transitions = self.env.rail.get_transitions(*agent.position, agent.direction)
......@@ -159,6 +162,8 @@ class ShortestPathPredictorForRailEnv(PredictionBuilder):
# prediction is ready
prediction[index] = [index, *new_position, new_direction, 0]
visited.add((new_position[0], new_position[1], new_direction))
self.env.dev_pred_dict[agent.handle] = visited
prediction_dict[agent.handle] = prediction
# cleanup: reset initial position
......
......@@ -128,6 +128,7 @@ class RailEnv(Environment):
self.obs_dict = {}
self.rewards_dict = {}
self.dev_obs_dict = {}
self.dev_pred_dict = {}
self.agents = [None] * number_of_agents # live agents
self.agents_static = [None] * number_of_agents # static agent information
......
......@@ -48,14 +48,14 @@ class View(object):
self.sGL = sGL
def display(self):
self.wOutput.clear_output()
self.output.clear_output()
return self.wMain
def init_canvas(self):
# update the rendertool with the env
self.new_env()
self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", show=False)
img = self.oRT.getImage()
self.oRT.render_env(spacing=False, arrows=False, sRailColor="gray", show=False)
img = self.oRT.get_image()
self.wImage = jpy_canvas.Canvas(img)
self.yxSize = self.wImage.data.shape[:2]
self.writableData = np.copy(self.wImage.data) # writable copy of image - wid_img.data is somehow readonly
......@@ -67,51 +67,51 @@ class View(object):
def init_widgets(self):
# Debug checkbox - enable logging in the Output widget
self.wDebug = ipywidgets.Checkbox(description="Debug")
self.wDebug.observe(self.controller.setDebug, names="value")
self.debug = ipywidgets.Checkbox(description="Debug")
self.debug.observe(self.controller.set_debug, names="value")
# Separate checkbox for mouse move events - they are very verbose
self.wDebug_move = Checkbox(description="Debug mouse move")
self.wDebug_move.observe(self.controller.setDebugMove, names="value")
self.debug_move = Checkbox(description="Debug mouse move")
self.debug_move.observe(self.controller.set_debug_move, names="value")
# This is like a cell widget where loggin goes
self.wOutput = Output()
self.output = Output()
# Filename textbox
self.wFilename = Text(description="Filename")
self.wFilename.value = self.model.env_filename
self.wFilename.observe(self.controller.setFilename, names="value")
self.filename = Text(description="Filename")
self.filename.value = self.model.env_filename
self.filename.observe(self.controller.set_filename, names="value")
# Size of environment when regenerating
self.wRegenSizeWidth = IntSlider(value=10, min=5, max=100, step=5, description="Regen Size (Width)",
tip="Click Regenerate after changing this")
self.wRegenSizeWidth.observe(self.controller.setRegenSizeWidth, names="value")
self.regen_width = IntSlider(value=10, min=5, max=100, step=5, description="Regen Size (Width)",
tip="Click Regenerate after changing this")
self.regen_width.observe(self.controller.set_regen_width, names="value")
self.wRegenSizeHeight = IntSlider(value=10, min=5, max=100, step=5, description="Regen Size (Height)",
tip="Click Regenerate after changing this")
self.wRegenSizeHeight.observe(self.controller.setRegenSizeHeight, names="value")
self.regen_height = IntSlider(value=10, min=5, max=100, step=5, description="Regen Size (Height)",
tip="Click Regenerate after changing this")
self.regen_height.observe(self.controller.set_regen_height, names="value")
# Number of Agents when regenerating
self.wRegenNAgents = IntSlider(value=1, min=0, max=5, step=1, description="# Agents",
tip="Click regenerate or reset after changing this")
self.wRegenMethod = RadioButtons(description="Regen\nMethod", options=["Empty", "Random Cell"])
self.regen_n_agents = IntSlider(value=1, min=0, max=5, step=1, description="# Agents",
tip="Click regenerate or reset after changing this")
self.regen_method = RadioButtons(description="Regen\nMethod", options=["Empty", "Random Cell"])
self.wReplaceAgents = Checkbox(value=True, description="Replace Agents")
self.replace_agents = Checkbox(value=True, description="Replace Agents")
self.wTab = Tab()
tab_contents = ["Regen", "Observation"]
for i, title in enumerate(tab_contents):
self.wTab.set_title(i, title)
self.wTab.children = [
VBox([self.wRegenSizeWidth, self.wRegenSizeHeight, self.wRegenNAgents, self.wRegenMethod])
VBox([self.regen_width, self.regen_height, self.regen_n_agents, self.regen_method])
]
# abbreviated description of buttons and the methods they call
ldButtons = [
dict(name="Refresh", method=self.controller.refresh, tip="Redraw only"),
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.restart_agents,
tip="Move agents back to start positions"),
dict(name="Random", method=self.controller.reset,
tip="Generate a randomized scene, including regen rail + agents"),
......@@ -119,7 +119,7 @@ class View(object):
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="Save as image", method=self.controller.saveImage)
dict(name="Save as image", method=self.controller.save_image)
]
self.lwButtons = []
......@@ -130,13 +130,13 @@ class View(object):
self.lwButtons.append(wButton)
self.wVbox_controls = VBox([
self.wFilename,
self.filename,
*self.lwButtons,
self.wTab])
self.wMain = HBox([self.wImage, self.wVbox_controls])
def drawStroke(self):
def draw_stroke(self):
pass
def new_env(self):
......@@ -145,7 +145,7 @@ class View(object):
self.oRT = rt.RenderTool(self.editor.env, gl=self.sGL)
def redraw(self):
with self.wOutput:
with self.output:
self.oRT.set_new_rail()
self.model.env.agents = self.model.env.agents_static
......@@ -155,10 +155,10 @@ class View(object):
if hasattr(a, 'old_direction') is False:
a.old_direction = a.direction
self.oRT.renderEnv(spacing=False, arrows=False, sRailColor="gray", agents=True,
show=False, iSelectedAgent=self.model.iSelectedAgent,
show_observations=False)
img = self.oRT.getImage()
self.oRT.render_env(spacing=False, arrows=False, sRailColor="gray", agents=True,
show=False, selected_agent=self.model.selected_agent,
show_observations=False)
img = self.oRT.get_image()
self.wImage.data = img
self.writableData = np.copy(self.wImage.data)
......@@ -167,7 +167,7 @@ class View(object):
self.yxSize = self.wImage.data.shape[:2]
return img
def redisplayImage(self):
def redisplay_image(self):
if self.writableData is not None:
# This updates the image in the browser to be the new edited version
self.wImage.data = self.writableData
......@@ -186,8 +186,8 @@ class View(object):
return rcCell
def log(self, *args, **kwargs):
if self.wOutput:
with self.wOutput:
if self.output:
with self.output:
print(*args, **kwargs)
else:
print(*args, **kwargs)
......@@ -207,7 +207,7 @@ class Controller(object):
self.qEvents = deque()
self.drawMode = "Draw"
def setModel(self, model):
def set_model(self, model):
self.model = model
def on_click(self, wid, event):
......@@ -227,26 +227,26 @@ class Controller(object):
self.model.add_target(rcCell)
self.lrcStroke = []
elif bAlt and not bShift and not bCtrl:
self.model.clearCell(rcCell)
self.model.clear_cell(rcCell)
self.lrcStroke = []
self.debug("click in cell", rcCell)
self.model.debug_cell(rcCell)
if self.model.iSelectedAgent is not None:
if self.model.selected_agent is not None:
self.lrcStroke = []
def setDebug(self, dEvent):
self.model.setDebug(dEvent["new"])
def set_debug(self, dEvent):
self.model.set_debug(dEvent["new"])
def setDebugMove(self, dEvent):
def set_debug_move(self, dEvent):
self.model.setDebug_move(dEvent["new"])
def setDrawMode(self, dEvent):
def set_draw_mode(self, dEvent):
self.drawMode = dEvent["new"]
def setFilename(self, event):
self.model.setFilename(event["new"])
def set_filename(self, event):
self.model.set_filename(event["new"])
def on_mouse_move(self, wid, event):
"""Mouse motion event handler for drawing.
......@@ -285,7 +285,7 @@ class Controller(object):
else:
self.lrcStroke = []
if self.model.iSelectedAgent is not None:
if self.model.selected_agent is not None:
self.lrcStroke = []
while len(qEvents) > 0:
t, x, y = qEvents.popleft()
......@@ -307,7 +307,7 @@ class Controller(object):
rcCell = self.view.xy_to_rc(x, y)
self.editor.drag_path_element(rcCell)
self.view.redisplayImage()
self.view.redisplay_image()
else:
self.model.mod_path(not event["shiftKey"])
......@@ -320,25 +320,25 @@ class Controller(object):
self.model.clear()
def reset(self, event):
self.log("Reset - nAgents:", self.view.wRegenNAgents.value)
self.log("Reset - nAgents:", self.view.regen_n_agents.value)
self.log("Reset - size:", self.model.regen_size_width)
self.log("Reset - size:", self.model.regen_size_height)
self.model.reset(replace_agents=self.view.wReplaceAgents.value,
nAgents=self.view.wRegenNAgents.value)
self.model.reset(replace_agents=self.view.replace_agents.value,
nAgents=self.view.regen_n_agents.value)
def rotate_agent(self, event):
self.log("Rotate Agent:", self.model.iSelectedAgent)
if self.model.iSelectedAgent is not None:
self.log("Rotate Agent:", self.model.selected_agent)
if self.model.selected_agent is not None:
for iAgent, agent in enumerate(self.model.env.agents_static):
if agent is None:
continue
if iAgent == self.model.iSelectedAgent:
if iAgent == self.model.selected_agent:
agent.direction = (agent.direction + 1) % 4
agent.old_direction = agent.direction
self.model.redraw()
def restartAgents(self, event):
self.log("Restart Agents - nAgents:", self.view.wRegenNAgents.value)
def restart_agents(self, event):
self.log("Restart Agents - nAgents:", self.view.regen_n_agents.value)
if self.model.init_agents_static is not None:
self.model.env.agents_static = [EnvAgentStatic(d[0], d[1], d[2], moving=False) for d in
self.model.init_agents_static]
......@@ -349,15 +349,15 @@ class Controller(object):
self.refresh(event)
def regenerate(self, event):
method = self.view.wRegenMethod.value
nAgents = self.view.wRegenNAgents.value
self.model.regenerate(method, nAgents)
method = self.view.regen_method.value
n_agents = self.view.regen_n_agents.value
self.model.regenerate(method, n_agents)
def setRegenSizeWidth(self, event):
self.model.setRegenSizeWidth(event["new"])
def set_regen_width(self, event):
self.model.set_regen_width(event["new"])
def setRegenSizeHeight(self, event):
self.model.setRegenSizeHeight(event["new"])
def set_regen_height(self, event):
self.model.set_regen_height(event["new"])
def load(self, event):
self.model.load()
......@@ -365,8 +365,8 @@ class Controller(object):
def save(self, event):
self.model.save()
def saveImage(self, event):
self.model.saveImage()
def save_image(self, event):
self.model.save_image()
def step(self, event):
self.model.step()
......@@ -392,13 +392,13 @@ class EditorModel(object):
self.iTransLast = -1
self.gRCTrans = array([[-1, 0], [0, 1], [1, 0], [0, -1]]) # NESW in RC
self.bDebug = False
self.bDebug_move = False
self.debug_bool = False
self.debug_move_bool = False
self.wid_output = None
self.drawMode = "Draw"
self.env_filename = "temp.pkl"
self.set_env(env)
self.iSelectedAgent = None
self.selected_agent = None
self.init_agents_static = None
self.thread = None
self.saveImageCnt = 0
......@@ -409,15 +409,15 @@ class EditorModel(object):
"""
self.env = env
def setDebug(self, bDebug):
self.bDebug = bDebug
self.log("Set Debug:", self.bDebug)
def set_debug(self, bDebug):
self.debug_bool = bDebug
self.log("Set Debug:", self.debug_bool)
def setDebugMove(self, bDebug):
self.bDebug_move = bDebug
self.log("Set DebugMove:", self.bDebug_move)
def set_debug_move(self, bDebug):
self.debug_move_bool = bDebug
self.log("Set DebugMove:", self.debug_move_bool)
def setDrawMode(self, sDrawMode):
def set_draw_mode(self, sDrawMode):
self.drawMode = sDrawMode
def interpolate_path(self, rcLast, rcCell):
......@@ -605,7 +605,7 @@ class EditorModel(object):
self.redraw()
def clearCell(self, rcCell):
def clear_cell(self, rcCell):
self.debug_cell(rcCell)
self.env.rail.grid[rcCell[0], rcCell[1]] = 0
self.redraw()
......@@ -614,11 +614,11 @@ class EditorModel(object):
self.regenerate("complex", nAgents=nAgents)
self.redraw()
def restartAgents(self):
def restart_agents(self):
self.env.agents = EnvAgent.list_from_static(self.env.agents_static)
self.redraw()
def setFilename(self, filename):
def set_filename(self, filename):
self.env_filename = filename
def load(self):
......@@ -650,8 +650,8 @@ class EditorModel(object):
# reset agents current (current position)
self.env.agents = temp_store
def saveImage(self):
self.view.oRT.gl.saveImage('frame_{:04d}.bmp'.format(self.saveImageCnt))
def save_image(self):
self.view.oRT.gl.save_image('frame_{:04d}.bmp'.format(self.saveImageCnt))
self.saveImageCnt += 1
self.view.redraw()
......@@ -681,10 +681,10 @@ class EditorModel(object):
self.view.new_env()
self.redraw()
def setRegenSizeWidth(self, size):
def set_regen_width(self, size):
self.regen_size_width = size
def setRegenSizeHeight(self, size):
def set_regen_height(self, size):
self.regen_size_height = size
def find_agent_at(self, rcCell):
......@@ -702,37 +702,37 @@ class EditorModel(object):
"""
# Has the user clicked on an existing agent?
iAgent = self.find_agent_at(rcCell)
agent_idx = self.find_agent_at(rcCell)
if iAgent is None:
if agent_idx is None:
# No
if self.iSelectedAgent is None:
if self.selected_agent is None:
# Create a new agent and select it.
agent_static = EnvAgentStatic(position=rcCell, direction=0, target=rcCell, moving=False)
self.iSelectedAgent = self.env.add_agent_static(agent_static)
self.selected_agent = self.env.add_agent_static(agent_static)
self.view.oRT.update_background()
else:
# Move the selected agent to this cell
agent_static = self.env.agents_static[self.iSelectedAgent]
agent_static = self.env.agents_static[self.selected_agent]
agent_static.position = rcCell
agent_static.old_position = rcCell
self.env.agents = []
else:
# Yes
# Have they clicked on the agent already selected?
if self.iSelectedAgent is not None and iAgent == self.iSelectedAgent:
if self.selected_agent is not None and agent_idx == self.selected_agent:
# Yes - deselect the agent
self.iSelectedAgent = None
self.selected_agent = None
else:
# No - select the agent
self.iSelectedAgent = iAgent
self.selected_agent = agent_idx
self.init_agents_static = None
self.redraw()
def add_target(self, rcCell):
if self.iSelectedAgent is not None:
self.env.agents_static[self.iSelectedAgent].target = rcCell
if self.selected_agent is not None:
self.env.agents_static[self.selected_agent].target = rcCell
self.init_agents_static = None
self.view.oRT.update_background()
self.redraw()
......@@ -748,7 +748,7 @@ class EditorModel(object):
self.view.log(*args, **kwargs)
def debug(self, *args, **kwargs):
if self.bDebug:
if self.debug_bool:
self.log(*args, **kwargs)
def debug_cell(self, rcCell):
......
......@@ -33,19 +33,19 @@ class GraphicsLayer(object):
def clf(self):
pass
def beginFrame(self):
def begin_frame(self):
pass
def endFrame(self):
pass
def getImage(self):
def get_image(self):
pass
def saveImage(self, filename):
def save_image(self, filename):
pass