Skip to content
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

Simplify agent vehicle capture. #1884

Closed
wants to merge 4 commits into from
Closed
Changes from 2 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
26 changes: 16 additions & 10 deletions smarts/core/sumo_traffic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import random
import time
import weakref
from enum import IntEnum
from pathlib import Path
from typing import Iterable, List, Optional, Sequence, Tuple
from typing import Iterable, List, Optional, Sequence, Tuple, Union

import numpy as np
from shapely.affinity import rotate as shapely_rotate
Expand Down Expand Up @@ -51,6 +52,12 @@
import traci.constants as tc # isort:skip


class AgentRemovalMode(IntEnum):
KEEP = 0
REMOVE_AGENTS = 1
REMOVE_ALL = 2


class SumoTrafficSimulation(TrafficProvider):
"""
Args:
Expand Down Expand Up @@ -86,9 +93,9 @@ def __init__(
auto_start: bool = True,
allow_reload: bool = True,
debug: bool = True,
remove_agents_only_mode: bool = False,
remove_agents_only_mode: Union[bool, AgentRemovalMode] = False,
):
self._remove_agents_only_mode = remove_agents_only_mode
self._remove_agents_only_mode = AgentRemovalMode(remove_agents_only_mode)
self._log = logging.getLogger(self.__class__.__name__)

self._debug = debug
Expand Down Expand Up @@ -390,14 +397,16 @@ def _handle_traci_exception(
self._traci_conn.close_traci_and_pipes()
self._handling_error = False

def _remove_vehicles(self):
def _remove_all_sumo_vehicles(self):
vehicles_to_remove = None
if self._remove_agents_only_mode:
if self._remove_agents_only_mode == AgentRemovalMode.REMOVE_AGENTS:
vehicles_to_remove = self._non_sumo_vehicle_ids
else:
elif self._remove_agents_only_mode == AgentRemovalMode.REMOVE_ALL:
vehicles_to_remove = self._non_sumo_vehicle_ids.union(
self._sumo_vehicle_ids
)
else:
vehicles_to_remove = []
Gamenot marked this conversation as resolved.
Show resolved Hide resolved
sim = self._sim()
for vehicle_id in vehicles_to_remove:
if sim:
Expand All @@ -420,10 +429,7 @@ def teardown(self):
assert self._is_setup

if self.connected:
try:
self._remove_vehicles()
except traci.exceptions.FatalTraCIError:
pass
self._remove_all_sumo_vehicles()

if self._allow_reload:
self._cumulative_sim_seconds = 0
Expand Down