コード例 #1
0
ファイル: test_control.py プロジェクト: blf11139/highway-env
def test_step():
    v = ControlledVehicle(road=None, position=[0, 0], velocity=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.velocity == pytest.approx(20)
    assert v.heading == pytest.approx(0)
コード例 #2
0
ファイル: test_road.py プロジェクト: sebastopol06/highway-env
def test_network():
    # Diamond
    net = RoadNetwork()
    net.add_lane(0, 1, StraightLane([0, 0], [10, 0]))
    net.add_lane(1, 2, StraightLane([10, 0], [5, 5]))
    net.add_lane(2, 0, StraightLane([5, 5], [0, 0]))
    net.add_lane(1, 3, StraightLane([10, 0], [5, -5]))
    net.add_lane(3, 0, StraightLane([5, -5], [0, 0]))
    print(net.graph)

    # Road
    road = Road(network=net)
    v = ControlledVehicle(road, [5, 0], heading=0, target_velocity=2)
    road.vehicles.append(v)
    assert v.lane_index == (0, 1, 0)

    # Lane changes
    dt = 1 / 15
    lane_index = v.target_lane_index
    lane_changes = 0
    for _ in range(int(20 / dt)):
        road.act()
        road.step(dt)
        if lane_index != v.target_lane_index:
            lane_index = v.target_lane_index
            lane_changes += 1
    assert lane_changes == 3
コード例 #3
0
ファイル: test_control.py プロジェクト: blf11139/highway-env
def test_velocity_control():
    road = Road(RoadNetwork.straight_road_network(1))
    v = ControlledVehicle(road=road,
                          position=road.network.get_lane(
                              (0, 1, 0)).position(0, 0),
                          velocity=20,
                          heading=0)
    v.act('FASTER')
    for _ in range(int(3 * v.TAU_A * FPS)):
        v.act()
        v.step(dt=1 / FPS)
    assert v.velocity == pytest.approx(20 + v.DELTA_VELOCITY, abs=0.5)
    assert v.position[1] == pytest.approx(0)
    assert v.lane_index[2] == 0
コード例 #4
0
ファイル: test_control.py プロジェクト: blf11139/highway-env
def test_lane_change():
    road = Road(RoadNetwork.straight_road_network(2))
    v = ControlledVehicle(road=road,
                          position=road.network.get_lane(
                              (0, 1, 0)).position(0, 0),
                          velocity=20,
                          heading=0)
    v.act('LANE_RIGHT')
    for _ in range(3 * FPS):
        v.act()
        v.step(dt=1 / FPS)
    assert v.velocity == pytest.approx(20)
    assert v.position[1] == pytest.approx(StraightLane.DEFAULT_WIDTH,
                                          abs=StraightLane.DEFAULT_WIDTH / 4)
    assert v.lane_index[2] == 1
コード例 #5
0
    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
        """

        max_l = 300
        road = self.road
        other_vehicles_type = utils.class_from_path(
            self.config["other_vehicles_type"])
        car_number_each_lane = 2
        # reset_position_range = (20,40)
        # reset_lane = random.choice(road.lanes)
        reset_lane = ("s2", "ee", 0)
        ego_vehicle = None
        birth_place = [("s1", "ex", 0), ("s1", "ex", 1), ("s1", "ex", 2),
                       ("s2", "ee", 0)]
        destinations = ["x1"]
        position_deviation = 10
        velocity_deviation = 2
        for l in self.road.network.LANES:
            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(1, car_number_each_lane))
            for i in range(cars_on_lane):
                if i == reset_position and not ego_vehicle:
                    ego_lane = self.road.network.get_lane(("s2", "ee", 0))
                    ego_vehicle = ControlledVehicle(
                        self.road,
                        ego_lane.position(0, 0),
                        velocity=20,
                        heading=ego_lane.heading_at(0)).plan_route_to("x1")
                    ego_vehicle.id = 0
                    road.vehicles.append(ego_vehicle)
                    self.vehicle = ego_vehicle
                else:
                    car = other_vehicles_type.make_on_lane(
                        road,
                        birth_place[np.random.randint(0, 4)],
                        longitudinal=5 +
                        np.random.randint(1, 7) * position_deviation,
                        velocity=5 +
                        np.random.randint(1, 5) * velocity_deviation)
                    destination = destinations[0]
                    # print("destination:",destination)
                    car.plan_route_to(destination)
                    car.randomize_behavior()
                    road.vehicles.append(car)
                    lane.vehicles.append(car)
        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