-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Add test gym utils play. Fix #2729 #2743
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
Changes from 1 commit
87f34da
7982347
2a73571
02b81e5
7c65be7
1b3485f
48d5f07
3b9e08d
9a1d4b6
a6a7cd9
42b5ead
7dabd13
97fa4e9
808d0a1
bf4d2d8
52b37cc
5a8f6a4
b5aae1b
e9d333b
b0081c2
a02323c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,13 +20,15 @@ | |||||||
|
|
||||||||
|
|
||||||||
| class PlayableGame: | ||||||||
| def __init__(self, env, keys_to_action=None): | ||||||||
| def __init__(self, env, keys_to_action=None, zoom=None): | ||||||||
| self.env = env | ||||||||
| self.relevant_keys = self.get_relevant_keys(keys_to_action) | ||||||||
| self.relevant_keys = self._get_relevant_keys(keys_to_action) | ||||||||
| self.video_size = self._get_video_size(zoom) | ||||||||
| self.screen = pygame.display.set_mode(self.video_size) | ||||||||
| self.pressed_keys = [] | ||||||||
| self.running = True | ||||||||
|
|
||||||||
| def get_relevant_keys(self, keys_to_action): | ||||||||
| def _get_relevant_keys(self, keys_to_action): | ||||||||
| if keys_to_action is None: | ||||||||
| if hasattr(self.env, "get_keys_to_action"): | ||||||||
| keys_to_action = self.env.get_keys_to_action() | ||||||||
|
|
@@ -41,7 +43,7 @@ def get_relevant_keys(self, keys_to_action): | |||||||
| relevant_keys = set(sum(map(list, keys_to_action.keys()), [])) | ||||||||
| return relevant_keys | ||||||||
|
|
||||||||
| def get_video_size(self, zoom=None): | ||||||||
| def _get_video_size(self, zoom=None): | ||||||||
| rendered = self.env.render(mode="rgb_array") | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put like a TODO here so that we remember to update this when the render API change goes through? |
||||||||
| video_size = [rendered.shape[1], rendered.shape[0]] | ||||||||
|
|
||||||||
|
|
@@ -62,9 +64,8 @@ def process_event(self, event): | |||||||
| elif event.type == pygame.QUIT: | ||||||||
| self.running = False | ||||||||
| elif event.type == VIDEORESIZE: | ||||||||
| video_size = event.size | ||||||||
| screen = pygame.display.set_mode(video_size) | ||||||||
| print(video_size) | ||||||||
| self.video_size = event.size | ||||||||
| self.screen = pygame.display.set_mode(self.video_size) | ||||||||
|
|
||||||||
|
|
||||||||
| def display_arr(screen, arr, video_size, transpose): | ||||||||
|
||||||||
|
|
@@ -132,37 +133,30 @@ def callback(obs_t, obs_tp1, action, rew, done, info): | |||||||
| If None, default key_to_action mapping for that env is used, if provided. | ||||||||
| """ | ||||||||
| env.reset() | ||||||||
| game = PlayableGame(env, keys_to_action) | ||||||||
| game = PlayableGame(env, keys_to_action, zoom) | ||||||||
|
|
||||||||
| video_size = game.get_video_size(zoom) | ||||||||
|
|
||||||||
| env_done = True | ||||||||
|
|
||||||||
| screen = pygame.display.set_mode(video_size) | ||||||||
| done = True | ||||||||
| clock = pygame.time.Clock() | ||||||||
|
|
||||||||
| while game.running: | ||||||||
| if env_done: | ||||||||
| env_done = False | ||||||||
| if done: | ||||||||
| done = False | ||||||||
| obs = env.reset() | ||||||||
| else: | ||||||||
| action = keys_to_action.get(tuple(sorted(game.pressed_keys)), 0) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we always safely assume that In particular this seems like it'd crash with any continuous-action environments?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is actually the default action if no relevant keys are pressed; I have the same doubt regarding
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RedTachyon About this I think we can safely say that, since we can not map a continuos-action environment to discrete keyboard presses it is ok to leave gym/gym/envs/classic_control/mountain_car.py Lines 240 to 242 in 5ae6bf9
|
||||||||
| prev_obs = obs | ||||||||
| obs, rew, env_done, info = env.step(action) | ||||||||
| obs, rew, done, info = env.step(action) | ||||||||
| if callback is not None: | ||||||||
| callback(prev_obs, obs, action, rew, env_done, info) | ||||||||
| callback(prev_obs, obs, action, rew, done, info) | ||||||||
| if obs is not None: | ||||||||
| rendered = env.render(mode="rgb_array") | ||||||||
| display_arr(screen, rendered, transpose=transpose, video_size=video_size) | ||||||||
| display_arr( | ||||||||
| game.screen, rendered, transpose=transpose, video_size=game.video_size | ||||||||
| ) | ||||||||
|
|
||||||||
| # process pygame events | ||||||||
| for event in pygame.event.get(): | ||||||||
| game.process_event(event) | ||||||||
| # test events, set key states | ||||||||
| if event.type == VIDEORESIZE: | ||||||||
| video_size = event.size | ||||||||
| screen = pygame.display.set_mode(video_size) | ||||||||
| print(video_size) | ||||||||
|
|
||||||||
| pygame.display.flip() | ||||||||
| clock.tick(fps) | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mapis nice (functional programming is great), but in Python it's typically more readable to use a comprehension, so it'd be something likeset(sum(list(key) for key in keys_to_action), []))But is this actually what we want this to do? I'm not sure what each key is meant to be (see previous comment about types), so take another look at the logic here. Intuition tells me that you might have wanted to just do
set(keys_to_actions.keys())or something like that (which might be equivalent to justset(keys_to_actions))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to explore it more in depth, this logic was already there in the old
play(), it seems also to me thatset(keys_to_actions.keys())may be fine