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, 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 _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) 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 i in range(self.config["controlled_vehicles"]): if self.heading_ego is not None: if self.position_ego is not None: vehicle = self.action_type.vehicle_class( self.road, [self.position_ego[0], self.position_ego[1]], 2 * np.pi * self.heading_ego, 0) else: vehicle = self.action_type.vehicle_class( self.road, [i * 20, 0], 2 * np.pi * self.heading_ego, 0) else: if self.position_ego is not None: vehicle = self.action_type.vehicle_class( self.road, [self.position_ego[0], self.position_ego[1]], 2 * np.pi * self.np_random.rand(), 0) else: 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) if self.parked_vehicles_lane_indices is not None: for i in range(len(self.parked_vehicles_lane_indices)): lane_idx = self.parked_vehicles_lane_indices[i] assert 0 <= lane_idx < len( self.road.network.lanes_list()), 'Invalid lane num' assert lane_idx != self.goal_lane_idx, 'Parked vehicle cannot be in the target position' lane = self.road.network.lanes_list()[lane_idx] vehicle = self.action_type.vehicle_class( road=self.road, position=lane.position(lane.length / 2, 0), heading=lane.heading, speed=0) self.road.vehicles.append(vehicle) if self.goal_lane_idx is not None: assert 0 <= self.goal_lane_idx < len( self.road.network.lanes_list()), 'Invalid lane num' lane = self.road.network.lanes_list()[self.goal_lane_idx] else: 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 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