def make_vehicles(self, other_vehicles_mandatory=False): """ Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle. :param other_vehicles_mandatory: if the lane changing maneuvers of other vehicles are mandatory :return: None """ max_l = 500 road = self.road other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) car_number_each_lane = 15 # reset_position_range = (30, 40) reset_position_range = (20, 30) # reset_lane = random.choice(road.lanes) reset_lane = road.lanes[0] for l in road.lanes[:3]: cars_on_lane = car_number_each_lane reset_position = None if l is reset_lane: cars_on_lane += 1 reset_position = random.choice(range(5, 6)) # reset_position = 2 for i in range(cars_on_lane): if i == reset_position: if self.switch: ego_vehicle = MDPVehicle( road, l.position( (i + 1) * np.random.randint(*reset_position_range), 0), velocity=20, max_length=max_l) else: ego_vehicle = IDMVehicle( road, l.position( (i + 1) * np.random.randint(*reset_position_range), 0), velocity=20, max_length=max_l) ego_vehicle.destination = 1 ego_vehicle.id = 0 road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle l.vehicles.append(ego_vehicle) else: car = other_vehicles_type( road, l.position( (i + 1) * np.random.randint(*reset_position_range), 0), velocity=np.random.randint(18, 25), dst=3, max_length=max_l) if other_vehicles_mandatory: car.destination = 1 road.vehicles.append(car) l.vehicles.append(car) for l in [road.lanes[3]]: cars_on_lane = car_number_each_lane reset_position = None if l is reset_lane: cars_on_lane += 1 reset_position = random.choice(range(5, 6)) # reset_position = 2 for i in range(cars_on_lane): if i < 8: continue if i == reset_position: # ego_vehicle = MDPVehicle(road, l.position((i+1) * np.random.randint(*reset_position_range), 0), # velocity=20, max_length=max_l) ego_vehicle = IDMVehicle( road, l.position( (i + 1) * np.random.randint(*reset_position_range), 0), velocity=20, max_length=max_l) ego_vehicle.destination = 1 ego_vehicle.id = 0 road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle l.vehicles.append(ego_vehicle) else: car = other_vehicles_type( road, l.position( (i + 1) * np.random.randint(*reset_position_range), 0), velocity=np.random.randint(18, 25), dst=3, max_length=max_l) if other_vehicles_mandatory: car.destination = 1 road.vehicles.append(car) l.vehicles.append(car) for lane in road.lanes: lane.vehicles = sorted( lane.vehicles, key=lambda x: lane.local_coordinates(x.position)[0]) for i, v in enumerate(lane.vehicles): v.vehicle_index_in_line = i
def _make_vehicles(self): """ Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle. :return: the ego-vehicle """ """ road = self.road other_vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) car_number_each_lane = 5 # reset_position_range = (30, 40) reset_position_range = (100, 0) # reset_lane = random.choice(road.lanes) for l in road.lanes[:3]: reset_lane = road.lanes[2] cars_on_lane = car_number_each_lane reset_position = (32, 0) if l is reset_lane: cars_on_lane += 1 # reset_position = random.choice(range(5, 6)) # github-version position_deviation = 2 velocity_deviation = 2 # Ego-vehicle # ego_lane = self.road.network.get_lane(("ser", "ses", 0)) ego_lane = self.road.get_lane((100, 0)) print("\nego_lane.position:", ego_lane.position(140, 0), "\n") # ego_vehicle = MDPVehicle(self.road, # ego_lane.position(140, 0), # velocity=5, # heading=ego_lane.heading_at(140)).plan_route_to("nxs") ego_vehicle = IDMVehicle(road, (60, 0), np.pi / 2, velocity=10, max_length=500) # MDPVehicle.SPEED_MIN = 0 # MDPVehicle.SPEED_MAX = 15 # MDPVehicle.SPEED_COUNT = 4 self.road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle # Incoming vehicle # destinations = ["exr", "sxr", "nxr"] other_vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) vehicle = other_vehicles_type.make_on_lane(self.road, (0, 30), longitudinal=5 + self.np_random.randn() * position_deviation, velocity=16 + self.np_random.randn() * velocity_deviation) # if self.config["incoming_vehicle_destination"] is not None: # destination = destinations[self.config["incoming_vehicle_destination"]] # else: # destination = self.np_random.choice(destinations) # vehicle.plan_route_to(destination) # vehicle.randomize_behavior() self.road.vehicles.append(vehicle) # Other vehicles for i in list(range(1, 2)) + list(range(-1, 0)): vehicle = other_vehicles_type.make_on_lane(self.road, (30 * i, 0), longitudinal=20 * i + self.np_random.randn() * position_deviation, velocity=16 + self.np_random.randn() * velocity_deviation) # vehicle.plan_route_to(self.np_random.choice(destinations)) # vehicle.randomize_behavior() self.road.vehicles.append(vehicle) # Entering vehicle # vehicle = other_vehicles_type.make_on_lane(self.road, # ("eer", "ees", 0), # longitudinal=50 + self.np_random.randn() * position_deviation, # velocity=16 + self.np_random.randn() * velocity_deviation) # vehicle.plan_route_to(self.np_random.choice(destinations)) # vehicle.randomize_behavior() # self.road.vehicles.append(vehicle) vehicle = MDPVehicle.create_random(self.road, 25, spacing=3, np_random=self.np_random) self.road.vehicles.append(vehicle) for v in self.road.vehicles: lane_index = v.lane_index self.road.lanes[lane_index].vehicles.append(v) """ position_deviation = 5 velocity_deviation = 1.5 other_vehicles_mandatory = False max_l = 700 road = self.road other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) car_number_each_lane = 1 # reset_position_range = (30, 40) # reset_lane = random.choice(road.lanes) reset_lane = ("ser", "ses", 0) ego_vehicle = None num = 0 print("lanes:", self.road.network.LANES, "\n") # self.road.network.LANES_NUMBER = 28 for l in self.road.network.LANES: # print("l:",l,"\n") lane = road.network.get_lane(l) cars_on_lane = car_number_each_lane reset_position = None if l == reset_lane: cars_on_lane += 1 reset_position = random.choice(range(0, 3)) # reset_position = 2 for j in range(cars_on_lane): if not ego_vehicle: ego_lane = self.road.network.get_lane(("ser", "ses", 0)) if self.switch: ego_vehicle = MDPVehicle(self.road, ego_lane.position(110, 0), velocity=15, heading=ego_lane.heading_at( 110)).plan_route_to("nxs") else: ego_vehicle = IDMVehicle(self.road, ego_lane.position(110, 0), velocity=15, heading=ego_lane.heading_at( 110)).plan_route_to("wxs") # ego_vehicle.destination = 1 ego_vehicle.id = 0 self.road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle lane.vehicles.append(ego_vehicle) else: destinations = ["wxr", "sxr", "nxr", "exr"] birth_place = [("ee", "nx", 0), ("ne", "wx", 0), ("we", "sx", 0), ("se", "ex", 0), ("ee", "nx", 1), ("ne", "wx", 1), ("we", "sx", 1), ("se", "ex", 1), ("wer", "wes", 0), ("eer", "ees", 0), ("ner", "nes", 0), ("ser", "ses", 0), ("wxs", "wxr", 0), ("exs", "exr", 0), ("nxs", "nxr", 0), ("sxs", "sxr", 0)] car = other_vehicles_type.make_on_lane( self.road, birth_place[np.random.randint(0, 16)], longitudinal=70 + np.random.randint(1, 5) * position_deviation, velocity=5 + np.random.randint(1, 10) * velocity_deviation) if self.config["incoming_vehicle_destination"] is not None: destination = destinations[ self.config["incoming_vehicle_destination"]] else: destination = destinations[np.random.randint(0, 4)] # destination = self.np_random.choice(destinations) """ if 0<= num <=6: destinations = ["wxr", "sxr", "nxr"] car = other_vehicles_type.make_on_lane(self.road, ("ee", "nx", 1), longitudinal=5 + self.np_random.randn() * position_deviation, velocity=5 + self.np_random.randn() * velocity_deviation) if self.config["incoming_vehicle_destination"] is not None: destination = destinations[self.config["incoming_vehicle_destination"]] else: destination = self.np_random.choice(destinations) # if other_vehicles_mandatory: # car.destination = 1 # car.plan_route_to(destination) # car.randomize_behavior() # self.road.vehicles.append(car) # road.vehicles.append(car) # lane.vehicles.append(car) elif 7<=num<=13: destinations = ["exr", "sxr", "wxr"] car = other_vehicles_type.make_on_lane(self.road, ("ne", "wx", 1), longitudinal=5 + self.np_random.randn() * position_deviation, velocity=5 + self.np_random.randn() * velocity_deviation) if self.config["incoming_vehicle_destination"] is not None: destination = destinations[self.config["incoming_vehicle_destination"]] else: destination = self.np_random.choice(destinations) # if other_vehicles_mandatory: # car.destination = 1 # car.plan_route_to(destination) # car.randomize_behavior() # self.road.vehicles.append(car) # road.vehicles.append(car) # lane.vehicles.append(car) elif 14<=num<=20: destinations = ["exr", "sxr", "nxr"] car = other_vehicles_type.make_on_lane(self.road, ("we", "sx", 1), longitudinal=5 + self.np_random.randn() * position_deviation, velocity=5 + self.np_random.randn() * velocity_deviation) if self.config["incoming_vehicle_destination"] is not None: destination = destinations[self.config["incoming_vehicle_destination"]] else: destination = self.np_random.choice(destinations) # if other_vehicles_mandatory: # car.destination = 1 # car.plan_route_to(destination) # car.randomize_behavior() # self.road.vehicles.append(car) # road.vehicles.append(car) # lane.vehicles.append(car) else : destinations = ["exr", "wxr", "nxr"] car = other_vehicles_type.make_on_lane(self.road, ("se", "ex", 1), longitudinal=5 + self.np_random.randn() * position_deviation, velocity=5 + self.np_random.randn() * velocity_deviation) if self.config["incoming_vehicle_destination"] is not None: destination = destinations[self.config["incoming_vehicle_destination"]] else: destination = self.np_random.choice(destinations) # if other_vehicles_mandatory: # car.destination = 1 # car.plan_route_to(destination) # car.randomize_behavior() # self.road.vehicles.append(car) # road.vehicles.append(car) # lane.vehicles.append(car) """ car.plan_route_to(destination) car.randomize_behavior() self.road.vehicles.append(car) # road.vehicles.append(car) lane.vehicles.append(car) # if other_vehicles_mandatory: # car.destination = 1 # road.vehicles.append(car) # l.vehicles.append(car) num += 1 # print("make_vehicle finish") for i in range(self.road.network.LANES_NUMBER): lane = road.network.get_lane(self.road.network.LANES[i]) # print("lane:", lane.LANEINDEX, "\n") lane.vehicles = sorted( lane.vehicles, key=lambda x: lane.local_coordinates(x.position)[0]) # print("len of lane.vehicles:", len(lane.vehicles), "\n") for j, v in enumerate(lane.vehicles): # print("i:",i,"\n") v.vehicle_index_in_line = j