コード例 #1
0
ファイル: intersection_env.py プロジェクト: mxu34/mbrl-gpmm
    def _make_road(self):
        """
            Make an 4-way intersection.

            The horizontal road has the right of way. More precisely, the levels of priority are:
                - 3 for horizontal straight lanes and right-turns
                - 1 for vertical straight lanes and right-turns
                - 2 for horizontal left-turns
                - 0 for vertical left-turns
            The code for nodes in the road network is:
            (o:outer | i:inner + [r:right, l:left]) + (0:south | 1:west | 2:north | 3:east)
        :return: the intersection road
        """
        lane_width = AbstractLane.DEFAULT_WIDTH
        right_turn_radius = lane_width + 5  # [m}
        left_turn_radius = right_turn_radius + lane_width  # [m}
        outer_distance = right_turn_radius + lane_width / 2
        access_length = 50 + 50  # [m]

        net = RoadNetwork()
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED
        for corner in range(4):
            angle = np.radians(90 * corner)
            is_horizontal = corner % 2
            priority = 3 if is_horizontal else 1
            rotation = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
            # Incoming
            start = rotation @ np.array([lane_width / 2, access_length + outer_distance])
            end = rotation @ np.array([lane_width / 2, outer_distance])
            net.add_lane("o" + str(corner), "ir" + str(corner),
                         StraightLane(start, end, line_types=[s, c], priority=priority, speed_limit=10))
            # Right turn
            r_center = rotation @ (np.array([outer_distance, outer_distance]))
            net.add_lane("ir" + str(corner), "il" + str((corner - 1) % 4),
                         CircularLane(r_center, right_turn_radius, angle + np.radians(180), angle + np.radians(270),
                                      line_types=[n, c], priority=priority, speed_limit=10))
            # Left turn
            l_center = rotation @ (np.array([-left_turn_radius + lane_width / 2, left_turn_radius - lane_width / 2]))
            net.add_lane("ir" + str(corner), "il" + str((corner + 1) % 4),
                         CircularLane(l_center, left_turn_radius, angle + np.radians(0), angle + np.radians(-90),
                                      clockwise=False, line_types=[n, n], priority=priority - 1, speed_limit=10))
            # Straight
            start = rotation @ np.array([lane_width / 2, outer_distance])
            end = rotation @ np.array([lane_width / 2, -outer_distance])
            net.add_lane("ir" + str(corner), "il" + str((corner + 2) % 4),
                         StraightLane(start, end, line_types=[s, n], priority=priority, speed_limit=10))
            # Exit
            start = rotation @ np.flip([lane_width / 2, access_length + outer_distance], axis=0)
            end = rotation @ np.flip([lane_width / 2, outer_distance], axis=0)
            net.add_lane("il" + str((corner - 1) % 4), "o" + str((corner - 1) % 4),
                         StraightLane(end, start, line_types=[n, c], priority=priority, speed_limit=10))

        # TODO: double check here change from regulated to general road

        road = RegulatedRoad(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        self.road = road
    def _make_road(self) -> None:
        """
        Make an 4-way intersection.

        The horizontal road has the right of way. More precisely, the levels of priority are:
            - 3 for horizontal straight lanes and right-turns
            - 1 for vertical straight lanes and right-turns
            - 2 for horizontal left-turns
            - 0 for vertical left-turns

        The code for nodes in the road network is:
        (o:outer | i:inner + [r:right, l:left]) + (0:south | 1:west | 2:north | 3:east)

        :return: the intersection road
        """
        lane_width = AbstractLane.DEFAULT_WIDTH
        right_turn_radius = lane_width  # + 5   # [m}
        left_turn_radius = right_turn_radius + lane_width  # [m}
        outer_distance = right_turn_radius + lane_width / 2  # 7.5
        access_length = 50 + 50  # [m]

        net = RoadNetwork()
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED

        if self.config["scenario"] in [9, 10]:
            # 2nd lane for this scenario (and 3rd invisible lane)
            # incoming from south
            rotation = np.array([[1, 0], [0, 1]])
            start = rotation @ np.array(
                [3 * lane_width / 2, access_length + outer_distance])
            end = rotation @ np.array(
                [3 * lane_width / 2, outer_distance + lane_width])
            net.add_lane(
                "o0", "ir0",
                StraightLane(start,
                             end,
                             line_types=[s, c],
                             priority=0,
                             speed_limit=10,
                             forbidden=True))
            if self.config["scenario"] == 10:
                start = rotation @ np.array(
                    [5 * lane_width / 2, access_length + outer_distance])
                end = rotation @ np.array(
                    [5 * lane_width / 2, outer_distance + lane_width])
                net.add_lane(
                    "o0", "ir0",
                    StraightLane(start,
                                 end,
                                 line_types=[n, n],
                                 priority=0,
                                 speed_limit=10,
                                 forbidden=True))
            # intersection
            r_center = rotation @ (np.array(
                [outer_distance + lane_width, outer_distance + lane_width]))
            net.add_lane(
                "ir0", "il3",
                CircularLane(r_center,
                             right_turn_radius,
                             0 + np.radians(180),
                             0 + np.radians(270),
                             line_types=[n, c],
                             priority=0,
                             speed_limit=10))
            # outgoing east
            start = rotation @ np.flip(
                [3 * lane_width / 2, access_length + outer_distance], axis=0)
            end = rotation @ np.flip(
                [3 * lane_width / 2, outer_distance + lane_width], axis=0)
            net.add_lane(
                "il3", "o3",
                StraightLane(end,
                             start,
                             line_types=[s, c],
                             priority=0,
                             speed_limit=10))

        for corner in range(4):
            angle = np.radians(90 * corner)
            is_horizontal = corner % 2
            priority = 3 if is_horizontal else 1
            rotation = np.array([[np.cos(angle), -np.sin(angle)],
                                 [np.sin(angle), np.cos(angle)]])
            # Incoming
            lt = [
                c, s
            ] if self.config["scenario"] in [9, 10] and corner == 0 else [
                s, c
            ]
            start = rotation @ np.array(
                [lane_width / 2, access_length + outer_distance])
            end = rotation @ np.array([lane_width / 2, outer_distance])
            net.add_lane(
                "o" + str(corner), "ir" + str(corner),
                StraightLane(start,
                             end,
                             line_types=lt,
                             priority=priority,
                             speed_limit=10,
                             forbidden=True))

            # replace s by n to hide ped lanes
            ped_display = s
            if self.config["scenario"] in [9]:
                # Pedestrian lanes (other direction and further from intersection)
                start = rotation @ np.array(
                    [lane_width * 2.5, outer_distance * 2])
                end = rotation @ np.array(
                    [lane_width * 2.5, -outer_distance * 2])
                net.add_lane(
                    "p" + str(corner), "p" + str(corner) + "_end",
                    StraightLane(end,
                                 start,
                                 line_types=[ped_display, ped_display],
                                 priority=1234,
                                 width=AbstractLane.DEFAULT_WIDTH / 2,
                                 speed_limit=2))
            elif self.config["scenario"] in [10]:
                # Pedestrian lanes (other direction and further from intersection)
                start = rotation @ np.array(
                    [lane_width * 8, outer_distance * 2.5])
                end = rotation @ np.array(
                    [lane_width * 8, -outer_distance * 2.5])
                net.add_lane(
                    "p" + str(corner), "p" + str(corner) + "_end",
                    StraightLane(end,
                                 start,
                                 line_types=[ped_display, ped_display],
                                 priority=1234,
                                 width=AbstractLane.DEFAULT_WIDTH / 2,
                                 speed_limit=2))
            else:
                # Pedestrian lanes
                start = rotation @ np.array(
                    [lane_width * 1.5, outer_distance * 2])
                end = rotation @ np.array(
                    [lane_width * 1.5, -outer_distance * 2])
                net.add_lane(
                    "p" + str(corner), "p" + str(corner) + "_end",
                    StraightLane(start,
                                 end,
                                 line_types=[ped_display, ped_display],
                                 priority=1234,
                                 width=AbstractLane.DEFAULT_WIDTH / 2,
                                 speed_limit=2))

            # Right turn
            lt = [
                c, s
            ] if self.config["scenario"] in [9, 10] and corner == 0 else [
                n, c
            ]
            r_center = rotation @ (np.array([outer_distance, outer_distance]))
            net.add_lane(
                "ir" + str(corner), "il" + str((corner - 1) % 4),
                CircularLane(r_center,
                             right_turn_radius,
                             angle + np.radians(180),
                             angle + np.radians(270),
                             line_types=[n, c],
                             priority=priority,
                             speed_limit=10))
            # Left turn
            l_center = rotation @ (np.array([
                -left_turn_radius + lane_width / 2,
                left_turn_radius - lane_width / 2
            ]))
            net.add_lane(
                "ir" + str(corner), "il" + str((corner + 1) % 4),
                CircularLane(l_center,
                             left_turn_radius,
                             angle + np.radians(0),
                             angle + np.radians(-90),
                             clockwise=False,
                             line_types=[n, n],
                             priority=priority - 1,
                             speed_limit=10))
            # Straight
            start = rotation @ np.array([lane_width / 2, outer_distance])
            end = rotation @ np.array([lane_width / 2, -outer_distance])
            net.add_lane(
                "ir" + str(corner), "il" + str((corner + 2) % 4),
                StraightLane(start,
                             end,
                             line_types=[s, n],
                             priority=priority,
                             speed_limit=10))
            # Exit
            lt = [
                c, s
            ] if self.config["scenario"] in [9, 10] and corner == 0 else [
                s, c
            ]
            start = rotation @ np.flip(
                [lane_width / 2, access_length + outer_distance], axis=0)
            end = rotation @ np.flip([lane_width / 2, outer_distance], axis=0)
            net.add_lane(
                "il" + str((corner - 1) % 4), "o" + str((corner - 1) % 4),
                StraightLane(end,
                             start,
                             line_types=lt,
                             priority=priority,
                             speed_limit=10))

        road = RegulatedRoad(network=net,
                             np_random=self.np_random,
                             record_history=self.config["show_trajectories"])
        self.road = road