Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 13 additions & 10 deletions envs/carla_env/server/carla_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
The environment wraps CARLA scenarios and provides OpenEnv-compatible API.
"""

import copy
import html
import math
import re
Expand Down Expand Up @@ -265,7 +266,8 @@ def __init__(
super().__init__()

# Load scenario
self.scenario: BaseScenario = get_scenario(scenario_name, scenario_config)
self._base_scenario: BaseScenario = get_scenario(scenario_name, scenario_config)
self.scenario: BaseScenario = self._base_scenario

# Set rubric based on scenario type
self.rubric = _rubric_for_scenario(self.scenario)
Expand Down Expand Up @@ -312,21 +314,22 @@ def reset(

Args:
scenario_name: Optional scenario name to switch to. If None, uses current scenario.
scenario_config: Optional dict of config field overrides (e.g. weather, max_steps).
Keys must match fields on the scenario's config dataclass.
scenario_config: Optional dict of config field overrides (e.g. weather, max_steps)
for this episode only. Keys must match fields on the scenario's config dataclass.

Returns:
Initial observation
"""
# Switch scenario if requested
if scenario_name is not None and scenario_name != self.scenario.config.name:
self.scenario = get_scenario(scenario_name, scenario_config)
self.rubric = _rubric_for_scenario(self.scenario)
elif scenario_config:
# Same scenario, apply config overrides in-place
for key, value in scenario_config.items():
if hasattr(self.scenario.config, key):
setattr(self.scenario.config, key, value)
self._base_scenario = get_scenario(scenario_name)
self.rubric = _rubric_for_scenario(self._base_scenario)

# Overrides apply to a copy, so they last for this episode only
self.scenario = copy.deepcopy(self._base_scenario)
for key, value in (scenario_config or {}).items():
if hasattr(self.scenario.config, key):
setattr(self.scenario.config, key, value)

# Reset rubric state for new episode
self._reset_rubric()
Expand Down
11 changes: 11 additions & 0 deletions tests/envs/test_carla_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ def test_reset(self):
assert isinstance(obs, CarlaObservation)
assert obs.scenario_name == "trolley_saves"

def test_reset_scenario_config_applies_to_one_episode(self):
"""Overrides given to reset() do not leak into later resets."""
env = CarlaEnvironment(scenario_name="trolley_saves", mode="mock")
default = env.scenario.config.max_steps

env.reset(scenario_config={"max_steps": default + 5})
assert env.scenario.config.max_steps == default + 5

env.reset()
assert env.scenario.config.max_steps == default

def test_step_observe(self):
"""Test step with observe action."""
env = CarlaEnvironment(scenario_name="trolley_saves", mode="mock")
Expand Down