Exemplo n.º 1
0
    def test_one_agent_at_goal_state_limits(self):
        param_server = ParameterServer()
        # Model Definition
        behavior_model = BehaviorConstantVelocity(param_server)
        execution_model = ExecutionModelInterpolate(param_server)
        dynamic_model = SingleTrackModel(param_server)

        # Agent Definition
        agent_2d_shape = CarLimousine()
        init_state = np.array(
            [0, -191.789, -50.1725, 3.14 * 3.0 / 4.0, 150 / 3.6])
        agent_params = param_server.AddChild("agent1")
        goal_polygon = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon = goal_polygon.Translate(Point2d(-191.789, -50.1725))

        agent = Agent(
            init_state, behavior_model, dynamic_model, execution_model,
            agent_2d_shape, agent_params,
            GoalDefinitionStateLimits(
                goal_polygon,
                (3.14 * 3.0 / 4.0 - 0.08, 3.14 * 3.0 / 4.0 + 0.08)), None)

        world = World(param_server)
        world.AddAgent(agent)
        evaluator = EvaluatorGoalReached(agent.id)
        world.AddEvaluator("success", evaluator)

        info = world.Evaluate()
        self.assertEqual(info["success"], True)
Exemplo n.º 2
0
  def test_planning_time(self):
    param_server = ParameterServer()
    # Model Definition
    behavior_model = BehaviorConstantAcceleration(param_server)
    execution_model = ExecutionModelInterpolate(param_server)
    dynamic_model = SingleTrackModel(param_server)

    # Agent Definition
    agent_2d_shape = CarLimousine()
    init_state = np.array([0, -191.789,-50.1725, 3.14*3.0/4.0, 150/3.6])
    agent_params = param_server.AddChild("agent1")
    goal_polygon = Polygon2d([0, 0, 0],
                             [Point2d(-4,-4),
                              Point2d(-4,4),
                              Point2d(4,4),
                              Point2d(4,-4)])
    goal_polygon = goal_polygon.Translate(Point2d(-191.789,-50.1725))

    agent = Agent(init_state,
                behavior_model,
                dynamic_model,
                execution_model,
                agent_2d_shape,
                agent_params,
                GoalDefinitionPolygon(goal_polygon),
                  None)

    world = World(param_server)
    world.AddAgent(agent)
    evaluator = EvaluatorPlanningTime(agent.id)
    world.AddEvaluator("time", evaluator)


    info = world.Evaluate()
    self.assertEqual(info["time"], 0.0)
Exemplo n.º 3
0
    def test_number_of_agents(self):
        # World Definition
        params = ParameterServer()
        world = World(params)

        # Model Definitions
        behavior_model = BehaviorConstantAcceleration(params)
        execution_model = ExecutionModelInterpolate(params)
        dynamic_model = SingleTrackModel(params)

        behavior_model2 = BehaviorConstantAcceleration(params)
        execution_model2 = ExecutionModelInterpolate(params)
        dynamic_model2 = SingleTrackModel(params)

        # Map Definition
        map_interface = MapInterface()
        xodr_map = MakeXodrMapOneRoadTwoLanes()
        map_interface.SetOpenDriveMap(xodr_map)
        world.SetMap(map_interface)

        agent_2d_shape = CarLimousine()
        init_state = np.array([0, 13, -1.75, 0, 5])
        agent_params = params.AddChild("agent1")
        goal_polygon = Polygon2d(
            [1, 1, 0],
            [Point2d(0, 0),
             Point2d(0, 2),
             Point2d(2, 2),
             Point2d(2, 0)])
        goal_polygon = goal_polygon.Translate(Point2d(50, -2))

        agent = Agent(init_state, behavior_model, dynamic_model,
                      execution_model, agent_2d_shape, agent_params,
                      GoalDefinitionPolygon(goal_polygon), map_interface)
        world.AddAgent(agent)

        init_state2 = np.array([0, 16, -1.75, 0, 5])
        agent2 = Agent(init_state2, behavior_model2, dynamic_model2,
                       execution_model2, agent_2d_shape, agent_params,
                       GoalDefinitionPolygon(goal_polygon), map_interface)
        world.AddAgent(agent2)

        evaluator = EvaluatorNumberOfAgents(agent.id)
        world.AddEvaluator("num_agents", evaluator)

        info = world.Evaluate()
        self.assertEqual(info["num_agents"], len(world.agents))
        # do it once more
        self.assertEqual(info["num_agents"], len(world.agents))

        world.RemoveAgentById(agent2.id)
        info = world.Evaluate()
        # evaluator should still hold two
        self.assertNotEqual(info["num_agents"], len(world.agents))
        self.assertEqual(info["num_agents"], 2)

        world.Step(0.1)
        info = world.Evaluate()
        # evaluator should still hold two
        self.assertEqual(info["num_agents"], 2)
  def test_observed_agents_selection(self):
    agent_limit = 10
    params = ParameterServer()
    params["ML"]["GraphObserver"]["AgentLimit"] = agent_limit
    observer = GraphObserver(params=params)

    obs, obs_world = self._get_observation(
      observer=observer,
      world=self.world,
      eval_id=self.eval_id)

    obs = tf.expand_dims(obs, 0) # add a batch dimension

    nodes, _, _ = GraphObserver.graph(obs, graph_dims=observer.graph_dimensions)
    nodes = nodes[0] # remove batch dim

    ego_node = nodes[0]
    ego_node_pos = Point2d(
      ego_node[0].numpy(), # x coordinate
      ego_node[1].numpy()) # y coordinate

    # verify that the nodes are ordered by
    # ascending distance to the ego node
    max_distance_to_ego = 0
    for node in nodes:
      pos = Point2d(
        node[0].numpy(), # x coordinate
        node[1].numpy()) # y coordinate
      distance_to_ego = Distance(pos, ego_node_pos)

      self.assertGreaterEqual(distance_to_ego, max_distance_to_ego,
        msg='Nodes are not sorted by distance relative to '\
          + 'the ego node in ascending order.')

      max_distance_to_ego = distance_to_ego
Exemplo n.º 5
0
    def test_lane_change(self):
        # World Definition
        params = ParameterServer()
        world = World(params)

        # Model Definitions
        behavior_model = BehaviorMobil(params)
        execution_model = ExecutionModelInterpolate(params)
        dynamic_model = SingleTrackModel(params)

        behavior_model2 = BehaviorIDMLaneTracking(params)
        execution_model2 = ExecutionModelInterpolate(params)
        dynamic_model2 = SingleTrackModel(params)

        # Map Definition
        map_interface = MapInterface()
        xodr_map = MakeXodrMapOneRoadTwoLanes()
        map_interface.SetOpenDriveMap(xodr_map)
        world.SetMap(map_interface)

        #agent_2d_shape = CarLimousine()
        agent_2d_shape = CarRectangle()
        init_state = np.array([0, 3, -1.75, 0, 5])
        agent_params = params.AddChild("agent1")
        goal_polygon = Polygon2d(
            [1, 1, 0],
            [Point2d(0, 0),
             Point2d(0, 2),
             Point2d(2, 2),
             Point2d(2, 0)])
        goal_polygon = goal_polygon.Translate(Point2d(50, -2))

        agent = Agent(init_state, behavior_model, dynamic_model,
                      execution_model, agent_2d_shape, agent_params,
                      GoalDefinitionPolygon(goal_polygon), map_interface)
        world.AddAgent(agent)

        init_state2 = np.array([0, 15, -1.75, 0, 2])
        agent2 = Agent(init_state2, behavior_model2, dynamic_model2,
                       execution_model2, agent_2d_shape, agent_params,
                       GoalDefinitionPolygon(goal_polygon), map_interface)
        world.AddAgent(agent2)

        # viewer
        viewer = MPViewer(params=params, use_world_bounds=True)

        # World Simulation
        sim_step_time = params["simulation"]["step_time",
                                             "Step-time in simulation", 0.05]
        sim_real_time_factor = params["simulation"][
            "real_time_factor", "execution in real-time or faster", 100]

        # Draw map
        for _ in range(0, 10):
            viewer.clear()
            world.Step(sim_step_time)
            viewer.drawWorld(world)
            viewer.show(block=False)
            time.sleep(sim_step_time / sim_real_time_factor)
Exemplo n.º 6
0
 def goal(self, world):
     goal_polygon = Polygon2d(
         [-1000, -1000, -1000],
         [Point2d(-1, -1),
          Point2d(-1, 1),
          Point2d(1, 1),
          Point2d(1, -1)])
     return GoalDefinitionPolygon(goal_polygon)
Exemplo n.º 7
0
def make_initial_world(primitives):
    # must be within examples params folder
    params = ParameterServer()
    world = World(params)

    # Define two behavior models
    behavior_model = BehaviorMPContinuousActions(params)
    primitive_mapping = {}
    for prim in primitives:
        idx = behavior_model.AddMotionPrimitive(
            np.array(prim))  # adding action
        primitive_mapping[idx] = prim

    behavior_model.ActionToBehavior(0)  # setting initial action

    execution_model = ExecutionModelInterpolate(params)
    dynamic_model = SingleTrackModel(params)

    behavior_model2 = BehaviorConstantVelocity(params)
    execution_model2 = ExecutionModelInterpolate(params)
    dynamic_model2 = SingleTrackModel(params)

    # Define the map interface and load a testing map
    map_interface = MapInterface()
    xodr_map = MakeXodrMapOneRoadTwoLanes()
    map_interface.SetOpenDriveMap(xodr_map)
    world.SetMap(map_interface)

    # Define the agent shapes
    agent_2d_shape = CarRectangle()
    init_state = np.array([0, 3, -5.25, 0, 20])

    # Define the goal definition for agents
    center_line = Line2d()
    center_line.AddPoint(Point2d(0.0, -1.75))
    center_line.AddPoint(Point2d(100.0, -1.75))

    max_lateral_dist = (0.4, 0.5)
    max_orientation_diff = (0.08, 0.1)
    velocity_range = (5.0, 20.0)
    goal_definition = GoalDefinitionStateLimitsFrenet(center_line,
                                                      max_lateral_dist,
                                                      max_orientation_diff,
                                                      velocity_range)

    # define two agents with the different behavior models
    agent_params = params.AddChild("agent1")
    agent = Agent(init_state, behavior_model, dynamic_model, execution_model,
                  agent_2d_shape, agent_params, goal_definition, map_interface)
    world.AddAgent(agent)

    init_state2 = np.array([0, 25, -5.25, 0, 15])
    agent2 = Agent(init_state2, behavior_model2, dynamic_model2,
                   execution_model2, agent_2d_shape, agent_params,
                   goal_definition, map_interface)
    world.AddAgent(agent2)

    return world
Exemplo n.º 8
0
    def test_gap_distance_front(self):
        # World Definition
        params = ParameterServer()
        world = World(params)

        gap = 10

        # Model Definitions
        behavior_model = BehaviorConstantAcceleration(params)
        execution_model = ExecutionModelInterpolate(params)
        dynamic_model = SingleTrackModel(params)

        behavior_model2 = BehaviorConstantAcceleration(params)
        execution_model2 = ExecutionModelInterpolate(params)
        dynamic_model2 = SingleTrackModel(params)

        # Map Definition
        map_interface = MapInterface()
        xodr_map = MakeXodrMapOneRoadTwoLanes()
        map_interface.SetOpenDriveMap(xodr_map)
        world.SetMap(map_interface)

        agent_2d_shape = CarLimousine()
        init_state = np.array([0, 13, -1.75, 0, 5])
        agent_params = params.AddChild("agent1")
        goal_polygon = Polygon2d(
            [1, 1, 0],
            [Point2d(0, 0),
             Point2d(0, 2),
             Point2d(2, 2),
             Point2d(2, 0)])
        goal_polygon = goal_polygon.Translate(Point2d(50, -2))

        agent = Agent(init_state, behavior_model, dynamic_model,
                      execution_model, agent_2d_shape, agent_params,
                      GoalDefinitionPolygon(goal_polygon), map_interface)
        world.AddAgent(agent)

        init_state2 = np.array([0, 13 + gap, -1.75, 0, 5])
        agent2 = Agent(init_state2, behavior_model2, dynamic_model2,
                       execution_model2, agent_2d_shape, agent_params,
                       GoalDefinitionPolygon(goal_polygon), map_interface)
        world.AddAgent(agent2)

        world.Step(0.1)

        evaluator = EvaluatorGapDistanceFront(agent.id)
        world.AddEvaluator("gap", evaluator)

        info = world.Evaluate()
        self.assertAlmostEqual(info["gap"],
                               gap - agent_2d_shape.front_dist -
                               agent_2d_shape.rear_dist,
                               places=4)
Exemplo n.º 9
0
    def test_relevant_agents(self):

        params = ParameterServer()
        map = "bark/runtime/tests/data/city_highway_straight.xodr"
        params["EvaluatorRss"]["MapFilename"] = map

        map_interface = EvaluatorRSSTests.load_map(map)
        world = World(params)
        world.SetMap(map_interface)

        goal_polygon_1 = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon_1 = goal_polygon_1.Translate(Point2d(5.5, 120))

        goal_polygon_2 = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon_2 = goal_polygon_2.Translate(Point2d(1.8, 120))

        ego_state = np.array([0, 5.5, 10, np.pi / 2, 10])
        other_1_state = np.array([0, 1.8, -10, np.pi / 2, 15])
        other_2_state = np.array([0, 1.8, -120, np.pi / 2, 10])

        ego = TestAgent(ego_state, goal_polygon_1, map_interface, params)
        other_1 = TestAgent(other_1_state, goal_polygon_2, map_interface,
                            params)
        other_2 = TestAgent(other_2_state, goal_polygon_2, map_interface,
                            params)

        world.AddAgent(ego)
        world.AddAgent(other_1)
        world.AddAgent(other_2)

        viewer = MPViewer(params=params, use_world_bounds=True)
        viewer.drawWorld(world)
        viewer.show(block=False)

        evaluator_rss = EvaluatorRSS(ego.id, params)
        responses = evaluator_rss.PairwiseEvaluate(world)

        self.assertEqual(1, len(responses))
        self.assertTrue(responses[other_1.id])
        self.assertFalse(other_2.id in responses)
Exemplo n.º 10
0
    def test_lateral_highway_unsafe(self):
        """
        Checking Lateral Responses (true means safe)
        """

        params = ParameterServer()
        map = "bark/runtime/tests/data/city_highway_straight.xodr"
        params["EvaluatorRss"]["MapFilename"] = map

        map_interface = EvaluatorRSSTests.load_map(map)
        world = World(params)
        world.SetMap(map_interface)

        goal_polygon_1 = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon_1 = goal_polygon_1.Translate(Point2d(5.5, 120))

        goal_polygon_2 = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon_2 = goal_polygon_2.Translate(Point2d(1.8, 120))

        # Hard coded
        ego_state = np.array([0, 5.0, 10, np.pi / 2, 10])  # straight north
        other_state = np.array([0, 3.1, 0, np.pi / 2, 10])  # straight north

        ego = TestAgent(ego_state, goal_polygon_1, map_interface, params)
        other = TestAgent(other_state, goal_polygon_2, map_interface, params)

        world.AddAgent(ego)
        world.AddAgent(other)
        world.UpdateAgentRTree()

        viewer = MPViewer(params=params, use_world_bounds=True)
        viewer.drawWorld(world)
        viewer.show(block=False)

        evaluator_rss = EvaluatorRSS(ego.id, params)

        self.assertEqual(
            False,
            evaluator_rss.PairwiseDirectionalEvaluate(world)[other.id][1])
Exemplo n.º 11
0
    def test_lateral_same_direction(self):
        map = "bark/runtime/tests/data/city_highway_straight.xodr"
        map_interface = EvaluatorRSSTests.load_map(map)
        world = self.defaults["world"].Copy()
        world.SetMap(map_interface)

        goal_polygon_1 = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon_1 = goal_polygon_1.Translate(Point2d(5.5, 120))

        goal_polygon_2 = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon_2 = goal_polygon_2.Translate(Point2d(1.8, 120))

        # Hard coded
        ego_state = np.array([0, 5.5, 10, 0, 10])
        other_state = np.array([0, 1.8, -10, 0, 15])

        ego = Agent(ego_state, self.defaults["ego_behavior"],
                    self.defaults["ego_dynamic"],
                    self.defaults["ego_execution"], self.defaults["ego_shape"],
                    self.defaults["agent_params"],
                    GoalDefinitionPolygon(goal_polygon_1), map_interface)
        other = Agent(other_state, self.defaults["other_behavior"],
                      self.defaults["other_dynamic"],
                      self.defaults["other_execution"],
                      self.defaults["other_shape"],
                      self.defaults["agent_params"],
                      GoalDefinitionPolygon(goal_polygon_2), map_interface)

        world.AddAgent(ego)
        world.AddAgent(other)

        evaluator_rss = EvaluatorRss(ego.id, map,
                                     self.defaults["default_vehicle_dynamics"])

        for _ in range(7):
            world.Step(1)
            self.assertEqual(
                True,
                evaluator_rss.PairwiseDirectionalEvaluate(world)[other.id][1])
Exemplo n.º 12
0
def GoalDefinitionFromTrack(track, end):
    states = list(dict_utils.get_item_iterator(track.motion_states))
    motion_state = states[-1][1]
    bark_state = BarkStateFromMotionState(motion_state)
    goal_polygon = Polygon2d(
        np.array([0.0, 0.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(bark_state[0, int(StateDefinition.X_POSITION)],
                bark_state[0, int(StateDefinition.Y_POSITION)]))
    goal_definition = GoalDefinitionPolygon(goal_polygon)
    return goal_definition
Exemplo n.º 13
0
    def create_cpp_plan_view(self, plan_view, header):

        new_plan_view = PlanView()
        # create plan view..
        for geometry in plan_view["geometries"]:
            starting_point = Point2d(float(geometry["x"]),
                                     float(geometry["y"]))
            if geometry["geometry"]["type"] == "line":
                new_plan_view.AddLine(starting_point, float(geometry["hdg"]),
                                      float(geometry["length"]))
            if geometry["geometry"]["type"] == "arc":
                new_plan_view.AddArc(starting_point, float(geometry["hdg"]),
                                     float(geometry["length"]),
                                     float(geometry["geometry"]["curvature"]),
                                     0.2)  # TODO: s_inc
            if geometry["geometry"]["type"] == "spiral":
                new_plan_view.AddSpiral(
                    starting_point, float(geometry["hdg"]),
                    float(geometry["length"]),
                    float(geometry["geometry"]["curv_start"]),
                    float(geometry["geometry"]["curv_end"]),
                    0.2)  # TODO: s_inc

        # now use header/ offset to modify plan view
        if "offset" in header:
            off_x = header["offset"]["x"]
            off_y = header["offset"]["y"]
            off_hdg = header["offset"]["hdg"]
            logger.debug("Transforming PlanView with given offset",
                         header["offset"])
            new_plan_view.ApplyOffsetTransform(off_x, off_y, off_hdg)

        return new_plan_view
Exemplo n.º 14
0
  def test_road(self):
    newXodrRoad = XodrRoad()
    newXodrRoad.id = 1
    newXodrRoad.name = "Autobahn A9"

    newPlanView = PlanView()
    newPlanView.AddLine(Point2d(0, 0), 1.57079632679, 10)

    newXodrRoad.plan_view = newPlanView

    line = newXodrRoad.plan_view.GetReferenceLine().ToArray()

    # Spiral
    p = Point2d(line[-1][0], line[-1][1])
    newXodrRoad.plan_view.AddSpiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4)
    line = newXodrRoad.plan_view.GetReferenceLine().ToArray()
Exemplo n.º 15
0
 def test_write_params_agent(self):
     params = ParameterServer()
     behavior = BehaviorConstantAcceleration(params)
     execution = ExecutionModelInterpolate(params)
     dynamic = SingleTrackModel(params)
     shape = Polygon2d([1.25, 1, 0], [
         Point2d(0, 0),
         Point2d(0, 2),
         Point2d(4, 2),
         Point2d(4, 0),
         Point2d(0, 0)
     ])
     init_state = np.zeros(4)
     agent = Agent(init_state, behavior, dynamic, execution, shape,
                   params.AddChild("agent"))
     params.Save("written_agents_param_test.json")
Exemplo n.º 16
0
    def test_between_lanes(self):
        xodr_parser = XodrParser(
            os.path.join(
                os.path.dirname(__file__),
                "../../../runtime/tests/data/city_highway_straight.xodr"))
        np.set_printoptions(precision=8)
        params = ParameterServer()

        world = World(params)
        map_interface = MapInterface()
        map_interface.SetOpenDriveMap(xodr_parser.map)
        world.SetMap(map_interface)

        # Simple test
        point_close = Point2d(2, -92.55029)
        lane_sw = map_interface.FindLane(point_close)
        self.assertIsNotNone(
            lane_sw,
            "This point is still in the left lane! XodrLane boundary is 5114.683"
        )

        switched_lane = False
        lng_coord = -92.55029
        i = 1.817
        lane_sw = map_interface.FindLane(Point2d(i, lng_coord))
        assert lane_sw != None
        prev = lane_sw.lane_id
        prev_i = i
        while (i < 4.817):
            lane_sw = map_interface.FindLane(Point2d(i, lng_coord))
            self.assertIsNotNone(
                lane_sw,
                "Should always be on at least one lane! Currently at ({}, {})".
                format(i, lng_coord))
            if prev != lane_sw.lane_id:
                # print(prev)
                # print(prev_i)
                # print(lane_sw.lane_id)
                # print(i)
                self.assertFalse(switched_lane,
                                 "XodrLane switch should only happens once!")
                switched_lane = True
            prev_i = i
            prev = lane_sw.lane_id
            i = i + 0.01
        self.assertTrue(switched_lane,
                        "Eventually should have switched lanes!")
Exemplo n.º 17
0
    def test_longitude_ego_follow_other(self):
        map = "bark/runtime/tests/data/city_highway_straight.xodr"
        map_interface = EvaluatorRSSTests.load_map(map)
        world = self.defaults["world"].Copy()
        world.SetMap(map_interface)

        goal_polygon = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon = goal_polygon.Translate(Point2d(1.8, 120))

        # The safety distance seems more conservative than in the paper
        # Hard coded
        ego_state = np.array([0, 1.8, -114.9, 0, 10])
        other_state = np.array([0, 1.8, -72.95, 0, 7])

        ego = Agent(ego_state, self.defaults["ego_behavior"],
                    self.defaults["ego_dynamic"],
                    self.defaults["ego_execution"], self.defaults["ego_shape"],
                    self.defaults["agent_params"],
                    GoalDefinitionPolygon(goal_polygon), map_interface)
        other = Agent(other_state, self.defaults["other_behavior"],
                      self.defaults["other_dynamic"],
                      self.defaults["other_execution"],
                      self.defaults["other_shape"],
                      self.defaults["agent_params"],
                      GoalDefinitionPolygon(goal_polygon), map_interface)

        world.AddAgent(ego)
        world.AddAgent(other)

        evaluator_rss = EvaluatorRss(ego.id,
                                     map,
                                     self.defaults["default_vehicle_dynamics"],
                                     checking_relevent_range=1.5)
        world.Step(0.01)
        self.assertEqual(
            True,
            evaluator_rss.PairwiseDirectionalEvaluate(world)[other.id][0])
        world.Step(0.01)
        self.assertEqual(
            False,
            evaluator_rss.PairwiseDirectionalEvaluate(world)[other.id][0])
Exemplo n.º 18
0
    def test_lateral_merging(self):
        map = "bark/runtime/tests/data/DR_DEU_Merging_MT_v01_centered.xodr"
        map_interface = EvaluatorRSSTests.load_map(map)
        world = self.defaults["world"].Copy()
        world.SetMap(map_interface)

        goal_polygon = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon = goal_polygon.Translate(Point2d(-16, 108))

        # Hard coded
        ego_state = np.array([0, 68, 108, 0, 5])
        other_state = np.array([0, 64, 104, 0, 5])

        ego = Agent(ego_state, self.defaults["ego_behavior"],
                    self.defaults["ego_dynamic"],
                    self.defaults["ego_execution"], self.defaults["ego_shape"],
                    self.defaults["agent_params"],
                    GoalDefinitionPolygon(goal_polygon), map_interface)
        other = Agent(other_state, self.defaults["other_behavior"],
                      self.defaults["other_dynamic"],
                      self.defaults["other_execution"],
                      self.defaults["other_shape"],
                      self.defaults["agent_params"],
                      GoalDefinitionPolygon(goal_polygon), map_interface)

        world.AddAgent(ego)
        world.AddAgent(other)

        evaluator_rss = EvaluatorRss(ego.id, map,
                                     self.defaults["default_vehicle_dynamics"])
        world.Step(1)
        self.assertEqual(
            True,
            evaluator_rss.PairwiseDirectionalEvaluate(world)[other.id][1])

        world.Step(1)
        self.assertEqual(
            False,
            evaluator_rss.PairwiseDirectionalEvaluate(world)[other.id][1])
Exemplo n.º 19
0
    def test_longitude_highway_unsafe(self):
        """
        Checking Longitudinal Responses (true means safe)
        """

        params = ParameterServer()
        map = "bark/runtime/tests/data/city_highway_straight.xodr"
        params["EvaluatorRss"]["MapFilename"] = map

        map_interface = EvaluatorRSSTests.load_map(map)
        world = World(params)
        world.SetMap(map_interface)

        goal_polygon = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon = goal_polygon.Translate(Point2d(1.8, 120))

        # The safety distance seems more conservative than in the paper
        # Hard coded
        ego_state = np.array([0, 1.8, -60.0, np.pi / 2, 10])
        other_state = np.array([0, 1.8, -68.0, np.pi / 2, 10])

        ego = TestAgent(ego_state, goal_polygon, map_interface, params)
        other = TestAgent(other_state, goal_polygon, map_interface, params)

        world.AddAgent(ego)
        world.AddAgent(other)
        world.UpdateAgentRTree()

        viewer = MPViewer(params=params, use_world_bounds=True)
        viewer.drawWorld(world)
        viewer.show(block=False)

        evaluator_rss = EvaluatorRSS(ego.id, params)

        pw_directional_evaluation_return = evaluator_rss.PairwiseDirectionalEvaluate(
            world)
        self.assertEqual(False, pw_directional_evaluation_return[other.id][0])
Exemplo n.º 20
0
    def test_lateral_merging_safe(self):
        """
        Checking Lateral Responses (true means safe)
        """

        params = ParameterServer()
        map = "bark/runtime/tests/data/DR_DEU_Merging_MT_v01_centered.xodr"
        params["EvaluatorRss"]["MapFilename"] = map

        map_interface = EvaluatorRSSTests.load_map(map)
        world = World(params)
        world.SetMap(map_interface)

        goal_polygon = Polygon2d(
            [0, 0, 0],
            [Point2d(-1, -1),
             Point2d(-1, 1),
             Point2d(1, 1),
             Point2d(1, -1)])
        goal_polygon = goal_polygon.Translate(Point2d(-15.4, 108.6))

        # Hard coded
        ego_state = np.array([0, 68.1, 108, -np.pi, 5])
        other_state = np.array([0, 64.1, 105, -np.pi, 5])

        ego = TestAgent(ego_state, goal_polygon, map_interface, params)
        other = TestAgent(other_state, goal_polygon, map_interface, params)

        world.AddAgent(ego)
        world.AddAgent(other)
        world.UpdateAgentRTree()

        viewer = MPViewer(params=params, use_world_bounds=True)
        viewer.drawWorld(world)
        viewer.show(block=False)

        evaluator_rss = EvaluatorRSS(ego.id, params)
        world.AddEvaluator("rss", evaluator_rss)

        pw_directional_evaluation_return = evaluator_rss.PairwiseDirectionalEvaluate(
            world)
        self.assertEqual(True, pw_directional_evaluation_return[other.id][1])
Exemplo n.º 21
0
  def test_one_agent_at_goal_sequential(self):
    param_server = ParameterServer()
    # Model Definition
    dynamic_model = SingleTrackModel(param_server)
    behavior_model = BehaviorMPContinuousActions(param_server)
    idx = behavior_model.AddMotionPrimitive(np.array([1, 0]))
    behavior_model.ActionToBehavior(idx)
    execution_model = ExecutionModelInterpolate(param_server)


    # Agent Definition
    agent_2d_shape = CarLimousine()
    init_state = np.array([0, 0, 0, 0, 0])
    agent_params = param_server.AddChild("agent1")
    goal_frame = Polygon2d([0, 0, 0],
                             [Point2d(-1,-1),
                              Point2d(-1,1),
                              Point2d(1,1),
                              Point2d(1,-1)])

    goal_polygon1 = goal_frame.Translate(Point2d(10, 0))
    goal_polygon2 = goal_frame.Translate(Point2d(20, 0))
    goal_polygon3 = goal_frame.Translate(Point2d(30, 0))

    goal_def1 = GoalDefinitionStateLimits(goal_polygon1, [-0.08, 0.08])
    goal_def2 = GoalDefinitionStateLimits(goal_polygon2, [-0.08, 0.08])
    goal_def3 = GoalDefinitionStateLimits(goal_polygon3, [-0.08, 0.08])

    goal_definition = GoalDefinitionSequential([goal_def1,
                                                goal_def2,
                                                goal_def3])

    self.assertEqual(len(goal_definition.sequential_goals),3)
    agent = Agent(init_state,
                behavior_model,
                dynamic_model,
                execution_model,
                agent_2d_shape,
                agent_params,
                goal_definition,
                  None)

    world = World(param_server)
    world.AddAgent(agent)
    evaluator = EvaluatorGoalReached(agent.id)
    world.AddEvaluator("success", evaluator)

    # just drive with the single motion primitive should be successful 
    for _ in range(0,1000):
        world.Step(0.2)
        info = world.Evaluate()
        if info["success"]:
            break
    
    self.assertEqual(info["success"], True)
    self.assertAlmostEqual(agent.state[int(StateDefinition.X_POSITION)], 30, delta=0.5)
Exemplo n.º 22
0
    def test_draw_agents(self):
        params = ParameterServer()
        behavior = BehaviorConstantAcceleration(params)
        execution = ExecutionModelInterpolate(params)
        dynamic = SingleTrackModel(params)
        shape = Polygon2d([1.25, 1, 0], [
            Point2d(0, 0),
            Point2d(0, 2),
            Point2d(4, 2),
            Point2d(4, 0),
            Point2d(0, 0)
        ])
        shape2 = CarLimousine()

        init_state = [0, 3, 2, 1]
        init_state2 = [0, 0, 5, 4]

        agent = Agent(init_state, behavior, dynamic, execution, shape,
                      params.AddChild("agent"))
        agent2 = Agent(init_state2, behavior, dynamic, execution, shape2,
                       params.AddChild("agent"))
Exemplo n.º 23
0
    def __find_first_ts_on_map__(self, id_ego):
        traj = TrajectoryFromTrack(self._track_dict[id_ego])
        for state in traj:
            point_agent = Point2d(state[1], state[2])
            lane_list = self._map_interface.find_nearest_lanes(point_agent, 3)
            for lane in lane_list:
                polygon = self._map_interface.GetRoadgraph(
                ).GetLanePolygonForLaneId(lane.lane_id)
                if Collide(polygon, point_agent):
                    time_ego_first = state[0] * 1e3  # use timestamp in ms
                    return time_ego_first

        return None
Exemplo n.º 24
0
  def test_line(self):
    pv = PlanView()

    # Line
    pv.AddLine(Point2d(0, 0), 1.57079632679, 10)
    line = pv.GetReferenceLine().ToArray()

    # Spiral
    p = Point2d(line[-1][0], line[-1][1])
    pv.AddSpiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4)
    line = pv.GetReferenceLine().ToArray()

    offset = XodrLaneOffset(1.5, 0, 0, 0)
    lane_width = XodrLaneWidth(0.0, 59.9, offset)

    lane = XodrLane.CreateLaneFromLaneWidth(-1,
                                            pv.GetReferenceLine(), lane_width, 0.5)

    print(lane)
    lane = XodrLane.CreateLaneFromLaneWidth(
        1, pv.GetReferenceLine(), lane_width, 0.5)
    print(lane)
Exemplo n.º 25
0
    def __find_first_ts_on_map__(self, id_ego):
        traj = TrajectoryFromTrack(self._track_dict[id_ego])
        for state in traj:
            point_agent = Point2d(state[1], state[2])
            lane_list = self._map_interface.find_nearest_lanes(point_agent, 3)
            for lane in lane_list:
                polygon = self._map_interface.GetRoadgraph().GetLanePolygonForLaneId(lane.lane_id)
                if Collide(polygon, point_agent):
                    timestamp_scaling = 1e3 # scale timestamp from s (BARK) to ms (dataset)
                    time_ego_first = state[0]*timestamp_scaling + self._starting_offset_ms 
                    return time_ego_first

        return None
Exemplo n.º 26
0
    def test_two_roads_one_lane(self):
        params = ParameterServer()
        world = World(params)

        xodr_map = MakeXodrMapOneRoadTwoLanes()

        map_interface = MapInterface()
        map_interface.SetOpenDriveMap(xodr_map)
        world.SetMap(map_interface)

        start_point = Point2d(0, -11)
        lanes_near_start = map_interface.find_nearest_lanes(start_point, 1)
        assert(len(lanes_near_start) == 1)

        goal_point = Point2d(-191.789, -50.1725)
        lanes_near_goal = map_interface.find_nearest_lanes(goal_point, 1)
        assert(len(lanes_near_goal) == 1)

        viewer = MPViewer(params=params, use_world_bounds=True)
        viewer.drawWorld(world)

        # if this is not here, the second unit test is not executed (maybe parsing takes too long?)
        time.sleep(2)
Exemplo n.º 27
0
    def __find_first_ts_on_map__(self, id_ego):
        traj = TrajectoryFromTrack(self._track_dict[id_ego])
        for state in traj:
            point_agent = Point2d(state[1], state[2])
            car_shape = CarRectangle()
            agent_shape = car_shape.Transform([state[1], state[2], state[3]])
            lane_list = self._map_interface.find_nearest_lanes(point_agent, 3)
            for lane in lane_list:
                lane_polygon = self._map_interface.GetRoadgraph(
                ).GetLanePolygonForLaneId(lane.lane_id)
                # if Collide(lane_polygon, point_agent) and Within(agent_shape, lane_polygon):
                if Collide(lane_polygon, point_agent):
                    offset_fix = 500  # adding some time offset to always find lane corridor
                    time_ego_first = state[
                        0] * 1e3 + offset_fix  # use timestamp in ms
                    return time_ego_first

        return None
Exemplo n.º 28
0
    def create_cpp_plan_view(self, plan_view, header):

        new_plan_view = PlanView()
        # create plan view..
        for geo in plan_view["geometries"]:
            start_p = Point2d(float(geo["x"]), float(geo["y"]))
            if geo["geometry"]["type"] == "line":
                if self._s_inc_straight_line is None:
                    s_inc_straight_line = float(geo["length"])
                elif isinstance(self._s_inc_straight_line, float):
                    s_inc_straight_line = self._s_inc_straight_line
                else:
                    raise TypeError("s_inc_straight_line not specified")
                new_plan_view.AddLine(start_p, float(geo["hdg"]),
                                      float(geo["length"]),
                                      s_inc_straight_line)

            if geo["geometry"]["type"] == "arc":
                new_plan_view.AddArc(start_p, float(geo["hdg"]),
                                     float(geo["length"]),
                                     float(geo["geometry"]["curvature"]),
                                     self._s_inc_curves)

            if geo["geometry"]["type"] == "spiral":
                new_plan_view.AddSpiral(start_p, float(geo["hdg"]),
                                        float(geo["length"]),
                                        float(geo["geometry"]["curv_start"]),
                                        float(geo["geometry"]["curv_end"]),
                                        self._s_inc_curves)

        # now use header/ offset to modify plan view
        if "offset" in header:
            off_x = header["offset"]["x"]
            off_y = header["offset"]["y"]
            off_hdg = header["offset"]["hdg"]
            logger.info("Transforming PlanView with given offset",
                        header["offset"])
            new_plan_view.ApplyOffsetTransform(off_x, off_y, off_hdg)

        return new_plan_view
Exemplo n.º 29
0
    def test_world(self):
        # create agent
        params = ParameterServer()
        behavior = BehaviorConstantAcceleration(params)
        execution = ExecutionModelInterpolate(params)
        dynamic = SingleTrackModel(params)
        shape = Polygon2d([1.25, 1, 0], [
            Point2d(0, 0),
            Point2d(0, 2),
            Point2d(4, 2),
            Point2d(4, 0),
            Point2d(0, 0)
        ])
        init_state = np.array([0, 0, 0, 0, 5])
        agent = Agent(init_state, behavior, dynamic, execution, shape,
                      params.AddChild("agent"))
        road_map = OpenDriveMap()
        newXodrRoad = XodrRoad()
        newXodrRoad.id = 1
        newXodrRoad.name = "Autobahn A9"
        newPlanView = PlanView()
        newPlanView.AddLine(Point2d(0, 0), 1.57079632679, 10)
        newXodrRoad.plan_view = newPlanView
        line = newXodrRoad.plan_view.GetReferenceLine().ToArray()
        p = Point2d(line[-1][0], line[-1][1])
        newXodrRoad.plan_view.AddSpiral(p, 1.57079632679, 50.0, 0.0, 0.3, 0.4)
        line = newXodrRoad.plan_view.GetReferenceLine()
        lane_section = XodrLaneSection(0)
        lane = XodrLane()
        lane.line = line
        lane_section.AddLane(lane)
        newXodrRoad.AddLaneSection(lane_section)
        road_map.AddRoad(newXodrRoad)

        r = Roadgraph()
        map_interface = MapInterface()
        map_interface.SetOpenDriveMap(road_map)
        map_interface.SetRoadgraph(r)
        world = World(params)
        world.AddAgent(agent)
Exemplo n.º 30
0
def GoalDefinitionFromTrack(track, end, xy_offset):
    goal_size = 12.0
    states = list(dict_utils.get_item_iterator(track.motion_states))
    # Goal position is spatial position of last state
    motion_state = states[-1][1]
    bark_state = BarkStateFromMotionState(motion_state, xy_offset=xy_offset)
    goal_polygon = Polygon2d(np.array([0.5 * goal_size, 0.5 * goal_size, 0.0]),
                             [
                                 Point2d(0.0, 0.0),
                                 Point2d(goal_size, 0.0),
                                 Point2d(goal_size, goal_size),
                                 Point2d(0.0, goal_size),
                                 Point2d(0.0, 0.0)
                             ])
    goal_point = Point2d(
        bark_state[0, int(StateDefinition.X_POSITION)] - 0.5 * goal_size,
        bark_state[0, int(StateDefinition.Y_POSITION)] - 0.5 * goal_size)
    goal_polygon = goal_polygon.Translate(goal_point)
    goal_definition = GoalDefinitionPolygon(goal_polygon)
    return goal_definition