feat(lane_route_core): zone deviation cost and graph/lattice completeness fixes (R2.12)
- Graph tier: add zone_deviation_cost (weight * ds * dev_deg/45) to compiled, query and shortcut edges, so aligned travel is preferred and a diagonal is only used where a straight move is not possible. - Drop the route_too_long rejection; its relaxed standability reference ignored heading continuity and discarded verified long routes. - Graph retries: skip duplicate candidates and block the edge pair nearest the verifier failure location. - Lattice: heading-sector guide field, zone-transition retry, finer-bin refinement before reporting lattice_approx_exhausted, analytic connector best-cost choice. The lattice plans with zone_deviation_weight 0. - lane_route_eval: zone_deviation_weight and time_limit_s keys, zone_dev_len_m / zone_dev_integral output. Sim config: random 300 scenes hook 224 vs 212, kit 199 vs 196; regress7 3416 = R2.11 baseline; independent recheck 0 violations; 128 tests pass.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "route_planner_internal.h"
|
||||
#include "standability.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
@@ -34,6 +33,15 @@ bool hasSoftCost(const uint8_t* master, std::size_t count)
|
||||
return false;
|
||||
}
|
||||
|
||||
double segmentDistanceSquared(const Point2& p, const Point2& a, const Point2& b)
|
||||
{
|
||||
const double dx = b.x - a.x, dy = b.y - a.y;
|
||||
const double squared = dx * dx + dy * dy;
|
||||
const double t = squared > 0.0 ? std::clamp(((p.x - a.x) * dx + (p.y - a.y) * dy) / squared, 0.0, 1.0) : 0.0;
|
||||
const double x = a.x + t * dx - p.x, y = a.y + t * dy - p.y;
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
/** Route graph, then the same without soft cost values. Hard rules hold in both passes. */
|
||||
bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal, const GridRef& grid, const std::vector<WorldBox>* changed,
|
||||
TimePoint deadline, bool allow_soft_fallback, const GridRef& verify_grid, const PlannerConfig& config, Path* path,
|
||||
@@ -44,18 +52,13 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
clearanceThresholdMm(config.collision_check_distance_m), {} };
|
||||
context.info = info;
|
||||
context.cancel = cancel;
|
||||
Standability standability(verify_grid, config, std::pmr::get_default_resource(), &info->graph_standability_tests,
|
||||
&info->graph_standability_rejected, cancel, &deadline);
|
||||
context.standability = &standability;
|
||||
ReferenceMemo reference_memo;
|
||||
context.reference = &reference_memo;
|
||||
const Point2 start_point{ start.x, start.y };
|
||||
const Point2 goal_point{ goal.x, goal.y };
|
||||
const double dev = graph.deviation_deg;
|
||||
const std::size_t cell_count = static_cast<std::size_t>(grid.width) * static_cast<std::size_t>(grid.height);
|
||||
const double max_straight_m = std::hypot(static_cast<double>(grid.height), static_cast<double>(grid.width)) * grid.resolution;
|
||||
|
||||
QueryGraph query(graph);
|
||||
QueryGraph query(graph, grid, config.zone_deviation_weight);
|
||||
int32_t s = 0, g = 0;
|
||||
TimePoint timer = Clock::now();
|
||||
addQueryEdges(&query, context, start_point, goal_point, &s, &g);
|
||||
@@ -118,6 +121,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
|
||||
std::string reason;
|
||||
std::vector<int32_t> route;
|
||||
CandidateHistory candidates_seen;
|
||||
for (int pass = 0; pass < 2; ++pass)
|
||||
{
|
||||
const bool neutral_pass = pass == 1;
|
||||
@@ -218,15 +222,30 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
std::string why = candidate.why.reason;
|
||||
bool has_corner = candidate.why.has_corner;
|
||||
const Point2 corner = candidate.why.corner;
|
||||
if (candidate.ok)
|
||||
bool has_failure_location = false;
|
||||
Point2 failure_location{};
|
||||
const bool duplicate = candidate.ok && candidates_seen.seenOrRemember(candidate.path);
|
||||
if (duplicate)
|
||||
{
|
||||
const auto previous = candidates_seen.failure(candidate.path);
|
||||
why = previous.reason.empty() ? "independent_verification_failed" : previous.reason;
|
||||
has_failure_location = previous.located;
|
||||
failure_location = previous.location;
|
||||
has_corner = false;
|
||||
}
|
||||
if (candidate.ok && !duplicate)
|
||||
{
|
||||
if (trace_attempt != nullptr) trace_attempt->candidate_length_m = pathLength(candidate.path);
|
||||
timer = Clock::now();
|
||||
PathCheck path_check = verifyPath(candidate.path, verify_grid, dev, config, context.cancel, &deadline);
|
||||
has_failure_location = path_check.has_failure_location;
|
||||
failure_location = path_check.failure_location;
|
||||
if (trace_attempt != nullptr)
|
||||
{
|
||||
trace_attempt->zone_violations = path_check.zone_violations;
|
||||
trace_attempt->footprint_collisions = path_check.footprint_collisions;
|
||||
trace_attempt->has_failure_location = has_failure_location;
|
||||
trace_attempt->failure_location = failure_location;
|
||||
}
|
||||
bool ends_ok = endpointOk(candidate.path, start, goal);
|
||||
bool clear_ok = freeClearanceOk(candidate.path, verify_grid, context.min_mm);
|
||||
@@ -240,7 +259,6 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
}
|
||||
|
||||
bool gap_checked = false;
|
||||
bool reference_checked = false;
|
||||
double raise_used = 0.0;
|
||||
GapCheck gap;
|
||||
if (path_check.valid && ends_ok && clear_ok)
|
||||
@@ -261,7 +279,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
timer = Clock::now();
|
||||
Candidate alternative = constructCandidate(route, query, &context, master, start, goal, config.clearance_min_m + extra);
|
||||
info->build_ms += msSince(timer);
|
||||
if (!alternative.ok)
|
||||
if (!alternative.ok || candidates_seen.seenOrRemember(alternative.path))
|
||||
continue;
|
||||
timer = Clock::now();
|
||||
const PathCheck alternative_check = verifyPath(alternative.path, verify_grid, dev, config, context.cancel, &deadline);
|
||||
@@ -281,16 +299,6 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
continue;
|
||||
}
|
||||
const double length = pathLength(alternative.path);
|
||||
if (length > config.suspicious_ratio * distanceOf(start_point, goal_point) + config.suspicious_margin_m)
|
||||
{
|
||||
const double bound = (length - config.route_too_long_margin_m) / config.route_too_long_ratio;
|
||||
double reference = 0.0;
|
||||
if (cachedStandableReference(verify_grid, start_point, goal_point, dev, bound, &reference, standability, reference_memo,
|
||||
context.cancel, &deadline) &&
|
||||
length > config.route_too_long_ratio * reference + config.route_too_long_margin_m)
|
||||
continue;
|
||||
reference_checked = true;
|
||||
}
|
||||
candidate = std::move(alternative);
|
||||
if (trace_attempt != nullptr) trace_attempt->candidate_length_m = length;
|
||||
path_check = alternative_check;
|
||||
@@ -314,27 +322,8 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
}
|
||||
|
||||
const bool passed = path_check.valid && ends_ok && clear_ok && gap_checked && gap.valid;
|
||||
if (passed && !reference_checked)
|
||||
{
|
||||
const double length = pathLength(candidate.path);
|
||||
if (length > config.suspicious_ratio * distanceOf(start_point, goal_point) + config.suspicious_margin_m)
|
||||
{
|
||||
timer = Clock::now();
|
||||
const double bound = (length - config.route_too_long_margin_m) / config.route_too_long_ratio;
|
||||
double reference = 0.0;
|
||||
const bool known = cachedStandableReference(verify_grid, start_point, goal_point, dev, bound, &reference, standability,
|
||||
reference_memo, context.cancel, &deadline);
|
||||
if (trace_attempt != nullptr && known) trace_attempt->reference_length_m = reference;
|
||||
info->verify_ms += msSince(timer);
|
||||
if (known && length > config.route_too_long_ratio * reference + config.route_too_long_margin_m)
|
||||
{
|
||||
info->graph_reference_length_m = reference;
|
||||
reason = "route_too_long";
|
||||
trace_reason(reason);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// The 2D reference does not prove a forward, footprint-zone-legal shortcut exists.
|
||||
// A ratio against it therefore cannot invalidate a fully verified candidate.
|
||||
if (passed)
|
||||
{
|
||||
*path = std::move(candidate.path);
|
||||
@@ -350,6 +339,7 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
}
|
||||
why = gap_checked && !gap.valid ? "footprint_gap_below_minimum" :
|
||||
path_check.valid && ends_ok && !clear_ok ? "free_clearance_below_minimum" : "independent_verification_failed";
|
||||
candidates_seen.setFailure(candidate.path, {why, has_failure_location, failure_location});
|
||||
has_corner = false;
|
||||
}
|
||||
|
||||
@@ -362,10 +352,24 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
std::vector<std::pair<int32_t, int32_t>> pairs;
|
||||
if (!has_corner)
|
||||
{
|
||||
// Not proof that either edge is unusable. Try a different pair.
|
||||
// A shortcut may span several edges. Change the transition nearest the failure without
|
||||
// declaring either entire edge illegal for other routes.
|
||||
if (route.size() > 1)
|
||||
{
|
||||
const std::size_t index = std::min(static_cast<std::size_t>(attempt), route.size() - 2);
|
||||
std::size_t index = std::min(static_cast<std::size_t>(attempt), route.size() - 2);
|
||||
if (has_failure_location)
|
||||
{
|
||||
double nearest = kInfinity;
|
||||
for (std::size_t j = 0; j < route.size(); ++j)
|
||||
{
|
||||
const auto& points = query.edge(route[j]).points;
|
||||
for (std::size_t k = 1; k < points.size(); ++k)
|
||||
{
|
||||
const double d = segmentDistanceSquared(failure_location, points[k-1], points[k]);
|
||||
if (d < nearest) { nearest = d; index = std::min(j, route.size() - 2); }
|
||||
}
|
||||
}
|
||||
}
|
||||
pairs.emplace_back(route[index], route[index + 1]);
|
||||
}
|
||||
}
|
||||
@@ -388,6 +392,15 @@ bool planCore(const CompiledGraph& graph, const Pose2& start, const Pose2& goal,
|
||||
}
|
||||
if (pairs.empty())
|
||||
{
|
||||
if (!has_corner && route.size() == 1)
|
||||
{
|
||||
blocked[static_cast<std::size_t>(route.front())] = 1;
|
||||
if (trace_attempt != nullptr) trace_attempt->rejected_edge = route.front();
|
||||
++info->retries;
|
||||
reason = why;
|
||||
trace_reason(reason);
|
||||
continue;
|
||||
}
|
||||
reason = why;
|
||||
trace_reason(reason);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user