def _create_vehicles(self): """ Create some new random vehicles of a given type, and add them on the road. """ self.vehicle = MDPVehicle.create_random( self.road, 25, spacing=self.config["initial_spacing"]) self.road.vehicles.append(self.vehicle) # create conservative cars on the road vehicles_type1 = utils.class_from_path( self.config["other_vehicles_type"]) vehicles_type2 = utils.class_from_path( self.config["aggressive_vehicle_type"]) vehicles_type3 = utils.class_from_path( self.config["aggressive_vehicle_type2"]) # add some aggressive vehicles in the road count_aggressive = 0 for _ in range(self.config["vehicles_count"] + self.config["num_aggressive"]): a = np.random.randint(low=1, high=5) if a == 1: count_aggressive += 1 self.road.vehicles.append( vehicles_type2.create_random(self.road)) if count_aggressive < 3: self.road.vehicles.append( vehicles_type3.create_random(self.road)) else: self.road.vehicles.append( vehicles_type1.create_random(self.road)) print("number of aggressive vehicles ", count_aggressive)
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" self.controlled_vehicles = [] for i in range(self.config["controlled_vehicles"]): vehicle = self.action_type.vehicle_class( self.road, [i * 20, 0], 2 * np.pi * self.np_random.rand(), 0) self.road.vehicles.append(vehicle) self.controlled_vehicles.append(vehicle) self.initial_vehicle_count = [] c = 1 for i in range(1, self.config["initial_vehicle_count"]): c = c * -1 other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicle_1 = other_vehicles_type(self.road, [c * i * 10, c * i * 3], speed=.7) # #vehicle_1 = self.action_type.vehicle_class(self.road, [c*i*10, c*i*3], 1*np.pi*self.np_random.rand(), .7) #vehicle_1.act([.2,.3]) self.road.vehicles.append(vehicle_1) self.initial_vehicle_count.append(vehicle_1) for i in range(3, 10): c = c * -1 other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicle_2 = other_vehicles_type(self.road, [c * i * 5, c * i * 1], speed=0) # self.road.vehicles.append(vehicle_2) lane = self.np_random.choice(self.road.network.lanes_list()) self.goal = Landmark(self.road, lane.position(lane.length / 2, 0), heading=lane.heading) self.road.objects.append(self.goal)
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" self.controlled_vehicles = [] for _ in range(self.config["controlled_vehicles"]): vehicle = self.action_type.vehicle_class.create_random( self.road, 25, spacing=self.config["initial_spacing"]) self.controlled_vehicles.append(vehicle) self.road.vehicles.append(vehicle) vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicles_type2 = utils.class_from_path( "highway_env.vehicle.behavior.IDMVehicle") for _ in range(self.config["vehicles_count"]): self.road.vehicles.append(vehicles_type.create_random(self.road))
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" self.controlled_vehicles = [] for _ in range(self.config["controlled_vehicles"]): vehicle = self.action_type.vehicle_class.create_random( self.road, speed=25, lane_from="0", lane_to="1", lane_id=0, spacing=self.config["ego_spacing"]) vehicle.SPEED_MIN = 18 self.controlled_vehicles.append(vehicle) self.road.vehicles.append(vehicle) vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) for _ in range(self.config["vehicles_count"]): lanes = np.arange(self.config["lanes_count"]) lane_id = self.road.np_random.choice(lanes, size=1, p=lanes / lanes.sum()).astype(int)[0] lane = self.road.network.get_lane(("0", "1", lane_id)) vehicle = vehicles_type.create_random( self.road, lane_from="0", lane_to="1", lane_id=lane_id, speed=lane.speed_limit, spacing=1 / self.config["vehicles_density"], ).plan_route_to("3") vehicle.enable_lane_change = False self.road.vehicles.append(vehicle)
def _create_vehicles(self, parked_probability: float = 0.75) -> None: """ Create some new random vehicles of a given type, and add them on the road. :param parked_probability: probability that a spot is occupied """ self.vehicle = self.action_type.vehicle_class(self.road, self.vehicle_starting, 2 * np.pi * self.np_random.rand(), 0) self.road.vehicles.append(self.vehicle) goal_position = [self.np_random.choice([-2 * self.spots - 10, 2 * self.spots + 10]), 0] self.goal = Landmark(self.road, goal_position, heading=0) self.road.objects.append(self.goal) vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) for i in range(self.config["vehicles_count"]): is_parked = self.np_random.rand() <= parked_probability if not is_parked: # Just an effort to spread the vehicles out idx = self.np_random.randint(0, self.num_middle_lanes) longitudinal = (i * 5) - (self.x_range / 8) * self.np_random.randint(-1, 1) self.road.vehicles.append( vehicles_type.make_on_lane(self.road, ("d", "e", idx), longitudinal, speed=2)) else: lane = ("a", "b", i) if self.np_random.rand() >= 0.5 else ("b", "c", i) self.road.vehicles.append(Vehicle.make_on_lane(self.road, lane, 4, speed=0)) for v in self.road.vehicles: # Prevent early collisions if v is not self.vehicle and np.linalg.norm(v.position - self.vehicle.position) < 20: self.road.vehicles.remove(v)
def _spawn_vehicle_intersection(self, longitudinal: float = 0, position_deviation: float = 1., speed_deviation: float = 1., spawn_probability: float = 1, go_straight: bool = False) -> None: if self.np_random.rand() > spawn_probability: return route = self.np_random.choice(range(4), size=2, replace=False) route[1] = (route[0] + 2) % 4 if go_straight else route[1] baseline_vehicle = utils.class_from_path( self.scenario.baseline_vehicle["vehicles_type"]) vehicle_type = baseline_vehicle vehicle = vehicle_type.make_on_lane( self.road, ("o" + str(route[0]), "ir" + str(route[0]), 0), longitudinal=longitudinal + 5 + self.np_random.randn() * position_deviation, speed=8 + self.np_random.randn() * speed_deviation) for v in self.road.vehicles: if np.linalg.norm(v.position - vehicle.position) < 25: return vehicle.plan_route_to("o" + str(route[1])) # vehicle.randomize_behavior() self.road.vehicles.append(vehicle) return vehicle
def _make_vehicles(self): """ Populate a road with several vehicles on the road :return: the ego-vehicle """ road = self.road ego_vehicle = MDPVehicle(road, road.network.get_lane(("a", "b", 1)).position(30, 0), velocity=30) road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) for i in range(3): self.road.vehicles.append( vehicles_type(road, position=road.network.get_lane(("a", "b", 1)) .position(70+40*i + 10*self.np_random.randn(), 0), heading=road.network.get_lane(("a", "b", 1)).heading_at(70+40*i), velocity=24 + 2*self.np_random.randn(), enable_lane_change=False) ) for i in range(2): v = vehicles_type(road, position=road.network.get_lane(("b", "a", 0)) .position(200+100*i + 10*self.np_random.randn(), 0), heading=road.network.get_lane(("b", "a", 0)).heading_at(200+100*i), velocity=20 + 5*self.np_random.randn(), enable_lane_change=False) v.target_lane_index = ("b", "a", 0) self.road.vehicles.append(v)
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 """ # Ego-vehicle ego_lane = self.road.network.get_lane(("ser", "ses", 0)) ego_vehicle = MDPVehicle(self.road, ego_lane.position(130, 0), velocity=15, heading=ego_lane.heading_at(130)).plan_route_to("nxs") MDPVehicle.SPEED_MIN = 5 MDPVehicle.SPEED_MAX = 15 MDPVehicle.SPEED_COUNT = 3 self.road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle # Other vehicles other_vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) self.road.vehicles.append( other_vehicles_type( self.road, self.road.network.get_lane(("we", "sx", 0)).position(0, 0), velocity=16, heading=self.road.network.get_lane(("we", "sx", 0)).heading_at(0)).plan_route_to("exs")) for i in list(range(2, 3)) + list(range(-1, 0)): self.road.vehicles.append( other_vehicles_type( self.road, self.road.network.get_lane(("we", "sx", 0)).position(20*i, 0), velocity=16, heading=self.road.network.get_lane(("we", "sx", 0)).heading_at(20*i)))
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) other_per_controlled = near_split( self.config["vehicles_count"], num_bins=self.config["controlled_vehicles"]) self.controlled_vehicles = [] for others in other_per_controlled: vehicle = Vehicle.create_random( self.road, speed=25, lane_id=self.config["initial_lane_id"], spacing=self.config["ego_spacing"]) vehicle = self.action_type.vehicle_class(self.road, vehicle.position, vehicle.heading, vehicle.speed) self.controlled_vehicles.append(vehicle) self.road.vehicles.append(vehicle) for _ in range(others): vehicle = other_vehicles_type.create_random( self.road, spacing=1 / self.config["vehicles_density"]) vehicle.randomize_behavior() self.road.vehicles.append(vehicle)
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 """ for _ in range(6): self._spawn_vehicles() [(self.road.act(), self.road.step(0.1)) for _ in range(15)] other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) other_vehicles_type.DISTANCE_WANTED = 2 # Low jam distance other_vehicles_type.COMFORT_ACC_MAX = 6 other_vehicles_type.COMFORT_ACC_MIN = -3 vehicle = other_vehicles_type.make_on_lane(self.road, ("o1", "ir1", 0), longitudinal=0, velocity=None) vehicle.plan_route_to("o3") self.road.vehicles.append(vehicle) # Ego-vehicle ego_lane = self.road.network.get_lane(("o0", "ir0", 0)) ego_vehicle = MDPVehicle(self.road, ego_lane.position(0, 0), velocity=ego_lane.speed_limit, heading=ego_lane.heading_at(0)).plan_route_to( "o" + str(self.np_random.randint(4))) MDPVehicle.SPEED_MIN = 0 MDPVehicle.SPEED_MAX = 9 MDPVehicle.SPEED_COUNT = 3 self.road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle
def _make_vehicles(self) -> None: """ Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle. :return: the ego-vehicle """ position_deviation = 2 speed_deviation = 2 # Ego-vehicle ego_lane = self.road.network.get_lane(("ser", "ses", 0)) ego_vehicle = self.action_type.vehicle_class( self.road, ego_lane.position(125, 0), speed=8, heading=ego_lane.heading_at(140)) try: ego_vehicle.plan_route_to("nxs") except AttributeError: pass 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, ("we", "sx", 1), longitudinal=5 + self.np_random.randn() * position_deviation, speed=16 + self.np_random.randn() * speed_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, ("we", "sx", 0), longitudinal=20 * i + self.np_random.randn() * position_deviation, speed=16 + self.np_random.randn() * speed_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, speed=16 + self.np_random.randn() * speed_deviation) vehicle.plan_route_to(self.np_random.choice(destinations)) vehicle.randomize_behavior() self.road.vehicles.append(vehicle)
def _spawn_vehicle(self, longitudinal=0, position_deviation=1., velocity_deviation=1., spawn_probability=0.6, go_straight=False): if self.np_random.rand() > spawn_probability: return route = self.np_random.choice(range(4), size=2, replace=False) route[1] = (route[0] + 2) % 4 if go_straight else route[1] vehicle_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicle = vehicle_type.make_on_lane( self.road, ("o" + str(route[0]), "ir" + str(route[0]), 0), longitudinal=longitudinal + 5 + self.np_random.randn() * position_deviation, velocity=8 + self.np_random.randn() * velocity_deviation) for v in self.road.vehicles: if np.linalg.norm(v.position - vehicle.position) < 15: return vehicle.plan_route_to("o" + str(route[1])) vehicle.randomize_behavior() self.road.vehicles.append(vehicle) return vehicle
def _make_vehicles(self, n_vehicles: int = 10) -> None: """ Populate a road with several vehicles on the highway and on the merging lane :return: the ego-vehicle """ # Configure vehicles vehicle_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicle_type.DISTANCE_WANTED = 7 # Low jam distance vehicle_type.COMFORT_ACC_MAX = 6 vehicle_type.COMFORT_ACC_MIN = -3 # Random vehicles simulation_steps = 3 for t in range(n_vehicles - 1): self._spawn_vehicle(np.linspace(0, 80, n_vehicles)[t]) for _ in range(simulation_steps): [(self.road.act(), self.road.step(1 / self.config["simulation_frequency"])) for _ in range(self.config["simulation_frequency"])] # Challenger vehicle self._spawn_vehicle(60, spawn_probability=1, go_straight=True, position_deviation=0.1, speed_deviation=0) # Controlled vehicles self.controlled_vehicles = [] for ego_id in range(0, self.config["controlled_vehicles"]): ego_lane = self.road.network.get_lane( ("o{}".format(ego_id % 4), "ir{}".format(ego_id % 4), 0)) destination = self.config["destination"] or "o" + str( self.np_random.randint(1, 4)) ego_vehicle = self.action_type.vehicle_class( self.road, ego_lane.position(60 + 5 * self.np_random.randn(1), 0), speed=ego_lane.speed_limit, heading=ego_lane.heading_at(60)) try: ego_vehicle.plan_route_to(destination) ego_vehicle.SPEED_MIN = 0 ego_vehicle.SPEED_MAX = self.config["reward_speed_range"][1] ego_vehicle.SPEED_COUNT = 3 ego_vehicle.speed_index = ego_vehicle.speed_to_index( ego_lane.speed_limit) ego_vehicle.target_speed = ego_vehicle.index_to_speed( ego_vehicle.speed_index) except AttributeError: pass self.road.vehicles.append(ego_vehicle) self.controlled_vehicles.append(ego_vehicle) for v in self.road.vehicles: # Prevent early collisions if v is not ego_vehicle and np.linalg.norm( v.position - ego_vehicle.position) < 20: self.road.vehicles.remove(v)
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 ego_vehicle = MDPVehicle(road, road.network.get_lane( ("a", "b", 1)).position(30, 0), velocity=30) road.vehicles.append(ego_vehicle) other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) road.vehicles.append( other_vehicles_type(road, road.network.get_lane( ("a", "b", 0)).position(90, 0), velocity=29)) if 0: other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) road.vehicles.append( other_vehicles_type(road, road.network.get_lane( ("a", "b", 0)).position(90, 0), velocity=29)) road.vehicles.append( other_vehicles_type(road, road.network.get_lane( ("a", "b", 1)).position(70, 0), velocity=31)) road.vehicles.append( other_vehicles_type(road, road.network.get_lane( ("a", "b", 0)).position(5, 0), velocity=31.5)) merging_v = other_vehicles_type(road, road.network.get_lane( ("j", "k", 0)).position(110, 0), velocity=20) merging_v.target_velocity = 30 road.vehicles.append(merging_v) self.vehicle = ego_vehicle
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" self.vehicle = self.action_type.vehicle_class.create_random(self.road, 25, spacing=self.config["initial_spacing"]) self.road.vehicles.append(self.vehicle) vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) for _ in range(self.config["vehicles_count"]): self.road.vehicles.append(vehicles_type.create_random(self.road))
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 """ position_deviation = 2 velocity_deviation = 2 # Ego-vehicle ego_lane = self.road.network.get_lane(("ser", "ses", 0)) ego_vehicle = MDPVehicle( self.road, ego_lane.position(140, 0), velocity=5, heading=ego_lane.heading_at(140)).plan_route_to("nxs") 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, ("we", "sx", 1), 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, ("we", "sx", 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)
def create_random(cls, road, velocity=None, spacing=1): """ Create a random vehicle on the road. The lane and /or velocity are chosen randomly, while longitudinal position is chosen behind the last vehicle in the road with density based on the number of lanes. :param road: the road where the vehicle is driving :param velocity: initial velocity in [m/s]. If None, will be chosen randomly :param spacing: ratio of spacing to the front vehicle, 1 being the default :return: A vehicle with random position and/or velocity """ if velocity is None: aggressiveVehicle = utils.class_from_path( "highway_env.vehicle.behavior.AggressiveCar") aggressiveVehicle2 = utils.class_from_path( "highway_env.vehicle.behavior.VeryAggressiveCar") if (cls == aggressiveVehicle): Vehicle.DEFAULT_VELOCITIES = [30, 35] Vehicle.MAX_VELOCITY = 50 elif (cls == aggressiveVehicle2): Vehicle.DEFAULT_VELOCITIES = [25, 30] Vehicle.MAX_VELOCITY = 40 else: Vehicle.DEFAULT_VELOCITIES = [23, 25] Vehicle.MAX_VELOCITY = 30 velocity = road.np_random.uniform(Vehicle.DEFAULT_VELOCITIES[0], Vehicle.DEFAULT_VELOCITIES[1]) default_spacing = 1.0 * velocity _from = road.np_random.choice(list(road.network.graph.keys())) _to = road.np_random.choice(list(road.network.graph[_from].keys())) _id = road.np_random.choice(len(road.network.graph[_from][_to])) offset = spacing * default_spacing * np.exp( -5 / 30 * len(road.network.graph[_from][_to])) x0 = np.max([v.position[0] for v in road.vehicles]) if len( road.vehicles) else 3 * offset x0 += offset * road.np_random.uniform(0.9, 1.1) v = cls(road, road.network.get_lane((_from, _to, _id)).position(x0, 0), road.network.get_lane((_from, _to, _id)).heading_at(x0), velocity) return v
def fake_step(self): """ :return: """ for k in range(int(self.SIMULATION_FREQUENCY // self.POLICY_FREQUENCY)): self.road.act() self.road.step(1 / self.SIMULATION_FREQUENCY) # Automatically render intermediate simulation steps if a viewer has been launched self._automatic_rendering() # Stop at terminal states if self.done or self._is_terminal(): break self.enable_auto_render = False self.steps += 1 from highway_env.extractors import Extractor extractor = Extractor() extractor_features = extractor.FeatureExtractor( self.road.vehicles, 0, 1) for i in range(1): birth_place = [("s1", "inter1", 0), ("s1", "inter1", 1), ("s2", "inter1", 0), ("s2", "inter1", 1)] destinations = ["e1", "e2"] position_deviation = 5 velocity_deviation = 1.5 other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) birth = birth_place[np.random.randint(0, 4)] lane = self.road.network.get_lane(birth) car = other_vehicles_type.make_on_lane( self.road, birth, longitudinal=0 + 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, 2)] car.plan_route_to(destination) car.randomize_behavior() self.road.vehicles.append(car) lane.vehicles.append(car) # obs = self._observation() # reward = self._reward(action) terminal = self._is_terminal() info = {} return terminal, extractor_features
def _create_vehicles(self): """ Create some new random vehicles of a given type, and add them on the road. """ self.vehicle = MDPVehicle.create_random(self.road, 0.6) self.road.vehicles.append(self.vehicle) vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) for _ in range(self.config["vehicles_count"]): self.road.vehicles.append( vehicles_type.create_random(self.road, 0.6))
def _make_vehicles(self) -> None: road = self.road ego_vehicle = self.action_type.vehicle_class(road, road.network.get_lane(("a", "b", 0)).position(10, 0), speed=13) road.vehicles.append(ego_vehicle) #try: # ego_vehicle.plan_route_to("d") #except AttributeError: # pass self.vehicle = ego_vehicle other_vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) spead=self.np_random.randn()
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" self.controlled_vehicles = [] for _ in range(self.config["controlled_vehicles"]): vehicle = self.action_type.vehicle_class.create_random(self.road, speed=25, lane_id=self.config["initial_lane_id"], spacing=self.config["ego_spacing"]) self.controlled_vehicles.append(vehicle) self.road.vehicles.append(vehicle) vehicles_type = utils.class_from_path(self.config["other_vehicles_type"]) for _ in range(self.config["vehicles_count"]): self.road.vehicles.append(vehicles_type.create_random(self.road, spacing=1 / self.config["vehicles_density"]))
def _make_vehicles(self, n_vehicles: int = 10) -> None: """ Populate a road with several vehicles on the highway and on the merging lane :return: the ego-vehicle """ # Configure vehicles vehicle_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicle_type.DISTANCE_WANTED = 7 # Low jam distance vehicle_type.COMFORT_ACC_MAX = 6 vehicle_type.COMFORT_ACC_MIN = -3 # Random vehicles simulation_steps = 3 for t in range(n_vehicles - 1): self._spawn_vehicle(np.linspace(0, 80, n_vehicles)[t]) for _ in range(simulation_steps): [(self.road.act(), self.road.step(1 / self.config["simulation_frequency"])) for _ in range(self.config["simulation_frequency"])] # Challenger vehicle self._spawn_vehicle(60, spawn_probability=1, go_straight=True, position_deviation=0.1, speed_deviation=0) # Ego-vehicle MDPVehicle.SPEED_MIN = 0 MDPVehicle.SPEED_MAX = 9 MDPVehicle.SPEED_COUNT = 3 # MDPVehicle.TAU_A = 1.0 ego_lane = self.road.network.get_lane(("o0", "ir0", 0)) destination = self.config["destination"] or "o" + str( self.np_random.randint(1, 4)) ego_vehicle = self.action_type.vehicle_class( self.road, ego_lane.position(60, 0), speed=ego_lane.speed_limit, heading=ego_lane.heading_at(50)) \ .plan_route_to(destination) self.road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle for v in self.road.vehicles: # Prevent early collisions if v is not ego_vehicle and np.linalg.norm( v.position - ego_vehicle.position) < 20: self.road.vehicles.remove(v)
def _make_vehicles(self, n_vehicles=10): """ Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle. :return: the ego-vehicle """ # Configure vehicles vehicle_type = utils.class_from_path(self.config["other_vehicles_type"]) vehicle_type.DISTANCE_WANTED = 2 # Low jam distance vehicle_type.COMFORT_ACC_MAX = 6 vehicle_type.COMFORT_ACC_MIN = -3 # Random vehicles simulation_steps = 3 for t in range(n_vehicles - 1): self._spawn_vehicle(np.linspace(0, 80, n_vehicles)[t]) for _ in range(simulation_steps): [(self.road.act(), self.road.step(1 / self.SIMULATION_FREQUENCY)) for _ in range(self.SIMULATION_FREQUENCY)] # Challenger vehicle route = self.config['route'] if self.config['route'] is None: route_to = self.np_random.choice([0,2,3], size=1, replace=False) route = [1, route_to[0]] for i in range(self.config["challenge_vel"]): self._spawn_vehicle(97 - 20*i, spawn_probability=1, go_straight=True, position_deviation=0, velocity_deviation=0, route=route, velocity_init=8) # Ego-vehicle MDPVehicle.SPEED_MIN = 0 MDPVehicle.SPEED_MAX = 9 MDPVehicle.SPEED_COUNT = 3 # MDPVehicle.TAU_A = 1.0 ego_lane = self.road.network.get_lane(("o0", "ir0", 0)) destination = self.config["destination"] or "o" + str(self.np_random.randint(1, 4)) # ego_vehicle = Vehicle(self.road, ego_lane.position(100, 0), # velocity=ego_lane.speed_limit*0.0, # heading=ego_lane.heading_at(50)) ego_vehicle = ControlledLowLevelVehicle(self.road, ego_lane.position(100, 0), velocity=ego_lane.speed_limit, heading=ego_lane.heading_at(50)).plan_route_to(destination) self.road.vehicles.append(ego_vehicle) self.vehicle = ego_vehicle for v in self.road.vehicles: # Prevent early collisions if v is not ego_vehicle and np.linalg.norm(v.position - ego_vehicle.position) < 20: self.road.vehicles.remove(v)
def change_vehicles(self, vehicle_class_path): """ Change the type of all vehicles on the road :param vehicle_class_path: The path of the class of behavior for other vehicles Example: "highway_env.vehicle.behavior.IDMVehicle" :return: a new environment with modified behavior model for other vehicles """ vehicle_class = utils.class_from_path(vehicle_class_path) env_copy = copy.deepcopy(self) vehicles = env_copy.road.vehicles for i, v in enumerate(vehicles): if v is not env_copy.vehicle and not isinstance(v, Obstacle): vehicles[i] = vehicle_class.create_from(v) return env_copy
def _create_road(self): road = Road.create_random_road( lanes_count=self.config["lanes_count"], vehicles_count=self.config["vehicles_count"], vehicles_type=utils.class_from_path( self.config["other_vehicles_type"]), np_random=self.np_random) vehicle = MDPVehicle.create_random( road, 25, spacing=self.config["initial_spacing"], prepend=True, np_random=self.np_random) road.vehicles.append(vehicle) return road, vehicle
def _spawn_vehicles(self): if self.np_random.rand() < 1 - 0.6: return position_deviation = 1 velocity_deviation = 1 route = self.np_random.choice(range(4), size=2, replace=False) other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) vehicle = other_vehicles_type.make_on_lane( self.road, ("o" + str(route[0]), "ir" + str(route[0]), 0), longitudinal=5 + self.np_random.randn() * position_deviation, velocity=8 + self.np_random.randn() * velocity_deviation) for v in self.road.vehicles: if np.linalg.norm(v.position - vehicle.position) < 15: return vehicle.plan_route_to("o" + str(route[1])) vehicle.randomize_behavior() self.road.vehicles.append(vehicle)
def _create_vehicles(self): """ Create some new random vehicles of a given type, and add them on the road. """ # target AV self.target_vehicle = MDPVehicle.create_random( self.road, 25, spacing=self.config["initial_spacing"]) self.target_vehicle.color = (200, 0, 150) # purple self.road.vehicles.append(self.target_vehicle) # attacker: training agent self.vehicle = MDPVehicle.create_random( self.road, 25, spacing=self.config["initial_spacing"]) self.road.vehicles.append(self.vehicle) vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) for _ in range(self.config["vehicles_count"]): self.road.vehicles.append(vehicles_type.create_random(self.road))
def _make_vehicles(self) -> None: road = self.road #add random ego vechicle random_es = 4.5 * np.random.randint(-1, 1) + 6 ego_vehicle = self.action_type.vehicle_class(road, road.network.get_lane( ("m", "n", 0)).position(60, 0), speed=random_es) road.vehicles.append(ego_vehicle) #try: # ego_vehicle.plan_route_to("d") #except AttributeError: # pass self.vehicle = ego_vehicle other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) spead = np.random.randint(-1, 1) other_vehicle1 = other_vehicles_type(road, road.network.get_lane( ("a", "b", 0)).position(70, 0), speed=6 + (4 * spead)) other_vehicle2 = other_vehicles_type(road, road.network.get_lane( ("a", "b", 0)).position(55, 0), speed=5 + (3 * spead)) other_vehicle3 = other_vehicles_type(road, road.network.get_lane( ("a", "b", 0)).position(35, 0), speed=5 + (3 * spead)) #other_vehicle3 = other_vehicles_type(road, road.network.get_lane(("b", "d", 0)).position(0, 0),speed=4 + (3 * spead)) road.vehicles.append(other_vehicle1) road.vehicles.append(other_vehicle2) road.vehicles.append(other_vehicle3) other_vehicle1.plan_route_to("e") other_vehicle2.plan_route_to("e") other_vehicle3.plan_route_to("e")
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" path = "highway_env.vehicle.derby.DerbyCar" vehicle_class = utils.class_from_path(path) self.controlled_vehicles = [] for i in range(self.config["controlled_vehicles"]): XPos = self.np_random.rand() * self.config[ "derby_radius"] * 1.5 - self.config["derby_radius"] * .75 YPos = self.np_random.rand() * self.config[ "derby_radius"] * 1.5 - self.config["derby_radius"] * .75 Heading = 2 * np.pi * self.np_random.rand() Speed = 0. vehicle = self.action_type.vehicle_class(road=self.road, position=np.array( [XPos, YPos]), heading=Heading, speed=Speed) vehicle = vehicle_class.create_from(vehicle) self.road.vehicles.append(vehicle) self.controlled_vehicles.append(vehicle)
def _create_vehicles(self) -> None: """Create some new random vehicles of a given type, and add them on the road.""" other_vehicles_type = utils.class_from_path( self.config["other_vehicles_type"]) other_per_controlled = near_split( self.config["vehicles_count"], num_bins=self.config["controlled_vehicles"]) self.controlled_vehicles = [] t = 0 l = 1 for others in other_per_controlled: # controlled_vehicle = self.action_type.vehicle_class.create_random( # self.road, # amir=False, # speed=np.random.randint(23,29), # lane_id=1, # spacing=self.config["ego_spacing"] # ) flags = [False, True] + [False for i in range(others - 1)] # array=[t,None,np.random.randint(23,25)] for i in range(others + 1): if i == t: controlled_vehicle = self.action_type.vehicle_class.create_random( self.road, amir=False, speed=np.random.randint(23, 29), lane_id=1, spacing=self.config["ego_spacing"]) self.controlled_vehicles.append(controlled_vehicle) self.road.vehicles.append(controlled_vehicle) else: back = False speed = np.random.randint(23, 29) self.road.vehicles.append( other_vehicles_type.create_random( self.road, amir=flags[i], speed=speed, spacing=1 / self.config["vehicles_density"]))