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)
예제 #2
0
def test_front():
    r = Road(RoadNetwork.straight_road_network(1))
    v1 = Vehicle(road=r, position=[0, 0], speed=20)
    v2 = Vehicle(road=r, position=[10, 0], speed=10)
    r.vehicles.extend([v1, v2])

    assert v1.lane_distance_to(v2) == pytest.approx(10)
    assert v2.lane_distance_to(v1) == pytest.approx(-10)
예제 #3
0
    def _create_vehicles(self):
        """
            Create some new random vehicles of a given type, and add them on the road.
        """
        self.vehicle = Vehicle(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 = Obstacle(self.road, lane.position(lane.length/2, 0), heading=lane.heading)

        self.goal.COLLISIONS_ENABLED = False
        self.road.vehicles.insert(0, self.goal)

        lane_index = self.road.network.get_closest_lane_index(lane.position(lane.length/2, 0))
        side_lanes = [self.road.network.get_lane(side_lane_index) for side_lane_index in self.road.network.side_lanes(lane_index)]
        i = 1
        self.obstacles_features = []
        for side_lane in side_lanes:
            obstacle = Obstacle(self.road, side_lane.position(side_lane.length/2, 0), heading=side_lane.heading)
            obstacle.COLLISIONS_ENABLED = True
            self.road.vehicles.insert(i, obstacle)
            i += 1
            obstacle_feature = np.array([obstacle.position[0], obstacle.position[1], 0, 0, 
                math.cos(obstacle.heading), math.sin(obstacle.heading)]) / self.config['observation']['scales']
            self.obstacles_features.append(obstacle_feature)
예제 #4
0
def test_step():
    v = Vehicle(road=None, position=[0, 0], speed=20, heading=0)
    for _ in range(2 * FPS):
        v.step(dt=1 / FPS)
    assert v.position[0] == pytest.approx(40)
    assert v.position[1] == pytest.approx(0)
    assert v.speed == pytest.approx(20)
    assert v.heading == pytest.approx(0)
예제 #5
0
def test():
    from highway_env.vehicle.kinematics import Vehicle
    r = None
    v = Vehicle(r, [0, 0], 0, 20)
    v.dump()
    v.dump()
    v.dump()
    v.dump()
    print(v.get_log())
예제 #6
0
def test_brake():
    v = Vehicle(road=None, position=[0, 0], velocity=20, heading=0)
    for _ in range(10 * FPS):
        v.act({
            'acceleration': min(max(-1 * v.velocity, -6), 6),
            'steering': 0
        })
        v.step(dt=1 / FPS)
    assert v.velocity == pytest.approx(0, abs=0.01)
    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)
예제 #8
0
def test_act():
    v = Vehicle(road=None, position=[0, 0], speed=20, heading=0)
    v.act({'acceleration': 1, 'steering': 0})
    for _ in range(1 * FPS):
        v.step(dt=1 / FPS)
    assert v.speed == pytest.approx(21)

    v.act({'acceleration': 0, 'steering': 0.5})
    for _ in range(1 * FPS):
        v.step(dt=1 / FPS)
    assert v.speed == pytest.approx(21)
    assert v.position[1] > 0
예제 #9
0
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
    def _create_vehicles(self) -> None:
        """
        Create some new random vehicles of a given type, and add them on the road.
        """
        self.vehicle = Vehicle(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)
예제 #11
0
    def _create_vehicles(self):
        """
            Create some new random vehicles of a given type, and add them on the road.
        """
        self.vehicle = Vehicle(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 = Obstacle(self.road,
                             lane.position(lane.length / 2, 0),
                             heading=lane.heading)
        self.goal.COLLISIONS_ENABLED = False
        self.road.obstacles.append(self.goal)
예제 #12
0
    def _create_vehicles(self, parked_probability=0.75):
        """
            Create some new random vehicles of a given type, and add them on the road.
        """

        self.vehicle = Vehicle(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 = Obstacle(self.road, goal_position, heading=0)
        self.goal.COLLISIONS_ENABLED = False
        self.road.vehicles.insert(0, 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,
                                               velocity=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, velocity=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)