def test_network():
    # Diamond
    net = RoadNetwork()
    net.add_lane(0, 1, StraightLane([0, 0], [10, 0]))
    net.add_lane(1, 2, StraightLane([10, 0], [5, 5]))
    net.add_lane(2, 0, StraightLane([5, 5], [0, 0]))
    net.add_lane(1, 3, StraightLane([10, 0], [5, -5]))
    net.add_lane(3, 0, StraightLane([5, -5], [0, 0]))
    print(net.graph)

    # Road
    road = Road(network=net)
    v = ControlledVehicle(road, [5, 0], heading=0, target_velocity=2)
    road.vehicles.append(v)
    assert v.lane_index == (0, 1, 0)

    # Lane changes
    dt = 1 / 15
    lane_index = v.target_lane_index
    lane_changes = 0
    for _ in range(int(20 / dt)):
        road.act()
        road.step(dt)
        if lane_index != v.target_lane_index:
            lane_index = v.target_lane_index
            lane_changes += 1
    assert lane_changes >= 3
Beispiel #2
0
    def _make_road(self) -> None:
        net = RoadNetwork()

        radius = self.config["radius"]  # [m]
        # radius = np.random.choice([50, 500])  # [m]
        # radius = np.random.choice([50, 200, 500, 1000])  # [m]
        center = [0, StraightLane.DEFAULT_WIDTH + radius]  # [m]
        alpha = 0  # [deg]
        radii = [
            radius, radius + StraightLane.DEFAULT_WIDTH,
            radius + 2 * StraightLane.DEFAULT_WIDTH
        ]
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED
        line = [[c, s], [n, s], [n, c]]

        for lane in [0, 1, 2]:
            net.add_lane(
                "a",
                "b",
                # CircularLane(center, radii[lane], np.deg2rad(90 - alpha), np.deg2rad(-90+alpha),
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(90 - alpha),
                             np.deg2rad(-360 + alpha),
                             clockwise=False,
                             line_types=line[lane]))

        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
        self.road.record_history = True
Beispiel #3
0
 def __init__(self, circuit_config, np_random):
     self._circuit_config = circuit_config
     self._road_network = RoadNetwork()
     self._road = Road(network=self._road_network,
                       np_random=np_random,
                       record_history=True)
     self._route = list()
     self._circuit_length = 0
     self._create_circuit()
Beispiel #4
0
 def _make_road(self):
     """
         Make a road composed of a straight highway and a merging lane.
     :return: the road
     """
     net = RoadNetwork()
     c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE
     y = [0, StraightLane.DEFAULT_WIDTH, 2 * StraightLane.DEFAULT_WIDTH]
     line_type = [[c, s], [n, s], [n, c]]
     for i in range(3):
         net.add_lane("a", "b", StraightLane([0, y[i]], [self.HIGHWAY_LENGTH, y[i]], line_types=line_type[i]))
     road = Road(network=net, np_random=self.np_random)
     self.road = road
Beispiel #5
0
    def _make_road(self, length=800):
        """
            Make a road composed of a two-way road.
        :return: the road
        """
        net = RoadNetwork()

        # Lanes
        net.add_lane(
            "a", "b",
            StraightLane(
                [0, 0], [length, 0],
                line_types=[LineType.CONTINUOUS_LINE, LineType.STRIPED]))
        net.add_lane(
            "a", "b",
            StraightLane([0, StraightLane.DEFAULT_WIDTH],
                         [length, StraightLane.DEFAULT_WIDTH],
                         line_types=[LineType.NONE, LineType.CONTINUOUS_LINE]))
        net.add_lane(
            "b", "a",
            StraightLane([length, 0], [0, 0],
                         line_types=[LineType.NONE, LineType.NONE]))

        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
Beispiel #6
0
 def _make_road(self):
     net = RoadNetwork()
     lane = SineLane([0, 0], [500, 0],
                     amplitude=5,
                     pulsation=2 * np.pi / 100,
                     phase=0,
                     width=10,
                     line_types=[LineType.STRIPED, LineType.STRIPED])
     net.add_lane("a", "b", lane)
     other_lane = StraightLane(
         [50, 50], [115, 15],
         line_types=[LineType.STRIPED, LineType.STRIPED],
         width=10)
     net.add_lane("c", "d", other_lane)
     self.lanes = [other_lane, lane]
     self.lane = self.lanes.pop(0)
     net.add_lane(
         "d", "a",
         StraightLane([115, 15],
                      [115 + 20, 15 + 20 * (15 - 50) / (115 - 50)],
                      line_types=[LineType.NONE, LineType.STRIPED],
                      width=10))
     road = Road(network=net,
                 np_random=self.np_random,
                 record_history=self.config["show_trajectories"])
     self.road = road
    def _create_road(self, spots=15):
        """
            Create a road composed of straight adjacent lanes.
        """
        net = RoadNetwork()

        width = 4.0
        lt = (LineType.CONTINUOUS, LineType.CONTINUOUS)
        x_offset = 0
        y_offset = 12
        length = 8
        # Parking spots
        for k in range(spots):
            x = (k - spots // 2) * (width + x_offset) - width / 2
            net.add_lane("a", "b", StraightLane([x, y_offset], [x, y_offset + length],
                                                width=width, line_types=lt, speed_limit=5))
            net.add_lane("b", "c", StraightLane([x, -y_offset], [x, -y_offset - length],
                                                width=width, line_types=lt, speed_limit=5))

        self.spots = spots
        self.vehicle_starting = [x, y_offset + (length / 2)]
        self.num_middle_lanes = 0
        self.x_range = (int(spots / 2) + 1) * width

        # Generate the middle lane for the busy parking lot
        for y in np.arange(-y_offset + width, y_offset, width):
            net.add_lane("d", "e", StraightLane([-self.x_range, y], [self.x_range, y],
                                                width=width,
                                                line_types=(LineType.STRIPED, LineType.STRIPED),
                                                speed_limit=5))
            self.num_middle_lanes += 1

        self.road = Road(network=net,
                         np_random=self.np_random,
                         record_history=self.config["show_trajectories"])
Beispiel #8
0
 def _create_road(self):
     """
         Create a road composed of straight adjacent lanes.
     """
     self.road = Road(network=RoadNetwork.straight_road_network(
         self.config["lanes_count"]),
                      np_random=self.np_random)
def test_partial():
    road = Road(RoadNetwork.straight_road_network())
    v = IntervalVehicle(road, position=[0, 0], velocity=20, heading=0)
    for _ in range(2 * FPS):
        v.step(dt=1/FPS, mode="partial")
        assert v.interval.position[0, 0] <= v.position[0] <= v.interval.position[1, 0]
        assert v.interval.position[0, 1] <= v.position[1] <= v.interval.position[1, 1]
        assert v.interval.heading[0] <= v.heading <= v.interval.heading[1]
Beispiel #10
0
 def _create_road(self):
     """
         Create a road composed of straight adjacent lanes.
     """
     self.road = Road(network=RoadNetwork.straight_road_network(
         self.config["lanes_count"]),
                      np_random=self.np_random,
                      record_history=self.config["show_trajectories"])
def test_front():
    r = Road(RoadNetwork.straight_road_network(1))
    v1 = Vehicle(road=r, position=[0, 0], velocity=20)
    v2 = Vehicle(road=r, position=[10, 0], velocity=10)
    r.vehicles.extend([v1, v2])

    assert v1.lane_distance_to(v2) == pytest.approx(10)
    assert v2.lane_distance_to(v1) == pytest.approx(-10)
Beispiel #12
0
def test_speed_control():
    road = Road(RoadNetwork.straight_road_network(1))
    v = ControlledVehicle(road=road, position=road.network.get_lane(("0", "1", 0)).position(0, 0), speed=20, heading=0)
    v.act('FASTER')
    for _ in range(int(3 * v.TAU_ACC * FPS)):
        v.act()
        v.step(dt=1/FPS)
    assert v.speed == pytest.approx(20 + v.DELTA_SPEED, abs=0.5)
    assert v.position[1] == pytest.approx(0)
    assert v.lane_index[2] == 0
Beispiel #13
0
def test_lane_change():
    road = Road(RoadNetwork.straight_road_network(2))
    v = ControlledVehicle(road=road, position=road.network.get_lane(("0", "1", 0)).position(0, 0), speed=20, heading=0)
    v.act('LANE_RIGHT')
    for _ in range(3 * FPS):
        v.act()
        v.step(dt=1/FPS)
    assert v.speed == pytest.approx(20)
    assert v.position[1] == pytest.approx(StraightLane.DEFAULT_WIDTH, abs=StraightLane.DEFAULT_WIDTH/4)
    assert v.lane_index[2] == 1
Beispiel #14
0
    def _create_road(self) -> None:

        # Circle lanes: (s)outh/(e)ast/(n)orth/(w)est (e)ntry/e(x)it.
        center = [0, 0]  # [m]

        radius = self.config["derby_radius"]  # [m]
        alpha = 90  # [deg]

        net = RoadNetwork()
        radii = [radius]
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.CONTINUOUS
        line = [[c, s], [n, c]]
        for lane in [0]:

            net.add_lane(
                "se", "ee",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(-90 - alpha),
                             np.deg2rad(alpha + 5),
                             clockwise=True,
                             line_types=line[lane]))
            net.add_lane(
                "ee", "se",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(alpha - 5),
                             np.deg2rad(92 + alpha),
                             clockwise=True,
                             line_types=line[lane]))

        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
Beispiel #15
0
    def _create_road(self, spots=15):
        """
            Create a road composed of straight adjacent lanes.
        """
        net = RoadNetwork()
        width = 4.0
        lt = (LineType.CONTINUOUS, LineType.CONTINUOUS)
        x_offset = 0
        y_offset = 10
        length = 8
        for k in range(spots):
            x = (k - spots // 2) * (width + x_offset) - width / 2
            net.add_lane(
                "a", "b",
                StraightLane([x, y_offset], [x, y_offset + length],
                             width=width,
                             line_types=lt))
            net.add_lane(
                "b", "c",
                StraightLane([x, -y_offset], [x, -y_offset - length],
                             width=width,
                             line_types=lt))

        self.road = Road(network=net,
                         np_random=self.np_random,
                         record_history=self.config["show_trajectories"])
Beispiel #16
0
    def _create_road(self,
                     road_length=1000,
                     exit_position=400,
                     exit_length=100) -> None:
        net = RoadNetwork.straight_road_network(self.config["lanes_count"],
                                                start=0,
                                                length=exit_position,
                                                nodes_str=("0", "1"))
        net = RoadNetwork.straight_road_network(self.config["lanes_count"] + 1,
                                                start=exit_position,
                                                length=exit_length,
                                                nodes_str=("1", "2"),
                                                net=net)
        net = RoadNetwork.straight_road_network(
            self.config["lanes_count"],
            start=exit_position + exit_length,
            length=road_length - exit_position - exit_length,
            nodes_str=("2", "3"),
            net=net)
        for _from in net.graph:
            for _to in net.graph[_from]:
                for _id in range(len(net.graph[_from][_to])):
                    net.get_lane(
                        (_from, _to, _id)).speed_limit = 26 - 3.4 * _id
        exit_position = np.array([
            exit_position + exit_length,
            self.config["lanes_count"] * CircularLane.DEFAULT_WIDTH
        ])
        radius = 150
        exit_center = exit_position + np.array([0, radius])
        lane = CircularLane(center=exit_center,
                            radius=radius,
                            start_phase=3 * np.pi / 2,
                            end_phase=2 * np.pi,
                            forbidden=True)
        net.add_lane("2", "exit", lane)

        self.road = Road(network=net,
                         np_random=self.np_random,
                         record_history=self.config["show_trajectories"])
Beispiel #17
0
def test_velocity_control():
    road = Road(RoadNetwork.straight_road_network(1))
    v = ControlledVehicle(road=road,
                          position=road.network.get_lane(
                              (0, 1, 0)).position(0, 0),
                          velocity=20,
                          heading=0)
    v.act('FASTER')
    for _ in range(int(3 * v.TAU_A * FPS)):
        v.act()
        v.step(dt=1 / FPS)
    assert v.velocity == pytest.approx(20 + v.DELTA_VELOCITY, abs=0.5)
    assert v.position[1] == pytest.approx(0)
    assert v.lane_index[2] == 0
def test_stop_before_obstacle(vehicle_type):
    road = Road(RoadNetwork.straight_road_network(lanes=1))
    vehicle = vehicle_type(road=road, position=[0, 0], speed=20, heading=0)
    obstacle = Obstacle(road=road, position=[80, 0])
    road.vehicles.append(vehicle)
    road.objects.append(obstacle)
    for _ in range(10 * FPS):
        road.act()
        road.step(dt=1/FPS)
    assert not vehicle.crashed
    assert vehicle.position[0] == pytest.approx(obstacle.position[0] - vehicle_type.DISTANCE_WANTED, abs=1)
    assert vehicle.position[1] == pytest.approx(0)
    assert vehicle.speed == pytest.approx(0, abs=1)
    assert vehicle.heading == pytest.approx(0)
Beispiel #19
0
def test_collision():
    # Collision between two vehicles
    r = Road(RoadNetwork.straight_road_network(1))
    v1 = Vehicle(road=r, position=[0, 0], speed=10)
    v2 = Vehicle(road=r, position=[4, 0], speed=20)
    v1.check_collision(v2)

    assert v1.crashed and v2.crashed
    # Collision between a vehicle and an obstacle
    v3 = Vehicle(road=r, position=[20, 0], speed=10)
    o = Obstacle(road=r, position=[23, 0])
    v3.check_collision(o)

    assert v3.crashed and o.hit
    # Collision between a vehicle and a landmark
    v4 = Vehicle(road=r, position=[40, 0], speed=10)
    l = Landmark(road=r, position=[43, 0])
    v4.check_collision(l)

    assert v4.crashed is False
    assert l.hit
Beispiel #20
0
    def _make_road(self) -> None:
        """
        Make a road composed of a straight highway and a merging lane.

        :return: the road

        net = RoadNetwork()

        # Highway lanes
        ends = [150, 80, 20, 80]  # Before, converging, merge, after
        c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE
        y = [0, StraightLane.DEFAULT_WIDTH]
        line_type = [[c, s], [n, c]]
        line_type_merge = [[c, s], [n, s]]
        amplitude = 3.25
        ljk = StraightLane([0, 6.5 + 4 + 4], [ends[0], 6.5 + 4 + 4], line_types=[c, c], forbidden=True)
        lke = SineLane(ljk.position(ends[0], -amplitude), ljk.position(sum(ends[:2]), -amplitude),
                       amplitude, 2 * np.pi / (2*ends[1]), np.pi / 2, line_types=[c, c], forbidden=True)
        net.add_lane("j", "k", ljk)
        net.add_lane("k", "b", lke)
        for i in range(2):
            net.add_lane("a", "b", StraightLane([0, y[i]], [sum(ends[:2]), y[i]], line_types=line_type[i]))
            net.add_lane("b", "c", StraightLane([sum(ends[:2]), y[i]], [sum(ends[:3]), y[i]], line_types=line_type_merge[i]))
            net.add_lane("c", "d", StraightLane([sum(ends[:3]), y[i]], [sum(ends), y[i]], line_types=line_type[i]))

        #net.add_lane("e", "c", lbc)
        road = Road(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        #road.objects.append(Obstacle(road, lbc.position(ends[2], 0)))
        self.road = road



        Make a road composed of a straight highway and a merging lane.

        :return: the road
 """
        net = RoadNetwork()

        # Highway lanes
        ends = [400, 217, 182, 0]  # Before, converging, merge, after
        c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE
        y = [10.85,14.85]
        #line_type = [[c, s], [n, c]]
        line_type_merge=[[s,s],[n,c]]
        line_type_highway=[[c,s],[n,c]]
        #line_type_merge = [[c, s], [n, s]]
        amplitude = 2
        ljk = StraightLane([0,6.85],[183,6.85],line_types=[c,c], forbidden=True)
        lbc = SineLane(ljk.position(182,amplitude), ljk.position(218, amplitude),
                       -amplitude, 2 * np.pi / 72, np.pi / 2, line_types=[c, n], forbidden=True)
        net.add_lane("b", "c", lbc)
        net.add_lane("a", "b", StraightLane([0, 6.85], [183, 6.85], line_types=[c,n]))
        for i in range(2):
            net.add_lane("a", "c", StraightLane([0, y[i]], [218, y[i]], line_types=line_type_merge[i]))
            net.add_lane("c", "d", StraightLane([218, y[i]], [400, y[i]], line_types=line_type_highway[i]))

        #net.add_lane("e", "c", lbc)
        #net.add_lane("a","b",StraightLane([400,4.85],[218,4.85],line_types=line_type_merge,forbidden=True))
        #net.add_lane("a","c",StraightLane([400,8.85],[218,8.85],line_types=line_type_highway))
        #net.add_lane("b","c",StraightLane([218,4.85],[182,4.85],line_type=line_type_merge))
        #net.add_lane("b","c",StraightLane([218,8.85],[182,8.85],line_type=line_type_highway))
        #net.add_lane("c","d",StraightLane([182,8.85],[0,8.85],line_types=line_type_merge))
        road = Road(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        #road.objects.append(Obstacle(road, lbc.position(ends[2], 0)))
        self.road = road
Beispiel #21
0
class Circuit(object):
    def __init__(self, circuit_config, np_random):
        self._circuit_config = circuit_config
        self._road_network = RoadNetwork()
        self._road = Road(network=self._road_network,
                          np_random=np_random,
                          record_history=True)
        self._route = list()
        self._circuit_length = 0
        self._create_circuit()

    @property
    def speed_limit(self):
        return self._circuit_config['speed_limit']

    @property
    def circuit_width(self):
        return self._circuit_config['circuit_width']

    @property
    def circuit_length(self):
        return self._circuit_length

    @property
    def road(self):
        return self._road

    @property
    def start_lane_index(self):
        return self._route and self._route[0]

    def get_route(self):
        return list(self._route)

    def _create_circuit(self):
        circular_radius = 50
        line_types = [LineType.STRIPED, LineType.CONTINUOUS]
        circular_lane_1 = CircularLane(center=(0.0, 0.0),
                                       radius=circular_radius,
                                       start_phase=0,
                                       end_phase=math.pi * 0.5,
                                       speed_limit=self.speed_limit,
                                       width=self.circuit_width,
                                       line_types=line_types)
        circular_lane_2 = CircularLane(
            center=(0.0, 0.0),
            radius=circular_radius,
            start_phase=math.pi * 0.5,
            end_phase=math.pi,
            speed_limit=self.speed_limit,
            width=self.circuit_width,
            line_types=line_types,
        )
        circular_lane_start = circular_lane_1.position(0, 0)
        circular_lane_end = circular_lane_2.position(circular_lane_2.length, 0)
        sine_lane = SineLane(
            circular_lane_end,
            circular_lane_start,
            amplitude=10,
            pulsation=2 * math.pi / (circular_radius * 2),
            phase=0,
            speed_limit=self.speed_limit,
            width=self.circuit_width,
            line_types=line_types,
        )

        self._add_lane('start', 'int1', circular_lane_1)
        self._add_lane('int1', 'int2', circular_lane_2)
        self._add_lane('int2', 'end', sine_lane)

    def _add_lane(self, src, dest, lane):
        self._road_network.add_lane(src, dest, lane)
        self._circuit_length += lane.length
        self._route.append((src, dest, 0))

    def get_circuit_pos(self, pos):
        current_lane_index = self._road_network.get_closest_lane_index(pos)
        assert current_lane_index is not None

        abs_pos = 0.0
        for lane_index in self._route:
            lane = self._road_network.get_lane(lane_index)
            if lane_index != current_lane_index:
                abs_pos += lane.length
            else:
                abs_pos += lane.local_coordinates(pos)[0]
                break

        # The position can go out of bounds if we aren't exactly within a lane, so we need to normalize
        while abs_pos < 0:
            abs_pos += self.circuit_length
        while abs_pos > self.circuit_length:
            abs_pos -= self.circuit_length

        return abs_pos

    def get_lap_progress(self, prev_pos, cur_pos):
        prev_circuit_pos = self.get_circuit_pos(prev_pos)
        cur_circuit_pos = self.get_circuit_pos(cur_pos)

        # Consider both possibilities of moving
        progress1 = cur_circuit_pos - prev_circuit_pos
        if cur_circuit_pos > prev_circuit_pos:
            progress2 = cur_circuit_pos - prev_circuit_pos - self.circuit_length
        else:
            progress2 = cur_circuit_pos - prev_circuit_pos + self.circuit_length

        # Select smallest move
        return progress1 if abs(progress1) < abs(progress2) else progress2
Beispiel #22
0
    def _make_road(self) -> None:
        # Circle lanes: (s)outh/(e)ast/(n)orth/(w)est (e)ntry/e(x)it.
        center = [0, 0]  # [m]
        radius = 20  # [m]
        alpha = 24  # [deg]

        net = RoadNetwork()
        radii = [radius, radius + 4]
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED
        line = [[c, s], [n, c]]
        for lane in [0, 1]:
            net.add_lane(
                "se", "ex",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(90 - alpha),
                             np.deg2rad(alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "ex", "ee",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(alpha),
                             np.deg2rad(-alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "ee", "nx",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(-alpha),
                             np.deg2rad(-90 + alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "nx", "ne",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(-90 + alpha),
                             np.deg2rad(-90 - alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "ne", "wx",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(-90 - alpha),
                             np.deg2rad(-180 + alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "wx", "we",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(-180 + alpha),
                             np.deg2rad(-180 - alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "we", "sx",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(180 - alpha),
                             np.deg2rad(90 + alpha),
                             clockwise=False,
                             line_types=line[lane]))
            net.add_lane(
                "sx", "se",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(90 + alpha),
                             np.deg2rad(90 - alpha),
                             clockwise=False,
                             line_types=line[lane]))

        # Access lanes: (r)oad/(s)ine
        access = 170  # [m]
        dev = 85  # [m]
        a = 5  # [m]
        delta_st = 0.2 * dev  # [m]

        delta_en = dev - delta_st
        w = 2 * np.pi / dev
        net.add_lane(
            "ser", "ses",
            StraightLane([2, access], [2, dev / 2], line_types=(s, c)))
        net.add_lane(
            "ses", "se",
            SineLane([2 + a, dev / 2], [2 + a, dev / 2 - delta_st],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=(c, c)))
        net.add_lane(
            "sx", "sxs",
            SineLane([-2 - a, -dev / 2 + delta_en], [-2 - a, dev / 2],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=(c, c)))
        net.add_lane(
            "sxs", "sxr",
            StraightLane([-2, dev / 2], [-2, access], line_types=(n, c)))

        net.add_lane(
            "eer", "ees",
            StraightLane([access, -2], [dev / 2, -2], line_types=(s, c)))
        net.add_lane(
            "ees", "ee",
            SineLane([dev / 2, -2 - a], [dev / 2 - delta_st, -2 - a],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=(c, c)))
        net.add_lane(
            "ex", "exs",
            SineLane([-dev / 2 + delta_en, 2 + a], [dev / 2, 2 + a],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=(c, c)))
        net.add_lane(
            "exs", "exr",
            StraightLane([dev / 2, 2], [access, 2], line_types=(n, c)))

        net.add_lane(
            "ner", "nes",
            StraightLane([-2, -access], [-2, -dev / 2], line_types=(s, c)))
        net.add_lane(
            "nes", "ne",
            SineLane([-2 - a, -dev / 2], [-2 - a, -dev / 2 + delta_st],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=(c, c)))
        net.add_lane(
            "nx", "nxs",
            SineLane([2 + a, dev / 2 - delta_en], [2 + a, -dev / 2],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=(c, c)))
        net.add_lane(
            "nxs", "nxr",
            StraightLane([2, -dev / 2], [2, -access], line_types=(n, c)))

        net.add_lane(
            "wer", "wes",
            StraightLane([-access, 2], [-dev / 2, 2], line_types=(s, c)))
        net.add_lane(
            "wes", "we",
            SineLane([-dev / 2, 2 + a], [-dev / 2 + delta_st, 2 + a],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=(c, c)))
        net.add_lane(
            "wx", "wxs",
            SineLane([dev / 2 - delta_en, -2 - a], [-dev / 2, -2 - a],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=(c, c)))
        net.add_lane(
            "wxs", "wxr",
            StraightLane([-dev / 2, -2], [-access, -2], line_types=(n, c)))

        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
Beispiel #23
0
    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   # [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))

        road = RegulatedRoad(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        self.road = road
    def _make_road(self):
        # Circle lanes: (s)outh/(e)ast/(n)orth/(w)est (e)ntry/e(x)it.
        center = [0, 0]  # [m]
        radius = 30  # [m]
        alpha = 20  # [deg]

        net = RoadNetwork()
        radii = [radius, radius + 4]
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED
        line = [[c, s], [n, c]]
        for lane in [0, 1]:
            net.add_lane(
                "se", "ex",
                CircularLane(center,
                             radii[lane],
                             rad(90 - alpha),
                             rad(alpha),
                             line_types=line[lane]))
            net.add_lane(
                "ex", "ee",
                CircularLane(center,
                             radii[lane],
                             rad(alpha),
                             rad(-alpha),
                             line_types=line[lane]))
            net.add_lane(
                "ee", "nx",
                CircularLane(center,
                             radii[lane],
                             rad(-alpha),
                             rad(-90 + alpha),
                             line_types=line[lane]))
            net.add_lane(
                "nx", "ne",
                CircularLane(center,
                             radii[lane],
                             rad(-90 + alpha),
                             rad(-90 - alpha),
                             line_types=line[lane]))
            net.add_lane(
                "ne", "wx",
                CircularLane(center,
                             radii[lane],
                             rad(-90 - alpha),
                             rad(-180 + alpha),
                             line_types=line[lane]))
            net.add_lane(
                "wx", "we",
                CircularLane(center,
                             radii[lane],
                             rad(-180 + alpha),
                             rad(-180 - alpha),
                             line_types=line[lane]))
            net.add_lane(
                "we", "sx",
                CircularLane(center,
                             radii[lane],
                             rad(180 - alpha),
                             rad(90 + alpha),
                             line_types=line[lane]))
            net.add_lane(
                "sx", "se",
                CircularLane(center,
                             radii[lane],
                             rad(90 + alpha),
                             rad(90 - alpha),
                             line_types=line[lane]))

        # Access lanes: (r)oad/(s)ine
        access = 200  # [m]
        dev = 120  # [m]
        a = 5  # [m]
        delta_st = 0.20 * dev  # [m]

        delta_en = dev - delta_st
        w = 2 * np.pi / dev
        net.add_lane(
            "ser", "ses",
            StraightLane([2, access], [2, dev / 2], line_types=[s, c]))
        net.add_lane(
            "ses", "se",
            SineLane([2 + a, dev / 2], [2 + a, dev / 2 - delta_st],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=[c, c]))
        net.add_lane(
            "sx", "sxs",
            SineLane([-2 - a, -dev / 2 + delta_en], [-2 - a, dev / 2],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=[c, c]))
        net.add_lane(
            "sxs", "sxr",
            StraightLane([-2, dev / 2], [-2, access], line_types=[n, c]))

        net.add_lane(
            "eer", "ees",
            StraightLane([access, -2], [dev / 2, -2], line_types=[s, c]))
        net.add_lane(
            "ees", "ee",
            SineLane([dev / 2, -2 - a], [dev / 2 - delta_st, -2 - a],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=[c, c]))
        net.add_lane(
            "ex", "exs",
            SineLane([-dev / 2 + delta_en, 2 + a], [dev / 2, 2 + a],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=[c, c]))
        net.add_lane(
            "exs", "exr",
            StraightLane([dev / 2, 2], [access, 2], line_types=[n, c]))

        net.add_lane(
            "ner", "nes",
            StraightLane([-2, -access], [-2, -dev / 2], line_types=[s, c]))
        net.add_lane(
            "nes", "ne",
            SineLane([-2 - a, -dev / 2], [-2 - a, -dev / 2 + delta_st],
                     a,
                     w,
                     -np.pi / 2,
                     line_types=[c, c]))
        net.add_lane(
            "nx", "nxs",
            SineLane([2 + a, dev / 2 - delta_en], [2 + a, -dev / 2],
                     a,
                     w,
                     -np.pi / 2 + w * delta_en,
                     line_types=[c, c]))
        net.add_lane(
            "nxs", "nxr",
            StraightLane([2, -dev / 2], [2, -access], line_types=[n, c]))

        road = Road(network=net, np_random=self.np_random)
        self.road = road
Beispiel #25
0
    def make_roads(self):
        net = RoadNetwork()
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED
        net.add_lane(
            "s1", "ex",
            StraightLane(np.array([0, 0]),
                         np.array([100, 0]),
                         line_types=[c, s]))
        net.add_lane(
            "ex", "em",
            StraightLane(np.array([100, 0]),
                         np.array([200, 0]),
                         line_types=[c, s]))
        net.add_lane(
            "em", "x1",
            StraightLane(np.array([200, 0]),
                         np.array([300, 0]),
                         line_types=[c, s]))
        # lm10 = StraightLane(np.array([0, 0]), 0, 4.0, [LineType.CONTINUOUS_LINE, LineType.STRIPED],bounds=[0,300])
        # l1 = LanesConcatenation([lm10])
        net.add_lane(
            "s1", "ex",
            StraightLane(np.array([0, 4]),
                         np.array([100, 4]),
                         line_types=[s, s]))
        net.add_lane(
            "ex", "em",
            StraightLane(np.array([100, 4]),
                         np.array([200, 4]),
                         line_types=[s, s]))
        net.add_lane(
            "em", "x1",
            StraightLane(np.array([200, 4]),
                         np.array([300, 4]),
                         line_types=[s, s]))
        # lm20 = StraightLane(l1.position(0,4), 0, 4.0, [LineType.STRIPED, LineType.STRIPED],bounds=[0,300])
        # l2 = LanesConcatenation([lm20])
        net.add_lane(
            "s1", "ex",
            StraightLane(np.array([0, 8]),
                         np.array([100, 8]),
                         line_types=[s, c]))
        # lm30 = StraightLane(l2.position(0,4), 0, 4.0, [LineType.STRIPED, LineType.CONTINUOUS_LINE],bounds=[0,100])
        net.add_lane(
            "ex", "em",
            StraightLane(np.array([100, 8]),
                         np.array([200, 8]),
                         line_types=[s, n]))
        net.add_lane(
            "em", "x1",
            StraightLane(np.array([200, 8]),
                         np.array([300, 8]),
                         line_types=[s, c]))
        # lm31 = StraightLane(lm30.position(0,0), 0, 4.0, [LineType.STRIPED, LineType.STRIPED],bounds=[0,300])
        # l3 = LanesConcatenation([lm30,lm31])
        amplitude = 4.5
        net.add_lane(
            "s2", "ee",
            StraightLane(np.array([0, 8 + 3 * amplitude]),
                         np.array([50, 8 + 3 * amplitude]),
                         line_types=[c, c],
                         forbidden=True))
        # lm40 = StraightLane(l3.position(0,2*amplitude+4), 0, 4.0, [LineType.CONTINUOUS_LINE, LineType.CONTINUOUS_LINE],bounds=[0,50])
        net.add_lane(
            "ee", "ex",
            SineLane(np.array([50, 8 + 2 * amplitude]),
                     np.array([100, 8 + 2 * amplitude]),
                     amplitude,
                     2 * np.pi / (2 * 50),
                     np.pi / 2,
                     line_types=[c, c],
                     forbidden=True))
        # lm41 = SineLane(lm40.position(50, -amplitude), 0, 4.0, amplitude, 2 * np.pi / (2*50), np.pi / 2,
        # [LineType.CONTINUOUS, LineType.CONTINUOUS], bounds=[0, 50], forbidden=True)
        net.add_lane(
            "ex", "over",
            StraightLane(np.array([100, 8 + amplitude]),
                         np.array([200, 8 + amplitude]),
                         line_types=[s, c],
                         forbidden=True))
        # net.add_lane("over", "x1",
        # SineLane(np.array([200, 8 +  amplitude/2]), np.array([220, 8 + amplitude/2]), amplitude/2,
        # 2 * np.pi / (2 * 50), np.pi / 2, line_types=[c, c], forbidden=True))
        # lm42 = StraightLane(lm41.position(50,0), 0, 4.0, [LineType.STRIPED, LineType.CONTINUOUS_LINE],bounds=[0,150],forbidden=True)
        # l4 = LanesConcatenation([lm40,lm41,lm42])
        # road = Road([l1,l2,l3,l4])
        # road = Road([ l3])

        # road = Road([lm0,lm2])
        road = Road(network=net, np_random=self.np_random)
        road.vehicles.append(Obstacle(road, [200, 8 + amplitude]))
        self.road = road
Beispiel #26
0
    def _make_road(self):
        """
            Make a road composed of a straight highway and a merging lane.
        :return: the road
        """
        net = RoadNetwork()

        # Highway lanes
        ends = [150, 80, 80, 150]  # Before, converging, merge, after
        c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE
        y = [0, StraightLane.DEFAULT_WIDTH]
        line_type = [[c, s], [n, c]]
        line_type_merge = [[c, s], [n, s]]
        for i in range(2):
            net.add_lane(
                "a", "b",
                StraightLane([0, y[i]], [sum(ends[:2]), y[i]],
                             line_types=line_type[i]))
            net.add_lane(
                "b", "c",
                StraightLane([sum(ends[:2]), y[i]], [sum(ends[:3]), y[i]],
                             line_types=line_type_merge[i]))
            net.add_lane(
                "c", "d",
                StraightLane([sum(ends[:3]), y[i]], [sum(ends), y[i]],
                             line_types=line_type[i]))

        # Merging lane
        amplitude = 3.25
        ljk = StraightLane([0, 6.5 + 4 + 4], [ends[0], 6.5 + 4 + 4],
                           line_types=[c, c],
                           forbidden=True)
        lkb = SineLane(ljk.position(ends[0], -amplitude),
                       ljk.position(sum(ends[:2]), -amplitude),
                       amplitude,
                       2 * np.pi / (2 * ends[1]),
                       np.pi / 2,
                       line_types=[c, c],
                       forbidden=True)
        lbc = StraightLane(lkb.position(ends[1], 0),
                           lkb.position(ends[1], 0) + [ends[2], 0],
                           line_types=[n, c],
                           forbidden=True)
        net.add_lane("j", "k", ljk)
        net.add_lane("k", "b", lkb)
        net.add_lane("b", "c", lbc)
        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        road.vehicles.append(Obstacle(road, lbc.position(ends[2], 0)))
        self.road = road
Beispiel #27
0
    def _make_road(self, length=128):
        """
        Making double lane road with counter-clockwise U-Turn.
        :return: the road
        """
        net = RoadNetwork()

        # Defining upper starting lanes after the U-Turn.
        # These Lanes are defined from x-coordinate 'length' to 0.
        net.add_lane(
            "c", "d",
            StraightLane([length, StraightLane.DEFAULT_WIDTH],
                         [0, StraightLane.DEFAULT_WIDTH],
                         line_types=(LineType.CONTINUOUS_LINE,
                                     LineType.STRIPED)))
        net.add_lane(
            "c", "d",
            StraightLane([length, 0], [0, 0],
                         line_types=(LineType.NONE, LineType.CONTINUOUS_LINE)))

        # Defining counter-clockwise circular U-Turn lanes.
        center = [length, StraightLane.DEFAULT_WIDTH + 20]  # [m]
        radius = 20  # [m]
        alpha = 0  # [deg]

        radii = [radius, radius + StraightLane.DEFAULT_WIDTH]
        n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED
        line = [[c, s], [n, c]]
        for lane in [0, 1]:
            net.add_lane(
                "b", "c",
                CircularLane(center,
                             radii[lane],
                             np.deg2rad(90 - alpha),
                             np.deg2rad(-90 + alpha),
                             clockwise=False,
                             line_types=line[lane]))

        offset = 2 * radius

        # Defining lower starting lanes before the U-Turn.
        # These Lanes are defined from x-coordinate 0 to 'length'.
        net.add_lane(
            "a", "b",
            StraightLane([
                0,
                ((2 * StraightLane.DEFAULT_WIDTH + offset) -
                 StraightLane.DEFAULT_WIDTH)
            ], [
                length,
                ((2 * StraightLane.DEFAULT_WIDTH + offset) -
                 StraightLane.DEFAULT_WIDTH)
            ],
                         line_types=(LineType.CONTINUOUS_LINE,
                                     LineType.STRIPED)))
        net.add_lane(
            "a", "b",
            StraightLane([0, (2 * StraightLane.DEFAULT_WIDTH + offset)],
                         [length, (2 * StraightLane.DEFAULT_WIDTH + offset)],
                         line_types=(LineType.NONE, LineType.CONTINUOUS_LINE)))

        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
Beispiel #28
0
    def _make_road(self) -> None:
        net = RoadNetwork()

        # Set Speed Limits for Road Sections - Straight, Turn20, Straight, Turn 15, Turn15, Straight, Turn25x2, Turn18
        speedlimits = [None, 10, 10, 10, 10, 10, 10, 10, 10]

        # Initialise First Lane
        lane = StraightLane([42, 0], [100, 0],
                            line_types=(LineType.CONTINUOUS, LineType.STRIPED),
                            width=5,
                            speed_limit=speedlimits[1])
        self.lane = lane

        # Add Lanes to Road Network - Straight Section
        net.add_lane("a", "b", lane)
        net.add_lane(
            "a", "b",
            StraightLane([42, 5], [100, 5],
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         width=5,
                         speed_limit=speedlimits[1]))

        # 2 - Circular Arc #1
        center1 = [100, -20]
        radii1 = 20
        net.add_lane(
            "b", "c",
            CircularLane(center1,
                         radii1,
                         np.deg2rad(90),
                         np.deg2rad(-1),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.CONTINUOUS, LineType.NONE),
                         speed_limit=speedlimits[2]))
        net.add_lane(
            "b", "c",
            CircularLane(center1,
                         radii1 + 5,
                         np.deg2rad(90),
                         np.deg2rad(-1),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         speed_limit=speedlimits[2]))

        # 3 - Vertical Straight
        net.add_lane(
            "c", "d",
            StraightLane([120, -20], [120, -30],
                         line_types=(LineType.CONTINUOUS, LineType.NONE),
                         width=5,
                         speed_limit=speedlimits[3]))
        net.add_lane(
            "c", "d",
            StraightLane([125, -20], [125, -30],
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         width=5,
                         speed_limit=speedlimits[3]))

        # 4 - Circular Arc #2
        center2 = [105, -30]
        radii2 = 15
        net.add_lane(
            "d", "e",
            CircularLane(center2,
                         radii2,
                         np.deg2rad(0),
                         np.deg2rad(-181),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.CONTINUOUS, LineType.NONE),
                         speed_limit=speedlimits[4]))
        net.add_lane(
            "d", "e",
            CircularLane(center2,
                         radii2 + 5,
                         np.deg2rad(0),
                         np.deg2rad(-181),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         speed_limit=speedlimits[4]))

        # 5 - Circular Arc #3
        center3 = [70, -30]
        radii3 = 15
        net.add_lane(
            "e", "f",
            CircularLane(center3,
                         radii3 + 5,
                         np.deg2rad(0),
                         np.deg2rad(136),
                         width=5,
                         clockwise=True,
                         line_types=(LineType.CONTINUOUS, LineType.STRIPED),
                         speed_limit=speedlimits[5]))
        net.add_lane(
            "e", "f",
            CircularLane(center3,
                         radii3,
                         np.deg2rad(0),
                         np.deg2rad(137),
                         width=5,
                         clockwise=True,
                         line_types=(LineType.NONE, LineType.CONTINUOUS),
                         speed_limit=speedlimits[5]))

        # 6 - Slant
        net.add_lane(
            "f", "g",
            StraightLane([55.7, -15.7], [35.7, -35.7],
                         line_types=(LineType.CONTINUOUS, LineType.NONE),
                         width=5,
                         speed_limit=speedlimits[6]))
        net.add_lane(
            "f", "g",
            StraightLane([59.3934, -19.2], [39.3934, -39.2],
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         width=5,
                         speed_limit=speedlimits[6]))

        # 7 - Circular Arc #4 - Bugs out when arc is too large, hence written in 2 sections
        center4 = [18.1, -18.1]
        radii4 = 25
        net.add_lane(
            "g", "h",
            CircularLane(center4,
                         radii4,
                         np.deg2rad(315),
                         np.deg2rad(170),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.CONTINUOUS, LineType.NONE),
                         speed_limit=speedlimits[7]))
        net.add_lane(
            "g", "h",
            CircularLane(center4,
                         radii4 + 5,
                         np.deg2rad(315),
                         np.deg2rad(165),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         speed_limit=speedlimits[7]))
        net.add_lane(
            "h", "i",
            CircularLane(center4,
                         radii4,
                         np.deg2rad(170),
                         np.deg2rad(56),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.CONTINUOUS, LineType.NONE),
                         speed_limit=speedlimits[7]))
        net.add_lane(
            "h", "i",
            CircularLane(center4,
                         radii4 + 5,
                         np.deg2rad(170),
                         np.deg2rad(58),
                         width=5,
                         clockwise=False,
                         line_types=(LineType.STRIPED, LineType.CONTINUOUS),
                         speed_limit=speedlimits[7]))

        # 8 - Circular Arc #5 - Reconnects to Start
        center5 = [43.2, 23.4]
        radii5 = 18.5
        net.add_lane(
            "i", "a",
            CircularLane(center5,
                         radii5 + 5,
                         np.deg2rad(240),
                         np.deg2rad(270),
                         width=5,
                         clockwise=True,
                         line_types=(LineType.CONTINUOUS, LineType.STRIPED),
                         speed_limit=speedlimits[8]))
        net.add_lane(
            "i", "a",
            CircularLane(center5,
                         radii5,
                         np.deg2rad(238),
                         np.deg2rad(268),
                         width=5,
                         clockwise=True,
                         line_types=(LineType.NONE, LineType.CONTINUOUS),
                         speed_limit=speedlimits[8]))

        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
Beispiel #29
0
    def _make_road(self) -> None:
        """
        Make a road composed of a straight highway and a merging lane.

        :return: the road
        ljk = StraightLane([0, 6.5 + 4 + 4], [ends[0], 6.5 + 4 + 4], line_types=[c, c], forbidden=True)
        lke = SineLane(ljk.position(ends[0], -amplitude), ljk.position(sum(ends[:2]), -amplitude),
                       amplitude, 2 * np.pi / (2*ends[1]), np.pi / 2, line_types=[c, c], forbidden=True)
        net.add_lane("j", "k", ljk)
        net.add_lane("k", "b", lke)
        #net.add_lane("e", "c", lbc)
        road = Road(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        #road.objects.append(Obstacle(road, lbc.position(ends[2], 0)))
        self.road = road

        Make a road composed of a straight highway and a merging lane.

        :return: the road
 """
        net = RoadNetwork()
        # Highway lanes
        ends = [90, 155, 180]  # Before, converging, merge, after
        c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE
        y = [18.9, 21.2, 25.5]
        #line_type = [[c, s], [n, c]]
        #line_type_merge=[[s,s],[n,c]]
        line_type_merge = [[c, n], [s, c]]
        #line_type_highway=[[c,s],[n,c]]
        #line_type_merge = [[c, s], [n, s]]
        line_type = [c, c]
        #amplitude = 2
        #ljk = StraightLane([0,6.85],[183,6.85],line_types=[c,c], forbidden=True)
        #lbc = SineLane(ljk.position(182,amplitude), ljk.position(218, amplitude),
        #               -amplitude, 2 * np.pi / 72, np.pi / 2, line_types=[c, n], forbidden=True)
        #net.add_lane("b", "c", lbc)
        #net.add_lane("a", "b", StraightLane([0, 6.85], [183, 6.85], line_types=[c,n]))
        '''
        for i in range(2):
            net.add_lane("a", "c", StraightLane([0, y[i]], [218, y[i]], line_types=line_type_merge[i]))
            net.add_lane("c", "d", StraightLane([218, y[i]], [400, y[i]], line_types=line_type_highway[i]))
        #net.add_lane("e", "c", lbc)
        road = Road(network=net, np_random=self.np_random, record_history=self.config["show_trajectories"])
        '''
        amplitude = 2.15

        lmn = StraightLane([0, y[0]], [90, y[0]],
                           2.2,
                           line_types=[c, n],
                           forbidden=True)
        lab = StraightLane([0, y[1]], [90, y[1]],
                           2.2,
                           line_types=[c, n],
                           forbidden=True)
        net.add_lane(
            "m", "n",
            StraightLane([0, y[0]], [90, y[0]],
                         2.2,
                         line_types=line_type_merge[0]))
        net.add_lane(
            "a", "b",
            StraightLane([0, y[1]], [90, y[1]],
                         2.2,
                         line_types=line_type_merge[1]))
        net.add_lane(
            "d", "e",
            StraightLane([155, y[2]], [180, y[2]], 2.2, line_types=line_type))
        lnd = SineLane(lmn.position(90, amplitude),
                       lmn.position(155, amplitude),
                       -amplitude,
                       2 * np.pi / 130,
                       np.pi / 2,
                       2.2,
                       line_types=[c, n],
                       forbidden=True)
        lbd = SineLane(lab.position(90, amplitude),
                       lab.position(155, amplitude),
                       -amplitude,
                       2 * np.pi / 130,
                       np.pi / 2,
                       2.2,
                       line_types=[s, c],
                       forbidden=False)
        net.add_lane("n", "d", lnd)
        net.add_lane("b", "d", lbd)
        #road.objects.append(Obstacle(road, lbc.position(ends[2], 0)))
        road = Road(network=net,
                    np_random=self.np_random,
                    record_history=self.config["show_trajectories"])
        self.road = road
        road.objects.append(Obstacle(road, lnd.position(65, 0)))
    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