Skip to content

Commit

Permalink
Fix hiway-v1 unformatted observation options (#1822)
Browse files Browse the repository at this point in the history
* Fix issue with metrics

* Remove redundant check.

* Fix minor issues with environments.

* Fix test.

* Update CHANGELOG.md

Co-authored-by: Saul Field <saul.field@gmail.com>

---------

Co-authored-by: Saul Field <saul.field@gmail.com>
  • Loading branch information
Gamenot and saulfield authored Feb 3, 2023
1 parent 82d2756 commit 97c8e1f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ Copy and pasting the git commit messages is __NOT__ enough.

## [Unreleased]
### Added
- Added action formatting option to `hiway-v0`.
### Changed
- Moved action and observation conversions from `smarts.env.gymnasium.utils` to `smarts.env.utils`.
### Deprecated
### Fixed
- Fixed an issue where metrics break down with unformatted observations.
- Fixed an issue where `hiway-v1` would cause an exception when using "unformatted" observations.
- Unformatted actions and observations in `hiway-v0` provide `None` rather than an incorrect space.
### Removed
### Security

Expand Down
1 change: 0 additions & 1 deletion smarts/core/smarts.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,6 @@ def __del__(self):
try:
self.destroy()
except (TypeError, AttributeError) as e:
# This is a print statement because the logging module may be deleted at program exit.
raise SMARTSDestroyedError(
"ERROR: A SMARTS instance may have been deleted by gc before a call to destroy."
" Please explicitly call `del obj` or `SMARTS.destroy()` to make this error"
Expand Down
6 changes: 5 additions & 1 deletion smarts/core/utils/sumo.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def __init__(
)

def __del__(self) -> None:
self.close_traci_and_pipes()
# We should not raise in delete.
try:
self.close_traci_and_pipes()
except Exception:
pass

def connect(
self,
Expand Down
17 changes: 9 additions & 8 deletions smarts/env/gymnasium/hiway_env_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@
from smarts.core.scenario import Scenario
from smarts.core.sumo_traffic_simulation import SumoTrafficSimulation
from smarts.core.utils.visdom_client import VisdomClient
from smarts.env.gymnasium.utils.action_conversion import (
ActionOptions,
ActionSpacesFormatter,
)
from smarts.env.gymnasium.utils.observation_conversion import (
from smarts.env.utils.action_conversion import ActionOptions, ActionSpacesFormatter
from smarts.env.utils.observation_conversion import (
ObservationOptions,
ObservationSpacesFormatter,
)
Expand Down Expand Up @@ -127,6 +124,10 @@ class HiWayEnvV1(gym.Env):
for how the formatting matches the observation space. String version
can be used instead. See :class:`ObservationOptions`. Defaults to
:attr:`ObservationOptions.default`.
action_options (ActionOptions, string): Defines the options
for how the formatting matches the action space. String version
can be used instead. See :class:`ActionOptions`. Defaults to
:attr:`ActionOptions.default`.
"""

metadata = {"render.modes": ["human"]}
Expand Down Expand Up @@ -304,9 +305,9 @@ def step(
dones["__all__"],
info,
)
elif (
self._observations_formatter.observation_options
== ObservationOptions.multi_agent
elif self._observations_formatter.observation_options in (
ObservationOptions.multi_agent,
ObservationOptions.unformatted,
):
return (
self._observations_formatter.format(observations),
Expand Down
12 changes: 11 additions & 1 deletion smarts/env/hiway_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from smarts.core.smarts import SMARTS
from smarts.core.sumo_traffic_simulation import SumoTrafficSimulation
from smarts.core.utils.visdom_client import VisdomClient
from smarts.env.utils.action_conversion import ActionOptions, ActionSpacesFormatter
from smarts.zoo.agent_spec import AgentSpec


Expand Down Expand Up @@ -80,6 +81,8 @@ class HiWayEnv(gym.Env):
to None.
agent_interfaces (Dict[str, AgentInterface]): Specification of the agents
needs that will be used to configure the environment.
action_options (ActionOptions, str): Specifies format of the action space
of the environment.
"""

metadata = {"render.modes": ["human"]}
Expand All @@ -106,6 +109,7 @@ def __init__(
timestep_sec: Optional[
float
] = None, # for backwards compatibility (deprecated)
action_options: Union[ActionOptions, str] = ActionOptions.unformatted,
):
self._log = logging.getLogger(self.__class__.__name__)
self.seed(seed)
Expand Down Expand Up @@ -145,6 +149,10 @@ def __init__(
list(self._agent_interfaces.keys()),
shuffle_scenarios,
)
self._action_space_formatter = ActionSpacesFormatter(
agent_interfaces=self.agent_interfaces,
action_options=action_options,
)

envision_client = None
if not headless or envision_record_data_replay_path:
Expand Down Expand Up @@ -274,7 +282,9 @@ def step(
isinstance(key, str) for key in agent_actions.keys()
), "Expected Dict[str, any]"

observations, rewards, dones, extras = self._smarts.step(agent_actions)
observations, rewards, dones, extras = self._smarts.step(
self._action_space_formatter.format(agent_actions)
)

infos = {
agent_id: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ def space(self) -> gym.spaces.Dict:
Returns:
gym.spaces.Dict: A description of the action space that this formatter requires.
"""
if self._action_options is ActionOptions.unformatted:
return None
return gym.spaces.Dict(
{
agent_id: get_formats()[agent_interface.action].space
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,8 @@ def format(self, observations: Dict[str, Observation]):
@cached_property
def space(self):
"""The observation space this should format the smarts observations to match."""
if self.observation_options is ObservationOptions.unformatted:
return None
return gym.spaces.Dict(
{
agent_id: space_format.space
Expand Down

0 comments on commit 97c8e1f

Please sign in to comment.