def smarts(agent_spec):
    smarts = SMARTS(
        agent_interfaces={AGENT_ID: agent_spec.interface},
        traffic_sim=SumoTrafficSimulation(),
    )
    yield smarts
    smarts.destroy()
Exemple #2
0
    def __init__(self, config):
        self._config = config

        # XXX: These are intentionally left public at PyMARL's request
        self.n_agents = config.get("n_agents", 1)
        self.episode_limit = config.get("episode_limit", 1000)
        self.observation_space = config.get("observation_space",
                                            DEFAULT_OBSERVATION_SPACE)
        self.action_space = config.get("action_space", DEFAULT_ACTION_SPACE)
        self.state_space = config.get("state_space", DEFAULT_STATE_SPACE)

        self._agent_ids = ["Agent %i" % i for i in range(self.n_agents)]

        self._reward_adapter = config.get("reward_adapter",
                                          default_reward_adapter)
        self._observation_adapter = config.get("observation_adapter",
                                               default_obs_adapter)
        self._action_adapter = config.get("action_adapter",
                                          default_action_adapter)
        self._done_adapter = config.get("done_adapter",
                                        lambda dones: list(dones.values()))
        self._state_adapter = config.get("state_adapter",
                                         default_state_adapter)

        self._headless = config.get("headless", False)
        self._timestep_sec = config.get("timestep_sec", 0.01)
        self._observations = None
        self._state = None
        self._steps = 0

        seed = self._config.get("seed", 42)
        smarts.core.seed(seed)

        self._scenarios_iterator = Scenario.scenario_variations(
            config["scenarios"], self._agent_ids)

        agent_interfaces = {
            agent_id: AgentInterface.from_type(
                config.get("agent_type", AgentType.Laner),
                max_episode_steps=self.episode_limit,
                debug=config.get("debug", False),
            )
            for i, agent_id, in enumerate(self._agent_ids)
        }

        envision = None
        if not self._headless:
            envision = Envision(
                endpoint=config.get("envision_endpoint", None),
                output_dir=config.get("envision_record_data_replay_path",
                                      None),
            )

        self._smarts = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=SumoTrafficSimulation(
                time_resolution=self._timestep_sec),
            envision=envision,
            timestep_sec=self._timestep_sec,
        )
def _smarts_with_agent(agent) -> SMARTS:
    agents = {AGENT_ID: agent}
    return SMARTS(
        agents,
        traffic_sim=SumoTrafficSimulation(headless=True),
        envision=None,
    )
def main(scenarios, headless, seed):
    agent_spec = AgentSpec(
        interface=AgentInterface.from_type(AgentType.Laner,
                                           max_episode_steps=None),
        agent_builder=None,
        observation_adapter=None,
    )

    smarts = SMARTS(
        agent_interfaces={},
        traffic_sim=SumoTrafficSimulation(headless=True, auto_start=True),
        envision=Envision(),
    )
    scenarios_iterator = Scenario.scenario_variations(
        scenarios,
        list([]),
    )

    smarts.reset(next(scenarios_iterator))

    for _ in range(5000):
        smarts.step({})
        smarts.attach_sensors_to_vehicles(
            agent_spec, smarts.vehicle_index.social_vehicle_ids())
        obs, _, _, _ = smarts.observe_from(
            smarts.vehicle_index.social_vehicle_ids())
    def _build_smarts(self):
        agent_interfaces = {
            agent_id: spec.interface
            for agent_id, spec in self._agent_specs.items()
        }

        envision = None
        if not self._headless or self._envision_record_data_replay_path:
            envision = Envision(
                endpoint=self._envision_endpoint,
                sim_name=self._sim_name,
                output_dir=self._envision_record_data_replay_path,
                headless=self._headless,
            )

        sim = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=SumoTrafficSimulation(
                headless=self._sumo_headless,
                time_resolution=self._fixed_timestep_sec,
                num_external_sumo_clients=self._num_external_sumo_clients,
                sumo_port=self._sumo_port,
                auto_start=self._sumo_auto_start,
                endless_traffic=self._endless_traffic,
            ),
            envision=envision,
            fixed_timestep_sec=self._fixed_timestep_sec,
        )
        return sim
Exemple #6
0
def main(scenarios, headless, seed):
    scenarios_iterator = Scenario.scenario_variations(scenarios, [])
    for _ in scenarios:
        scenario = next(scenarios_iterator)
        agent_missions = scenario.discover_missions_of_traffic_histories()

        for agent_id, mission in agent_missions.items():
            scenario.set_ego_missions({agent_id: mission})

            agent_spec = AgentSpec(
                interface=AgentInterface.from_type(AgentType.Laner,
                                                   max_episode_steps=None),
                agent_builder=KeepLaneAgent,
            )

            agent = agent_spec.build_agent()

            smarts = SMARTS(
                agent_interfaces={agent_id: agent_spec.interface},
                traffic_sim=SumoTrafficSimulation(headless=True,
                                                  auto_start=True),
                envision=Envision(),
            )
            observations = smarts.reset(scenario)

            dones = {agent_id: False}
            while not dones[agent_id]:
                agent_obs = observations[agent_id]
                agent_action = agent.act(agent_obs)

                observations, rewards, dones, infos = smarts.step(
                    {agent_id: agent_action})
Exemple #7
0
 def reset(self, choose=True):
     if choose:
         try:
             self.current_observations = self._reset()
         except:
             self.close()
             self._smarts = SMARTS(
                 agent_interfaces=self.agent_interfaces,
                 traffic_sim=SumoTrafficSimulation(
                     headless=self.all_args.sumo_headless,
                     time_resolution=self.all_args.timestep_sec,
                     num_external_sumo_clients=self.all_args.
                     num_external_sumo_clients,
                     sumo_port=self.all_args.sumo_port,
                     auto_start=self.all_args.sumo_auto_start,
                     endless_traffic=self.all_args.endless_traffic,
                 ),
                 envision=self.envision_client,
                 visdom=self.visdom_client,
                 timestep_sec=self.all_args.timestep_sec,
                 zoo_workers=self.all_args.zoo_workers,
                 auth_key=self.all_args.auth_key,
             )
             self.current_observations = self._reset()
         return self.get_obs()
     else:
         return [np.zeros(self.obs_dim) for agent_id in self.agent_ids]
Exemple #8
0
def test_data_replay(agent_spec, scenarios_iterator, data_replay_path, monkeypatch):
    """We stub out the websocket the Envision client writes to and store the sent data.
    We do the same under Envision client's replay feature and compare that the data
    sent to the websocket is the same as before.
    """

    def step_through_episodes(agent_spec, smarts, scenarios_iterator):
        for i in range(NUM_EPISODES):
            agent = agent_spec.build_agent()
            scenario = next(scenarios_iterator)
            obs = smarts.reset(scenario)

            done = False
            while not done:
                obs = agent_spec.observation_adapter(obs[AGENT_ID])
                action = agent.act(obs)
                action = agent_spec.action_adapter(action)
                obs, _, dones, _ = smarts.step({AGENT_ID: action})
                done = dones[AGENT_ID]

    # 1. Inspect sent data during SMARTS simulation

    # Mock WebSocketApp so we can inspect the websocket frames being sent
    FakeWebSocketApp, original_sent_data = fake_websocket_app_class()
    monkeypatch.setattr(websocket, "WebSocketApp", FakeWebSocketApp)
    assert original_sent_data.qsize() == 0

    envision = Envision(output_dir=data_replay_path)
    smarts = SMARTS(
        agent_interfaces={AGENT_ID: agent_spec.interface},
        traffic_sim=SumoTrafficSimulation(time_resolution=TIMESTEP_SEC),
        envision=envision,
        timestep_sec=TIMESTEP_SEC,
    )
    step_through_episodes(agent_spec, smarts, scenarios_iterator)
    smarts.destroy()

    data_replay_path = Path(data_replay_path)
    data_replay_run_paths = [x for x in data_replay_path.iterdir() if x.is_dir()]
    assert len(data_replay_run_paths) == 1

    jsonl_paths = list(data_replay_run_paths[0].glob("*.jsonl"))
    assert len(jsonl_paths) == 1
    assert original_sent_data.qsize() > 0

    # 2. Inspect replay data

    # Mock WebSocketApp so we can inspect the websocket frames being sent
    FakeWebSocketApp, new_sent_data = fake_websocket_app_class()
    monkeypatch.setattr(websocket, "WebSocketApp", FakeWebSocketApp)
    assert new_sent_data.qsize() == 0

    # Now read data replay
    Envision.read_and_send(jsonl_paths[0], timestep_sec=TIMESTEP_SEC)

    # Verify the new data matches the original data
    assert original_sent_data.qsize() == new_sent_data.qsize()
    for _ in range(new_sent_data.qsize()):
        assert original_sent_data.get() == new_sent_data.get()
Exemple #9
0
    def __init__(
        self,
        scenarios: Sequence[str],
        agent_specs,
        shuffle_scenarios=True,
        headless=False,
        visdom=False,
        timestep_sec=0.1,
        seed=42,
        num_external_sumo_clients=0,
        sumo_headless=True,
        sumo_port=None,
        sumo_auto_start=True,
        endless_traffic=True,
        envision_endpoint=None,
        envision_record_data_replay_path=None,
        zoo_workers=None,
        auth_key=None,
    ):
        self._log = logging.getLogger(self.__class__.__name__)
        smarts.core.seed(seed)

        self._agent_specs = agent_specs
        self._dones_registered = 0

        self._scenarios_iterator = Scenario.scenario_variations(
            scenarios, list(agent_specs.keys()), shuffle_scenarios,
        )

        agent_interfaces = {
            agent_id: agent.interface for agent_id, agent in agent_specs.items()
        }

        envision_client = None
        if not headless:
            envision_client = Envision(
                endpoint=envision_endpoint, output_dir=envision_record_data_replay_path
            )

        visdom_client = None
        if visdom:
            visdom_client = VisdomClient()

        self._smarts = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=SumoTrafficSimulation(
                headless=sumo_headless,
                time_resolution=timestep_sec,
                num_external_sumo_clients=num_external_sumo_clients,
                sumo_port=sumo_port,
                auto_start=sumo_auto_start,
                endless_traffic=endless_traffic,
            ),
            envision=envision_client,
            visdom=visdom_client,
            timestep_sec=timestep_sec,
            zoo_workers=zoo_workers,
            auth_key=auth_key,
        )
Exemple #10
0
def smarts(scenarios, mock_provider):
    smarts_ = SMARTS(
        agent_interfaces={}, traffic_sim=SumoTrafficSimulation(time_resolution=0.1),
    )
    smarts_.add_provider(mock_provider)
    smarts_.reset(next(scenarios))
    yield smarts_
    smarts_.destroy()
Exemple #11
0
def smarts():
    smarts_ = SMARTS(
        agent_interfaces={
            AGENT_ID: AgentInterface.from_type(AgentType.Laner, max_episode_steps=30)
        },
        traffic_sim=SumoTrafficSimulation(time_resolution=0.1),
    )
    yield smarts_
    smarts_.destroy()
Exemple #12
0
def spawn_sumo(worker_idx, batch_id):
    sumo_sim = SumoTrafficSimulation(headless=True)

    scenarios_iterator = Scenario.scenario_variations(
        ["scenarios/loop"],
        ["Agent007"],
    )
    sumo_sim.setup(Scenario.next(scenarios_iterator, f"{batch_id}-{worker_idx}"))
    sumo_sim.teardown()
Exemple #13
0
def smarts():
    laner = AgentInterface(max_episode_steps=1000, action=ActionSpaceType.Lane,)
    buddha = AgentInterface(max_episode_steps=1000, action=ActionSpaceType.Lane,)
    agents = {AGENT_1: laner, AGENT_2: buddha}
    smarts = SMARTS(
        agents, traffic_sim=SumoTrafficSimulation(headless=True), envision=None,
    )

    yield smarts
    smarts.destroy()
    def __init__(
        self,
        scenarios: Sequence[str],
        agent_specs,
        visdom=False,
        headless=False,
        timestep_sec=0.1,
        seed=42,
        num_external_sumo_clients=0,
        sumo_headless=True,
        sumo_port=None,
        sumo_auto_start=True,
        endless_traffic=True,
        envision_endpoint=None,
        envision_record_data_replay_path=None,
    ):
        self._log = logging.getLogger(self.__class__.__name__)
        smarts.core.seed(seed)

        self._visdom_obs_queue = None
        if visdom:
            self._log.debug("Running with visdom")
            self._visdom_obs_queue = build_visdom_watcher_queue()

        self._agent_specs = agent_specs
        self._dones_registered = 0

        self._scenarios_iterator = Scenario.scenario_variations(
            scenarios,
            list(agent_specs.keys()),
        )

        agent_interfaces = {
            agent_id: agent.interface
            for agent_id, agent in agent_specs.items()
        }

        envision = None
        if not headless:
            envision = Envision(endpoint=envision_endpoint,
                                output_dir=envision_record_data_replay_path)

        self._smarts = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=SumoTrafficSimulation(
                headless=sumo_headless,
                time_resolution=timestep_sec,
                num_external_sumo_clients=num_external_sumo_clients,
                sumo_port=sumo_port,
                auto_start=sumo_auto_start,
                endless_traffic=endless_traffic,
            ),
            envision=envision,
            timestep_sec=timestep_sec,
        )
Exemple #15
0
def smarts():
    buddha = AgentInterface(
        max_episode_steps=1000, neighborhood_vehicles=True, action=ActionSpaceType.Lane,
    )
    smarts = SMARTS(
        agent_interfaces={"Agent-007": buddha},
        traffic_sim=SumoTrafficSimulation(headless=True),
        envision=None,
    )

    yield smarts
    smarts.destroy()
def main(scenarios: Sequence[str], headless: bool, seed: int):
    agent_spec = AgentSpec(
        interface=AgentInterface.from_type(AgentType.Laner,
                                           max_episode_steps=None),
        agent_builder=None,
        observation_adapter=None,
    )

    smarts = SMARTS(
        agent_interfaces={},
        traffic_sim=SumoTrafficSimulation(headless=headless, auto_start=True),
        envision=None if headless else Envision(),
    )
    scenarios_iterator = Scenario.scenario_variations(
        scenarios,
        list([]),
    )

    scenario = next(scenarios_iterator)
    obs = smarts.reset(scenario)

    collected_data = {}
    _record_data(smarts.elapsed_sim_time, obs, collected_data)

    # could also include "motorcycle" or "truck" in this set if desired
    vehicle_types = frozenset({"car"})

    while True:
        smarts.step({})
        current_vehicles = smarts.vehicle_index.social_vehicle_ids(
            vehicle_types=vehicle_types)

        if collected_data and not current_vehicles:
            print("no more vehicles.  exiting...")
            break

        smarts.attach_sensors_to_vehicles(agent_spec, current_vehicles)
        obs, _, _, dones = smarts.observe_from(current_vehicles)

        _record_data(smarts.elapsed_sim_time, obs, collected_data)

    # an example of how we might save the data per car
    for car, data in collected_data.items():
        outfile = f"data_{scenario.name}_{scenario.traffic_history.name}_{car}.pkl"
        with open(outfile, "wb") as of:
            pickle.dump(data, of)

    smarts.destroy()
def sim(request):
    shared_interface = AgentInterface(done_criteria=DoneCriteria(
        agents_alive=request.param))
    agents = {
        AGENT1: shared_interface,
        AGENT2: shared_interface,
        AGENT3: shared_interface,
    }
    smarts = SMARTS(
        agents,
        traffic_sim=SumoTrafficSimulation(headless=True),
        envision=None,
    )

    yield smarts
    smarts.destroy()
def main(scenarios, headless, seed):
    agent_spec = AgentSpec(
        interface=AgentInterface.from_type(AgentType.Laner,
                                           max_episode_steps=None),
        agent_builder=None,
        observation_adapter=None,
    )

    smarts = SMARTS(
        agent_interfaces={},
        traffic_sim=SumoTrafficSimulation(headless=headless, auto_start=True),
        envision=None if headless else Envision(),
    )
    scenarios_iterator = Scenario.scenario_variations(
        scenarios,
        list([]),
    )

    smarts.reset(next(scenarios_iterator))

    prev_vehicles = set()
    done_vehicles = set()
    for _ in range(5000):
        smarts.step({})

        current_vehicles = smarts.vehicle_index.social_vehicle_ids()
        # We explicitly watch for which agent/vehicles left the simulation here
        # since we don't have a "done criteria" that detects when a vehicle's
        # traffic history has played itself out.
        done_vehicles = prev_vehicles - current_vehicles
        prev_vehicles = current_vehicles

        smarts.attach_sensors_to_vehicles(agent_spec, current_vehicles)
        obs, _, _, dones = smarts.observe_from(current_vehicles)
        # The `dones` returned above should be empty for traffic histories
        # where all vehicles are assumed to stay on the road and not collide.
        # TODO:  add the following assert once the maps are accurate enough that
        # we don't have any agents accidentally go off-road.
        # assert not done
        for v in done_vehicles:
            dones[f"Agent-{v}"] = True
        # TODO: save observations for imitation learning

    smarts.destroy()
Exemple #19
0
    def setup_smarts(
        self,
        headless: bool = True,
        seed: int = 42,
        time_ratio: float = 1.0,
        sumo_traffic: bool = False,
    ):
        """Do the setup of the underlying SMARTS instance."""
        assert not self._smarts
        if not self._state_publisher:
            raise RuntimeError("must call setup_ros() first.")

        self._zoo_module = rospy.get_param("~zoo_module", "zoo")

        headless = rospy.get_param("~headless", headless)
        seed = rospy.get_param("~seed", seed)
        time_ratio = rospy.get_param("~time_ratio", time_ratio)
        assert time_ratio > 0.0
        self._time_ratio = time_ratio

        traffic_sim = None
        if rospy.get_param("~sumo_traffic", sumo_traffic):
            from smarts.core.sumo_traffic_simulation import SumoTrafficSimulation

            # Note that Sumo uses a fixed timestep, so if we have a highly-variable step rate,
            # we may want to set time_resolution to a mutiple of the target_freq?
            time_resolution = 1.0 / self._target_freq if self._target_freq else None
            traffic_sim = SumoTrafficSimulation(
                headless=headless,
                time_resolution=time_resolution,
            )

        self._smarts = SMARTS(
            agent_interfaces={},
            traffic_sim=traffic_sim,
            fixed_timestep_sec=None,
            envision=None if headless else Envision(),
            external_provider=True,
        )
        assert self._smarts.external_provider
        self._last_step_time = None
def main(scenarios, headless, seed):
    scenarios_iterator = Scenario.scenario_variations(scenarios, [])
    smarts = SMARTS(
        agent_interfaces={},
        traffic_sim=SumoTrafficSimulation(headless=True, auto_start=True),
        envision=Envision(),
    )
    for _ in scenarios:
        scenario = next(scenarios_iterator)
        agent_missions = scenario.discover_missions_of_traffic_histories()

        for agent_id, mission in agent_missions.items():
            agent_spec = AgentSpec(
                interface=AgentInterface.from_type(AgentType.Laner,
                                                   max_episode_steps=None),
                agent_builder=KeepLaneAgent,
            )
            agent = agent_spec.build_agent()

            smarts.switch_ego_agent({agent_id: agent_spec.interface})
            # required: get traffic_history_provider and set time offset
            traffic_history_provider = smarts.get_provider_by_type(
                TrafficHistoryProvider)
            assert traffic_history_provider
            traffic_history_provider.set_start_time(mission.start_time)

            modified_mission = replace(mission, start_time=0.0)
            scenario.set_ego_missions({agent_id: modified_mission})
            observations = smarts.reset(scenario)

            dones = {agent_id: False}
            while not dones[agent_id]:
                agent_obs = observations[agent_id]
                agent_action = agent.act(agent_obs)

                observations, rewards, dones, infos = smarts.step(
                    {agent_id: agent_action})

    smarts.destroy()
    def __init__(self,
                 scenarios: Sequence[str],
                 agent_specs: Dict,
                 shuffle_scenarios=True,
                 headless=False,
                 visdom=False,
                 timestep_sec=0.1,
                 seed=42,
                 num_external_sumo_clients=0,
                 sumo_headless=True,
                 sumo_port=None,
                 sumo_auto_start=True,
                 endless_traffic=True,
                 envision_endpoint=None,
                 envision_record_data_replay_path=None,
                 zoo_addrs=None):
        self._metircs = Metric(1)

        self.has_connection = False

        self._log = logging.getLogger(self.__class__.__name__)
        smarts.core.seed(seed)  # Set seed for np and random module.

        self._agent_specs = agent_specs
        self._dones_registered = 0

        # Setup ego.
        self._ego = agent_specs[EGO_ID].build_agent()

        # Setup sceanrios for benchmark.
        self._scenarios_iterator = Scenario.scenario_variations(
            scenarios,
            list(agent_specs.keys()),
            shuffle_scenarios,
        )

        # Setup envision and visdom.
        envision_client = None
        if not headless:
            envision_client = Envision(
                endpoint=envision_endpoint,
                output_dir=envision_record_data_replay_path)

        visdom_client = None
        if visdom:
            visdom_client = VisdomClient()

        # Setup SMARTS
        agent_interfaces = {
            agent_id: agent.interface
            for agent_id, agent in agent_specs.items()
        }

        self._smarts = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=SumoTrafficSimulation(
                headless=sumo_headless,
                time_resolution=timestep_sec,
                num_external_sumo_clients=num_external_sumo_clients,
                sumo_port=sumo_port,
                auto_start=sumo_auto_start,
                endless_traffic=endless_traffic,
            ),
            envision=envision_client,
            visdom=visdom_client,
            timestep_sec=timestep_sec,
            zoo_addrs=zoo_addrs)
def traffic_sim():
    return SumoTrafficSimulation(headless=True,
                                 num_external_sumo_clients=1,
                                 sumo_port=SUMO_PORT)
Exemple #23
0
def main(script: str, scenarios: Sequence[str], headless: bool, seed: int):
    logger = logging.getLogger(script)
    logger.setLevel(logging.INFO)

    agent_spec = AgentSpec(
        interface=AgentInterface.from_type(AgentType.Laner, max_episode_steps=None),
        agent_builder=None,
        observation_adapter=None,
    )

    smarts = SMARTS(
        agent_interfaces={},
        traffic_sim=SumoTrafficSimulation(headless=headless, auto_start=True),
        envision=None if headless else Envision(),
    )

    scenario_list = Scenario.get_scenario_list(scenarios)
    scenarios_iterator = Scenario.variations_for_all_scenario_roots(scenario_list, [])
    for scenario in scenarios_iterator:
        obs = smarts.reset(scenario)

        collected_data = {}
        _record_data(smarts.elapsed_sim_time, obs, collected_data)

        # could also include "motorcycle" or "truck" in this set if desired
        vehicle_types = frozenset({"car"})

        # filter off-road vehicles from observations
        vehicles_off_road = set()

        while True:
            smarts.step({})
            current_vehicles = smarts.vehicle_index.social_vehicle_ids(
                vehicle_types=vehicle_types
            )

            if collected_data and not current_vehicles:
                print("no more vehicles.  exiting...")
                break

            for veh_id in current_vehicles:
                try:
                    smarts.attach_sensors_to_vehicles(agent_spec.interface, {veh_id})
                except ControllerOutOfLaneException:
                    logger.warning(f"{veh_id} out of lane, skipped attaching sensors")
                    vehicles_off_road.add(veh_id)

            valid_vehicles = {v for v in current_vehicles if v not in vehicles_off_road}
            obs, _, _, dones = smarts.observe_from(valid_vehicles)
            _record_data(smarts.elapsed_sim_time, obs, collected_data)

        # an example of how we might save the data per car
        observation_folder = "collected_observations"
        if not os.path.exists(observation_folder):
            os.makedirs(observation_folder)
        for car, data in collected_data.items():
            outfile = f"{observation_folder}/{scenario.name}_{scenario.traffic_history.name}_{car}.pkl"
            with open(outfile, "wb") as of:
                pickle.dump(data, of)

    smarts.destroy()
Exemple #24
0
    def __init__(
            self,
            scenarios: Sequence[str],
            agent_specs: Dict[str, AgentSpec],
            sim_name=None,
            shuffle_scenarios=True,
            headless=False,
            visdom=False,
            fixed_timestep_sec=None,
            seed=42,
            num_external_sumo_clients=0,
            sumo_headless=True,
            sumo_port=None,
            sumo_auto_start=True,
            endless_traffic=True,
            envision_endpoint=None,
            envision_record_data_replay_path=None,
            zoo_addrs=None,
            timestep_sec=None,  # for backwards compatibility (deprecated)
    ):
        self._log = logging.getLogger(self.__class__.__name__)
        self.seed(seed)

        if timestep_sec and not fixed_timestep_sec:
            warnings.warn(
                "timestep_sec has been deprecated in favor of fixed_timestep_sec.  Please update your code.",
                category=DeprecationWarning,
            )
        if not fixed_timestep_sec:
            fixed_timestep_sec = timestep_sec or 0.1

        self._agent_specs = agent_specs
        self._dones_registered = 0

        self._scenarios_iterator = Scenario.scenario_variations(
            scenarios,
            list(agent_specs.keys()),
            shuffle_scenarios,
        )

        agent_interfaces = {
            agent_id: agent.interface
            for agent_id, agent in agent_specs.items()
        }

        envision_client = None
        if not headless or envision_record_data_replay_path:
            envision_client = Envision(
                endpoint=envision_endpoint,
                sim_name=sim_name,
                output_dir=envision_record_data_replay_path,
                headless=headless,
            )

        visdom_client = None
        if visdom:
            visdom_client = VisdomClient()

        all_sumo = Scenario.supports_traffic_simulation(scenarios)
        traffic_sim = None
        if not all_sumo:
            # We currently only support the Native SUMO Traffic Provider and Social Agents for SUMO maps
            if zoo_addrs:
                warnings.warn(
                    "`zoo_addrs` can only be used with SUMO scenarios")
                zoo_addrs = None
            warnings.warn(
                "We currently only support the Native SUMO Traffic Provider and Social Agents for SUMO maps."
                "All scenarios passed need to be of SUMO, to enable SUMO Traffic Simulation and Social Agents."
            )
            pass
        else:
            from smarts.core.sumo_traffic_simulation import SumoTrafficSimulation

            traffic_sim = SumoTrafficSimulation(
                headless=sumo_headless,
                time_resolution=fixed_timestep_sec,
                num_external_sumo_clients=num_external_sumo_clients,
                sumo_port=sumo_port,
                auto_start=sumo_auto_start,
                endless_traffic=endless_traffic,
            )
            zoo_addrs = zoo_addrs

        self._smarts = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=traffic_sim,
            envision=envision_client,
            visdom=visdom_client,
            fixed_timestep_sec=fixed_timestep_sec,
            zoo_addrs=zoo_addrs,
        )
Exemple #25
0
    def __init__(self, all_args):
        self.all_args = all_args

        self._dones_registered = 0

        self.neighbor_num = all_args.neighbor_num
        self.rews_mode = all_args.rews_mode
        self.n_agents = all_args.num_agents
        self.use_proximity = all_args.use_proximity
        self.use_discrete = all_args.use_discrete  # default True
        self.use_centralized_V = all_args.use_centralized_V

        self.scenarios = [(all_args.scenario_path + all_args.scenario_name)]

        self.agent_ids = ["Agent %i" % i for i in range(self.n_agents)]

        self.obs_space_dict = self.get_obs_space_dict()
        self.obs_dim = self.get_obs_dim()
        # ! TODO:
        self.share_obs_dim = self.get_state_dim(
        ) if self.use_centralized_V else self.get_obs_dim()
        self.observation_space = [
            gym.spaces.Box(low=-1e10, high=1e10, shape=(self.obs_dim, ))
        ] * self.n_agents
        self.share_observation_space = [
            gym.spaces.Box(low=-1e10, high=1e10, shape=(self.share_obs_dim, ))
        ] * self.n_agents

        if self.use_discrete:
            self.act_dim = 4
            self.action_space = [gym.spaces.Discrete(self.act_dim)
                                 ] * self.n_agents
            self.agent_type = AgentType.Vulner_with_proximity if self.use_proximity else AgentType.Vulner

        else:
            # TODO Add continous action space
            self.agent_type = AgentType.VulnerCon_with_proximity if self.use_proximity else AgentType.VulnerCon
            raise NotImplementedError

        self._agent_specs = {
            agent_id: AgentSpec(
                interface=AgentInterface.from_type(
                    self.agent_type, max_episode_steps=all_args.horizon),
                observation_adapter=self.get_obs_adapter(),
                reward_adapter=self.get_rew_adapter(self.rews_mode,
                                                    self.neighbor_num),
                action_adapter=self.get_act_adapter(),
            )
            for agent_id in self.agent_ids
        }

        self._scenarios_iterator = Scenario.scenario_variations(
            self.scenarios,
            list(self._agent_specs.keys()),
            all_args.shuffle_scenarios,
        )

        self.agent_interfaces = {
            agent_id: agent.interface
            for agent_id, agent in self._agent_specs.items()
        }

        self.envision_client = None
        if not all_args.headless:
            self.envision_client = Envision(
                endpoint=all_args.envision_endpoint,
                output_dir=all_args.envision_record_data_replay_path)

        self.visdom_client = None
        if all_args.visdom:
            self.visdom_client = VisdomClient()

        self._smarts = SMARTS(
            agent_interfaces=self.agent_interfaces,
            traffic_sim=SumoTrafficSimulation(
                headless=all_args.sumo_headless,
                time_resolution=all_args.timestep_sec,
                num_external_sumo_clients=all_args.num_external_sumo_clients,
                sumo_port=all_args.sumo_port,
                auto_start=all_args.sumo_auto_start,
                endless_traffic=all_args.endless_traffic,
            ),
            envision=self.envision_client,
            visdom=self.visdom_client,
            timestep_sec=all_args.timestep_sec,
            zoo_workers=all_args.zoo_workers,
            auth_key=all_args.auth_key,
        )
    client.configureDebugVisualizer(pybullet.COV_ENABLE_WIREFRAME, 1)

    while True:
        client.resetSimulation()
        client.setGravity(0, 0, -9.8)
        client.setPhysicsEngineParameter(
            fixedTimeStep=TIMESTEP_SEC,
            numSubSteps=int(TIMESTEP_SEC / (1 / 240)),
            # enableConeFriction=False,
            # erp=0.1,
            # contactERP=0.1,
            # frictionERP=0.1,
        )

        path = Path(__file__).parent / "../smarts/core/models/plane.urdf"
        path = str(path.absolute())
        plane_body_id = client.loadURDF(path, useFixedBase=True)

        vehicle_config = VEHICLE_CONFIGS["passenger"]

        traffic_sim = SumoTrafficSimulation(headless=False,
                                            time_resolution=0.1,
                                            debug=True)

        run(
            client,
            traffic_sim,
            plane_body_id,
            n_steps=int(1e6),
        )
Exemple #27
0
    def __init__(
        self,
        scenarios: Sequence[str],
        agent_specs: Dict[str, AgentSpec],
        sim_name: Optional[str] = None,
        shuffle_scenarios: bool = True,
        headless: bool = True,
        visdom: bool = False,
        fixed_timestep_sec: Optional[float] = None,
        seed: int = 42,
        num_external_sumo_clients: int = 0,
        sumo_headless: bool = True,
        sumo_port: Optional[str] = None,
        sumo_auto_start: bool = True,
        endless_traffic: bool = True,
        envision_endpoint: Optional[str] = None,
        envision_record_data_replay_path: Optional[str] = None,
        zoo_addrs: Optional[str] = None,
        timestep_sec: Optional[
            float
        ] = None,  # for backwards compatibility (deprecated)
    ):
        """
        Args:
            scenarios (Sequence[str]):  A list of scenario directories that
                will be simulated.
            agent_specs (Dict[str, AgentSpec]): Specification of the agents
                that will run in the environment.
            sim_name (Optional[str], optional): Simulation name. Defaults to
                None.
            shuffle_scenarios (bool, optional): If true, order of scenarios
                will be randomized, else it will be maintained. Defaults to
                True.
            headless (bool, optional): If True, disables visualization in
                Envision. Defaults to False.
            visdom (bool, optional): If True, enables visualization of observed
                RGB images in Visdom. Defaults to False.
            fixed_timestep_sec (Optional[float], optional): Step duration for
                all components of the simulation. May be None if time deltas
                are externally-driven. Defaults to None.
            seed (int, optional): Random number generator seed. Defaults to 42.
            num_external_sumo_clients (int, optional): Number of SUMO clients
                beyond SMARTS. Defaults to 0.
            sumo_headless (bool, optional): If True, disables visualization in
                SUMO GUI. Defaults to True.
            sumo_port (Optional[str], optional): SUMO port. Defaults to None.
            sumo_auto_start (bool, optional): Automatic starting of SUMO.
                Defaults to True.
            endless_traffic (bool, optional): SUMO's endless traffic setting.
                Defaults to True.
            envision_endpoint (Optional[str], optional): Envision's uri.
                Defaults to None.
            envision_record_data_replay_path (Optional[str], optional):
                Envision's data replay output directory. Defaults to None.
            zoo_addrs (Optional[str], optional): List of (ip, port) tuples of
                zoo server, used to instantiate remote social agents. Defaults
                to None.
            timestep_sec (Optional[float], optional): [description]. Defaults
                to None.
        """

        self._log = logging.getLogger(self.__class__.__name__)
        self.seed(seed)

        if timestep_sec and not fixed_timestep_sec:
            warnings.warn(
                "timestep_sec has been deprecated in favor of fixed_timestep_sec.  Please update your code.",
                category=DeprecationWarning,
            )
        if not fixed_timestep_sec:
            fixed_timestep_sec = timestep_sec or 0.1

        self._agent_specs = agent_specs
        self._dones_registered = 0

        self._scenarios_iterator = Scenario.scenario_variations(
            scenarios,
            list(agent_specs.keys()),
            shuffle_scenarios,
        )

        agent_interfaces = {
            agent_id: agent.interface for agent_id, agent in agent_specs.items()
        }

        envision_client = None
        if not headless or envision_record_data_replay_path:
            envision_client = Envision(
                endpoint=envision_endpoint,
                sim_name=sim_name,
                output_dir=envision_record_data_replay_path,
                headless=headless,
            )

        visdom_client = None
        if visdom:
            visdom_client = VisdomClient()

        all_sumo = Scenario.supports_traffic_simulation(scenarios)
        traffic_sim = None
        if not all_sumo:
            # We currently only support the Native SUMO Traffic Provider and Social Agents for SUMO maps
            if zoo_addrs:
                warnings.warn("`zoo_addrs` can only be used with SUMO scenarios")
                zoo_addrs = None
            warnings.warn(
                "We currently only support the Native SUMO Traffic Provider and Social Agents for SUMO maps."
                "All scenarios passed need to be of SUMO, to enable SUMO Traffic Simulation and Social Agents."
            )
            pass
        else:
            from smarts.core.sumo_traffic_simulation import SumoTrafficSimulation

            traffic_sim = SumoTrafficSimulation(
                headless=sumo_headless,
                time_resolution=fixed_timestep_sec,
                num_external_sumo_clients=num_external_sumo_clients,
                sumo_port=sumo_port,
                auto_start=sumo_auto_start,
                endless_traffic=endless_traffic,
            )
            zoo_addrs = zoo_addrs

        self._smarts = SMARTS(
            agent_interfaces=agent_interfaces,
            traffic_sim=traffic_sim,
            envision=envision_client,
            visdom=visdom_client,
            fixed_timestep_sec=fixed_timestep_sec,
            zoo_addrs=zoo_addrs,
        )
Exemple #28
0
def smarts():
    smarts = SMARTS({}, traffic_sim=SumoTrafficSimulation())
    yield smarts
    smarts.destroy()