def _create_vehicles(self) -> None: self.vehicle = Vehicle(self.road, [0, 0], 2 * np.pi * self.np_random.rand(), 0) self.road.vehicles.append(self.vehicle) """ positions = self.np_random.choice(self.road.network.lanes_list(), size=car_count+1, replace=False) for x in range(car_count): lane2 = positions[x] vehicle2 = Vehicle(self.road, lane2.position(lane2.length/2, 0), lane2.heading, 0) self.road.vehicles.append(vehicle2) lane = positions[-1] self.goal = Landmark(self.road, lane.position(lane.length/2, 0), heading=lane.heading) self.road.objects.append(self.goal) """ """ add vehicels to free parking slots """ spots = len(self.road.network.lanes_list()) lane_pos = self.np_random.choice(spots) lane = self.road.network.lanes_list()[lane_pos] self.goal = Landmark(self.road, lane.position(lane.length / 2, 0), heading=lane.heading) self.road.objects.append(self.goal) for x in range(spots): if x != lane_pos: lane_tmp = self.road.network.lanes_list()[x] vehicle_tmp = Vehicle( self.road, lane_tmp.position(lane_tmp.length / 2, 0), lane_tmp.heading, 0) self.road.vehicles.append(vehicle_tmp)
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(self.road, [0, 0], 2*np.pi*self.np_random.rand(), 0) self.road.vehicles.append(self.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, 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: self.vehicle = Vehicle(self.road, [0, 0], 2 * np.pi * self.np_random.rand(), 0) self.road.vehicles.append(self.vehicle) positions = self.np_random.choice(self.road.network.lanes_list(), size=car_count + 1, replace=False) for x in range(car_count): lane2 = positions[x] vehicle2 = Vehicle(self.road, lane2.position(lane2.length / 2, 0), lane2.heading, 0) self.road.vehicles.append(vehicle2) lane = positions[-1] 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 assert v1.speed == v2.speed == 10 # 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 assert v3.speed == 0 # 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