Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render API #2671

Merged
merged 94 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
1aa736b
add pygame GUI for frozen_lake.py env
younik Jan 21, 2022
73a9e6b
add new line at EOF
younik Jan 21, 2022
c9a359a
pre-commit reformat
younik Jan 24, 2022
354830c
improve graphics
younik Jan 28, 2022
95258f5
new images and dynamic window size
younik Feb 1, 2022
a121b7b
darker tile borders and fix ICC profile
younik Feb 2, 2022
91f3a03
pre-commit hook
younik Feb 2, 2022
6f56feb
adjust elf and stool size
younik Feb 3, 2022
0abec2f
Update frozen_lake.py
jkterry1 Feb 3, 2022
ad87dbc
reformat
younik Feb 5, 2022
48c22fe
Merge branch 'openai:master' into master
younik Feb 7, 2022
6c38481
fix #2600
younik Feb 7, 2022
2649a1c
#2600
younik Feb 7, 2022
c2da74a
add rgb_array support
younik Feb 8, 2022
45a3154
reformat
younik Feb 8, 2022
db9ba2f
Merge branch 'master' into master
younik Feb 10, 2022
9711282
test render api change on FrozenLake
younik Mar 4, 2022
cd85df4
add render support for reset on frozenlake
younik Mar 5, 2022
1beba40
Merge branch 'render_api'
younik Mar 5, 2022
15d9321
add clock on pygame render
younik Mar 5, 2022
3089e57
new render api for blackjack
younik Mar 5, 2022
fad0f6f
new render api for cliffwalking
younik Mar 5, 2022
ffd9fc8
new render api for Env class
younik Mar 5, 2022
5140dc9
update reset method, lunar and Env
younik Mar 5, 2022
311ac82
fix wrapper
younik Mar 5, 2022
1197a36
fix reset lunar
younik Mar 5, 2022
da77145
new render api for box2d envs
younik Mar 5, 2022
18fe379
new render api for mujoco envs
younik Mar 6, 2022
d586394
fix bug
younik Mar 6, 2022
b078647
new render api for classic control envs
younik Mar 6, 2022
415449c
fix tests
younik Mar 9, 2022
9363aa0
add render_mode None for CartPole
younik Mar 10, 2022
11ab948
Merge branch 'master' into master
younik Mar 10, 2022
4912778
new render api for test fake envs
younik Mar 10, 2022
d5107d4
Merge remote-tracking branch 'origin/master'
younik Mar 10, 2022
98ab069
pre-commit hook
younik Mar 10, 2022
e06d293
fix FrozenLake
younik Mar 10, 2022
6fab5a2
fix FrozenLake
younik Mar 10, 2022
0f970ff
more render_mode to super - frozenlake
younik Mar 11, 2022
9999527
Merge remote-tracking branch 'origin/master'
younik Mar 11, 2022
f945184
Merge branch 'master' into master
younik Mar 11, 2022
4771755
remove kwargs from frozen_lake new
younik Mar 11, 2022
b458f4f
Merge remote-tracking branch 'origin/master'
younik Mar 11, 2022
1f5ddf1
pre-commit hook
younik Mar 11, 2022
96b3a7a
solve conflicts
younik Mar 17, 2022
64dcf77
add deprecated render method
younik Mar 23, 2022
5601778
Merge branch 'master' into master
younik Apr 2, 2022
cffacd7
Merge remote-tracking branch 'origin/master'
younik Apr 2, 2022
9570f30
add backwards compatibility
younik Apr 4, 2022
4345b21
fix test
younik Apr 4, 2022
99c6680
add _render
younik Apr 5, 2022
8ad9ed7
Merge branch 'master' into master
younik Apr 8, 2022
765c014
move pygame.init() (avoid pygame dependency on init)
younik Apr 8, 2022
45cbabd
fix pygame dependencies
younik Apr 8, 2022
7b13622
Merge branch 'master' into master
younik Apr 9, 2022
f53aa27
remove collect_render() maintain multi-behaviours .render()
younik Apr 19, 2022
4d76fe1
Merge remote-tracking branch 'origin/master'
younik Apr 19, 2022
2ab1824
Merge branch 'master' into master
younik Apr 19, 2022
343f72a
add type hints
younik Apr 21, 2022
c4bfe84
fix renderer
younik Apr 21, 2022
2b0ca9a
don't call .render() with None
younik Apr 21, 2022
0869ee7
improve docstring
younik Apr 21, 2022
d08b80d
add single_rgb_array to all envs
younik Apr 25, 2022
c7156bd
remove None from metadata["render_modes"]
younik Apr 25, 2022
e8e3c26
Merge branch 'master' into master
younik Apr 25, 2022
3a2f9b6
add type hints to test_env_checkers
younik Apr 25, 2022
a3ac176
Merge remote-tracking branch 'origin/master'
younik Apr 25, 2022
66b0c23
fix lint
younik Apr 25, 2022
ff4aff3
add comments to renderer
younik Apr 26, 2022
e0753ef
add comments to single_depth_array and single_state_pixels
younik Apr 30, 2022
3a961a3
Merge branch 'master' into master
younik Apr 30, 2022
07cf336
reformat
younik Apr 30, 2022
694220d
add deprecation warnings and env.render_mode declaration
younik May 7, 2022
9d04c6a
fix lint
younik May 7, 2022
52268f9
reformat
younik May 7, 2022
a0a409f
fix tests
younik May 7, 2022
d319228
Merge branch 'master' of https://github.com/openai/gym
younik May 23, 2022
ba01803
add docs
younik May 23, 2022
ce8d471
fix car racing determinism
younik May 23, 2022
f6a0c42
remove warning test envs, customizable modes on renderer
younik Jun 2, 2022
11d2260
remove commments and add todo for env_checker
younik Jun 3, 2022
7efffb8
fix car racing
younik Jun 3, 2022
5fe6e80
replace render mode check with assert
younik Jun 3, 2022
e4dc18c
Merge remote-tracking branch 'openai-gym/master'
younik Jun 3, 2022
0a979cc
update new mujoco
younik Jun 3, 2022
69326af
reformat
younik Jun 3, 2022
c220b4a
Merge remote-tracking branch 'openai-gym/master'
younik Jun 3, 2022
efda297
reformat
younik Jun 3, 2022
edb2c7a
change metaclass definition
younik Jun 4, 2022
88da572
Merge remote-tracking branch 'openai-gym/master'
younik Jun 4, 2022
540019c
fix tests
younik Jun 5, 2022
6c55c7b
implement mark suggestions (test, docs, sets)
younik Jun 6, 2022
34b8d08
Merge remote-tracking branch 'openai-gym/master'
younik Jun 6, 2022
bdb3220
check_render
younik Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 72 additions & 29 deletions gym/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
"""Core API for Environment, Wrapper, ActionWrapper, RewardWrapper and ObservationWrapper."""
import sys
from typing import Generic, Optional, SupportsFloat, Tuple, TypeVar, Union
from typing import (
Any,
Dict,
Generic,
List,
Optional,
SupportsFloat,
Tuple,
TypeVar,
Union,
)

from gym import spaces
from gym.logger import deprecation, warn
Expand All @@ -14,6 +24,44 @@

ObsType = TypeVar("ObsType")
ActType = TypeVar("ActType")
RenderFrame = TypeVar("RenderFrame")


class _EnvDecorator(type): # TODO: remove with gym 1.0
"""Metaclass used for adding deprecation warning to the mode kwarg in the render method."""

def __new__(cls, name, bases, attr):
if "render" in attr.keys():
attr["render"] = _EnvDecorator._deprecate_mode(attr["render"])

return super().__new__(cls, name, bases, attr)

@staticmethod
def _deprecate_mode(render_func): # type: ignore
render_return = Optional[Union[RenderFrame, List[RenderFrame]]]

def render(
self: object, *args: Tuple[Any], **kwargs: Dict[str, Any]
) -> render_return:
if "mode" in kwargs.keys():
deprecation(
"The argument mode in render method is deprecated; "
"use render_mode during environment initialization instead.\n"
"See here for more information: https://www.gymlibrary.ml/content/api/"
)
elif self.spec is not None and "render_mode" not in self.spec.kwargs.keys(): # type: ignore
deprecation(
"You are calling render method, "
"but you didn't specified the argument render_mode at environment initialization. "
"To maintain backward compatibility, the environment will render in human mode.\n"
"If you want to render in human mode, initialize the environment in this way: "
"gym.make('EnvName', render_mode='human') and don't call the render method.\n"
"See here for more information: https://www.gymlibrary.ml/content/api/"
)

return render_func(self, *args, **kwargs)

return render


class Env(Generic[ObsType, ActType]):
Expand Down Expand Up @@ -43,8 +91,11 @@ class Env(Generic[ObsType, ActType]):
Note: a default reward range set to :math:`(-\infty,+\infty)` already exists. Set it if you want a narrower range.
"""

__metaclass__ = _EnvDecorator

# Set this in SOME subclasses
metadata = {"render_modes": []}
render_mode = None # define render_mode if your environment supports rendering
reward_range = (-float("inf"), float("inf"))
spec = None

Expand Down Expand Up @@ -130,42 +181,34 @@ def reset(
if seed is not None:
self._np_random, seed = seeding.np_random(seed)

def render(self, mode="human"):
"""Renders the environment.
# TODO: remove kwarg mode with gym 1.0
def render(self, mode="human") -> Optional[Union[RenderFrame, List[RenderFrame]]]:
"""Compute the render frames as specified by render_mode attribute during initialization of the environment.

A set of supported modes varies per environment. (And some
The set of supported modes varies per environment. (And some
third-party environments may not support rendering at all.)
By convention, if mode is:
By convention, if render_mode is:

- None (default): no render is computed.
- human: render return None.
The environment is continuously rendered in the current display or terminal. Usually for human consumption.
- single_rgb_array: return a single frame representing the current state of the environment.
A frame is a numpy.ndarray with shape (x, y, 3) representing RGB values for an x-by-y pixel image.
- rgb_array: return a list of frames representing the states of the environment since the last reset.
Each frame is a numpy.ndarray with shape (x, y, 3), as with single_rgb_array.
- ansi: Return a list of strings (str) or StringIO.StringIO containing a
terminal-style text representation for each time step.
The text can include newlines and ANSI escape sequences (e.g. for colors).

- human: render to the current display or terminal and
return nothing. Usually for human consumption.
- rgb_array: Return a numpy.ndarray with shape (x, y, 3),
representing RGB values for an x-by-y pixel image, suitable
for turning into a video.
- ansi: Return a string (str) or StringIO.StringIO containing a
terminal-style text representation. The text can include newlines
and ANSI escape sequences (e.g. for colors).
Note:
Rendering computations is performed internally even if you don't call render().
To avoid this, you can set render_mode = None and, if the environment supports it,
call render() specifying the argument 'mode'.

Note:
Make sure that your class's metadata 'render_modes' key includes
the list of supported modes. It's recommended to call super()
in implementations to use the functionality of this method.

Example:
>>> import numpy as np
>>> class MyEnv(Env):
... metadata = {'render_modes': ['human', 'rgb_array']}
...
... def render(self, mode='human'):
... if mode == 'rgb_array':
... return np.array(...) # return RGB frame suitable for video
... elif mode == 'human':
... ... # pop up a window and render
... else:
... super().render(mode=mode) # just raise an exception

Args:
mode: the mode to render with, valid modes are `env.metadata["render_modes"]`
"""
raise NotImplementedError

Expand Down
56 changes: 36 additions & 20 deletions gym/envs/box2d/bipedal_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from gym import error, spaces
from gym.error import DependencyNotInstalled
from gym.utils import EzPickle
from gym.utils.renderer import Renderer

try:
import Box2D
Expand Down Expand Up @@ -159,12 +160,13 @@ class BipedalWalker(gym.Env, EzPickle):

"""

metadata = {"render_modes": ["human", "rgb_array"], "render_fps": FPS}
metadata = {
"render_modes": ["human", "rgb_array", "single_rgb_array"],
"render_fps": FPS,
}

def __init__(self, hardcore: bool = False):
def __init__(self, render_mode: Optional[str] = None, hardcore: bool = False):
EzPickle.__init__(self)
self.screen = None
self.clock = None
self.isopen = True

self.world = Box2D.b2World()
Expand Down Expand Up @@ -252,6 +254,12 @@ def __init__(self, hardcore: bool = False):
# ]
# state += [l.fraction for l in self.lidar]

assert render_mode is None or render_mode in self.metadata["render_modes"]
self.render_mode = render_mode
self.renderer = Renderer(self.render_mode, self._render)
self.screen = None
self.clock = None

def _destroy(self):
if not self.terrain:
return
Expand Down Expand Up @@ -500,6 +508,7 @@ def ReportFixture(self, fixture, point, normal, fraction):
return fraction

self.lidar = [LidarCallback() for _ in range(10)]
self.renderer.reset()
if not return_info:
return self.step(np.array([0, 0, 0, 0]))[0]
else:
Expand Down Expand Up @@ -589,9 +598,18 @@ def step(self, action: np.ndarray):
done = True
if pos[0] > (TERRAIN_LENGTH - TERRAIN_GRASS) * TERRAIN_STEP:
done = True

self.renderer.render_step()
return np.array(state, dtype=np.float32), reward, done, {}

def render(self, mode: str = "human"):
if self.render_mode is not None:
return self.renderer.get_renders()
else:
return self._render(mode)

def _render(self, mode: str = "human"):
assert mode in self.metadata["render_modes"]
try:
import pygame
from pygame import gfxdraw
Expand All @@ -600,7 +618,7 @@ def render(self, mode: str = "human"):
"pygame is not installed, run `pip install gym[box2d]`"
)

if self.screen is None:
if self.screen is None and mode == "human":
pygame.init()
pygame.display.init()
self.screen = pygame.display.set_mode((VIEWPORT_W, VIEWPORT_H))
Expand Down Expand Up @@ -653,18 +671,19 @@ def render(self, mode: str = "human"):
self.lidar_render = (self.lidar_render + 1) % 100
i = self.lidar_render
if i < 2 * len(self.lidar):
l = (
single_lidar = (
self.lidar[i]
if i < len(self.lidar)
else self.lidar[len(self.lidar) - i - 1]
)
pygame.draw.line(
self.surf,
color=(255, 0, 0),
start_pos=(l.p1[0] * SCALE, l.p1[1] * SCALE),
end_pos=(l.p2[0] * SCALE, l.p2[1] * SCALE),
width=1,
)
if hasattr(single_lidar, "p1") and hasattr(single_lidar, "p2"):
pygame.draw.line(
self.surf,
color=(255, 0, 0),
start_pos=(single_lidar.p1[0] * SCALE, single_lidar.p1[1] * SCALE),
end_pos=(single_lidar.p2[0] * SCALE, single_lidar.p2[1] * SCALE),
width=1,
)

for obj in self.drawlist:
for f in obj.fixtures:
Expand Down Expand Up @@ -717,18 +736,16 @@ def render(self, mode: str = "human"):
)

self.surf = pygame.transform.flip(self.surf, False, True)
self.screen.blit(self.surf, (-self.scroll * SCALE, 0))

if mode == "human":
self.screen.blit(self.surf, (-self.scroll * SCALE, 0))
pygame.event.pump()
self.clock.tick(self.metadata["render_fps"])
pygame.display.flip()

if mode == "rgb_array":
elif mode in {"rgb_array", "single_rgb_array"}:
return np.transpose(
np.array(pygame.surfarray.pixels3d(self.screen)), axes=(1, 0, 2)
np.array(pygame.surfarray.pixels3d(self.surf)), axes=(1, 0, 2)
)
else:
return self.isopen

def close(self):
if self.screen is not None:
Expand Down Expand Up @@ -829,6 +846,5 @@ def __init__(self):
a[3] = knee_todo[1]
a = np.clip(0.5 * a, -1.0, 1.0)

env.render()
if done:
break
39 changes: 33 additions & 6 deletions gym/envs/box2d/car_racing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from gym.envs.box2d.car_dynamics import Car
from gym.error import DependencyNotInstalled, InvalidAction
from gym.utils import EzPickle
from gym.utils.renderer import Renderer

try:
import Box2D
Expand Down Expand Up @@ -151,12 +152,19 @@ class CarRacing(gym.Env, EzPickle):
"""

metadata = {
"render_modes": ["human", "rgb_array", "state_pixels"],
"render_modes": [
"human",
"rgb_array",
"state_pixels",
"single_rgb_array",
"single_state_pixels",
],
"render_fps": FPS,
}

def __init__(
self,
render_mode: Optional[str] = None,
verbose: bool = False,
lap_complete_percent: float = 0.95,
domain_randomize: bool = False,
Expand All @@ -170,6 +178,7 @@ def __init__(
self.contactListener_keepref = FrictionDetector(self, lap_complete_percent)
self.world = Box2D.b2World((0, 0), contactListener=self.contactListener_keepref)
self.screen = None
self.surf = None
self.clock = None
self.isopen = True
self.invisible_state_window = None
Expand Down Expand Up @@ -199,6 +208,10 @@ def __init__(
low=0, high=255, shape=(STATE_H, STATE_W, 3), dtype=np.uint8
)

assert render_mode is None or render_mode in self.metadata["render_modes"]
self.render_mode = render_mode
self.renderer = Renderer(self.render_mode, self._render)

def _destroy(self):
if not self.road:
return
Expand Down Expand Up @@ -441,6 +454,7 @@ def reset(
)
self.car = Car(self.world, *self.track[0][1:4])

self.renderer.reset()
if not return_info:
return self.step(None)[0]
else:
Expand All @@ -466,7 +480,7 @@ def step(self, action: Union[np.ndarray, int]):
self.world.Step(1.0 / FPS, 6 * 30, 2 * 30)
self.t += 1.0 / FPS

self.state = self.render("state_pixels")
self.state = self._render("single_state_pixels")

step_reward = 0
done = False
Expand All @@ -484,9 +498,17 @@ def step(self, action: Union[np.ndarray, int]):
done = True
step_reward = -100

self.renderer.render_step()
return self.state, step_reward, done, {}

def render(self, mode: str = "human"):
if self.render_mode is not None:
return self.renderer.get_renders()
else:
return self._render(mode)

def _render(self, mode: str = "human"):
assert mode in self.metadata["render_modes"]
try:
import pygame
except ImportError:
Expand All @@ -496,7 +518,6 @@ def render(self, mode: str = "human"):

pygame.font.init()

assert mode in ["human", "state_pixels", "rgb_array"]
if self.screen is None and mode == "human":
pygame.init()
pygame.display.init()
Expand All @@ -519,7 +540,13 @@ def render(self, mode: str = "human"):
trans = (WINDOW_W / 2 + trans[0], WINDOW_H / 4 + trans[1])

self._render_road(zoom, trans, angle)
self.car.draw(self.surf, zoom, trans, angle, mode != "state_pixels")
self.car.draw(
self.surf,
zoom,
trans,
angle,
mode not in ["state_pixels", "single_state_pixels"],
)

self.surf = pygame.transform.flip(self.surf, False, True)

Expand All @@ -539,9 +566,9 @@ def render(self, mode: str = "human"):
self.screen.blit(self.surf, (0, 0))
pygame.display.flip()

if mode == "rgb_array":
if mode in {"rgb_array", "single_rgb_array"}:
return self._create_image_array(self.surf, (VIDEO_W, VIDEO_H))
elif mode == "state_pixels":
elif mode in {"state_pixels", "single_state_pixels"}:
return self._create_image_array(self.surf, (STATE_W, STATE_H))
else:
return self.isopen
Expand Down
Loading