Introduction
Welcome back. In our last lesson, we focused on the most critical aspect of safety alignment: designing rule-based reward functions for hard safety constraints like collision avoidance. We learned how Plan-R1 uses a powerful multiplicative safety gate to ensure that any single violation of a non-negotiable rule results in a total reward of zero.
The full reward formula we examined was:
Having built the safety gate, our goal in this lesson is to design the second part of this equation: the weighted sum of "soft" costs. Specifically, you will learn to design rule-based reward functions for lane compliance and adherence to speed limits.
These functions encourage the model to drive not just safely, but also well—adhering to traffic laws and maintaining a good position on the road. Unlike the binary, all-or-nothing nature of the safety gate, these rewards are often continuous, providing a graded signal that helps the RL agent refine its behavior.
Designing the Lane Compliance Reward
A key aspect of good driving is staying properly positioned within a lane. In our last lesson, we saw that Plan-R1 implements "Driving area compliance" as a hard safety indicator. Let's quickly review that.
Safe and Feasible Trajectory Planning as Language Modeling
The Plan-R1 paper includes a rule for staying on the road as part of its non-negotiable safety gate. Let's revisit this to understand the distinction between hard and soft compliance.
Please read the 'Safety indicators' subsection in Appendix A.2. Focus on the 'Driving area compliance' rule. Note that this is a binary check: all predicted positions must lie within the drivable area polygon, or the reward is nullified.
This rule—staying within the drivable area—is a hard constraint. If the car drives off the road, the trajectory is an immediate failure, and the total reward is zero.
However, we can introduce a more nuanced, "soft" reward to encourage the car to stay centered within its lane, not just on the road. This reflects a preference for good driving style rather than a strict safety necessity.
A simple and effective way to implement this is with a continuous penalty that increases as the vehicle deviates from the lane's centerline.
Responsibility-Oriented Reward Design for Reinforcement Learning ...
This paper provides a clear formula for a 'Centering Reward' that encourages the vehicle to stay near the middle of the lane.
In Section 3.3, please locate and read the paragraph and formula for the 'Centering Reward' (Equation 3). This defines a straightforward penalty based on deviation distance.
As the paper describes, the Centering Reward can be formulated as a linear penalty:
Let's analyze this function:
deviation_distis the vehicle's lateral distance from the center of its current lane.c_centeringis a positive coefficient (a weight) that we can tune.- The reward is negative and proportional to the distance from the center. A larger deviation results in a larger penalty.
- If the vehicle is perfectly centered (
deviation_dist= 0), this reward component is 0, contributing no penalty.
This is a classic "soft" cost. It doesn't automatically zero out the entire reward. Instead, it contributes a negative value to the weighted sum, gently nudging the policy towards better lane discipline.
Designing the Speed Limit Reward
Adhering to the speed limit is another crucial aspect of rule-compliant driving. Like lane centering, this is typically handled as a soft cost. A small deviation might be acceptable, but significant or prolonged speeding should be penalized.
The Plan-R1 paper proposes an intuitive method for this.
Safe and Feasible Trajectory Planning as Language Modeling
Let's examine how Plan-R1 formulates its reward for speed limit compliance. It uses a normalized score that penalizes the vehicle based on the total distance it travels while speeding.
Please read the 'Soft cost terms' subsection in Appendix A.2. Focus on the bullet point for 'Speed limit compliance'. Pay attention to how the score is calculated based on a ratio.
The Plan-R1 approach is to calculate a score, , that starts at 1 (no penalty) and decreases linearly to 0 as the total "over-speed distance" increases.
Here's how you could implement the logic for this score:
- Calculate Over-Speed Distance: For each segment of the planned trajectory, if the vehicle's speed is above the limit, calculate the distance it travels during that segment. Sum these distances to get the
total_over_speed_distance. - Define a Threshold: Set a
max_over_speed_threshold(e.g., 50 meters). This represents the maximum distance of speeding you are willing to tolerate before the reward score drops to zero. - Compute the Ratio: Calculate the ratio
total_over_speed_distance / max_over_speed_threshold. - Compute the Score: The final score is .
- If there is no speeding, the ratio is 0, and the score is 1.
- If the over-speed distance equals or exceeds the threshold, the ratio is , and the score becomes 0.
- For intermediate values, the score decreases linearly.
This formulation is effective because it accounts for both the magnitude and duration of speeding. A brief moment of high speeding or a long period of slight speeding will both contribute to the total_over_speed_distance and be penalized.
Alternative: A Quadratic Penalty
As a point of comparison, the "Responsibility-Oriented Reward Design" paper you read earlier proposes a different formulation:
This quadratic penalty is harsher. Doubling the amount you are over the speed limit quadruples the penalty. This can be useful if you want to strongly discourage any significant speeding. The choice between linear and quadratic penalties is a design decision that depends on how strictly you want to enforce the rule.
The Complete Picture: Integrating Soft Costs
Now, let's place our newly designed functions for lane and speed compliance back into the full Plan-R1 reward structure:
Here's how the system evaluates a planned trajectory:
- Safety Gate Check: The system first checks all hard safety indicators. Does the trajectory result in a collision? Does it leave the drivable area?
- If YES (any indicator is 0), the entire reward is immediately 0. The soft costs are irrelevant. This enforces safety as the absolute top priority.
- Soft Cost Calculation:
- If NO (all safety indicators are 1), the system proceeds to calculate the soft costs.
- It computes the penalty for lane deviation () and the penalty for speeding (), along with other soft costs like comfort and progress.
- These scores are multiplied by their respective weights () and summed up. This sum becomes the final reward.
This two-part structure is elegant and effective. It allows the model to first learn the non-negotiable rules of the road and then fine-tune its behavior to be more efficient, legal, and pleasant, all guided by simple, verifiable code.
Conclusion
In this lesson, we've extended our understanding of rule-based reward design to cover "soft" costs that guide the model's driving style.
Key Takeaways:
- Lane compliance can be treated as both a hard constraint (staying on the road) and a soft cost (staying centered). A soft penalty can be a linear function of the distance from the lane centerline.
- Speed limit adherence is typically a soft cost. A common approach is a linearly decreasing reward score based on the ratio of "over-speed distance" to a maximum threshold.
- These soft rewards are calculated and summed only if the trajectory first passes the multiplicative safety gate of hard constraints (like collision avoidance).
- The use of weights () allows engineers to tune the trade-offs between different driving objectives, such as staying perfectly centered versus making faster progress.
Preview of the Next Lesson:
We have now designed reward functions for hard safety and for legal compliance (lanes and speed). In the next lesson, we will complete our tour of the Plan-R1 reward system by learning how to design the final two key soft costs: encouraging progress towards the goal and ensuring passenger comfort by minimizing jerk.