Teaching a car to park.
Reinforcement learning · PyBullet
I tried PPO and TRPO to teach a simulated car to park, starting with reaching goal points and then moving on to following planned paths. I could get a policy working on a particular path, but getting it to handle unfamiliar ones was where things became difficult.
A controller, a car, and a target
I ran the experiments in Gym and PyBullet, where I trained a model called a policy to choose throttle and steering from the car’s state and target. I fed it that information directly from the simulator so I could focus on how it learned to control the car.
What goes into the model?
I gave the path-following model 11 values, including velocity and heading, because I needed it to know how the car was moving as well as how close it was to the next waypoint.
state = [
x, y, cos(heading), sin(heading),
velocity_x, velocity_y,
target_x, target_y, target_heading,
distance_to_target, heading_error
]
throttle, steering = policy.sample(state)I represented the car’s heading with sine and cosine so nearby directions stayed close even when the angle wrapped from a full turn back to zero. I kept the target heading and heading error as separate scalar values in this implementation.
During training, I sampled continuous actions from the policy and let the environment clip them to its throttle and steering limits. I also rendered a camera view to watch the car, while the policy continued to receive the state values shown above.
Giving the controller a path
I started by asking the model to reach goal points, then tried using a planner to supply a path while the policy controlled the car. I gave it waypoints describing where the car should go and which way it should face, and left it to learn the throttle and steering needed to follow them.
What RRT* and Reeds–Shepp handle
I used RRT* with Reeds–Shepp connections to generate those paths. With RRT*, I could grow a tree of possible routes and reconnect nearby branches when a lower-cost connection turned up. The Reeds–Shepp curves let those connections include forward and reverse motion while respecting a turning constraint.
I used the planner’s existing implementation, which credits Atsushi Sakai’s sample code. I reversed its goal-to-start output into the order the car needed to follow, then moved the target to the next waypoint as the car reached each one.
That gave me a route through the scene, but I still needed the policy to turn it into a sequence of throttle and steering commands that actually moved the car along it.
Deciding what a good drive looks like
I scored the car’s actions using alignment with the path, distance to the next waypoint, and heading error, with penalties for collisions and leaving the map. I combined those rewards over time into a return so an earlier steering choice could get credit for what happened afterward, while giving later rewards less weight.
drives = collect_drives(policy, simulator)
returns = discounted_rewards(drives)
signal = learning_signal(drives, returns)
policy = update_with_ppo_or_trpo(
policy, drives, signal)I let the car collect a batch of driving experience before updating the model. I tried PPO and TRPO as separate ways of making that update, so I wasn’t running both algorithms every time the car steered.
Why the reward and learning signal matter
I included intermediate reward terms because a model that rarely reached the parking spot would otherwise have very little useful feedback to learn from early in training.
reward = path_alignment
reward -= distance_to_waypoint
reward -= abs(heading_error)
if collision or outside_map:
reward -= collision_penalty
if reached_waypoint:
reward += waypoint_bonusI also had to be careful about what those terms encouraged, because getting the car to reach a waypoint didn’t necessarily mean it had finished a clean parking maneuver. I needed to look at the final heading and speed alongside the reward it earned.
For PPO, I used an advantage estimate to judge whether an action worked out better or worse than expected from that state, with a value function supplying the expectation. The custom TRPO implementation I experimented with handled this differently: it used normalized discounted returns without a learned value critic.
PPO: learning without overcorrecting
I used Stable-Baselines3’s Proximal Policy Optimization implementation to update the policy from the drives it collected. PPO uses an advantage estimate to judge whether an action worked out better or worse than expected, then encourages actions with a positive advantage and discourages those with a negative one. Its clipped objective stops an increasingly large favorable change from earning extra credit on that sample.
I compared the new and old policy’s likelihood of the same throttle-and-steering action using the ratio r. In the pseudocode below, I calculate it from their log-likelihoods and use an illustrative eps = 0.2 to set the clipping width.
eps = 0.2
r = exp(
new_joint_logp - old_joint_logp)
raw = r * advantage
limited = clip(r, 1 - eps, 1 + eps)
clipped = limited * advantage
loss = -mean(minimum(raw, clipped))
gradient_step(policy, loss)I used minimum to take the smaller contribution for each sample, then averaged those contributions across the batch. I kept the old log-likelihoods and advantages fixed, and used the minus sign to turn the objective I wanted to maximize into a loss I could minimize.
Walk through one PPO update
Because I was working with continuous actions, I compared probability densities for the throttle-and-steering pair. Taking exp(new_joint_logp − old_joint_logp) gave me the new density divided by the old one.
To make that concrete, I can take a sample with advantage = +1 and use eps = 0.2. If the new policy makes that action 1.5 times as likely, the raw contribution becomes 1.5, but the clipped contribution stays at 1.2, which is the value the objective uses.
I checked the negative case too: with advantage = −1 and r = 0.5, the objective gives min(−0.5, −0.8) = −0.8. That stops me getting more benefit from this sample just by making the bad action even less likely, while still penalizing a change that makes it more likely.
I treated clipping as a change to the learning objective, because it doesn’t enforce a hard boundary on policy change or directly limit the steering command. The full PPO implementation also trains a value function and can make several optimization passes over the collected drives.
TRPO: checking the size of the update
I also experimented with a custom Trust Region Policy Optimization agent, where I checked proposed updates against a limit on KL divergence. That gave me a way to measure how much the policy’s action distribution changed across the sampled states, then try smaller steps until a candidate stayed within the limit and improved the batch objective.
I was looking at the same problem with both methods: how to improve the policy without pushing an update too far. With PPO, I used a clipped objective; with TRPO, I worked with a constrained step and a backtracking check.
How the trust-region step works
I’m using the canonical joint-action form below to explain the update; the code in the repository averages the action-dimension ratios separately. To work through that form, I keep the old policy, collected actions, old log-likelihoods, and learning signal fixed, then use the mean joint likelihood ratio times that signal as a surrogate score. Taking its gradient, g, gives me a direction that would increase it.
I used conjugate gradient to approximately solve Hx = g through matrix-vector products, which let me work with the local curvature of the KL change without building and inverting the full matrix. I then scaled the proposed step to the KL budget, delta, and checked it with backtracking.
old_params = copy(policy.parameters)
g = gradient(score, old_params)
Fv = damped_kl_hessian_product
x = conjugate_gradient(Fv, g)
curvature = dot(x, Fv(x))
if curvature <= 0:
return old_params
step = sqrt(2 * delta / curvature) * x
for scale in shrinking_scales:
trial = old_params + scale * step
kl = mean_kl(old_params, trial)
improves = score(trial) > score(old_params)
if kl <= delta and improves:
return trial
return old_paramsI used Fv for the KL Hessian-vector product at the old parameters, adding damping for numerical stability. For backtracking, I tried a bounded sequence of shrinking scales such as 1, beta, beta², …, where 0 < beta < 1, and checked the average KL(old || trial) across the collected states. If none of the candidates passed, I kept the old parameters.
The version I experimented with in the repository also uses normalized returns without a learned value critic, and its conjugate-gradient code credits an upstream implementation. I kept those details in mind when reading the results, because passing the sampled checks didn’t guarantee better parking on a new path.
Following one path wasn’t enough
I could learn a policy for a particular path, but I couldn’t get the attempts across new paths to work reliably. That was the result I needed to pay attention to, because a good run on a practiced route didn’t show that I had trained a general parking controller.
I came away looking more carefully at what each result actually told me. Alongside the reward, I needed to know whether the route was familiar, where the car finished, which way it was facing, and whether it could recover when something went wrong.
What I would test next
I’d set aside some start and goal configurations before training, then test them without making further updates to the model. I’d track completion, collisions, final pose, and speed, and look through the failed runs as well as the successful parks.
I also need to account for the current configuration, which generates one path per environment instance, even though the README describes broader experiments. I can’t use that setting alone to claim held-out performance, and I don’t have a recorded comparison here that would let me call PPO or TRPO the winner.
The default runner has since moved to TD3, so I’ve focused this explanation on the earlier PPO experiments and custom TRPO entry point that are still in the repository.