surmenage · · budding · testing · rl · search · python
hill climbing a driving agent until it crashes
This was the third assignment of DSAIT4015 Software Testing at TU Delft, so it is a course project. The task: take a pre-trained PPO agent driving in highway-fast-v0, and generate scenario configurations that make it crash. The search space is five parameters, a fitness function guides the search, and the result is compared against a random-search baseline that the course provided.
I will say up front that the headline result was not the one I expected. Random search beat my hill climber on the metric I cared about most, and the interesting part is why.
The search space
A scenario is a configuration of the environment, not a trajectory. The parameters and their bounds in config/search_space.py:
| parameter | type | range |
|---|---|---|
vehicles_count | int | 5 to 60 |
lanes_count | int | 3 to 10 |
initial_spacing | float | 0.5 to 5.0 |
ego_spacing | float | 1.0 to 4.0 |
initial_lane_id | int | 0 to 4 |
Every episode runs 30 seconds at 25 Hz simulation and 5 Hz policy frequency. The agent is fixed; only the scenario and the episode seed change.
Fitness: crashes first, proximity second
I wanted the fitness to be lexicographic. If there was any collision in the episode, fitness is -1e9. Otherwise it is the minimum clearance between the ego vehicle and any other vehicle over the whole episode, and lower is worse for the agent.
def compute_fitness(objectives):
if objectives["crash_count"] >= 1:
return -1e9
return float(objectives["min_distance"])
The reason for not using a weighted sum is that a very close miss should never score about the same as a collision. The reason for having the second term at all is that crashes are discrete, and a pure crash/no-crash fitness leaves the hill climber on a flat plateau where it is just a random walk. Clearance is computed from the gap between the two bounding boxes, using the vehicle lengths and widths. If the gaps are positive on both axes it is the Euclidean distance between the closest points; if the boxes overlap it is the larger of the two gaps, which is negative.
The hill climber
The climber is steepest-ascent with a few additions, all in search/hill_climbing.py:
- It starts from a uniformly random point in the space, not from the default config.
- Each iteration generates 5 neighbours and moves to the best one if it is strictly better.
- If the best neighbour has equal fitness (
np.isclose), it accepts it with probability 0.15. That is the sideways move, meant for the plateau where the ego car is nowhere near traffic andmin_distancebarely changes. - After 5 iterations without improvement it restarts from a new random configuration.
- In budget mode, finding a crash logs it and restarts, so the rest of the budget goes to other failures. In first-crash mode it stops.
Mutation is per type. vehicles_count takes a step from {-10, -5, -2, -1, 1, 2, 5, 10}, lanes_count and initial_lane_id take plus or minus one, and the two spacings get Gaussian noise with sigma 0.3 (initial_spacing) and 0.2 (ego_spacing). Everything is clamped to the bounds and initial_lane_id is repaired to stay below lanes_count. With probability 0.1 two parameters are mutated at once, on the theory that density and spacing are coupled and moving one alone can cross a bad valley.
One inconsistency I noticed while writing this up: the report text describes the integer clamps as [1, 50] for vehicles, but the search space file says 5 to 60, and the crash data does contain configs up to 60 vehicles. The code is what ran.
Experiment
Budget: 501 evaluations per run (1 initial plus 100 iterations times 5 neighbours for the climber, 501 scenarios for random search). Five seeds per method: 42, 1042, 2042, 3042, 4042. Two modes: a fixed budget that counts all crashes found, and a first-crash mode that stops at the first one. Each evaluation draws a fresh episode seed, so the same configuration is not guaranteed to give the same outcome twice. That matters later.
What happened
Both methods found at least one crash in all five seeds. In the budget runs random search found more of them:
| seed | HC crashes | RS crashes | HC evals to first crash | RS evals to first crash |
|---|---|---|---|---|
| 42 | 13 | 14 | 2 | 17 |
| 1042 | 9 | 18 | 82 | 32 |
| 2042 | 8 | 14 | 24 | 26 |
| 3042 | 11 | 13 | 245 | 9 |
| 4042 | 8 | 14 | 72 | 30 |
| mean | 9.8 | 14.6 | 85.0 | 22.8 |
So 49 crashes for the hill climber against 73 for random search, out of 501 evaluations each. Time to first crash was worse for the climber, and much more variable: 2 evaluations on seed 42 (it started next to a crash) and 245 on seed 3042. Random search ranged from 9 to 32. In the separate first-crash runs the report records 85.0 evaluations and 346.6 s mean for the climber against 22.8 and 84.2 s for random search.

Crashes found per seed in the 501-evaluation budget runs.

Evaluations to the first crash. The climber’s spread is large; random search is consistently low.
My reading is that this agent is easy to crash. If a large share of the space fails, random sampling hits a failure within a few tries and the climber’s neighbour evaluations are mostly overhead. A hill climber should only pay for itself when failures are rare and need a precise combination, and this was not that situation. I did not test that claim on a more robust agent, so it is a hypothesis, not a result.
What each method found
This is where the climber was more interesting, with a caveat I come back to below.
Random search crashes tended to sit in obviously hard setups. One example from seed 1042: 44 vehicles on 9 lanes with initial_spacing of 0.77. That is a crowded road, and a collision there says little about the policy.
The climber’s crashes were more often in configurations that are not crowded. One example from seed 3042: 17 vehicles on 5 lanes, initial_spacing 4.38, ego_spacing 3.25. Plenty of room, and it still crashed. I believe the spacing values were found by the search and not by luck, but I only have the final configuration, not a trace explaining the crash mechanism. The report speculates about a blind spot during a merge or a cut-in; I have not verified it by watching the episode, so treat that as a guess.
On the aggregate crash configurations the random search crashes had a somewhat higher mean vehicle count (31.2 versus 28.5), and the climber’s crashes clustered more in the density plots, with the report pointing to hotspots around 30 vehicles with about 2.5 m ego spacing and around 20 vehicles with about 3.5 m.

Crash density over vehicles_count and ego_spacing. The climber shows a few concentrated regions; random search spreads out.
I also ran a feature-importance analysis on the pooled crash configurations. Linear correlations with min_distance were all near zero. A random forest ranked ego_spacing first (0.26), then vehicles_count (0.25), initial_spacing (0.24), initial_lane_id (0.17) and lanes_count (0.09). The tidy interpretation is that the failure boundary is non-linear and spacing matters more than raw density.

Random forest importance for predicting min_distance among crash configurations.
Limits
These are the parts I would not defend strongly.
- Five seeds. Differences like 9.8 versus 14.6 crashes are consistent across seeds in direction, but I ran no significance test, and the first-crash numbers rest on five samples with a range from 2 to 245.
- Crash counts are not distinct failure modes. The “distinct failing scenarios” count equals the total crash count for both methods. It deduplicates by rounded configuration, not by cause, so two crashes in one region count twice.
- The feature-importance model is fitted on the crashes only. It predicts
min_distanceinside crash episodes, whose values are mostly small or negative, from a pool of 132 configurations. It says what varies among crashes, not what causes crashing versus not crashing. The correlation and importance numbers are weaker evidence than the text in the report suggests. - The numbers do not fully line up. The per-seed tables total 122 crashes (49 plus 73), while the pooled analysis file holds 132 (54 climber, 78 random). I did not reconcile this; the likely cause is that the pooled data also includes the first-crash runs, but that is an inference.
- Noise. Every evaluation uses a fresh episode seed, so fitness is a noisy measurement. The report says the climber sometimes rejected good moves because a lucky episode looked safe. That was an observation, and I did not quantify it. Re-evaluating configurations with fixed seeds would be the next check.
- The “clustering versus uniform” story is read from plots. I did not compute a clustering statistic. The picture is suggestive, and random restarts would also produce multiple clusters, which I cannot distinguish from other explanations.
- Oracle. A crash is the only failure signal. Scenarios where the agent forces others to brake hard, or drives badly without touching anything, count as passes. Time-to-collision would be a better second term.
- Odd configs in the data. Some logged crash configurations have
initial_lane_idvalues like 7 and 9, above the 0 to 4 range in the search space file. I did not track down where random search samples them from.
What I take from it
Random search is a strong baseline against a fragile agent and I should have expected that. The climber’s value was in the shape of the failures it found, not in how many or how fast, and the random restarts were what kept it from stalling on one bug. What I did not do is the experiment that would make the claim about rare failures: run both against a more robust agent and see whether the ordering flips.
- testing
- rl
- search
- python