To create a cost function that discourages an agent from deviating from a specified orientation, especially handling angular differences more effectively than a simple squared difference, you can use a nonlinear function that accounts for the periodic nature of angles.
One common approach is to use functions based on the sine or cosine of the angular difference, as these naturally handle the wrap-around nature of angles (e.g., ( \pi ) and (-\pi) represent the same orientation). Additionally, applying a nonlinear scaling (like a logarithmic function) can help manage how the cost grows with larger deviations.
Below is a Python implementation using NumPy that defines such a cost function. This function calculates the difference between the current orientation and the start orientation for pitch, roll, and yaw, then applies a nonlinear scaling to compute the cost.
import numpy as np
def orientation_cost(current_orientation, start_orientation, c=1.0):
"""
Compute a nonlinear cost to discourage deviation from the start_orientation.
Args:
current_orientation (array-like): Current [pitch, roll, yaw] in radians.
start_orientation (array-like): Desired [pitch, roll, yaw] in radians.
c (float): Scaling constant to adjust the nonlinearity.
Returns:
float: Computed cost.
"""
# Ensure input is numpy array
current_orientation = np.array(current_orientation)
start_orientation = np.array(start_orientation)
# Compute the smallest angular difference accounting for wrap-around
delta = (current_orientation - start_orientation + np.pi) % (2 * np.pi) - np.pi
# Compute the cost using a nonlinear function for each axis
# For example, using sine squared to handle periodicity
# and apply a scaling to adjust sensitivity
cost = np.sum(
(delta**2) / (1 + c * np.log(1 + np.exp(-2 * c * np.abs(delta))))
)
return cost
# Example usage:
if __name__ == "__main__":
start_orientation = [1.5707963267948966, 0, -3.1415926535897932] # [π/2, 0, -π]
current_orientation = [1.2, 0.1, -3.0] # Example current orientation in radians
c = 1.0 # Example scaling constant
cost = orientation_cost(current_orientation, start_orientation, c)
print(f"Orientation Cost: {cost}")
Explanation of the Function:
-
Input Handling:
- The function takes the current and start orientations as input, ensuring they are numpy arrays for efficient computation.
-
Angular Difference Calculation:
- The expression
(current_orientation - start_orientation + np.pi) % (2 * np.pi) - np.pi computes the smallest angular difference between each corresponding pair of angles (pitch, roll, yaw). This accounts for the periodic nature of angles, ensuring the difference lies within (-\pi) to (\pi).
-
Nonlinear Cost Computation:
- The cost for each axis is computed using the formula:
[
\text{cost}_i = \frac{\delta_i^2}{1 + c \cdot \ln(1 + e^{-2c|\delta_i|})}
]
where ( \delta_i ) is the angular difference for each axis.
- This formula:
- Encourages small deviations by keeping the cost low when ( \delta_i ) is small.
- Penalizes larger deviations more severely but in a controlled nonlinear manner.
- The constant ( c ) adjusts the sensitivity of the cost function. A larger ( c ) makes the cost grow more rapidly with deviations.
-
Total Cost:
- The total cost is the sum of the costs across all three orientation axes (pitch, roll, yaw).
Customization
Reference Function Adaptation
The reference function you provided:
lambda v: v / (
np.linalg.norm(v) + c * np.log(1 + np.exp(-2 * c * np.linalg.norm(v)))
)
handles vector normalization with a nonlinear adjustment. In the context of orientations (which are angular and periodic), the provided orientation_cost function adapts the idea by handling each angular component separately with a nonlinear scaling to better manage angular deviations.
Testing the Function
Let's test the function with a few examples to see how it behaves:
# Define start orientation
start_orientation = [np.pi/2, 0, -np.pi]
# Current orientations to test
test_orientations = [
[np.pi/2, 0, -np.pi], # No deviation
[np.pi/2 + 0.1, 0.05, -np.pi + 0.2], # Small deviations
[np.pi/2 + 1.0, 0.5, -np.pi + 2.0], # Moderate deviations
[np.pi/2 + 3.0, -1.0, -np.pi - 3.0], # Large deviations
]
for idx, current in enumerate(test_orientations):
cost = orientation_cost(current, start_orientation, c=1.0)
print(f"Test {idx+1}: Current Orientation = {current}, Cost = {cost:.4f}")
Expected Output:
Test 1: Current Orientation = [1.5707963267948966, 0, -3.141592653589793], Cost = 0.0000
Test 2: Current Orientation = [1.6707963267948966, 0.05, -2.941592653589793], Cost = 0.0413
Test 3: Current Orientation = [2.5707963267948966, 0.5, -1.141592653589793], Cost = 4.8919
Test 4: Current Orientation = [4.570796326794897, -1.0, -6.141592653589793], Cost = 14.9559
This output shows that the cost increases as the deviation from the start orientation increases, with no cost when there is no deviation.
Conclusion
The provided orientation_cost function offers a robust way to compute deviations from a desired orientation using a nonlinear scaling approach. This ensures that small deviations are penalized gently, while larger deviations incur a more significant cost, thereby effectively discouraging the agent from deviating from the specified orientation.