-
Notifications
You must be signed in to change notification settings - Fork 190
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 reset options to "hiway-v1". #1862
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# MIT License | ||
# | ||
# Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
import gymnasium as gym | ||
import pytest | ||
|
||
from smarts.core.agent_interface import AgentInterface, AgentType | ||
from smarts.core.utils.episodes import episodes | ||
from smarts.env.gymnasium.hiway_env_v1 import HiWayEnvV1 | ||
from smarts.core.scenario import Scenario | ||
|
||
AGENT_ID = "Agent-007" | ||
MAX_EPISODES = 3 | ||
|
||
|
||
@pytest.fixture | ||
def env(): | ||
agent_interfaces = { | ||
AGENT_ID: AgentInterface.from_type(AgentType.Laner, max_episode_steps=100) | ||
} | ||
agent_ids = set(agent_interfaces) | ||
env: HiWayEnvV1 = gym.make( | ||
"smarts.env:hiway-v1", | ||
scenarios=["scenarios/sumo/loop"], | ||
agent_interfaces=agent_interfaces, | ||
action_options="unformatted", | ||
observation_options="unformatted", | ||
headless=True, | ||
visdom=False, | ||
fixed_timestep_sec=0.01, | ||
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. Maybe we should simply make the 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. Why |
||
disable_env_checker=True, | ||
) | ||
assert isinstance(env.unwrapped, HiWayEnvV1) | ||
assert not (agent_ids - set(env.agent_interfaces)) | ||
matching_items = [ | ||
env.agent_interfaces[k] == agent_interfaces[k] | ||
for k in env.agent_interfaces | ||
if k in agent_interfaces | ||
] | ||
assert all(matching_items) | ||
assert len(env.agent_interfaces) == len(agent_interfaces) | ||
assert not (agent_ids - env.agent_ids) | ||
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. Wondering whether these checks could be simplified. 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. I also note that these checks should not be in the fixture. I am moving and simplifying them. |
||
yield env | ||
env.close() | ||
|
||
|
||
def test_hiway_env_v1_unformatted(env: HiWayEnvV1): | ||
episode = None | ||
for episode in episodes(n=MAX_EPISODES): | ||
observations = env.reset() | ||
episode.record_scenario(env.scenario_log) | ||
|
||
terminated = {"__all__": False} | ||
while not terminated["__all__"]: | ||
observations, rewards, terminated, truncated, infos = env.step( | ||
{AGENT_ID: "keep_lane"} | ||
) | ||
|
||
# Reward is currently the delta in distance travelled by the agent. | ||
# Ensure that it is infact a delta and not total distance travelled | ||
# since this bug has appeared a few times. Verify by ensuring the | ||
# reward does not grow unbounded. | ||
assert isinstance(rewards, dict) | ||
assert all( | ||
[-3 < reward < 3 for reward in rewards.values()] | ||
), f"Expected bounded reward per timestep, but got {rewards}." | ||
|
||
episode.record_step(observations, rewards, terminated, infos) | ||
|
||
assert episode is not None and episode.index == ( | ||
MAX_EPISODES - 1 | ||
), "Simulation must cycle through to the final episode." | ||
|
||
|
||
def test_hiway_env_v1_reset_with_scenario(env: HiWayEnvV1): | ||
scenarios = ["scenarios/sumo/loop"] | ||
scenario: Scenario = next(Scenario.scenario_variations(scenarios, [AGENT_ID])) | ||
|
||
env.reset(options={"scenario": scenario, "start_time": 100}) | ||
assert "loop" in env.scenario.root_filepath | ||
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. Given the env originally only had one scenario, namely 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. Right I was grabbing something quick, I will try "cloverleaf". 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. Used |
||
assert env.smarts.elapsed_sim_time >= 100 | ||
env.step({AGENT_ID: "keep_lane"}) |
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.
Considering we have previously provided
def scenario()
, what is the use case for additionally providingdef smarts()
?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.
Generally just for direct access to SMARTS, I am somewhat cautious about allowing this but if we do not allow it there may be little exposure to actually using SMARTS directly.
I am partially hoping people will be tempted to use it.