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

@@ -1099,6 +1099,169 @@ void expectLatticeMotion(const PlanResult& result, const PlannerConfig& config)
} // namespace
namespace
{
class DetourUpgradeTest : public ::testing::Test
{
protected:
LatticeTestGrid map{300, 240};
PlannerConfig config = latticeConfig();
RouteRequest request{pose(1.5, 5.0, 0.0), pose(13.5, 5.0, 0.0), {}};
CoarseGraph coarse;
void SetUp() override
{
for (int y = 50; y < 170; ++y)
for (int x = 70; x < 230; ++x)
map.direction[static_cast<std::size_t>(y * map.width + x)] = 3;
coarse.nodes = {{2.0, 8.8}, {13.0, 8.8}};
coarse.edges.push_back({0, 1, {coarse.nodes[0], coarse.nodes[1]}});
config.deviation_ladder_deg.clear();
config.max_deviation_deg = 90.0;
config.zone_deviation_weight = 20.0;
config.suspicious_ratio = 1.0;
config.suspicious_margin_m = 0.0;
}
void usePreferredDetour()
{
std::fill(map.direction.begin(), map.direction.end(), 0);
std::fill(map.cost.begin(), map.cost.end(), kUnpreferredSpace);
// A low-cost U-shaped corridor biases the graph, while the lattice ignores soft cost values.
for (int y = 75; y <= 185; ++y)
for (int x = 20; x < 280; ++x)
if (y >= 165 || x < 50 || x >= 250)
map.cost[static_cast<std::size_t>(y * map.width + x)] = kPreferredSpace;
coarse.nodes = {{1.5, 8.6}, {13.5, 8.6}};
coarse.edges = {{0, 1, {coarse.nodes[0], coarse.nodes[1]}}};
config.zone_deviation_weight = 0.0;
}
PlanResult run()
{
const auto grid = map.view();
const GraphProvider provider = [&](double deviation) {
return std::make_shared<const CompiledGraph>(compileGraph(coarse, grid, grid.cost, deviation, config, RidgeBackend{}));
};
return planRoute(provider, request, grid, config, RidgeBackend{});
}
};
void expectSamePath(const Path& a, const Path& b)
{
ASSERT_EQ(a.size(), b.size());
for (std::size_t i = 0; i < a.size(); ++i)
{
EXPECT_DOUBLE_EQ(a[i].x, b[i].x);
EXPECT_DOUBLE_EQ(a[i].y, b[i].y);
EXPECT_DOUBLE_EQ(a[i].yaw, b[i].yaw);
EXPECT_DOUBLE_EQ(a[i].curvature, b[i].curvature);
}
}
} // namespace
TEST(DetourUpgradeConfig, ValidatesBounds)
{
PlannerConfig config;
EXPECT_TRUE(config.validate().empty());
config.detour_upgrade_time_limit_s = 0.0;
EXPECT_FALSE(config.validate().empty());
config.detour_upgrade_time_limit_s = 2.0;
config.detour_upgrade_min_saving = 1.0;
EXPECT_FALSE(config.validate().empty());
config.detour_upgrade_min_saving = std::numeric_limits<double>::quiet_NaN();
EXPECT_FALSE(config.validate().empty());
config.detour_upgrade_min_saving = 0.15;
config.detour_upgrade_max_extra_zone_deviation_m = -0.1;
EXPECT_FALSE(config.validate().empty());
config.detour_upgrade_max_extra_zone_deviation_m = std::numeric_limits<double>::infinity();
EXPECT_FALSE(config.validate().empty());
}
TEST_F(DetourUpgradeTest, TakesVerifiedShorterLattice)
{
usePreferredDetour();
const auto result = run();
ASSERT_TRUE(result.ok) << result.info.reason;
EXPECT_TRUE(result.check.valid);
EXPECT_EQ(result.info.detour_upgrade_outcome, "taken");
EXPECT_EQ(result.info.tier_used, "lattice");
EXPECT_LT(result.info.detour_upgrade_lattice_length_m, 0.85 * result.info.detour_upgrade_graph_length_m);
}
TEST_F(DetourUpgradeTest, InsufficientSavingKeepsGraph)
{
usePreferredDetour();
config.detour_upgrade_enabled = false;
const auto baseline = run();
config.detour_upgrade_enabled = true;
config.detour_upgrade_min_saving = 0.99;
const auto result = run();
ASSERT_TRUE(result.ok);
EXPECT_EQ(result.info.detour_upgrade_outcome, "kept_graph");
EXPECT_EQ(result.info.tier_used, "graph");
EXPECT_EQ(result.info.reason, baseline.info.reason);
EXPECT_EQ(result.info.retries, baseline.info.retries);
expectSamePath(result.path, baseline.path);
}
TEST_F(DetourUpgradeTest, ZoneDeviationGuardKeepsGraphDespiteLengthSaving)
{
usePreferredDetour();
for (int y = 90; y < 110; ++y)
for (int x = 70; x < 230; ++x)
map.direction[static_cast<std::size_t>(y * map.width + x)] = 2;
// The upper, preferred graph leg follows an eastbound zone; the short direct path cuts across the diagonal zone.
for (int y = 165; y < 185; ++y)
for (int x = 70; x < 230; ++x)
map.direction[static_cast<std::size_t>(y * map.width + x)] = 1;
config.detour_upgrade_enabled = false;
const auto graph = run();
config.detour_upgrade_enabled = true;
const auto result = run();
ASSERT_TRUE(graph.ok);
ASSERT_TRUE(result.ok);
EXPECT_EQ(result.info.detour_upgrade_outcome, "kept_graph_zone_deviation");
EXPECT_LT(result.info.detour_upgrade_lattice_cost, 0.85 * result.info.detour_upgrade_graph_cost);
EXPECT_LT(result.info.detour_upgrade_graph_zone_deviation_m, 0.5);
EXPECT_GT(result.info.detour_upgrade_lattice_zone_deviation_m,
result.info.detour_upgrade_graph_zone_deviation_m + config.detour_upgrade_max_extra_zone_deviation_m);
expectSamePath(result.path, graph.path);
}
TEST_F(DetourUpgradeTest, WeightedCostRejectsExtraDeviation)
{
config.detour_upgrade_max_extra_zone_deviation_m = 100.0;
const auto result = run();
ASSERT_TRUE(result.ok);
EXPECT_EQ(result.info.detour_upgrade_outcome, "kept_graph_zone_deviation");
EXPECT_LT(result.info.detour_upgrade_lattice_length_m, 0.85 * result.info.detour_upgrade_graph_length_m);
EXPECT_GE(result.info.detour_upgrade_lattice_cost, 0.85 * result.info.detour_upgrade_graph_cost);
}
TEST_F(DetourUpgradeTest, DisabledLeavesGraphUnchanged)
{
config.lattice_enabled = false;
const auto baseline = run();
config.lattice_enabled = true;
config.detour_upgrade_enabled = false;
const auto result = run();
ASSERT_TRUE(result.ok);
EXPECT_EQ(result.info.detour_upgrade_outcome, "off");
EXPECT_EQ(result.info.tier_used, "graph");
expectSamePath(result.path, baseline.path);
}
TEST_F(DetourUpgradeTest, ExpiredBudgetKeepsGraph)
{
config.detour_upgrade_enabled = false;
const auto baseline = run();
config.detour_upgrade_enabled = true;
config.detour_upgrade_time_limit_s = 1e-9;
const auto result = run();
ASSERT_TRUE(result.ok);
EXPECT_EQ(result.info.detour_upgrade_outcome, "lattice_failed");
EXPECT_LT(result.info.detour_upgrade_time_ms, 200.0);
expectSamePath(result.path, baseline.path);
}
TEST(Lattice, StraightAndExactGoalYaw)
{
LatticeTestGrid map(120, 120);