-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
Add call, get_attr and set_attr methods to VectorEnv #1600
Conversation
Is this getting merged anytime soon? |
bf17629
to
afeff82
Compare
@pzhokhov I have rebased on master to remove any merge conflict. This PR is now ready to be merged or closed. |
afeff82
to
c309cd6
Compare
c309cd6
to
e7f19d3
Compare
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.
Nice work 🚀 I left some minor comments that you may choose to accept or reject depending on what you think the best solution is here.
Edit: FYI, I didn't review the test code; just the changes to the library code.
gym/vector/async_vector_env.py
Outdated
"Trying to call function `{0}` with " | ||
"`_call`. Use `{0}` directly instead.".format(name) |
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.
"Trying to call function `{0}` with " | |
"`_call`. Use `{0}` directly instead.".format(name) | |
f"Trying to call function `{name}` with " | |
f"`_call`. Use `{name}` directly instead." |
Perhaps this can become more readable with format strings.
gym/vector/async_vector_env.py
Outdated
if self._state != AsyncState.DEFAULT: | ||
raise AlreadyPendingCallError( | ||
"Calling `set_attr` while waiting " | ||
"for a pending call to `{0}` to complete.".format(self._state.value), |
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.
"for a pending call to `{0}` to complete.".format(self._state.value), | |
"for a pending call to `{self._state.value}` to complete.", |
Parameters | ||
---------- | ||
name : string | ||
Name of the method or property to call. |
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.
Do you think we should also add a line documenting args and kwargs here?
gym/vector/async_vector_env.py
Outdated
if self._state != AsyncState.DEFAULT: | ||
raise AlreadyPendingCallError( | ||
"Calling `call_async` while waiting " | ||
"for a pending call to `{0}` to complete.".format(self._state.value), |
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.
"for a pending call to `{0}` to complete.".format(self._state.value), | |
f"for a pending call to `{self._state.value}` to complete.", |
Since the minimum python version in CI is 3.6 I assume that we can use f-strings? If so, they might make things a little more readable here
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.
Indeed f-strings are much nicer! At the time CI was testing also for versions 2.7 and 3.5, that shows how old this PR is.
gym/vector/async_vector_env.py
Outdated
---------- | ||
timeout : int or float, optional | ||
Number of seconds before the call to `step_wait` times out. If | ||
`None`, the call to `step_wait` never times out. |
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.
`None`, the call to `step_wait` never times out. | |
`None` (default), the call to `step_wait` never times out. |
gym/vector/async_vector_env.py
Outdated
self._assert_is_running() | ||
if not isinstance(values, list): | ||
values = [values for _ in range(self.num_envs)] | ||
assert len(values) == self.num_envs |
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.
assert len(values) == self.num_envs | |
if len(values) == self.num_envs: | |
raise ValueError("values must have one value for each environment or be a single (non-list) object.") |
Potentially an opinionated comment: I'm not a fan of assert
outside of testing code. I'd raise a specific exception here that will tell the user what went wrong (instead of implicitly doing so by showing the assertion that failed.
Also, it may be worthwhile to remove this check and simply fail in the zip
loop below if necessary.
gym/vector/async_vector_env.py
Outdated
"Trying to call function `{0}` with " | ||
"`_call`. Use `{0}` directly instead.".format(name) |
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.
"Trying to call function `{0}` with " | |
"`_call`. Use `{0}` directly instead.".format(name) | |
f"Trying to call function `{name}` with " | |
f"`_call`. Use `{name}` directly instead." |
gym/vector/async_vector_env.py
Outdated
"be one of {`reset`, `step`, `seed`, `close`, `_call`, " | ||
"`_setattr`, `_check_observation_space`}.".format(command) |
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 can't suggest a format string here, since the relevant line was only partially modified, but I'd probably use it here, too.
gym/vector/sync_vector_env.py
Outdated
Parameters | ||
---------- | ||
name : string | ||
Name of the method or property to call. |
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.
Similar to above, would it be worthwhile to document args and kwargs?
gym/vector/sync_vector_env.py
Outdated
if not isinstance(values, list): | ||
values = [values for _ in range(self.num_envs)] | ||
assert len(values) == self.num_envs |
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.
Similar to above, are we worried about other iterables?
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.
Some updates based on the changes you added :)
gym/vector/async_vector_env.py
Outdated
|
||
args, kwargs : | ||
Arguments and keyword arguments to apply to the method call. |
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.
args, kwargs : | |
Arguments and keyword arguments to apply to the method call. | |
args, kwargs : | |
Arguments and keyword arguments to apply to the method call. |
Nitpick: remove extra whitespace.
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 didn't know what was the change you suggested (where the extra whitespace was), but I changed the docstring to match more closely the Numpy docstring guidelines. I also moved the docstrings from SyncVectorEnv
to the base class VectorEnv
to match what was done for the other methods.
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.
it's really just a small thing; there was a leading newline (\n
) above args, kwargs
that separated it from the previous parameter. If you look closely the suggestion changes 3 lines of code into 2 lines of code.
gym/vector/async_vector_env.py
Outdated
@@ -362,20 +362,25 @@ def set_attr(self, name, values): | |||
name : string | |||
Name of the property to be set in each individual environment. | |||
|
|||
values : list or object | |||
Values of the property to bet set to. If `values` is a list, then | |||
values : list of object |
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.
Should this be or
like before (suggested by the description and by the code), or of
as changed to now? If the latter, I think the description should be updated to reflect that change. Should the source code raise an exception on non-list types, too?
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.
No this was a mistake indeed, I thought it was a typo but it is list or object.
gym/vector/async_vector_env.py
Outdated
assert len(values) == self.num_envs | ||
if len(values) != self.num_envs: | ||
raise ValueError( | ||
"The values must be a list of length the number " |
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.
"The values must be a list of length the number " | |
"Values must be a list with length equal to the number " |
gym/vector/sync_vector_env.py
Outdated
@@ -125,14 +128,19 @@ def set_attr(self, name, values): | |||
name : string | |||
Name of the property to be set in each individual environment. | |||
|
|||
values : list or object | |||
Values of the property to bet set to. If `values` is a list, then | |||
values : list of object |
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.
Same question as previously; is the of
here intentional?
What is the status of this PR? Any timeline for this PR getting merged? @jkterry1 |
There's also merge conflicts. If you're happy with it I'll merge it once tests and merge conflicts are fixed. |
27d04f6
to
2d90563
Compare
LGTM |
* Add call, get_attr and set_attr methods * Use f-strings and remove assert * Allow tuples in set_attr and move docstrings * Replace CubeCrash by CartPole in tests
As suggested in #1513, this PR adds 3 new methods to
VectorEnv
:call(name, *args, **kwargs)
, which callsget_attr
in each individual environment, and returns either its propertyname
(in which case*args
and**kwargs
are ignored), or the result ofname(*args, **kwargs)
.get_attr(name)
, which is syntactic sugar for a call tocall
to get a property of the individual environments.set_attr(name, values)
, which allssetattr
on each individual environment.