Exemple #1
0
 def _reward(self, action: int) -> float:
     lane_change = action == 0 or action == 2
     reward = self.COLLISION_REWARD * self.vehicle.crashed \
              + self.HIGH_SPEED_REWARD * MDPVehicle.get_speed_index(self.vehicle) / max(MDPVehicle.SPEED_COUNT - 1, 1) \
              + self.LANE_CHANGE_REWARD * lane_change
     return utils.lmap(reward, [
         self.COLLISION_REWARD + self.LANE_CHANGE_REWARD,
         self.HIGH_SPEED_REWARD
     ], [0, 1])
 def _reward(self, action: int) -> float:
     lane_change = action == 0 or action == 2
     reward = self.config["collision_reward"] * self.vehicle.crashed \
         + self.config["high_speed_reward"] * \
              MDPVehicle.get_speed_index(self.vehicle) / max(MDPVehicle.SPEED_COUNT - 1, 1) \
         + self.config["lane_change_reward"] * lane_change
     return utils.lmap(reward,
                       [self.config["collision_reward"] + self.config["lane_change_reward"],
                        self.config["high_speed_reward"]], [0, 1])
    def _create_vehicles(self) -> None:
        """
            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))
Exemple #4
0
    def _make_vehicles(self, n_vehicles: int = 10) -> None:
        """
        Populate a road with several vehicles on the highway and on the merging lane

        :return: the ego-vehicle
        """
        # Configure vehicles
        vehicle_type = utils.class_from_path(
            self.config["other_vehicles_type"])
        vehicle_type.DISTANCE_WANTED = 7  # 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.config["simulation_frequency"]))
             for _ in range(self.config["simulation_frequency"])]

        # Challenger vehicle
        self._spawn_vehicle(60,
                            spawn_probability=1,
                            go_straight=True,
                            position_deviation=0.1,
                            speed_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))
        destination = self.config["destination"] or "o" + str(
            self.np_random.randint(1, 4))
        ego_vehicle = MDPVehicle(self.road,
                                 ego_lane.position(60, 0),
                                 speed=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)
Exemple #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
        """
        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))
        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
Exemple #6
0
 def _reward(self, action: Action) -> float:
     """
     The reward is defined to foster driving at high speed, on the rightmost lanes, and to avoid collisions.
     :param action: the last action performed
     :return: the corresponding reward
     """
     neighbours = self.road.network.all_side_lanes(self.vehicle.lane_index)
     lane = self.vehicle.target_lane_index[2] if isinstance(self.vehicle, ControlledVehicle) \
         else self.vehicle.lane_index[2]
     speed = self.vehicle.speed_index if isinstance(self.vehicle, MDPVehicle) \
         else MDPVehicle.speed_to_index_default(self.vehicle.speed)
     reward = \
         + self.config["collision_reward"] * self.vehicle.crashed \
         + self.RIGHT_LANE_REWARD * lane / (len(neighbours) - 1) \
         + self.HIGH_SPEED_REWARD * speed / (MDPVehicle.SPEED_COUNT - 1)
     reward = utils.lmap(reward,
                       [self.config["collision_reward"], self.HIGH_SPEED_REWARD + self.RIGHT_LANE_REWARD],
                       [0, 1])
     reward = 0 if not self.vehicle.on_road else reward
     return reward
    def _make_vehicles(self) -> None:
        """
            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),
                                 speed=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),
                    speed=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),
                speed=20 + 5 * self.np_random.randn(),
                enable_lane_change=False)
            v.target_lane_index = ("b", "a", 0)
            self.road.vehicles.append(v)