feat(lane_route_core): try the lattice when a verified graph route is a suspicious detour

When the graph tier returns a verified path longer than
suspicious_ratio * chord + suspicious_margin_m, and the lattice tier is
enabled, run one lattice attempt within its own time budget and the request
deadline. The lattice path replaces the graph path only if it is verified, not
cancelled, and its cost (length plus zone_deviation_weight * deviation / 45)
is at least detour_upgrade_min_saving lower, without adding more than
detour_upgrade_max_extra_zone_deviation_m of zone-deviated length.

- PlannerConfig: detour_upgrade_enabled (true), detour_upgrade_time_limit_s
  [s] (2.0), detour_upgrade_min_saving (0.15, in [0, 1)),
  detour_upgrade_max_extra_zone_deviation_m [m] (0.5); validated in
  PlannerConfig::validate().
- PlanInfo: detour_upgrade_outcome (off, not_triggered, taken, kept_graph,
  kept_graph_zone_deviation, lattice_failed, cancelled), graph/lattice length,
  cost, zone deviation, time and lattice reason.
- lane_route_eval: overrides for the new keys, outcome fields in the JSON.
- Tests: lane_route_core_test 135/135.
This commit is contained in:
2026-11-16 14:01:11 +07:00
parent 4cb395f525
commit 6367f13d7d
6 changed files with 255 additions and 1 deletions

View File

@@ -106,6 +106,11 @@ std::string PlannerConfig::validate() const
else if (!positive(route_too_long_ratio) || !not_negative(route_too_long_margin_m) || !positive(suspicious_ratio) ||
!not_negative(suspicious_margin_m))
error << "route length limits must be finite, ratios > 0, margins >= 0";
else if (!positive(detour_upgrade_time_limit_s) || detour_upgrade_time_limit_s > 86400.0 ||
!not_negative(detour_upgrade_min_saving) || detour_upgrade_min_saving >= 1.0)
error << "detour upgrade time must be in (0, 86400] and minimum saving in [0, 1)";
else if (!not_negative(detour_upgrade_max_extra_zone_deviation_m))
error << "detour_upgrade_max_extra_zone_deviation_m must be finite and >= 0";
else if (max_route_attempts < 1)
error << "max_route_attempts must be >= 1";
else if (!positive(time_limit_s) || !positive(output_step_m))

View File

@@ -40,6 +40,17 @@ double polylineLength(const Path& path)
length += std::hypot(path[i].x - path[i - 1].x, path[i].y - path[i - 1].y);
return length;
}
ZoneDeviation pathZoneDeviation(const Path& path, const GridRef& grid)
{
ZoneDeviation total;
for (std::size_t i = 1; i < path.size(); ++i)
{
const auto part = lineZoneDeviation({path[i-1].x, path[i-1].y}, {path[i].x, path[i].y}, grid);
total.integral_deg_m += part.integral_deg_m;
total.deviated_length_m += part.deviated_length_m;
}
return total;
}
} // namespace
PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, const GridRef& live, const PlannerConfig& config,
@@ -53,6 +64,7 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c
{
PlanResult result;
const TimePoint began = Clock::now();
result.info.detour_upgrade_outcome = config.detour_upgrade_enabled ? "not_triggered" : "off";
const auto finish = [&]() {
result.info.query_ms = msSince(began);
if (request.cancel && request.cancel->load(std::memory_order_relaxed))
@@ -211,6 +223,54 @@ PlanResult planRoute(const GraphProvider& graphs, const RouteRequest& request, c
result.info.tier_used = "graph";
result.info.rung_used_deg = deviation;
result.info.rung_reasons.push_back(result.info.reason);
if (config.detour_upgrade_enabled && config.lattice_enabled && !request.lattice_only)
{
const double graph_length = polylineLength(result.path);
result.info.detour_upgrade_graph_length_m = graph_length;
if (graph_length > config.suspicious_ratio * chord + config.suspicious_margin_m)
{
const TimePoint upgrade_began = Clock::now();
const TimePoint upgrade_deadline = std::min(deadline, upgrade_began + std::chrono::duration_cast<Clock::duration>(
std::chrono::duration<double>(config.detour_upgrade_time_limit_s)));
Path candidate;
PathCheck candidate_check;
PlanInfo candidate_info = result.info;
const bool candidate_ok = upgrade_began < upgrade_deadline &&
detail::planLatticeTier({ request, live, lattice_config, deviation, upgrade_deadline, upgrade_deadline },
{ candidate, candidate_check, candidate_info });
const double candidate_length = candidate_ok ? polylineLength(candidate) : 0.0;
const std::string lattice_reason = candidate_info.lattice_reason;
// Match the graph's zone term on the live direction field, not the lattice's forced zero weight.
const ZoneDeviation graph_zone = has_zones ? pathZoneDeviation(result.path, live) : ZoneDeviation{};
const ZoneDeviation candidate_zone = candidate_ok && has_zones ? pathZoneDeviation(candidate, live) : ZoneDeviation{};
const double graph_cost = graph_length + config.zone_deviation_weight * graph_zone.integral_deg_m / 45.0;
const double candidate_cost = candidate_length + config.zone_deviation_weight * candidate_zone.integral_deg_m / 45.0;
const bool excess_deviation = candidate_zone.deviated_length_m >
graph_zone.deviated_length_m + config.detour_upgrade_max_extra_zone_deviation_m;
const bool length_saves = candidate_length < (1.0 - config.detour_upgrade_min_saving) * graph_length;
const bool cost_saves = candidate_cost < (1.0 - config.detour_upgrade_min_saving) * graph_cost;
const bool zone_rejected = excess_deviation || (length_saves && !cost_saves);
const bool cancelled = request.cancel && request.cancel->load(std::memory_order_relaxed);
const bool taken = !cancelled && candidate_ok && candidate_check.valid && !excess_deviation && cost_saves;
if (taken)
{
result.path = std::move(candidate);
result.check = candidate_check;
result.info = std::move(candidate_info);
result.info.tier_used = "lattice";
}
result.info.detour_upgrade_outcome = cancelled ? "cancelled" : taken ? "taken" : !candidate_ok ? "lattice_failed" :
zone_rejected ? "kept_graph_zone_deviation" : "kept_graph";
result.info.detour_upgrade_graph_length_m = graph_length;
result.info.detour_upgrade_lattice_length_m = candidate_length;
result.info.detour_upgrade_graph_cost = graph_cost;
result.info.detour_upgrade_lattice_cost = candidate_cost;
result.info.detour_upgrade_graph_zone_deviation_m = graph_zone.deviated_length_m;
result.info.detour_upgrade_lattice_zone_deviation_m = candidate_zone.deviated_length_m;
result.info.detour_upgrade_time_ms = msSince(upgrade_began);
result.info.detour_upgrade_lattice_reason = lattice_reason;
}
}
return finish();
}
// Preserve R0 early exits exactly while the new tier is disabled.