コード例 #1
0
    def get_object_reward_params(self, racecar_name, model_point, car_pose):
        """Returns a dictionary with object-related reward function params."""
        with self._lock_:
            try:
                object_locations = [[
                    pose.position.x, pose.position.y
                ] for name, pose in self.object_poses.items()
                                    if racecar_name not in name]
                object_poses = [
                    pose for name, pose in self.object_poses.items()
                    if racecar_name not in name
                ]
                if not object_locations:
                    return {}

                # Sort the object locations based on projected distance
                num_objects = len(object_locations)
                object_pdists = [
                    self.center_line.project(Point(p))
                    for p in object_locations
                ]
                object_headings = [0.0] * num_objects
                object_speeds = [0.0] * num_objects
                if self._is_bot_car_:
                    for i, object_pose in enumerate(object_poses):
                        _, _, yaw = quaternion_to_euler(
                            x=object_pose.orientation.x,
                            y=object_pose.orientation.y,
                            z=object_pose.orientation.z,
                            w=object_pose.orientation.w,
                        )
                        object_headings[i] = yaw
                        object_speeds[i] = self._bot_car_speed_

                # Find the prev/next objects
                model_pdist = self.center_line.project(model_point)
                object_order = np.argsort(object_pdists)
                object_pdists_ordered = [
                    object_pdists[i] for i in object_order
                ]
                prev_object_index, next_object_index = find_prev_next(
                    object_pdists_ordered, model_pdist)
                prev_object_index = object_order[prev_object_index]
                next_object_index = object_order[next_object_index]

                # Figure out which one is the closest
                object_points = [
                    Point([object_location[0], object_location[1]])
                    for object_location in object_locations
                ]
                prev_object_point = object_points[prev_object_index]
                next_object_point = object_points[next_object_index]
                prev_object_dist = model_point.distance(prev_object_point)
                next_object_dist = model_point.distance(next_object_point)
                if prev_object_dist < next_object_dist:
                    closest_object_point = prev_object_point
                else:
                    closest_object_point = next_object_point

                # Figure out whether objects is left of center based on direction
                objects_left_of_center = [
                    self._inner_poly_.contains(p) ^ (not self.is_ccw)
                    for p in object_points
                ]

                # Get object distances to centerline
                objects_distance_to_center = [
                    self.center_line.distance(p) for p in object_points
                ]

                # Figure out if the next object is in the camera view
                objects_in_camera = self.get_objects_in_camera_frustums(
                    agent_name=racecar_name, car_pose=car_pose)
                is_next_object_in_camera = any(
                    object_in_camera_idx == next_object_index
                    for object_in_camera_idx, _ in objects_in_camera)

                # Determine if they are in the same lane
                return {
                    RewardParam.CLOSEST_OBJECTS.value[0]:
                    [prev_object_index, next_object_index],
                    RewardParam.OBJECT_LOCATIONS.value[0]:
                    object_locations,
                    RewardParam.OBJECTS_LEFT_OF_CENTER.value[0]:
                    objects_left_of_center,
                    RewardParam.OBJECT_SPEEDS.value[0]:
                    object_speeds,
                    RewardParam.OBJECT_HEADINGS.value[0]:
                    object_headings,
                    RewardParam.OBJECT_CENTER_DISTS.value[0]:
                    objects_distance_to_center,
                    RewardParam.OBJECT_CENTERLINE_PROJECTION_DISTANCES.value[0]:
                    object_pdists,
                    RewardParam.OBJECT_IN_CAMERA.value[0]:
                    is_next_object_in_camera,
                }
            except Exception as ex:
                raise GenericRolloutException(
                    "Unable to get object reward params: {}".format(ex))
コード例 #2
0
 def find_prev_next_waypoints(self, distance, normalized=False):
     ndist = distance if normalized else distance / self.line.length
     return find_prev_next(self.ndists, ndist)
コード例 #3
0
    def get_object_reward_params(self, racecar_name, model_point, reverse_dir,
                                 car_pose):
        '''Returns a dictionary with object-related reward function params.'''
        with self._lock_:
            try:
                object_locations = [[
                    pose.position.x, pose.position.y
                ] for name, pose in self.object_poses.items()
                                    if racecar_name not in name]
                object_poses = [
                    pose for name, pose in self.object_poses.items()
                    if racecar_name not in name
                ]
                if not object_locations:
                    return {}

                # Sort the object locations based on projected distance
                object_pdists = [
                    self.center_line.project(Point(p))
                    for p in object_locations
                ]
                object_order = np.argsort(object_pdists)
                object_pdists = [object_pdists[i] for i in object_order]
                object_locations = [object_locations[i] for i in object_order]
                object_headings = []
                object_speeds = []
                object_headings = [0.0 for _ in object_order]
                object_speeds = [0.0 for _ in object_order]
                if self._is_bot_car_:
                    for i in object_order:
                        object_pose = object_poses[i]
                        _, _, yaw = quaternion_to_euler(
                            x=object_pose.orientation.x,
                            y=object_pose.orientation.y,
                            z=object_pose.orientation.z,
                            w=object_pose.orientation.w)
                        object_headings[i] = yaw
                        object_speeds[i] = self._bot_car_speed_

                # Find the prev/next objects
                model_pdist = self.center_line.project(model_point)
                prev_object_index, next_object_index = find_prev_next(
                    object_pdists, model_pdist)

                # Figure out which one is the closest
                object_points = [
                    Point([object_location[0], object_location[1]])
                    for object_location in object_locations
                ]
                prev_object_point = object_points[prev_object_index]
                next_object_point = object_points[next_object_index]
                prev_object_dist = model_point.distance(prev_object_point)
                next_object_dist = model_point.distance(next_object_point)
                if prev_object_dist < next_object_dist:
                    closest_object_point = prev_object_point
                else:
                    closest_object_point = next_object_point

                # Figure out whether objects is left of center based on direction
                is_ccw = self._is_ccw_ ^ reverse_dir
                objects_left_of_center = [self._inner_poly_.contains(p) ^ (not is_ccw) \
                                          for p in object_points]

                # Figure out which lane the model is in
                model_nearest_pnts_dict = self.get_nearest_points(model_point)
                model_nearest_dist_dict = self.get_nearest_dist(
                    model_nearest_pnts_dict, model_point)
                model_is_inner = \
                    model_nearest_dist_dict[TrackNearDist.NEAR_DIST_IN.value] < \
                    model_nearest_dist_dict[TrackNearDist.NEAR_DIST_OUT.value]

                # Figure out which lane the object is in
                closest_object_nearest_pnts_dict = self.get_nearest_points(
                    closest_object_point)
                closest_object_nearest_dist_dict = \
                    self.get_nearest_dist(closest_object_nearest_pnts_dict, closest_object_point)
                closest_object_is_inner = \
                    closest_object_nearest_dist_dict[TrackNearDist.NEAR_DIST_IN.value] < \
                    closest_object_nearest_dist_dict[TrackNearDist.NEAR_DIST_OUT.value]
                objects_in_camera = self.get_objects_in_camera_frustums(
                    agent_name=racecar_name,
                    car_pose=car_pose,
                    object_order=object_order)
                is_next_object_in_camera = any(
                    object_in_camera_idx == next_object_index
                    for object_in_camera_idx, _ in objects_in_camera)
                # Determine if they are in the same lane
                return {
                    RewardParam.CLOSEST_OBJECTS.value[0]:
                    [prev_object_index, next_object_index],
                    RewardParam.OBJECT_LOCATIONS.value[0]:
                    object_locations,
                    RewardParam.OBJECTS_LEFT_OF_CENTER.value[0]:
                    objects_left_of_center,
                    RewardParam.OBJECT_SPEEDS.value[0]:
                    object_speeds,
                    RewardParam.OBJECT_HEADINGS.value[0]:
                    object_headings,
                    RewardParam.OBJECT_CENTERLINE_PROJECTION_DISTANCES.value[0]:
                    object_pdists,
                    RewardParam.OBJECT_IN_CAMERA.value[0]:
                    is_next_object_in_camera
                }
            except Exception as ex:
                raise GenericRolloutException(
                    "Unable to get object reward params: {}".format(ex))