Example #1
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
        """
        road = self.road
        main_init_xpos = 215
        auxi_init_xpos = 215
        init_interval = 20
        road.vehicles = []
        for i in range(self.leader_num):
            velocity = np.random.uniform(38, 40)
            road.vehicles.append(
                MDPVehicle(road,
                           road.network.get_lane(
                               ("a", "b", 0)).position(main_init_xpos, 0),
                           index=i,
                           velocity=velocity))
            main_init_xpos += init_interval

        for i in range(self.follower_num):
            velocity = np.random.uniform(38, 40)
            road.vehicles.append(
                MDPVehicle(road,
                           road.network.get_lane(
                               ("j", "k", 0)).position(auxi_init_xpos, 0),
                           index=i + self.leader_num,
                           velocity=velocity))
            auxi_init_xpos += init_interval

        self.vehicle = road.vehicles[0]
        road.leader_num = self.leader_num
        road.follower_num = self.follower_num
Example #2
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
        """
        road = self.road
        leader_v_one = MDPVehicle(road,
                                  road.network.get_lane(
                                      ("a", "b", 0)).position(100, 0),
                                  velocity=30)
        road.vehicles.append(leader_v_one)
        self.all_vehicles.append(leader_v_one)

        leader_v_two = MDPVehicle(road,
                                  road.network.get_lane(
                                      ("a", "b", 0)).position(80, 0),
                                  velocity=30)
        road.vehicles.append(leader_v_two)
        self.all_vehicles.append(leader_v_two)

        #test_vehicle = MDPVehicle(road, road.network.get_lane(("a", "b", 1)).position(50, 0), velocity=30)
        #road.vehicles.append(test_vehicle)
        #self.all_vehicles.append(test_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))
        #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_one = MDPVehicle(road,
                                   road.network.get_lane(
                                       ("j", "k", 0)).position(110, 0),
                                   velocity=30)
        #merging_v_one.target_velocity = 30
        road.vehicles.append(merging_v_one)

        self.vehicle = leader_v_one

        merging_v_two = MDPVehicle(road,
                                   road.network.get_lane(
                                       ("j", "k", 0)).position(90, 0),
                                   velocity=30)
        #merging_v_two.target_velocity = 30
        road.vehicles.append(merging_v_two)

        self.all_vehicles.append(merging_v_one)
        self.all_vehicles.append(merging_v_two)
Example #3
0
    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)
Example #4
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
        """

        # 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):
        """
            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)
Example #6
0
 def _make_vehicles(self, background_vehicle=None,
                    auto_vehicle=(0, 50, 24)):
     """
         Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle.
     :return: the ego-vehicle
     background_vehicle: Each item has [lane,position,velocity]
     """
     road = self.road
     ego_vehicle = MDPVehicle(road, road.network.get_lane(("a", "b", auto_vehicle[0])).position(auto_vehicle[1], 0),
                              velocity=auto_vehicle[2])
     road.vehicles.append(ego_vehicle)
     if self.mode == "ACC":
         other_vehicles_type = MDPVehicle
     elif self.mode == "NDD":
         other_vehicles_type = IDMVehicle
     else:
         raise NotImplementedError("Not supported mode")
     for lane_id in range(3):
         if lane_id != 0:
             position_tmp = np.random.uniform(low=0, high=100)
             velocity_tmp = 30
             road.vehicles.append(
                 other_vehicles_type(road, road.network.get_lane(("a", "b", lane_id)).position(position_tmp, 0),
                                     velocity=velocity_tmp))
         else:
             position_tmp = auto_vehicle[1]
             velocity_tmp = auto_vehicle[2]
         position_tmp = position_tmp + 50
         velocity_tmp = 20
         road.vehicles.append(
             other_vehicles_type(road, road.network.get_lane(("a", "b", lane_id)).position(position_tmp, 0),
                                 velocity=velocity_tmp))
     self.vehicle = ego_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
Example #8
0
def main():
    env = gym.make('highway-v0')
    road = Road.create_random_road(lanes_count=2,
                                   lane_width=4.0,
                                   vehicles_count=0,
                                   vehicles_type=IDMVehicle)
    vehicle = MDPVehicle.create_random(road)
    road.vehicles.append(vehicle)
    env.road = road
    env.vehicle = vehicle

    agent = MCTSAgent(env, iterations=100, temperature=20 *
                      5)  # compare step by subtree and step by prior
    sim = Simulation(env, agent)

    t = 0
    while not sim.done:
        sim.step()
        t += 1
        if t == 10:
            print('Added obstacle')
            env.road.vehicles.append(
                Obstacle(
                    road,
                    [env.vehicle.position[0] + 50., env.vehicle.position[1]]))
    sim.close()
    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)
Example #10
0
    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))
Example #11
0
    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)

        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))
Example #12
0
 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
Example #13
0
    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
        v = self._spawn_vehicle(60,
                                spawn_probability=1,
                                go_front=True,
                                position_deviation=0.1,
                                velocity_deviation=0)
        if v:
            v.color = (255, 0, 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(4))
        ego_vehicle = MDPVehicle(self.road,
                                 ego_lane.position(60, 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 _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(("o0", "ir0", 0))
     ego_vehicle = MDPVehicle(self.road,
                              ego_lane.position(0, 0),
                              velocity=9,
                              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
Example #15
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
        """
        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
Example #16
0
    def _make_vehicles(self, n_vehicles=10):
        """
            Populate a road with several vehicles on the highway and on the merging lane, as well as multiple 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
        # self._spawn_vehicle(60, spawn_probability=1, go_straight=True, position_deviation=0.1, velocity_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))
        ego_lane = self.road.network.get_lane(("o0", "ir0", 0))
        # destination = self.config["destination"] or "o" + str(self.np_random.randint(1, 4))
        destination = "o1"
#         ego_lane.position(max(0, np.random.normal(10, 10)), 0),
        ego_vehicle1 = MDPVehicle(self.road,
                                 ego_lane.position(max(0, np.random.normal(10, 5)), 0),
                                 velocity=ego_lane.speed_limit,
                                 heading=ego_lane.heading_at(50)) \
            .plan_route_to(destination)
        self.road.vehicles.append(ego_vehicle1)

        ego_lane = self.road.network.get_lane(("o1", "ir1", 0))
        # destination = self.config["destination"] or "o" + str(self.np_random.randint(1, 4))
        destination = "o2"
        ego_vehicle2 = MDPVehicle(self.road,
                                 ego_lane.position(max(0, np.random.normal(10, 5)), 0),
                                 velocity=ego_lane.speed_limit,
                                 heading=ego_lane.heading_at(50)) \
            .plan_route_to(destination)
        self.road.vehicles.append(ego_vehicle2)

        ego_lane = self.road.network.get_lane(("o2", "ir2", 0))
        # destination = self.config["destination"] or "o" + str(self.np_random.randint(1, 4))
        destination = "o3"
        ego_vehicle3 = MDPVehicle(self.road,
                                 ego_lane.position(max(0, np.random.normal(10, 5)), 0),
                                 velocity=ego_lane.speed_limit,
                                 heading=ego_lane.heading_at(50)) \
            .plan_route_to(destination)
        self.road.vehicles.append(ego_vehicle3)

        ego_lane = self.road.network.get_lane(("o3", "ir3", 0))
        # destination = self.config["destination"] or "o" + str(self.np_random.randint(1, 4))
        destination = "o0"
        ego_vehicle4 = MDPVehicle(self.road,
                                 ego_lane.position(max(0, np.random.normal(10, 5)), 0),
                                 velocity=ego_lane.speed_limit,
                                 heading=ego_lane.heading_at(50)) \
            .plan_route_to(destination)
        self.road.vehicles.append(ego_vehicle4)
        
        self.vehicle = [ego_vehicle1, ego_vehicle2, ego_vehicle3, ego_vehicle4]
Example #17
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
        """
        """
        road = self.road
        other_vehicles_type = utils.class_from_path(self.config["other_vehicles_type"])
        car_number_each_lane = 5
        # reset_position_range = (30, 40)
        reset_position_range = (100, 0)
        # reset_lane = random.choice(road.lanes)

        for l in road.lanes[:3]:
            reset_lane = road.lanes[2]
            cars_on_lane = car_number_each_lane
            reset_position = (32, 0)
            if l is reset_lane:
                cars_on_lane += 1
                # reset_position = random.choice(range(5, 6))

        # github-version
        position_deviation = 2
        velocity_deviation = 2

        # Ego-vehicle
        # ego_lane = self.road.network.get_lane(("ser", "ses", 0))
        ego_lane = self.road.get_lane((100, 0))
        print("\nego_lane.position:", ego_lane.position(140, 0), "\n")
        # ego_vehicle = MDPVehicle(self.road,
        # ego_lane.position(140, 0),
        # velocity=5,
        # heading=ego_lane.heading_at(140)).plan_route_to("nxs")
        ego_vehicle = IDMVehicle(road, (60, 0), np.pi / 2, velocity=10, max_length=500)
        # 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,
                                                   (0, 30),
                                                   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,
                                                       (30 * i, 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)
    
        vehicle = MDPVehicle.create_random(self.road, 25, spacing=3, np_random=self.np_random)
        self.road.vehicles.append(vehicle)
        for v in self.road.vehicles:
            lane_index = v.lane_index
            self.road.lanes[lane_index].vehicles.append(v)
        """
        position_deviation = 5
        velocity_deviation = 1.5
        other_vehicles_mandatory = False
        max_l = 700
        road = self.road
        other_vehicles_type = utils.class_from_path(
            self.config["other_vehicles_type"])
        car_number_each_lane = 1
        # reset_position_range = (30, 40)
        # reset_lane = random.choice(road.lanes)
        reset_lane = ("ser", "ses", 0)
        ego_vehicle = None
        num = 0

        print("lanes:", self.road.network.LANES, "\n")
        # self.road.network.LANES_NUMBER = 28
        for l in self.road.network.LANES:
            # print("l:",l,"\n")
            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(0, 3))
                # reset_position = 2
            for j in range(cars_on_lane):
                if not ego_vehicle:
                    ego_lane = self.road.network.get_lane(("ser", "ses", 0))
                    if self.switch:
                        ego_vehicle = MDPVehicle(self.road,
                                                 ego_lane.position(110, 0),
                                                 velocity=15,
                                                 heading=ego_lane.heading_at(
                                                     110)).plan_route_to("nxs")
                    else:
                        ego_vehicle = IDMVehicle(self.road,
                                                 ego_lane.position(110, 0),
                                                 velocity=15,
                                                 heading=ego_lane.heading_at(
                                                     110)).plan_route_to("wxs")
                        # ego_vehicle.destination = 1
                        ego_vehicle.id = 0
                    self.road.vehicles.append(ego_vehicle)
                    self.vehicle = ego_vehicle
                    lane.vehicles.append(ego_vehicle)
                else:
                    destinations = ["wxr", "sxr", "nxr", "exr"]
                    birth_place = [("ee", "nx", 0), ("ne", "wx", 0),
                                   ("we", "sx", 0), ("se", "ex", 0),
                                   ("ee", "nx", 1), ("ne", "wx", 1),
                                   ("we", "sx", 1), ("se", "ex", 1),
                                   ("wer", "wes", 0), ("eer", "ees", 0),
                                   ("ner", "nes", 0), ("ser", "ses", 0),
                                   ("wxs", "wxr", 0), ("exs", "exr", 0),
                                   ("nxs", "nxr", 0), ("sxs", "sxr", 0)]
                    car = other_vehicles_type.make_on_lane(
                        self.road,
                        birth_place[np.random.randint(0, 16)],
                        longitudinal=70 +
                        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, 4)]
                        # destination = self.np_random.choice(destinations)
                    """
                    if  0<= num <=6:
                        destinations = ["wxr", "sxr", "nxr"]
                        car = other_vehicles_type.make_on_lane(self.road, ("ee", "nx", 1),
                                                               longitudinal=5 + self.np_random.randn() * position_deviation,
                                                               velocity=5 + 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)
                        # if other_vehicles_mandatory:
                        # car.destination = 1
                        # car.plan_route_to(destination)
                        # car.randomize_behavior()
                        # self.road.vehicles.append(car)
                        # road.vehicles.append(car)
                        # lane.vehicles.append(car)
                    elif 7<=num<=13:
                        destinations = ["exr", "sxr", "wxr"]
                        car = other_vehicles_type.make_on_lane(self.road, ("ne", "wx", 1),
                                                               longitudinal=5 + self.np_random.randn() * position_deviation,
                                                               velocity=5 + 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)
                        # if other_vehicles_mandatory:
                        # car.destination = 1
                        # car.plan_route_to(destination)
                        # car.randomize_behavior()
                        # self.road.vehicles.append(car)
                        # road.vehicles.append(car)
                        # lane.vehicles.append(car)

                    elif 14<=num<=20:
                        destinations = ["exr", "sxr", "nxr"]
                        car = other_vehicles_type.make_on_lane(self.road, ("we", "sx", 1),
                                                               longitudinal=5 + self.np_random.randn() * position_deviation,
                                                               velocity=5 + 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)
                        # if other_vehicles_mandatory:
                        # car.destination = 1
                        # car.plan_route_to(destination)
                        # car.randomize_behavior()
                        # self.road.vehicles.append(car)
                        # road.vehicles.append(car)
                        # lane.vehicles.append(car)
                    else :
                        destinations = ["exr", "wxr", "nxr"]
                        car = other_vehicles_type.make_on_lane(self.road, ("se", "ex", 1),
                                                               longitudinal=5 + self.np_random.randn() * position_deviation,
                                                               velocity=5 + 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)
                        # if other_vehicles_mandatory:
                        # car.destination = 1
                        # car.plan_route_to(destination)
                        # car.randomize_behavior()
                        # self.road.vehicles.append(car)
                        # road.vehicles.append(car)
                        # lane.vehicles.append(car)
                    """
                    car.plan_route_to(destination)
                    car.randomize_behavior()
                    self.road.vehicles.append(car)
                    # road.vehicles.append(car)
                    lane.vehicles.append(car)
                    # if other_vehicles_mandatory:
                    # car.destination = 1
                    # road.vehicles.append(car)
                    # l.vehicles.append(car)
            num += 1
        # print("make_vehicle finish")

        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
    def _make_vehicles(self,
                       given_ini=None,
                       auto_vehicle=(0, global_val.initial_CAV_position,
                                     global_val.initial_CAV_speed)):
        """
            Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle.
        :return: the ego-vehicle
        given_ini: [[Lane 0], [Lane 1], [Lane 2]], Each item [Lane i] = [[x,velocity],..., [x,velocity]]
        """
        ego_vehicle = MDPVehicle(self.road,
                                 self.road.network.get_lane(
                                     ("a", "b", auto_vehicle[0])).position(
                                         auto_vehicle[1], 0),
                                 velocity=auto_vehicle[2])

        # ego_vehicle = IDMVehicle_CAV(self.road, self.road.network.get_lane(("a", "b", auto_vehicle[0])).position(auto_vehicle[1], 0),
        #                          velocity=auto_vehicle[2])
        # print("aaaa", ego_vehicle.COMFORT_ACC_MAX)

        self.road.vehicles.append(ego_vehicle)
        self.vehicle = ego_vehicle
        other_vehicles_type = Pure_NDDVehicle
        ini_data = None
        if given_ini:
            assert self.generate_vehicle_mode == "Given_ini"
        if self.generate_vehicle_mode == "Test":
            if len(self.presum_list_forward) == 0 or len(
                    self.presum_list_backward) == 0:
                # If it is the first time generate by NDD, preprocess the data first
                self._preprocess_CF_data()
                print(
                    "================Generate CF data presum_list finished!================"
                )
            lane_list = [0, 1, 2, 2]  #[1, 1, 1, 2, 2, 2]  # [1, 1, 1, 2, 2, 2]
            position_list = [
                412, 430, 420, 440
            ]  #, 105, 120, 90, 105, 120]  # [90, 105, 120, 90, 105, 120]
            velocity_list = [
                20, 26, 30, 30
            ]  #, 32, 28, 36, 34, 32] # [34, 32, 28, 36, 34, 32]
            for i in range(len(lane_list)):
                v = other_vehicles_type(
                    self.road,
                    self.road.network.get_lane(
                        ("a", "b",
                         lane_list[i])).position(position_list[i], 0), 0,
                    velocity_list[i])
                self.road.vehicles.append(v)

        if self.generate_vehicle_mode == "NDD":
            if len(self.presum_list_forward) == 0 or len(
                    self.presum_list_backward) == 0:
                # If it is the first time generate by NDD, preprocess the data first
                self._preprocess_CF_data()
                print(
                    "================Generate CF data presum_list finished!================"
                )

            vehicle_list = [
            ]  # each list in this container is for vehicles in each lane (without CAV)
            lane_num = len(self.vehicle.road.network.graph["a"]["b"])
            for lane_idx in range(lane_num):
                generate_forward = True
                generate_finish = False
                vehicle_forward_list_one_lane, vehicle_backward_list_one_lane = [], []
                if lane_idx == 0:
                    back_vehicle_speed, front_vehicle_speed = auto_vehicle[
                        2], auto_vehicle[2]
                    back_vehicle_position, front_vehicle_position = auto_vehicle[
                        1], auto_vehicle[1]
                else:
                    rand_speed, rand_position, _ = self._gen_NDD_veh()
                    back_vehicle_speed, front_vehicle_speed = rand_speed, rand_speed
                    back_vehicle_position, front_vehicle_position = rand_position, rand_position
                    vehicle_forward_list_one_lane.append(
                        (rand_position, rand_speed))

                while generate_finish is False:
                    if generate_forward is True:
                        # print(back_vehicle_speed)
                        if back_vehicle_speed < global_val.v_low:
                            presum_list = self.presum_list_forward[
                                global_val.v_to_idx_dic[global_val.v_low]]
                        else:
                            presum_list = self.presum_list_forward[
                                global_val.v_to_idx_dic[int(
                                    back_vehicle_speed)]]

                        # decide CF or FF
                        random_number_CF = np.random.uniform()
                        if random_number_CF > self.CF_percent:  # FF
                            rand_speed, rand_position, _ = self._gen_NDD_veh(
                            )  # self._gen_one_random_veh()
                            v_generate = rand_speed
                            pos_generate = back_vehicle_position + self.ff_dis + rand_position + global_val.LENGTH

                        else:  # CF
                            random_number = np.random.uniform()
                            r_idx, rr_idx = divmod(
                                bisect.bisect_left(presum_list, random_number),
                                self.num_rr)
                            try:
                                r, rr = global_val.r_to_idx_dic.inverse[
                                    r_idx], global_val.rr_to_idx_dic.inverse[
                                        rr_idx]
                            except:
                                # print("back_vehicle_speed:", back_vehicle_speed)
                                if back_vehicle_speed > 35:
                                    r, rr = 50, -2
                                else:
                                    r, rr = 50, 2

                            # Accelerated training for initialization
                            r = r - global_val.Initial_range_adjustment_AT + global_val.Initial_range_adjustment_SG
                            if r <= global_val.Initial_range_adjustment_SG:
                                r = r + global_val.r_high

                            v_generate = back_vehicle_speed + rr
                            pos_generate = back_vehicle_position + r + global_val.LENGTH

                        vehicle_forward_list_one_lane.append(
                            (pos_generate, v_generate))
                        back_vehicle_speed = v_generate
                        back_vehicle_position = pos_generate

                        v = other_vehicles_type(
                            self.road,
                            self.road.network.get_lane(
                                ('a', 'b',
                                 lane_idx)).position(pos_generate, 0), 0,
                            v_generate)
                        self.road.vehicles.append(v)

                        if back_vehicle_position >= self.gen_length:
                            generate_forward = False
                            generate_finish = True
                vehicle_list_each_lane = vehicle_backward_list_one_lane + vehicle_forward_list_one_lane
                vehicle_list.append(vehicle_list_each_lane)

        if self.generate_vehicle_mode == "Given_ini":
            for lane_idx in range(self.max_lane + 1):
                ini_one_lane = given_ini[lane_idx]
                for i in range(len(ini_one_lane)):
                    veh_data = ini_one_lane[i]
                    x, velocity = veh_data[0], veh_data[1]
                    v = other_vehicles_type(
                        self.road,
                        self.road.network.get_lane(
                            ("a", "b", lane_idx)).position(x, 0), 0, velocity)
                    self.road.vehicles.append(v)

        return ini_data
    def _make_vehicles(self,
                       given_ini=None,
                       auto_vehicle=(0, global_val.initial_CAV_position,
                                     global_val.initial_CAV_speed)):
        """
            Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle.
        :return: the ego-vehicle
        given_ini: [[Lane 0], [Lane 1], [Lane 2]], Each item [Lane i] = [[x,velocity],..., [x,velocity]]
        """
        ego_vehicle = MDPVehicle(self.road,
                                 self.road.network.get_lane(
                                     ("a", "b", auto_vehicle[0])).position(
                                         auto_vehicle[1], 0),
                                 velocity=auto_vehicle[2])
        self.road.vehicles.append(ego_vehicle)
        self.vehicle = ego_vehicle
        # other_vehicles_type = NDD_heuristic_Vehicle
        # other_vehicles_type = IDMVehicle
        # other_vehicles_type = IDM_heuristic_Vehicle
        other_vehicles_type = NDDVehicle
        # other_vehicles_type = NDD_heuristic_Vehicle
        # # !!!IDMVehicle NDDVehicle
        ini_data = None
        if given_ini:
            assert self.generate_vehicle_mode == "Given_ini"
        if self.generate_vehicle_mode == "Test":
            if len(self.presum_list_forward) == 0 or len(
                    self.presum_list_backward) == 0:
                # If it is the first time generate by NDD, preprocess the data first
                self._preprocess_CF_data()
                print(
                    "================Generate CF data presum_list finished!================"
                )
            lane_list = [1, 2, 1, 1, 2, 2,
                         2]  #[1, 1, 1, 2, 2, 2]  # [1, 1, 1, 2, 2, 2]
            position_list = [
                398, 398, 380, 346, 341, 488, 302
            ]  #, 105, 120, 90, 105, 120]  # [90, 105, 120, 90, 105, 120]
            velocity_list = [
                28, 21, 31, 27, 24, 27, 28
            ]  #, 32, 28, 36, 34, 32] # [34, 32, 28, 36, 34, 32]
            for i in range(len(lane_list)):
                v = other_vehicles_type(
                    self.road,
                    self.road.network.get_lane(
                        ("a", "b",
                         lane_list[i])).position(position_list[i], 0), 0,
                    velocity_list[i])
                self.road.vehicles.append(v)

        if self.generate_vehicle_mode == "Random":
            for _ in range(self.config["vehicles_count"]):
                new_vehicle = self.create_random_acc_training(
                    other_vehicles_type)
                if new_vehicle:
                    self.road.vehicles.append(new_vehicle)

        if self.generate_vehicle_mode == "NDD":
            if len(self.presum_list_forward) == 0 or len(
                    self.presum_list_backward) == 0:
                # If it is the first time generate by NDD, preprocess the data first
                self._preprocess_CF_data()
                print(
                    "================Generate CF data presum_list finished!================"
                )

            vehicle_list = [
            ]  # each list in this container is for vehicles in each lane (without CAV)
            lane_num = len(self.vehicle.road.network.graph["a"]["b"])
            for lane_idx in range(lane_num):
                generate_forward = True
                generate_finish = False
                vehicle_forward_list_one_lane, vehicle_backward_list_one_lane = [], []
                if lane_idx == 0:
                    back_vehicle_speed, front_vehicle_speed = auto_vehicle[
                        2], auto_vehicle[2]
                    back_vehicle_position, front_vehicle_position = auto_vehicle[
                        1], auto_vehicle[1]
                else:
                    rand_speed, rand_position, _ = self._gen_NDD_veh()
                    back_vehicle_speed, front_vehicle_speed = rand_speed, rand_speed
                    back_vehicle_position, front_vehicle_position = rand_position, rand_position
                    vehicle_forward_list_one_lane.append(
                        (rand_position, rand_speed))

                while generate_finish is False:
                    if generate_forward is True:
                        # print(back_vehicle_speed)
                        if back_vehicle_speed < global_val.v_low:
                            presum_list = self.presum_list_forward[
                                global_val.v_to_idx_dic[global_val.v_low]]
                        else:
                            presum_list = self.presum_list_forward[
                                global_val.v_to_idx_dic[int(
                                    back_vehicle_speed)]]

                        # decide CF or FF
                        random_number_CF = np.random.uniform()
                        if random_number_CF > self.CF_percent:  # FF
                            rand_speed, rand_position, _ = self._gen_NDD_veh(
                            )  # self._gen_one_random_veh()
                            v_generate = rand_speed
                            pos_generate = back_vehicle_position + self.ff_dis + rand_position + global_val.LENGTH

                        else:  # CF
                            random_number = np.random.uniform()
                            r_idx, rr_idx = divmod(
                                bisect.bisect_left(presum_list, random_number),
                                self.num_rr)
                            try:
                                r, rr = global_val.r_to_idx_dic.inverse[
                                    r_idx], global_val.rr_to_idx_dic.inverse[
                                        rr_idx]
                            except:
                                # print("back_vehicle_speed:", back_vehicle_speed)
                                if back_vehicle_speed > 35:
                                    r, rr = 50, -2
                                else:
                                    r, rr = 50, 2

                            # Accelerated training for initialization
                            r = r - global_val.Initial_range_adjustment_AT + global_val.Initial_range_adjustment_SG
                            if r <= global_val.Initial_range_adjustment_SG:
                                r = r + global_val.r_high

                            v_generate = back_vehicle_speed + rr
                            pos_generate = back_vehicle_position + r + global_val.LENGTH

                        vehicle_forward_list_one_lane.append(
                            (pos_generate, v_generate))
                        back_vehicle_speed = v_generate
                        back_vehicle_position = pos_generate

                        v = other_vehicles_type(
                            self.road,
                            self.road.network.get_lane(
                                ('a', 'b',
                                 lane_idx)).position(pos_generate, 0), 0,
                            v_generate)
                        self.road.vehicles.append(v)

                        if back_vehicle_position >= self.gen_length:
                            generate_forward = False
                            generate_finish = True
                vehicle_list_each_lane = vehicle_backward_list_one_lane + vehicle_forward_list_one_lane
                vehicle_list.append(vehicle_list_each_lane)
                # print(vehicle_list_each_lane)
            #     print('Lane idx:', str(lane_idx+1), " ", vehicle_list_each_lane, len(vehicle_list_each_lane))
            # print("\n")

        if self.generate_vehicle_mode == "NDD_with_exposure_frequency":
            if len(self.presum_list_forward) == 0 or len(
                    self.presum_list_backward) == 0:
                # If it is the first time generate by NDD, preprocess the data first
                self._preprocess_CF_data()
                print(
                    "================Generate CF data presum_list finished!================"
                )

            vehicle_list = [
            ]  # each list in this container is for vehicles in each lane (without CAV)
            lane_num = len(self.vehicle.road.network.graph["a"]["b"])
            exposure_freq_list = [
            ]  # exposure frequency for each lines initialization
            vehicle_data_list = []
            for lane_idx in range(lane_num):
                generate_forward = True
                generate_finish = False
                vehicle_forward_list_one_lane, vehicle_backward_list_one_lane = [], []
                exposure_freq_list_one_lane, vehicle_data_list_one_lane = [], []
                exposure_freq_one_lane = 1
                if lane_idx == 0:
                    back_vehicle_speed, front_vehicle_speed = auto_vehicle[
                        2], auto_vehicle[2]
                    back_vehicle_position, front_vehicle_position = auto_vehicle[
                        1], auto_vehicle[1]
                else:
                    rand_speed, rand_position, exposure_freq = self._gen_NDD_veh(
                    )
                    back_vehicle_speed, front_vehicle_speed = rand_speed, rand_speed
                    back_vehicle_position, front_vehicle_position = rand_position, rand_position
                    if global_val.critical_ini_start <= back_vehicle_position <= global_val.critical_ini_end:
                        exposure_freq_one_lane *= exposure_freq
                        exposure_freq_list_one_lane.append(exposure_freq)
                        vehicle_data_list_one_lane.append(
                            [None, None, None])  # For the first BV
                    vehicle_forward_list_one_lane.append(
                        (rand_position, rand_speed))

                while generate_finish is False:
                    if generate_forward is True:
                        # print(back_vehicle_speed)
                        if back_vehicle_speed < global_val.v_low:
                            presum_list = self.presum_list_forward[
                                global_val.v_to_idx_dic[global_val.v_low]]
                        else:
                            back_vehicle_speed = int(back_vehicle_speed)
                            presum_list = self.presum_list_forward[
                                global_val.v_to_idx_dic[back_vehicle_speed]]

                        # decide CF or FF
                        random_number_CF = np.random.uniform()
                        if random_number_CF > self.CF_percent or back_vehicle_speed == 40:  # FF
                            rand_speed, rand_position, exposure_freq = self._gen_NDD_veh(
                            )  # self._gen_one_random_veh()
                            v_generate = rand_speed
                            pos_generate = back_vehicle_position + self.ff_dis + rand_position + global_val.LENGTH

                            if global_val.critical_ini_start <= pos_generate <= global_val.critical_ini_end:
                                exposure_freq_one_lane *= exposure_freq
                                exposure_freq_list_one_lane.append(
                                    exposure_freq)
                                vehicle_data_list_one_lane.append([
                                    back_vehicle_speed, self.ff_dis +
                                    rand_position + global_val.LENGTH,
                                    rand_speed - back_vehicle_speed
                                ])  # !!! Need discuss

                        else:  # CF
                            random_number = np.random.uniform()
                            idx = bisect.bisect_left(presum_list,
                                                     random_number)
                            r_idx, rr_idx = divmod(idx, self.num_rr)

                            try:
                                r, rr = global_val.r_to_idx_dic.inverse[
                                    r_idx], global_val.rr_to_idx_dic.inverse[
                                        rr_idx]

                            except:
                                print("back_vehicle_speed:",
                                      back_vehicle_speed)
                                if back_vehicle_speed > 35:
                                    # back_vehicle_speed = 39
                                    # presum_list = self.presum_list_forward[global_val.v_to_idx_dic[back_vehicle_speed]]

                                    # random_number = np.random.uniform()
                                    # idx = bisect.bisect_left(presum_list, random_number)
                                    # r_idx, rr_idx = divmod(idx, self.num_rr)
                                    # r, rr = global_val.r_to_idx_dic.inverse[r_idx], global_val.rr_to_idx_dic.inverse[rr_idx]
                                    r, rr = 50, -2
                                else:
                                    r, rr = 50, 2
                                # assert("Back_vehicle_speed problem")

                            v_generate = back_vehicle_speed + rr
                            pos_generate = back_vehicle_position + r + global_val.LENGTH
                            if global_val.critical_ini_start <= pos_generate <= global_val.critical_ini_end:
                                exposure_freq = presum_list[idx] - presum_list[
                                    idx - 1] if idx >= 1 else presum_list[idx]
                                exposure_freq_one_lane *= exposure_freq
                                exposure_freq_list_one_lane.append(
                                    exposure_freq)
                                vehicle_data_list_one_lane.append([
                                    back_vehicle_speed, r + global_val.LENGTH,
                                    rr
                                ])

                        back_vehicle_speed = v_generate
                        back_vehicle_position = pos_generate

                        if back_vehicle_position >= self.gen_length:
                            generate_forward = False
                            generate_finish = True
                            continue
                        vehicle_forward_list_one_lane.append(
                            (pos_generate, v_generate))
                        v = other_vehicles_type(
                            self.road,
                            self.road.network.get_lane(
                                ('a', 'b',
                                 lane_idx)).position(pos_generate, 0), 0,
                            v_generate)
                        self.road.vehicles.append(v)

                vehicle_list_each_lane = vehicle_backward_list_one_lane + vehicle_forward_list_one_lane
                vehicle_list.append(vehicle_list_each_lane)
                # exposure_freq_list.append(exposure_freq_one_lane)
                exposure_freq_list.append(exposure_freq_list_one_lane)
                vehicle_data_list.append(vehicle_data_list_one_lane)
                # print(vehicle_list_each_lane)
            #     print('Lane idx:', str(lane_idx+1), " ", vehicle_list_each_lane, len(vehicle_list_each_lane))
            # print("\n")
            ini_data = {
                "data": vehicle_data_list,
                "exposure_frequency": exposure_freq_list,
                "All_initial_info": vehicle_list
            }

        if self.generate_vehicle_mode == "Given_ini":
            for lane_idx in range(self.max_lane + 1):
                ini_one_lane = given_ini[lane_idx]
                for i in range(len(ini_one_lane)):
                    veh_data = ini_one_lane[i]
                    x, velocity = veh_data[0], veh_data[1]
                    v = other_vehicles_type(
                        self.road,
                        self.road.network.get_lane(
                            ("a", "b", lane_idx)).position(x, 0), 0, velocity)
                    self.road.vehicles.append(v)

        return ini_data
Example #20
0
    def make_vehicles(self, other_vehicles_mandatory=False):
        """
        Populate a road with several vehicles on the highway and on the merging lane, as well as an ego-vehicle.

        :param other_vehicles_mandatory: if the lane changing maneuvers of other vehicles are mandatory
        :return: None
        """
        max_l = 500
        road = self.road
        other_vehicles_type = utils.class_from_path(
            self.config["other_vehicles_type"])
        car_number_each_lane = 15
        # reset_position_range = (30, 40)
        reset_position_range = (20, 30)
        # reset_lane = random.choice(road.lanes)
        reset_lane = road.lanes[0]
        for l in road.lanes[:3]:
            cars_on_lane = car_number_each_lane
            reset_position = None
            if l is reset_lane:
                cars_on_lane += 1
                reset_position = random.choice(range(5, 6))
                # reset_position = 2
            for i in range(cars_on_lane):
                if i == reset_position:
                    if self.switch:
                        ego_vehicle = MDPVehicle(
                            road,
                            l.position(
                                (i + 1) *
                                np.random.randint(*reset_position_range), 0),
                            velocity=20,
                            max_length=max_l)
                    else:
                        ego_vehicle = IDMVehicle(
                            road,
                            l.position(
                                (i + 1) *
                                np.random.randint(*reset_position_range), 0),
                            velocity=20,
                            max_length=max_l)
                        ego_vehicle.destination = 1
                        ego_vehicle.id = 0
                    road.vehicles.append(ego_vehicle)
                    self.vehicle = ego_vehicle
                    l.vehicles.append(ego_vehicle)
                else:
                    car = other_vehicles_type(
                        road,
                        l.position(
                            (i + 1) * np.random.randint(*reset_position_range),
                            0),
                        velocity=np.random.randint(18, 25),
                        dst=3,
                        max_length=max_l)
                    if other_vehicles_mandatory:
                        car.destination = 1
                    road.vehicles.append(car)
                    l.vehicles.append(car)

        for l in [road.lanes[3]]:
            cars_on_lane = car_number_each_lane
            reset_position = None
            if l is reset_lane:
                cars_on_lane += 1
                reset_position = random.choice(range(5, 6))
                # reset_position = 2
            for i in range(cars_on_lane):
                if i < 8:
                    continue
                if i == reset_position:
                    # ego_vehicle = MDPVehicle(road, l.position((i+1) * np.random.randint(*reset_position_range), 0),
                    #                          velocity=20, max_length=max_l)
                    ego_vehicle = IDMVehicle(
                        road,
                        l.position(
                            (i + 1) * np.random.randint(*reset_position_range),
                            0),
                        velocity=20,
                        max_length=max_l)
                    ego_vehicle.destination = 1
                    ego_vehicle.id = 0
                    road.vehicles.append(ego_vehicle)
                    self.vehicle = ego_vehicle
                    l.vehicles.append(ego_vehicle)
                else:
                    car = other_vehicles_type(
                        road,
                        l.position(
                            (i + 1) * np.random.randint(*reset_position_range),
                            0),
                        velocity=np.random.randint(18, 25),
                        dst=3,
                        max_length=max_l)
                    if other_vehicles_mandatory:
                        car.destination = 1
                    road.vehicles.append(car)
                    l.vehicles.append(car)

        for lane in road.lanes:
            lane.vehicles = sorted(
                lane.vehicles,
                key=lambda x: lane.local_coordinates(x.position)[0])
            for i, v in enumerate(lane.vehicles):
                v.vehicle_index_in_line = i