def create_single_scenario(self):
        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        scenario._map_interface = None
        world = scenario.GetWorldState()
        agent_list = []
        scenario._agent_list = []
        for agent_json_ in self._local_params["Agents"]:
            agent_json = agent_json_["VehicleModel"].copy()
            agent_json["map_interface"] = world.map
            goal_polygon = Polygon2d([0, 0, 0],
                                     np.array(
                                         agent_json["goal"]["polygon_points"]))
            goal_polygon = goal_polygon.Translate(
                Point2d(agent_json["goal"]["center_pose"][0],
                        agent_json["goal"]["center_pose"][1]))

            agent_json[
                "goal_definition"] = self._build_sequential_goal_definition()
            agent_state = np.array(agent_json["state"])
            if len(np.shape(agent_state)) > 1:
                agent_state = np.random.uniform(low=agent_state[:, 0],
                                                high=agent_state[:, 1])
            agent_json["state"] = agent_state.tolist()
            agent = self._json_converter.agent_from_json(
                agent_json, param_server=self._local_params)
            agent.SetAgentId(agent_json["id"])
            scenario._agent_list.append(agent)
        scenario._eval_agent_ids = [
            self._local_params["EgoAgentId", "ID of the ego-agent", 0]
        ]
        return scenario
Esempio n. 2
0
  def create_single_scenario(self):
    """Creates one scenario using the defined LaneCorridorConfig
    
    Returns:
        Scenario -- Returns a BARK scenario
    """
    scenario = Scenario(map_file_name=self._map_file_name,
                        json_params=self._params.ConvertToDict())
    # as we always use the same world, we can create the MapIntf. once
    if self._map_interface is None:
      scenario.CreateMapInterface(self._map_file_name)
    else:
      scenario.map_interface = self._map_interface
    self._map_interface = scenario.map_interface
    world = scenario.GetWorldState()
    map_interface = world.map
    # fill agent list of the BARK world and set agents that are controlled
    scenario._agent_list = []
    scenario._eval_agent_ids = []
    for lc_config in self._lane_corridor_configs:
      agent_state = True
      lc_agents = []
      if lc_config._source_pos is not None and lc_config._sink_pos is not None:
        lc_config.InferRoadIdsAndLaneCorr(world)
      while agent_state is not None:
        agent_state = lc_config.state(world)
        if agent_state is not None:
          agent_behavior = lc_config.behavior_model(world)
          agent_dyn = lc_config.dynamic_model
          agent_exec = lc_config.execution_model
          agent_polygon = lc_config.shape
          agent_params = self._params.AddChild("agent")
          agent_goal = lc_config.goal(world)
          new_agent = Agent(
            agent_state, 
            agent_behavior, 
            agent_dyn,
            agent_exec, 
            agent_polygon,
            agent_params,
            agent_goal,
            map_interface)
          new_agent.road_corridor = lc_config._road_corridor
          lc_agents.append(new_agent)
        # set the road corridor

      # handle controlled agents
      controlled_agent_ids = []
      for controlled_agent in lc_config.controlled_ids(lc_agents):
        controlled_agent.goal_definition = lc_config.controlled_goal(world)
        controlled_agent.behavior_model = \
          lc_config.controlled_behavior_model(world)
        controlled_agent_ids.append(controlled_agent.id)
      scenario._eval_agent_ids.extend(controlled_agent_ids)
      scenario._agent_list.extend(lc_agents)
      lc_config.reset()
    return scenario
Esempio n. 3
0
    def __create_single_scenario__(self, scen_track_info):
        scen_track_info.TimeSanityCheck()

        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict(),
                            map_interface=self._map_interface)

        world = scenario.GetWorldState()
        track_params = ParameterServer()
        track_params["execution_model"] = 'ExecutionModelInterpolate'
        track_params["dynamic_model"] = 'SingleTrackModel'
        track_params["map_interface"] = world.map

        all_track_ids = list(scen_track_info.GetOtherTrackInfos().keys())
        # also add ego id
        ego_track_id = scen_track_info.GetEgoTrackInfo().GetTrackId()
        all_track_ids.append(ego_track_id)

        agent_list = []
        model_converter = ModelJsonConversion()
        for track_id in all_track_ids:
            if self._behavior_model and track_id != ego_track_id:
                behavior_params = self.__fill_agent_params()
                behavior_model_name = self._behavior_model
                track_params["behavior_model"] = model_converter.convert_model(
                    behavior_model_name, behavior_params)
                # behavior_params.Save("/tmp/agent_prams_{}.json".format(track_id))
            else:
                # we do not change behavior model of ego agent -> will be set in benchmark
                track_params["behavior_model"] = None

            if self._use_goal_from_road:
                goal = self.__infer_goal_from_road__(lc=0)
            else:
                goal = None

            agent = self._interaction_ds_reader.AgentFromTrackfile(
                track_params,
                self._params,
                scen_track_info,
                track_id,
                goal_def=goal)

            # set first valid time stamp of the agent (in relation to scenario start)
            agent.first_valid_timestamp = scen_track_info.GetTimeOffsetOfAgentInSec(
                track_id)

            agent_list.append(agent)

        scenario._agent_list = agent_list  # must contain all agents!
        scenario._eval_agent_ids = [ego_track_id]
        scenario.json_params["track_file"] = scen_track_info.GetTrackFilename()

        return scenario
    def __create_single_scenario__(self, scenario_track_info):
        scenario_track_info.TimeSanityCheck()

        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        # as we always use the same world, we can create the MapIntf. once
        if self._map_interface is None:
            scenario.CreateMapInterface(self._map_file_name)
            print("Creating New Map Interface for Scenario!")
        else:
            scenario.map_interface = self._map_interface
        self._map_interface = scenario.map_interface

        world = scenario.GetWorldState()
        track_params = ParameterServer()
        track_params["execution_model"] = 'ExecutionModelInterpolate'
        track_params["dynamic_model"] = 'SingleTrackModel'
        track_params["map_interface"] = world.map

        all_track_ids = list(scenario_track_info.GetOtherTrackInfos().keys())
        # also add ego id
        ego_track_id = scenario_track_info.GetEgoTrackInfo().GetTrackId()
        all_track_ids.append(ego_track_id)

        agent_list = []
        model_converter = ModelJsonConversion()
        for track_id in all_track_ids:
            if self._behavior_model and track_id != ego_track_id:
                behavior_params = self.__fill_agent_params(
                    scenario_track_info.GetEgoTrackInfo(),
                    scenario_track_info.GetOtherTrackInfos()[track_id])
                behavior_model_name = self._behavior_model
                track_params["behavior_model"] = model_converter.convert_model(
                    behavior_model_name, behavior_params)
                # behavior_params.Save("/tmp/agent_prams_{}.json".format(track_id))
            else:
                track_params["behavior_model"] = None

            agent = self.interaction_ds_reader.AgentFromTrackfile(
                track_params, self._params, scenario_track_info, track_id)
            agent.first_valid_timestamp = scenario_track_info.GetOffsetOfAgentMillisec(
                track_id)
            agent_list.append(agent)

        scenario._agent_list = agent_list  # must contain all agents!
        scenario._eval_agent_ids = [
            scenario_track_info.GetEgoTrackInfo().GetTrackId()
        ]
        scenario.json_params[
            "track_file"] = scenario_track_info.GetTrackFilename()

        return scenario
Esempio n. 5
0
    def __create_single_scenario__(self, scenario_track_info):
        scenario_track_info.TimeSanityCheck()

        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        # as we always use the same world, we can create the MapIntf. once
        if self._map_interface is None:
            scenario.CreateMapInterface(self._map_file_name)
            print("Creating New Map Interface for Scenario!")
        else:
            scenario.map_interface = self._map_interface
        self._map_interface = scenario.map_interface

        world = scenario.GetWorldState()
        agent_list = []
        track_params = ParameterServer()
        track_params["execution_model"] = 'ExecutionModelInterpolate'
        track_params["dynamic_model"] = 'SingleTrackModel'
        track_params["map_interface"] = world.map

        for id_other in scenario_track_info.GetOtherTrackInfos().keys():
            if str(id_other) in self._behavior_models:
                track_params["behavior_model"] = self._behavior_models[str(
                    id_other)]
            else:
                track_params["behavior_model"] = None
            agent = self.interaction_ds_reader.AgentFromTrackfile(
                track_params, self._params, scenario_track_info, id_other)
            agent_list.append(agent)

        id_ego = scenario_track_info.GetEgoTrackInfo().GetTrackId()
        if "ego" in self._behavior_models:
            # Always set this behavior to the ego agent, regardless of agent's ID
            track_params["behavior_model"] = self._behavior_models["ego"]
        elif str(id_ego) in self._behavior_models:
            track_params["behavior_model"] = self._behavior_models[str(id_ego)]
        else:
            track_params["behavior_model"] = None
        agent = self.interaction_ds_reader.AgentFromTrackfile(
            track_params, self._params, scenario_track_info, id_ego)
        agent_list.append(agent)

        scenario._agent_list = agent_list  # must contain all agents!
        scenario._eval_agent_ids = [
            scenario_track_info.GetEgoTrackInfo().GetTrackId()
        ]
        scenario.json_params[
            "track_file"] = scenario_track_info.GetTrackFilename()

        return scenario
Esempio n. 6
0
  def create_single_scenario(self):
    scenario = Scenario(map_file_name=None,
                        json_params=self._params.ConvertToDict())
    scenario._agent_list = []
    for agent_json in self.drone_params:
      agent_json["drone_model"]["map_interface"] = None
      agent_json["drone_model"]["goal_definition"] = self._build_sequential_goal_definition()
      json_converter = ModelJsonConversion()
      agent = json_converter.agent_from_json(agent_json["drone_model"],
                                                   param_server=self._local_params)
      agent.SetAgentId(agent_json["drone_model"]["id"])
      scenario._agent_list.append(agent)

    scenario._eval_agent_ids = [self._local_params["EgoAgentId",
                                "ID of the ego-agent",
                                0]]
    return scenario
Esempio n. 7
0
    def create_single_scenario(self):
        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        world = scenario.GetWorldState()
        agent_list = []
        scenario._agent_list = []
        for agent_json_ in self._local_params["Agents"]:
            agent_json = agent_json_["VehicleModel"].clone()
            agent_json["map_interface"] = world.map
            goal_polygon = Polygon2d([0, 0, 0],
                                     np.array(
                                         agent_json["goal"]["polygon_points"]))
            goal_polygon = goal_polygon.Translate(
                Point2d(agent_json["goal"]["center_pose"][0],
                        agent_json["goal"]["center_pose"][1]))

            sequential_goals = []
            goal = GoalDefinitionPolygon(goal_polygon)
            if "goal_type" in agent_json["goal"]:
                goal_type = agent_json["goal"]["goal_type"]
                if goal_type == "GoalDefinitionStateLimits":
                    goal = GoalDefinitionStateLimits(goal_polygon,
                                                     (1.49, 1.65))

            # state_limit_goal = GoalDefinitionStateLimits(goal_polygon, (1.49, 1.65))
            for _ in range(self._local_params["goal"]["num_reached", "num",
                                                      5]):
                sequential_goals.append(goal)
            sequential_goal = GoalDefinitionSequential(sequential_goals)
            agent_json["goal_definition"] = sequential_goal

            agent_state = np.array(agent_json["state"])
            if len(np.shape(agent_state)) > 1:
                agent_state = np.random.uniform(low=agent_state[:, 0],
                                                high=agent_state[:, 1])
            agent_json["state"] = agent_state.tolist()
            agent = self._json_converter.agent_from_json(
                agent_json, param_server=self._params)
            agent.SetAgentId(agent_json["id"])
            scenario._agent_list.append(agent)

        # TODO(@hart): this could be mult. agents
        scenario._eval_agent_ids = self._local_params[
            "controlled_ids", "IDs of agents to be controlled. ", [0]]
        return scenario
Esempio n. 8
0
    def __create_single_scenario__(self, scenario_track_info):
        scenario_track_info.TimeSanityCheck()

        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())

        world = scenario.GetWorldState()
        track_params = ParameterServer()
        track_params["execution_model"] = 'ExecutionModelInterpolate'
        track_params["dynamic_model"] = 'SingleTrackModel'
        track_params["map_interface"] = world.map

        all_track_ids = list(scenario_track_info.GetOtherTrackInfos().keys())
        # also add ego id
        all_track_ids.append(
            scenario_track_info.GetEgoTrackInfo().GetTrackId())

        agent_list = []
        model_converter = ModelJsonConversion()
        for track_id in all_track_ids:
            if str(track_id) in self._behavior_models:
                behavior_params = self.__fill_agent_params__()
                behavior_model_name = self._behavior_models[str(track_id)]
                track_params["behavior_model"] = model_converter.convert_model(
                    behavior_model_name, behavior_params)
            else:
                track_params["behavior_model"] = None

            agent = self.interaction_ds_reader.AgentFromTrackfile(
                track_params, self._params, scenario_track_info, track_id)
            # agent_params.Save("/tmp/agent_params_{}.json".format(track_id))
            agent_list.append(agent)

        scenario._agent_list = agent_list  # must contain all agents!
        scenario._eval_agent_ids = [
            scenario_track_info.GetEgoTrackInfo().GetTrackId()
        ]

        return scenario
    def __create_single_scenario__(self, scenario_track_info):
        scenario_track_info.TimeSanityCheck()

        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        world = scenario.GetWorldState()
        agent_list = []
        track_params = ParameterServer()
        track_params["execution_model"] = 'ExecutionModelInterpolate'
        track_params["dynamic_model"] = 'SingleTrackModel'
        track_params["map_interface"] = world.map

        for id_other in scenario_track_info.GetOtherTrackInfos().keys():
            if str(id_other) in self._behavior_models:
                track_params["behavior_model"] = self._behavior_models[str(
                    id_other)]
            else:
                track_params["behavior_model"] = None
            agent = self.interaction_ds_reader.AgentFromTrackfile(
                track_params, self._params, scenario_track_info, id_other)
            agent_list.append(agent)

        id_ego = scenario_track_info.GetEgoTrackInfo().GetTrackId()
        if str(id_ego) in self._behavior_models:
            track_params["behavior_model"] = self._behavior_models[str(id_ego)]
        else:
            track_params["behavior_model"] = None
        agent = self.interaction_ds_reader.AgentFromTrackfile(
            track_params, self._params, scenario_track_info, id_ego)
        agent_list.append(agent)

        scenario._agent_list = agent_list  # must contain all agents!
        scenario._eval_agent_ids = [
            scenario_track_info.GetEgoTrackInfo().GetTrackId()
        ]

        return scenario
    def create_single_scenario(self):
        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        world = scenario.GetWorldState()
        collected_sources_sinks_agent_states_geometries = []
        collected_sources_sinks_default_param_configs = []

        # Loop through each source sink config and first only create state
        # and geometry information
        road_corridors = []
        kwargs_agent_states_geometry = []
        sink_source_default_params = []

        for idx, sink_source_config in enumerate(self._sinks_sources):
            road_corridor = self.get_road_corridor_from_source_sink(
                sink_source_config, world.map)
            road_corridors.append(road_corridor)

            #1) create agent states and geometries for this source
            args = [road_corridor]
            agent_states, agent_geometries, kwargs_dict, default_params_state_geometry = \
              self.eval_configuration( sink_source_config, "ConfigAgentStatesGeometries",
                                    args, {})
            kwargs_agent_states_geometry.append(kwargs_dict)

            # collect default parameters of this config
            #sink_source_default_params.append(sink_source_config)
            #sink_source_default_params[idx]["ConfigAgentStatesGeometries"] = default_params_state_geometry.ConvertToDict()
            collected_sources_sinks_agent_states_geometries.append(
                (agent_states, agent_geometries))

        #2 remove overlapping agent states from different sources and sinks
        collected_sources_sinks_agent_states_geometries = \
                self.resolve_overlaps_in_sources_sinks_agents(collected_sources_sinks_agent_states_geometries)

        agent_list = []
        controlled_agent_ids_all = []
        for idx, agent_states_geometries in enumerate(
                collected_sources_sinks_agent_states_geometries):
            sink_source_config = self._sinks_sources[idx]
            agent_states = agent_states_geometries[0]
            agent_geometries = agent_states_geometries[1]
            road_corridor = road_corridors[idx]

            if (len(agent_states) == 0):
                continue

            #3) create behavior, execution and dynamic models
            args_list = [road_corridor, agent_states]
            kwargs_dict = {**kwargs_agent_states_geometry[idx]}
            config_return, kwargs_dict_tmp, default_params_behavior = \
              self.eval_configuration(
                                    sink_source_config, "ConfigBehaviorModels",
                                    args_list, kwargs_dict)
            behavior_models = config_return
            #sink_source_default_params[idx]["ConfigBehaviorModels"] = default_params_behavior.ConvertToDict()

            kwargs_dict = {**kwargs_dict, **kwargs_dict_tmp}
            config_return, kwargs_dict_tmp, default_params_execution = \
              self.eval_configuration(
                                    sink_source_config, "ConfigExecutionModels",
                                    args_list, kwargs_dict)
            execution_models = config_return
            #sink_source_default_params[idx]["ConfigExecutionModels"] = default_params_execution.ConvertToDict()
            kwargs_dict = {**kwargs_dict, **kwargs_dict_tmp}


            config_return, kwargs_dict_tmp, default_params_dynamic = \
              self.eval_configuration(
                                    sink_source_config, "ConfigDynamicModels",
                                    args_list, kwargs_dict)
            dynamic_models = config_return
            #sink_source_default_params[idx]["ConfigDynamicModels"] = default_params_dynamic.ConvertToDict()
            kwargs_dict = {**kwargs_dict, **kwargs_dict_tmp}

            #4 create goal definitions and controlled agents
            config_return, kwargs_dict_tmp, default_params_controlled_agents = \
              self.eval_configuration(
                                    sink_source_config, "ConfigControlledAgents",
                                    args_list, kwargs_dict)
            controlled_agent_ids = config_return
            controlled_agent_ids_all.extend(controlled_agent_ids)
            #sink_source_default_params[idx]["ConfigControlledAgents"] = default_params_controlled_agents.ConvertToDict()
            kwargs_dict = {**kwargs_dict, **kwargs_dict_tmp}

            args_list = [*args_list, controlled_agent_ids]
            config_return, kwargs_dict_tmp, default_params_goals = \
              self.eval_configuration(
                                    sink_source_config, "ConfigGoalDefinitions",
                                    args_list, kwargs_dict)
            goal_definitions = config_return
            #sink_source_default_params[idx]["ConfigGoalDefinitions"] = default_params_goals.ConvertToDict()

            #5 Build all agents for this source config
            kwargs_dict = {**kwargs_dict, **kwargs_dict_tmp}
            agent_params = sink_source_config["AgentParams"]
            sink_source_agents, controlled_ids = self.create_source_config_agents(
                agent_states, agent_geometries, behavior_models,
                execution_models, dynamic_models, goal_definitions,
                controlled_agent_ids, world, agent_params)
            #sink_source_default_params[idx]["AgentParams"] = agent_params.ConvertToDict()

            self.update_road_corridors(sink_source_agents, road_corridor)
            agent_list.extend(sink_source_agents)
            #collected_sources_sinks_default_param_configs.append(sink_source_config)

        #self._sink_source_default_params = sink_source_default_params
        scenario._eval_agent_ids = controlled_ids
        scenario._agent_list = agent_list

        return scenario
    def create_single_scenario(self):
        scenario = Scenario(map_file_name=self._map_file_name,
                            json_params=self._params.ConvertToDict())
        world = scenario.GetWorldState()
        agent_list = []
        # OTHER AGENTS
        for idx, source in enumerate(self._others_source):
            connecting_center_line, s_start, s_end = \
              self.center_line_between_source_and_sink(world.map,
                                                       source,
                                                       self._others_sink[idx])
            goal_polygon = Polygon2d([0, 0, 0], [
                Point2d(-1.5, 0),
                Point2d(-1.5, 8),
                Point2d(1.5, 8),
                Point2d(1.5, 0)
            ])
            goal_polygon = goal_polygon.Translate(
                Point2d(self._others_sink[idx][0], self._others_sink[idx][1]))
            goal_definition = GoalDefinitionPolygon(goal_polygon)
            agent_list.extend(
                self.place_agents_along_linestring(world,
                                                   connecting_center_line,
                                                   s_start, s_end,
                                                   self._agent_params,
                                                   goal_definition))

        description = self._params.ConvertToDict()
        description["ScenarioGenerator"] = "UniformVehicleDistribution"

        # EGO AGENT
        ego_agent = None
        if len(self._ego_route) == 0:
            # take agent in the middle of list
            num_agents = len(agent_list)
            ego_agent = agent_list[math.floor(num_agents / 4)]
        else:
            connecting_center_line, s_start, s_end  = \
            self.center_line_between_source_and_sink(world.map,
                                                     self._ego_route[0],
                                                     self._ego_route[1])

            sego = self.sample_srange_uniform([s_start, s_end])
            xy_point = GetPointAtS(connecting_center_line, sego)
            angle = GetTangentAngleAtS(connecting_center_line, sego)
            velocity = self.sample_velocity_uniform(self._ego_velocity_range)
            agent_state = np.array(
                [0, xy_point.x(),
                 xy_point.y(), angle, velocity])

            agent_params = self._agent_params.copy()
            agent_params["state"] = agent_state
            # goal for driving corridor generation
            goal_polygon = Polygon2d([0, 0, 0], [
                Point2d(-1.5, 0),
                Point2d(-1.5, 8),
                Point2d(1.5, 8),
                Point2d(1.5, 0)
            ])
            goal_polygon = goal_polygon.Translate(
                Point2d(self._ego_route[1][0], self._ego_route[1][1]))
            goal_definition = GoalDefinitionPolygon(goal_polygon)
            agent_params["goal_definition"] = goal_definition
            agent_params["map_interface"] = world.map

            converter = ModelJsonConversion()
            ego_agent = converter.agent_from_json(agent_params,
                                                  self._params["Agent"])
            # TODO(@bernhard): ensure that ego agent not collides with others

        agent_list.append(ego_agent)

        # EGO Agent Goal Definition
        if len(self._ego_goal_start) == 0:
            if len(self._ego_route) == 0:
                # ego agent is one of the random agents, so the goal definition is
                # already set
                pass
            else:
                goal_polygon = Polygon2d([0, 0, 0], [
                    Point2d(-1.5, 0),
                    Point2d(-1.5, 8),
                    Point2d(1.5, 8),
                    Point2d(1.5, 0)
                ])
                goal_polygon = goal_polygon.Translate(
                    Point2d(self._ego_goal_end[0], self._ego_goal_end[1]))
                ego_agent.goal_definition = GoalDefinitionPolygon(goal_polygon)
        else:
            connecting_center_line, s_start, s_end = \
            self.center_line_between_source_and_sink(world.map,
                                                     self._ego_goal_start,
                                                     self._ego_goal_end)

            goal_center_line = GetLineFromSInterval(connecting_center_line,
                                                    s_start, s_end)

            # build polygon representing state limits
            lims = self._ego_goal_state_limits
            goal_limits_left = goal_center_line.Translate(
                Point2d(-lims[0], -lims[1]))
            goal_limits_right = goal_center_line.Translate(
                Point2d(lims[0], lims[1]))
            goal_limits_right.Reverse()
            goal_limits_left.AppendLinestring(goal_limits_right)
            polygon = Polygon2d([0, 0, 0], goal_limits_left)

            ego_agent.goal_definition = GoalDefinitionStateLimits(
                polygon, (1.57 - 0.08, 1.57 + 0.08))

        # only one agent is ego in the middle of all other agents
        scenario._agent_list = agent_list
        scenario._eval_agent_ids = [ego_agent.id]
        return scenario