Skip to content

Commit

Permalink
Merge branch 'fix-traffic-light-cycle-does-not-invalidate-cached-attr…
Browse files Browse the repository at this point in the history
…ibute' into 'develop'

fix: TrafficLightCycle does not invalidate cached attribute on modification

See merge request cps/commonroad/commonroad-io!303
  • Loading branch information
smaierhofer committed Dec 13, 2024
2 parents a057719 + 4c4c053 commit 856c1aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Fixed
- `DynamicObstacle` cannot be hashed if some optional attributes are missing
- `TrafficLightCycle` does not invalidate cached cycle init timesteps after modification

## [2024.2] - 2024-07-22

Expand Down
6 changes: 6 additions & 0 deletions commonroad/scenario/traffic_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def cycle_elements(self) -> Union[None, List[TrafficLightCycleElement]]:
@cycle_elements.setter
def cycle_elements(self, cycle_elements: Union[None, List[TrafficLightCycleElement]]):
self._cycle_elements = cycle_elements
# Invalidate cached attribute which was calculated based on the previous cycle elements
if hasattr(self, "_cycle_init_timesteps"):
del self._cycle_init_timesteps

@property
def time_offset(self) -> int:
Expand All @@ -152,6 +155,9 @@ def time_offset(self) -> int:
@time_offset.setter
def time_offset(self, time_offset: int):
self._time_offset = time_offset
# Invalidate cached attribute which was calculated based on the previous time offset
if hasattr(self, "_cycle_init_timesteps"):
del self._cycle_init_timesteps

@property
def active(self) -> bool:
Expand Down
27 changes: 27 additions & 0 deletions tests/scenario/test_traffic_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ def test_get_state_at_time_step(self):
light_cycle2 = TrafficLightCycle(cycle_elements=cycle, time_offset=10)
assert light_cycle2.get_state_at_time_step(12) == cycle[1].state

def test_update_cycle_elements(self):
# Verify that get_state_at_time_step is still correct after the cycle was updated
cycle = [
TrafficLightCycleElement(TrafficLightState.GREEN, 2),
TrafficLightCycleElement(TrafficLightState.YELLOW, 3),
TrafficLightCycleElement(TrafficLightState.RED, 2),
]
light_cycle = TrafficLightCycle(cycle_elements=cycle, time_offset=0)
assert light_cycle.get_state_at_time_step(7) == cycle[0].state

new_cycle = cycle + [TrafficLightCycleElement(TrafficLightState.RED_YELLOW, 1)]
light_cycle.cycle_elements = new_cycle
assert light_cycle.get_state_at_time_step(7) == new_cycle[-1].state

def test_update_time_offset(self):
# Verify that get_state_at_time_step is still correct after the time offset was updated
cycle = [
TrafficLightCycleElement(TrafficLightState.GREEN, 2),
TrafficLightCycleElement(TrafficLightState.YELLOW, 3),
TrafficLightCycleElement(TrafficLightState.RED, 2),
]
light_cycle = TrafficLightCycle(cycle_elements=cycle, time_offset=0)
assert light_cycle.get_state_at_time_step(1) == cycle[0].state

light_cycle.time_offset = 5
assert light_cycle.get_state_at_time_step(1) == cycle[1].state


class TestTrafficLight(unittest.TestCase):
def test_translate_rotate(self):
Expand Down

0 comments on commit 856c1aa

Please sign in to comment.