feat(lane_route): second heuristic field and diagonal zone margin (R2.10)

- lattice_alt_field_enabled (default false), lattice_alt_field_relax_m,
  lattice_alt_field_switch_fraction: when the search on the first field
  has not finished at the switch point, it restarts once on a field with
  no extra clearance and the alternate relax. Exhausted searches do not
  switch. PlanInfo/eval/debug line report field_pass.
- lattice_field_zone_margin_deg (default 0): diagonal field steps through
  a direction zone must stay that far inside the deviation limit; at
  exactly 45 deg the field otherwise follows a diagonal a turning robot
  cannot hold (sim M4, M7, 4a timed out even with 10 s).
- lane_route_eval compiles the graph with the live master when it holds
  preferred cells, as the plugin does.
- Measured 10/11 (sim settings, 10 s, margin 15, alternate on): regress7
  hook_150 1719, kit 1718, no query lost, independent recheck 0 bad.
This commit is contained in:
2026-11-10 13:42:23 +07:00
parent 1d26e29899
commit a4b655b74b
8 changed files with 160 additions and 10 deletions

View File

@@ -82,6 +82,7 @@ struct PlanInfo
std::string lattice_smooth_stop; ///< accepted, no_route, verify_failed, budget (checks/states/memory), or deadline; empty when not run.
std::string graph_radius_pass; ///< "regular" or "tight" for a graph path; see PlannerConfig::regular_turn_radius_m.
std::string lattice_gap_pass; ///< "preferred" or "minimum" for a successful lattice path; empty otherwise.
std::string lattice_field_pass; ///< "primary" or "alternate": the field of the last lattice search; empty when none ran.
bool lattice_gap_exact = false; ///< True when the nearest lethal/unknown gap is certified within the scan margin; excludes map boundary.
};

View File

@@ -160,6 +160,19 @@ struct PlannerConfig
double lattice_field_extra_clearance_m = 0.0;
/// [m] Shrink inflated footprint half sizes for heuristic-field standability only; 0 is strict.
double lattice_field_standability_relax_m = 0.0;
/// Second heuristic field for queries the first one misleads. One 2D field cannot fit both kinds of narrow passage: a
/// strict field (extra clearance, no relax) closes passages the robot fits off the cell centre, an optimistic one lures
/// the search into post gaps no heading fits. When the first search has not finished at lattice_alt_field_switch_fraction
/// of the fallback slice, the search restarts once on a field without extra clearance and with
/// lattice_alt_field_relax_m. Search and verification rules are the same for both fields.
/// [deg] The heuristic field allows a diagonal grid step through a direction zone only within (deviation - margin) of the
/// zone direction. Grid steps are 45 deg apart, so at a deviation of exactly 45 deg the field would follow a diagonal at
/// the limit that a turning robot cannot hold. Axis steps keep the full deviation. 0 uses the plain rule. Search and
/// verification always use the full deviation.
double lattice_field_zone_margin_deg = 0.0;
bool lattice_alt_field_enabled = false;
double lattice_alt_field_relax_m = 0.035; ///< [m] Standability relax of the second field.
double lattice_alt_field_switch_fraction = 0.35; ///< Share of the fallback slice for the first field and search, in (0, 1).
double lattice_field_time_fraction = 0.4; ///< Fraction of a rung's fallback slice for the field.
int lattice_field_cell_factor = 0; ///< 0 selects automatically, 1 uses the original grid resolution.
int64_t lattice_field_max_cells = 1000000; ///< Maximum cells for automatic field sizing.

View File

@@ -117,6 +117,12 @@ std::string PlannerConfig::validate() const
else if (lattice_enabled && (!not_negative(lattice_tight_turn_cost_m) || !not_negative(lattice_field_extra_clearance_m) ||
!not_negative(lattice_field_standability_relax_m)))
error << "lattice_tight_turn_cost_m, lattice_field_extra_clearance_m and lattice_field_standability_relax_m must be finite and >= 0";
else if (lattice_enabled && (!not_negative(lattice_field_zone_margin_deg) || lattice_field_zone_margin_deg >= 45.0))
error << "lattice_field_zone_margin_deg must be in [0, 45)";
else if (lattice_enabled && lattice_alt_field_enabled &&
(!not_negative(lattice_alt_field_relax_m) || !positive(lattice_alt_field_switch_fraction) ||
lattice_alt_field_switch_fraction >= 1.0))
error << "lattice_alt_field_relax_m must be finite and >= 0; lattice_alt_field_switch_fraction must be in (0, 1)";
else if (lattice_enabled && (!not_negative(lattice_preferred_gap_m) ||
!positive(lattice_preferred_time_fraction) || lattice_preferred_time_fraction > 1.0))
error << "lattice_preferred_gap_m must be >= 0; lattice_preferred_time_fraction must be in (0, 1]";

View File

@@ -46,7 +46,8 @@ double LatticeField::at(double x, double y, const LatticeContext& context) const
return complete ? std::min(maximum, frontier + fallback) : std::max(frontier, fallback);
}
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m)
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m,
double relax_m)
{
const GridRef& g = context.grid;
const auto cellCount = [&](int factor) -> int64_t {
@@ -94,7 +95,7 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
// This changes only the heuristic; search and verification use the full footprint and gap.
Standability standability(g, config, &context.memory, &context.info.lattice_standability_tests,
&context.info.lattice_standability_rejected, context.request.cancel, &field_deadline,
config.lattice_field_standability_relax_m);
relax_m >= 0.0 ? relax_m : config.lattice_field_standability_relax_m);
const auto fits = [&](std::size_t cell) {
return (extra_clearance_m <= 0.0 || g.clearance_mm[cell] >= fit_mm) &&
standability.fits(cell);
@@ -159,6 +160,12 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
}
return state == 1;
};
// Grid steps come in 45 deg increments, so a deviation of exactly 45 deg lets the field step diagonally through a zone at
// the very limit of the rule. A robot that has to turn into and out of such a line cannot follow it, and the search floods
// the region until the budget runs out (sim dumps M4, M7: solved in 1 s at 0-30 deg, timeout at 45 deg even with 10 s).
// The margin applies to diagonal steps only: applied to axis steps it also closed the perpendicular steps at 90 deg and
// slowed regress7 sim_005_zones from under 1 s to 2-3 s. Search and verification still use the full rule.
const double diagonal_deviation = std::max(0.0, context.deviation_deg - context.config.lattice_field_zone_margin_deg);
// Start and goal are always free here: a robot parked closer than the fit rule must still get a field; the C-space decides the rest.
const auto isFree = [&](int32_t cell) {
if (cell == start || cell == goal) return true;
@@ -202,7 +209,8 @@ bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint f
const int32_t next = ny * width + nx;
if (!isFree(next)) continue;
const double angle = std::atan2(-dy, -dx);
if (!allows(zone(next), angle, context.deviation_deg) || !allows(zone(top.cell), angle, context.deviation_deg)) continue;
const double step_deviation = dx != 0 && dy != 0 ? diagonal_deviation : context.deviation_deg;
if (!allows(zone(next), angle, step_deviation) || !allows(zone(top.cell), angle, step_deviation)) continue;
const double clearance_m = std::max(0.05, clear(top.cell) / 1000.0);
const float next_distance = static_cast<float>(top.distance + std::hypot(dx, dy) * field_resolution *
(1.0 + context.config.lattice_field_clearance_weight_m / clearance_m));

View File

@@ -162,7 +162,9 @@ bool rejectLatticeCrossing(LatticeContext& context, const Path& path);
bool verifyLatticeCandidate(LatticeContext& context, const Path& path, PathCheck* check);
/** Reverse field from the goal. extra_clearance_m raises the passable threshold of the field only (see
* PlannerConfig::lattice_field_extra_clearance_m). The field must be empty. */
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m = 0.0);
/** @param relax_m Standability relax of this field; a negative value takes lattice_field_standability_relax_m. */
bool buildLatticeField(LatticeContext& context, LatticeField* field, TimePoint field_deadline, double extra_clearance_m = 0.0,
double relax_m = -1.0);
bool searchLattice(LatticeContext& context, LatticeCSpace& space, const LatticeField& field, Path* path, PathCheck* check);
void smoothLatticePath(LatticeContext& context, LatticeCSpace& space, Path* path, PathCheck* check);
bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output);

View File

@@ -42,6 +42,7 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
LatticeContext context{ input.request, input.grid, input.config, input.deviation_deg, input.deadline, output.info, memory };
output.info.lattice_reason.clear();
output.info.lattice_gap_pass.clear();
output.info.lattice_field_pass.clear();
output.info.lattice_gap_exact = false;
bool ok = false;
bool search_timed_out = false;
@@ -53,7 +54,17 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
{
LatticeCSpace space(context);
LatticeField field(&memory);
LatticeField alternate(&memory);
const LatticeField* used = &field;
const TimePoint field_deadline = began + std::chrono::duration_cast<Clock::duration>(slice * input.config.lattice_field_time_fraction);
// The second field runs only when it differs from the first one.
const double alternate_relax = input.config.lattice_alt_field_relax_m;
const bool use_alternate = input.config.lattice_alt_field_enabled &&
(input.config.lattice_field_extra_clearance_m > 0.0 ||
std::abs(input.config.lattice_field_standability_relax_m - alternate_relax) > 1e-9);
const TimePoint switch_at = use_alternate
? began + std::chrono::duration_cast<Clock::duration>(slice * input.config.lattice_alt_field_switch_fraction)
: input.deadline;
bool field_ok = false;
{
StageTimer timer(output.info.lattice_field_ms);
@@ -73,10 +84,34 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
}
if (field_ok)
{
output.info.lattice_field_pass = "primary";
context.deadline = switch_at;
{
StageTimer timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
ok = searchLattice(context, space, field, &output.path, &output.check);
}
context.deadline = input.deadline;
// Only an interrupted search can gain from another heuristic: the field orders the states but does not change which
// are legal, so an exhausted search already proved there is no path.
if (!ok && use_alternate && !context.cancelled() && Clock::now() >= switch_at && Clock::now() < input.deadline &&
output.info.lattice_reason != "lattice_exhausted" && output.info.lattice_reason != "lattice_resource_limit")
{
bool alternate_ok = false;
{
StageTimer timer(output.info.lattice_field_ms);
const TimePoint now = Clock::now();
const TimePoint alternate_deadline =
now + std::chrono::duration_cast<Clock::duration>((input.deadline - now) * input.config.lattice_field_time_fraction);
alternate_ok = buildLatticeField(context, &alternate, alternate_deadline, 0.0, alternate_relax);
}
if (alternate_ok && !context.cancelled() && Clock::now() < input.deadline)
{
output.info.lattice_field_pass = "alternate";
used = &alternate;
StageTimer timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
ok = searchLattice(context, space, alternate, &output.path, &output.check);
}
}
search_timed_out = !ok && Clock::now() >= input.deadline;
if (ok) output.info.lattice_gap_pass = "minimum";
if (ok && preferred && !context.cancelled() && Clock::now() < input.deadline)
@@ -107,7 +142,7 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
bool found = false;
{
StageTimer search_timer(output.info.lattice_search_ms, &output.info.lattice_verify_ms);
found = searchLattice(context, space, field, &candidate, &candidate_check);
found = searchLattice(context, space, *used, &candidate, &candidate_check);
}
if (found)
{
@@ -139,12 +174,13 @@ bool planLatticeTier(const LatticeTierInput& input, GraphTierOutput output)
}
if (ok && !search_timed_out && input.config.lattice_smooth_enabled && !context.cancelled())
{
// The wavefront is no longer needed; release it before the smoother allocates its anchor graph.
// The wavefronts are no longer needed; release them before the smoother allocates its anchor graph.
for (LatticeField* done : { &field, &alternate })
{
std::pmr::vector<float> values(&memory);
std::pmr::vector<uint8_t> settled(&memory);
values.swap(field.values);
settled.swap(field.settled);
values.swap(done->values);
settled.swap(done->settled);
}
const TimePoint smooth_began = Clock::now();
context.deadline = input.smooth_deadline;

View File

@@ -1457,6 +1457,80 @@ TEST(Lattice, GraphUnavailableFallsBackOnSameRung)
EXPECT_DOUBLE_EQ(result.info.rung_used_deg, 90.0);
}
TEST(Lattice, FieldZoneMarginKeepsDiagonalStepsOffTheLimit)
{
LatticeTestGrid map(100, 100);
std::fill(map.direction.begin(), map.direction.end(), 1); // One zone along +x everywhere.
PlannerConfig config = latticeConfig();
// The goal lies 45 deg off the zone direction: only diagonal steps reach it from the start.
const RouteRequest request = latticeRequest(pose(1.025, 1.025, kPi / 4.0), pose(2.025, 2.025, kPi / 4.0));
const GridRef grid = map.view();
const std::size_t start_cell = static_cast<std::size_t>(20) * map.width + 20;
const auto settled = [&](double margin_deg) {
config.lattice_field_zone_margin_deg = margin_deg;
PlanInfo info;
detail::LatticeMemory memory(16 * 1024 * 1024);
const auto deadline = detail::Clock::now() + std::chrono::seconds(10);
detail::LatticeContext context{ request, grid, config, 45.0, deadline, info, memory };
detail::LatticeField field(&memory);
EXPECT_TRUE(detail::buildLatticeField(context, &field, deadline));
EXPECT_EQ(field.factor, 1);
return field.settled[start_cell] != 0;
};
EXPECT_TRUE(settled(0.0)); // At 45 deg the plain field steps diagonally at the limit of the rule.
EXPECT_FALSE(settled(15.0)); // With the margin it may not.
config.lattice_field_zone_margin_deg = 45.0;
EXPECT_FALSE(config.validate().empty());
config.lattice_field_zone_margin_deg = -1.0;
EXPECT_FALSE(config.validate().empty());
}
TEST(Lattice, AlternateFieldValidation)
{
PlannerConfig config = latticeConfig();
config.lattice_alt_field_enabled = true;
EXPECT_TRUE(config.validate().empty());
config.lattice_alt_field_switch_fraction = 0.0;
EXPECT_FALSE(config.validate().empty());
config.lattice_alt_field_switch_fraction = 1.0;
EXPECT_FALSE(config.validate().empty());
config.lattice_alt_field_switch_fraction = 0.35;
config.lattice_alt_field_relax_m = -0.01;
EXPECT_FALSE(config.validate().empty());
config.lattice_alt_field_enabled = false; // Unused values are not checked.
EXPECT_TRUE(config.validate().empty());
}
TEST(Lattice, AlternateFieldTakesOverAfterTheSwitchPoint)
{
LatticeTestGrid map(120, 120);
PlannerConfig config = latticeConfig();
config.lattice_field_extra_clearance_m = 0.1; // The first field differs from the alternate one.
const RouteRequest request = latticeRequest(pose(1.0, 3.0, 0.0), pose(4.0, 3.0, 0.0));
const PlanResult primary = map.plan(request, config);
ASSERT_TRUE(primary.ok) << primary.info.reason;
EXPECT_EQ(primary.info.lattice_field_pass, "primary");
// A switch point in the first microseconds stops the first search at once; the second field must still find the path.
config.lattice_alt_field_enabled = true;
config.lattice_alt_field_switch_fraction = 1e-6;
const PlanResult alternate = map.plan(request, config);
ASSERT_TRUE(alternate.ok) << alternate.info.reason;
EXPECT_EQ(alternate.info.tier_used, "lattice");
EXPECT_EQ(alternate.info.lattice_field_pass, "alternate");
EXPECT_TRUE(alternate.check.valid);
EXPECT_GE(alternate.info.min_gap_m, config.footprint_min_gap_m);
// Same field twice is pointless: no extra clearance and the same relax keep the first field to the end.
config.lattice_field_extra_clearance_m = 0.0;
config.lattice_field_standability_relax_m = config.lattice_alt_field_relax_m;
config.lattice_alt_field_switch_fraction = 0.35;
const PlanResult same = map.plan(request, config);
ASSERT_TRUE(same.ok) << same.info.reason;
EXPECT_EQ(same.info.lattice_field_pass, "primary");
}
TEST(Lattice, SameStartAndGoal)
{
LatticeTestGrid map(100, 100);

View File

@@ -146,6 +146,9 @@ void applyOverrides(PlannerConfig* config)
{ "lattice_tight_turn_cost_m", &config->lattice_tight_turn_cost_m },
{ "lattice_field_extra_clearance_m", &config->lattice_field_extra_clearance_m },
{ "lattice_field_standability_relax_m", &config->lattice_field_standability_relax_m },
{ "lattice_alt_field_relax_m", &config->lattice_alt_field_relax_m },
{ "lattice_field_zone_margin_deg", &config->lattice_field_zone_margin_deg },
{ "lattice_alt_field_switch_fraction", &config->lattice_alt_field_switch_fraction },
};
const std::map<std::string, int*> ints{
{ "lattice_heading_bins", &config->lattice_heading_bins },
@@ -163,6 +166,7 @@ void applyOverrides(PlannerConfig* config)
const std::map<std::string, bool*> bools{
{ "lattice_smooth_enabled", &config->lattice_smooth_enabled },
{ "lattice_tight_arcs", &config->lattice_tight_arcs },
{ "lattice_alt_field_enabled", &config->lattice_alt_field_enabled },
};
std::stringstream stream(text);
std::string item;
@@ -332,14 +336,19 @@ int main(int argc, char** argv)
coarse = &found->second;
}
// The plugin compiles with the live master, whose preferred cells add their region centre lines to the graph. A live
// grid with preferred cells is compiled the same way here and not cached, since another row may carry other zones.
const bool live_preferred = std::find(live_cost.begin(), live_cost.end(), kPreferredSpace) != live_cost.end();
std::map<double, std::shared_ptr<const CompiledGraph>> row_graphs;
for (double deviation : deviations)
{
config.max_deviation_deg = deviation;
const GraphProvider provider = [&](double limit) -> std::shared_ptr<const CompiledGraph> {
if (coarse == nullptr) return {};
auto& slot = map->graphs[std::make_pair(variant, limit)];
auto& slot = live_preferred ? row_graphs[limit] : map->graphs[std::make_pair(variant, limit)];
if (slot == nullptr)
slot = std::make_shared<const CompiledGraph>(compileGraph(*coarse, compile_grid, compile_grid.cost, limit, config, backend));
slot = std::make_shared<const CompiledGraph>(
compileGraph(*coarse, compile_grid, live_preferred ? live_cost.data() : compile_grid.cost, limit, config, backend));
return slot;
};
const PlanResult result = planRoute(provider, request, live_grid, config, backend);
@@ -381,6 +390,7 @@ int main(int argc, char** argv)
<< ",\"lattice_smooth_rejects_verify\":" << info.lattice_smooth_rejects_verify
<< ",\"lattice_smooth_stop\":\"" << info.lattice_smooth_stop << "\""
<< ",\"lattice_gap_pass\":\"" << info.lattice_gap_pass << "\""
<< ",\"lattice_field_pass\":\"" << info.lattice_field_pass << "\""
<< ",\"lattice_gap_exact\":" << (info.lattice_gap_exact ? 1 : 0)
<< ",\"lattice_min_gap_m\":";
if (result.ok && info.tier_used == "lattice" && std::isfinite(info.min_gap_m)) out << info.min_gap_m;