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
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"])
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, 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.objects.append(Obstacle(road, lbc.position(ends[2], 0))) self.road = road
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
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): 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 = 40 # [m] # Corners: 0:south, 1:west, 2:north, 3:east. i:inner, o:outer, r:right, l:left net = RoadNetwork() n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED for corner in range(4): angle = rad(90 * corner) 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])) # 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 + rad(180), angle + rad(270), line_types=[n, c])) # 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 + rad(0), angle + rad(-90), clockwise=False, line_types=[n, n])) # 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])) # 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])) road = Road(network=net, np_random=self.np_random) self.road = road
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
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
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
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
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"])
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 _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
def make_road(self): net = RoadNetwork() # pre-processing c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE W = StraightLane.DEFAULT_WIDTH # Highway lanes ends = [150, 10, 80] ptr_LB, ptr_UB = 0, 0 ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[0] net.add_lane("a", "b", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[c, s])) # Lane 1 net.add_lane("a", "b", StraightLane([ptr_LB, 2 * W], [ptr_UB, 2 * W], line_types=[s, c])) # Lane 2 ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[1] net.add_lane("b", "c", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[c, s])) # Lane 1 net.add_lane("b", "c", StraightLane([ptr_LB, 2 * W], [ptr_UB, 2 * W], line_types=[s, c])) # Lane 2 ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[2] net.add_lane("c", "d", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[c, s])) # Lane 1 net.add_lane("c", "d", StraightLane([ptr_LB, 2 * W], [ptr_UB, 2 * W], line_types=[s, c])) # Lane 2 # Crossing Lane ends = [2, 2 * W, 2] ptr_LB, ptr_UB = 0, 0 ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[0] lij = PedestrianLane([150, ptr_LB], [150, ptr_UB], width=10) ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[1] ljk = PedestrianLane([150, ptr_LB], [150, ptr_UB], width=10) ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[2] lkl = PedestrianLane([150, ptr_LB], [150, ptr_UB], width=10) net.add_lane("i", "j", lij) net.add_lane("j", "k", ljk) net.add_lane("k", "l", lkl) road = Road(network=net) # road.vehicles.append(Obstacle(road, [150-10, 2*W])) road.vehicles.append(Occlusion(road, [150 - 10, 2 * W], [2 * W, W])) self.road = road
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
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
def make_road2(self): """ Make a road composed of a straight highway and a merging lane. :return: the road """ net = RoadNetwork() # pre-processing ends = [150, 80, 80, 150] c, s, n = LineType.CONTINUOUS_LINE, LineType.STRIPED, LineType.NONE W = StraightLane.DEFAULT_WIDTH # Highway lanes ptr_LB, ptr_UB = 0, ends[0] + ends[ 1] # Miss what is relevant only for merging lane net.add_lane("a", "b", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[c, s])) # Lane 1 net.add_lane("a", "b", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[n, c])) # Lane 2 ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[1] net.add_lane("b", "c", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[c, s])) net.add_lane("b", "c", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[n, s])) ptr_LB, ptr_UB = ptr_UB, ptr_UB + ends[2] net.add_lane("c", "d", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[c, s])) net.add_lane("c", "d", StraightLane([ptr_LB, W], [ptr_UB, W], line_types=[n, c])) # Crossing Lane # TBA # Merging lane amplitude = 3.25 offset = 6.5 ljk = StraightLane([0, offset + 2 * W], [ends[0], offset + 2 * W], line_types=[c, c], forbidden=True) if 1: 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) else: lkb = StraightLane([ends[0], offset + 2 * W], [ends[0] + ends[1], 2 * W], 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) # road.vehicles.append(Obstacle(road, lbc.position(ends[2], 0))) self.road = road
def make_road(self): """ Make a road composed of a straight highway and a merging lane. :return: the road """ amp = 3.25 str_len = 150 sin_len = 100 width = 4 net = RoadNetwork() n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED net.add_lane( "s1", "inter1", SineLane([0, 0], [sin_len, 0], -amp * 1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "s1", "inter1", SineLane([0, width], [sin_len, width], -amp * 1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "s2", "inter1", SineLane([0, 2 * amp + 2 * width], [sin_len, 2 * amp + 2 * width], -amp * -1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "s2", "inter1", SineLane([0, 2 * amp + 3 * width], [sin_len, 2 * amp + 3 * width], -amp * -1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "inter1", "inter2", StraightLane([sin_len, amp], [sin_len + str_len, amp], line_types=[c, s])) net.add_lane( "inter1", "inter2", StraightLane([sin_len, amp + width], [sin_len + str_len, amp + width], line_types=[s, s])) net.add_lane( "inter1", "inter2", StraightLane([sin_len, amp + 2 * width], [sin_len + str_len, amp + 2 * width], line_types=[s, s])) net.add_lane( "inter1", "inter2", StraightLane([sin_len, amp + 3 * width], [sin_len + str_len, amp + 3 * width], line_types=[s, c])) net.add_lane( "inter2", "e1", SineLane([sin_len + str_len, 0], [2 * sin_len + str_len, 0], amp * 1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "inter2", "e1", SineLane([sin_len + str_len, width], [2 * sin_len + str_len, width], amp * 1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "inter2", "e2", SineLane([sin_len + str_len, 2 * width + 2 * amp], [2 * sin_len + str_len, 2 * width + 2 * amp], amp * -1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) net.add_lane( "inter2", "e2", SineLane([sin_len + str_len, 3 * width + 2 * amp], [2 * sin_len + str_len, 3 * width + 2 * amp], amp * -1, np.pi / sin_len, np.pi / 2, line_types=[c, c], forbidden=False)) road = Road(network=net, np_random=self.np_random) # road.vehicles.append(Obstacle(road, [0, 12])) 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
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_roads(self): net = RoadNetwork() n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED net.add_lane( "s1", "inter1", StraightLane(np.array([0, 0]), np.array([100, 0]), line_types=[c, s])) net.add_lane( "inter1", "inter2", StraightLane(np.array([100, 0]), np.array([150, 0]), line_types=[c, s])) net.add_lane( "inter2", "inter3", StraightLane(np.array([150, 0]), np.array([200, 0]), line_types=[c, s])) net.add_lane( "inter3", "x1", StraightLane(np.array([200, 0]), np.array([300, 0]), line_types=[c, s])) net.add_lane( "s1", "inter1", StraightLane(np.array([0, 4]), np.array([100, 4]), line_types=[s, s])) net.add_lane( "inter1", "inter2", StraightLane(np.array([100, 4]), np.array([150, 4]), line_types=[s, s])) net.add_lane( "inter2", "inter3", StraightLane(np.array([150, 4]), np.array([200, 4]), line_types=[s, s])) net.add_lane( "inter3", "x1", StraightLane(np.array([200, 4]), np.array([300, 4]), line_types=[s, s])) net.add_lane( "s1", "inter1", StraightLane(np.array([0, 8]), np.array([100, 8]), line_types=[s, s])) net.add_lane( "inter1", "inter2", StraightLane(np.array([100, 8]), np.array([150, 8]), line_types=[s, s])) net.add_lane( "inter2", "inter3", StraightLane(np.array([150, 8]), np.array([200, 8]), line_types=[s, c])) net.add_lane( "inter3", "x1", StraightLane(np.array([200, 8]), np.array([300, 8]), line_types=[s, c])) amplitude = 4.5 net.add_lane( "s1", "inter1", StraightLane(np.array([0, 12]), np.array([100, 12]), line_types=[s, c])) net.add_lane( "inter1", "inter2", StraightLane(np.array([100, 12]), np.array([150, 12]), line_types=[s, c])) net.add_lane( "inter2", "ee", StraightLane(np.array([150, 12]), np.array([200, 12]), line_types=[s, c], forbidden=True)) net.add_lane( "ee", "ex", SineLane(np.array([200, 12 + amplitude]), np.array([250, 12 + amplitude]), -amplitude, 2 * np.pi / (2 * 50), np.pi / 2, line_types=[c, c], forbidden=True)) net.add_lane( "ex", "x2", StraightLane(np.array([250, 17 + amplitude]), np.array([300, 17 + amplitude]), line_types=[c, c], forbidden=True)) road = Road(network=net, np_random=self.np_random) # road.vehicles.append(RedLight(road, [150, 0])) # road.vehicles.append(RedLight(road, [150, 4])) # road.vehicles.append(RedLight(road, [150, 8])) self.road = road
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
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
def make_roads(self): net = RoadNetwork() n, c, s = LineType.NONE, LineType.CONTINUOUS, LineType.STRIPED """ e:entrance before_inter:a node before inter_node inter: stripe lines --> solid lines """ net.add_lane("se", "inters", StraightLane(np.array([0, 0]), np.array([0, -70]), line_types=[c, s])) net.add_lane("se", "inters", StraightLane(np.array([4, 0]), np.array([4, -70]), line_types=[s, s])) net.add_lane("se", "inters", StraightLane(np.array([8, 0]), np.array([8, -70]), line_types=[s, c])) # net.add_lane("before_inters", "inters", StraightLane(np.array([0, -60]), np.array([0, -70]), line_types=[c, s])) # net.add_lane("before_inters", "inters", StraightLane(np.array([4, -60]), np.array([4, -70]), line_types=[s, s])) # net.add_lane("before_inters", "inters", StraightLane(np.array([8, -60]), np.array([8, -70]), line_types=[s, c])) net.add_lane("inters", "sel", StraightLane(np.array([0, -70]), np.array([0, -100]), line_types=[c, c], forbidden=True)) net.add_lane("inters", "sem", StraightLane(np.array([4, -70]), np.array([4, -100]), line_types=[c, c], forbidden=True)) net.add_lane("inters", "ser", StraightLane(np.array([8, -70]), np.array([8, -100]), line_types=[c, c], forbidden=True)) net.add_lane("nxl", "inter_n", StraightLane(np.array([0, -130]), np.array([0, -150]), line_types=[c, s])) net.add_lane("nxm", "inter_n", StraightLane(np.array([4, -130]), np.array([4, -150]), line_types=[s, s])) net.add_lane("nxr", "inter_n", StraightLane(np.array([8, -130]), np.array([8, -150]), line_types=[s, c])) net.add_lane("interw", "wel", StraightLane(np.array([-30, -110]), np.array([-6, -110]), line_types=[c, c])) net.add_lane("interw", "wer", StraightLane(np.array([-30, -106]), np.array([-6, -106]), line_types=[c, c])) net.add_lane("wxl", "inter_w", StraightLane(np.array([-6, -118]), np.array([-30, -118]), line_types=[c, s])) net.add_lane("wxr", "inter_w", StraightLane(np.array([-6, -122]), np.array([-30, -122]), line_types=[s, c])) net.add_lane("intere", "eel", StraightLane(np.array([40, -118]), np.array([14, -118]), line_types=[c, c])) net.add_lane("intere", "eer", StraightLane(np.array([40, -122]), np.array([14, -122]), line_types=[c, c])) net.add_lane("exl", "inter_e", StraightLane(np.array([14, -110]), np.array([40, -110]), line_types=[c, s])) net.add_lane("exr", "inter_e", StraightLane(np.array([14, -106]), np.array([40, -106]), line_types=[s, c])) net.add_lane("sel", "wxl", StraightLane(np.array([0, -100]), np.array([-6, -118]), line_types=[n, n])) net.add_lane("sem", "nxm", StraightLane(np.array([4, -100]), np.array([4, -130]), line_types=[n, n])) net.add_lane("ser", "exr", StraightLane(np.array([8, -100]), np.array([14, -106]), line_types=[n, n])) road = Road(network=net, np_random=self.np_random) green_time = 5 red_time = 8 green_flash_time = 2 yellow_time = 1 """ southwest crossroad traffic lights """ self.traffic_lights = [ RedLight(road, [0, -100], red_time, green_time, green_flash_time, yellow_time, 1), RedLight(road, [4, -100], red_time, green_time, green_flash_time, yellow_time, 1), RedLight(road, [8, -100], 0, 10, 0, 0, 1), ] self.road = road self.road.vehicles = self.traffic_lights