ClearWake computes optimal sea routes: A* over a graph of ocean waypoints, avoiding protected zones, optionally riding favourable currents. Two problems in it were more interesting than the pathfinding itself.
A* is only optimal if your heuristic under-estimates
A* guarantees the shortest path on one condition: the heuristic must never over-estimate the remaining cost. Great-circle distance to the destination is the textbook choice for maps, because nothing can be shorter than a straight line. Admissible, provably optimal, done.
Then I added ocean currents. A favourable current makes an edge *cheaper* to traverse than its physical length — in our model, down to half. Suddenly the straight-line distance is no longer a lower bound on cost: the true remaining cost can be half of it. The heuristic over-estimates, admissibility breaks, and A* quietly starts returning paths that are not optimal.
Quietly is the problem. Nothing crashes. You get a route, it looks like a route, and it is simply not the best one. The fix is one line — scale the heuristic by the largest discount any edge can receive, so it is a lower bound again:
# effective_weight() can discount an edge to 0.5x its haversine
# length, so the plain haversine heuristic would over-estimate and
# break admissibility; scale it down to the minimum possible cost.
h_scale = 0.5 if use_current_weights else 1.0
The lesson is not about currents. It is that A* has a correctness precondition which lives in your cost function, not in your search code — so changing how edges are priced can silently invalidate a search you wrote weeks earlier and never touched.
Eight thousand queries to plan one route
The eco strategy avoids restricted zones. Zones live in MongoDB as GeoJSON polygons with a 2dsphere index, and asking "does this edge cross a closed zone?" is a `$geoIntersects` query.
The obvious implementation checks every edge in the graph before searching. That is around eight thousand round trips to the database to plan a single route, and it took minutes.
But A* does not look at every edge — that is the entire point of it. It explores a small envelope around the promising direction and never touches the rest of the graph. So the check moved inside the search: an edge is tested against the database only when A* actually tries to expand it.
That drops it to roughly fifty to a hundred queries per route, and the runtime from minutes to under a second. Same answer, two orders of magnitude less work — purely from asking the question later.
It generalises: when an expensive predicate is filtering a search, pushing it into the search almost always beats precomputing it, because a good search algorithm is already spending its life avoiding most of the space.