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.
565 lines
27 KiB
C++
565 lines
27 KiB
C++
/** Offline batch runner: plans scenes from text files and writes one JSON line per query, for comparison
|
|
* with the Python prototype. Usage:
|
|
* lane_route_eval <scenes.txt> <out.jsonl> <profile sehc|kit|hook_150> <deviations, e.g. 90,45> <ladder, e.g. 0.01 or none> [graph|fallback|lattice]
|
|
* Each scenes.txt line: id grid_file variant live_file|- has_start_yaw sx sy syaw has_goal_yaw gx gy gyaw box_count {x0 y0 x1 y1}
|
|
* Grid files are kept in memory, so scenes that share one map also share its graphs, as on the robot.
|
|
* Set LANE_ROUTE_EVAL_DUMP_GRAPH to a directory for per-query graph, Dijkstra attempts and final-path JSON. */
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include <lane_route_core/grid_checks.h>
|
|
#include <lane_route_core/route_planner.h>
|
|
|
|
#include <lane_route_core/costmap_backend.h>
|
|
|
|
using namespace lane_route_core;
|
|
|
|
namespace
|
|
{
|
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
double msSince(Clock::time_point begin) { return std::chrono::duration<double, std::milli>(Clock::now() - begin).count(); }
|
|
|
|
void jsonString(std::ostream& out, const std::string& value)
|
|
{
|
|
out << '"';
|
|
for (unsigned char c : value)
|
|
{
|
|
if (c == '"' || c == '\\') out << '\\' << static_cast<char>(c);
|
|
else if (c < 0x20) out << "\\u00" << "0123456789abcdef"[c >> 4] << "0123456789abcdef"[c & 15];
|
|
else out << static_cast<char>(c);
|
|
}
|
|
out << '"';
|
|
}
|
|
|
|
const char* edgeKind(EdgeKind kind)
|
|
{
|
|
switch (kind)
|
|
{
|
|
case EdgeKind::kVoronoi: return "kVoronoi";
|
|
case EdgeKind::kLane: return "kLane";
|
|
case EdgeKind::kJunction: return "kJunction";
|
|
case EdgeKind::kPreferredRidge: return "kPreferredRidge";
|
|
case EdgeKind::kPreferredAttach: return "kPreferredAttach";
|
|
case EdgeKind::kAttach: return "kAttach";
|
|
case EdgeKind::kDirect: return "kDirect";
|
|
case EdgeKind::kTemporaryLane: return "kTemporaryLane";
|
|
}
|
|
return "unknown";
|
|
}
|
|
|
|
void writeFinite(std::ostream& out, double value)
|
|
{
|
|
if (std::isfinite(value)) out << value;
|
|
else out << "null";
|
|
}
|
|
|
|
void writeGraphDump(const std::string& directory, std::size_t ordinal, const std::string& id, double deviation,
|
|
const RouteRequest& request, const GraphTrace& trace, const PlanResult& result)
|
|
{
|
|
try
|
|
{
|
|
std::filesystem::create_directories(directory);
|
|
std::ostringstream name;
|
|
name << "query_" << std::setw(5) << std::setfill('0') << ordinal << ".json";
|
|
std::ofstream out(std::filesystem::path(directory) / name.str());
|
|
if (!out) { std::cerr << "cannot write graph dump " << name.str() << std::endl; return; }
|
|
out.precision(17);
|
|
out << "{\"scene\":"; jsonString(out, id);
|
|
out << ",\"requested_deviation_deg\":"; writeFinite(out, deviation);
|
|
out << ",\"start\":["; writeFinite(out, request.start.x); out << ','; writeFinite(out, request.start.y); out << ',';
|
|
writeFinite(out, request.start.yaw); out << "],\"goal\":["; writeFinite(out, request.goal.x); out << ',';
|
|
writeFinite(out, request.goal.y); out << ','; writeFinite(out, request.goal.yaw);
|
|
out << "],\"ok\":" << (result.ok ? "true" : "false") << ",\"reason\":"; jsonString(out, result.info.reason);
|
|
out << ",\"graph_reason\":"; jsonString(out, result.info.graph_reason);
|
|
out << ",\"runs\":[";
|
|
for (std::size_t ri = 0; ri < trace.runs.size(); ++ri)
|
|
{
|
|
const GraphTraceRun& run = trace.runs[ri];
|
|
if (ri) out << ',';
|
|
out << "{\"source\":"; jsonString(out, run.source);
|
|
out << ",\"deviation_deg\":" << run.deviation_deg << ",\"min_turn_radius_m\":" << run.min_turn_radius_m
|
|
<< ",\"max_kink_deg\":" << run.max_kink_deg << ",\"corner_radii_m\":[";
|
|
for (std::size_t i = 0; i < run.corner_radii_m.size(); ++i) { if (i) out << ','; out << run.corner_radii_m[i]; }
|
|
out << "],\"base_nodes\":" << run.base_nodes << ",\"base_edges\":" << run.base_edges
|
|
<< ",\"start_node\":" << run.start_node << ",\"goal_node\":" << run.goal_node << ",\"nodes\":[";
|
|
for (std::size_t i = 0; i < run.nodes.size(); ++i)
|
|
{ if (i) out << ','; out << '['; writeFinite(out, run.nodes[i].x); out << ','; writeFinite(out, run.nodes[i].y); out << ']'; }
|
|
out << "],\"edges\":[";
|
|
for (std::size_t i = 0; i < run.edges.size(); ++i)
|
|
{
|
|
if (i) out << ',';
|
|
const GraphEdge& edge = run.edges[i];
|
|
double length = 0.0;
|
|
for (std::size_t j = 1; j < edge.points.size(); ++j) length += distanceOf(edge.points[j - 1], edge.points[j]);
|
|
out << "{\"id\":" << i << ",\"from\":" << edge.from << ",\"to\":" << edge.to << ",\"kind\":";
|
|
jsonString(out, edgeKind(edge.kind));
|
|
out << ",\"query_edge\":" << (i >= static_cast<std::size_t>(run.base_edges) ? "true" : "false")
|
|
<< ",\"length_m\":"; writeFinite(out, length); out << ",\"cost\":"; writeFinite(out, edge.cost);
|
|
out << ",\"kind_weight\":" << (edge.kind == EdgeKind::kVoronoi ? run.voronoi_weight : 1.0);
|
|
out << ",\"factor\":"; if (i < run.factors.size()) writeFinite(out, run.factors[i]); else out << "null";
|
|
out << ",\"neutral_factor\":"; if (i < run.neutral_factors.size()) writeFinite(out, run.neutral_factors[i]); else out << "null";
|
|
out << ",\"polyline\":[";
|
|
for (std::size_t j = 0; j < edge.points.size(); ++j)
|
|
{ if (j) out << ','; out << '['; writeFinite(out, edge.points[j].x); out << ','; writeFinite(out, edge.points[j].y); out << ']'; }
|
|
out << "]}";
|
|
}
|
|
out << "],\"initially_blocked_edges\":[";
|
|
for (std::size_t i = 0; i < run.initially_blocked_edges.size(); ++i)
|
|
{ if (i) out << ','; out << run.initially_blocked_edges[i]; }
|
|
out << "],\"attempts\":[";
|
|
for (std::size_t i = 0; i < run.attempts.size(); ++i)
|
|
{
|
|
if (i) out << ',';
|
|
const GraphTraceAttempt& attempt = run.attempts[i];
|
|
out << "{\"pass\":" << attempt.pass << ",\"attempt\":" << attempt.attempt << ",\"reason\":";
|
|
jsonString(out, attempt.reason);
|
|
out << ",\"rejected_edge\":" << attempt.rejected_edge << ",\"candidate_length_m\":" << attempt.candidate_length_m
|
|
<< ",\"reference_length_m\":" << attempt.reference_length_m << ",\"zone_violations\":" << attempt.zone_violations
|
|
<< ",\"footprint_collisions\":" << attempt.footprint_collisions << ",\"failure_location\":";
|
|
if (attempt.has_failure_location)
|
|
{ out << '['; writeFinite(out, attempt.failure_location.x); out << ','; writeFinite(out, attempt.failure_location.y); out << ']'; }
|
|
else out << "null";
|
|
out << ",\"blocked_pairs_added\":[";
|
|
for (std::size_t j = 0; j < attempt.blocked_pairs_added.size(); ++j)
|
|
{ if (j) out << ','; out << '[' << attempt.blocked_pairs_added[j].first << ',' << attempt.blocked_pairs_added[j].second << ']'; }
|
|
out << ']';
|
|
out << ",\"route\":[";
|
|
for (std::size_t j = 0; j < attempt.route.size(); ++j) { if (j) out << ','; out << attempt.route[j]; }
|
|
out << "]}";
|
|
}
|
|
out << "]}";
|
|
}
|
|
out << "],\"path\":[";
|
|
for (std::size_t i = 0; i < result.path.size(); ++i)
|
|
{ if (i) out << ','; const PathPoint& p = result.path[i]; out << '['; writeFinite(out, p.x); out << ','; writeFinite(out, p.y);
|
|
out << ','; writeFinite(out, p.yaw); out << ','; writeFinite(out, p.curvature); out << ']'; }
|
|
out << "]}\n";
|
|
if (!out) std::cerr << "failed to finish graph dump " << name.str() << std::endl;
|
|
}
|
|
catch (const std::exception& ex) { std::cerr << "graph dump: " << ex.what() << std::endl; }
|
|
}
|
|
|
|
std::vector<uint8_t> readRuns(std::istream& in, const std::string& name)
|
|
{
|
|
std::string key;
|
|
std::size_t runs = 0;
|
|
in >> key >> runs;
|
|
if (key != name)
|
|
throw std::runtime_error("expected " + name + ", got " + key);
|
|
std::vector<uint8_t> out;
|
|
for (std::size_t i = 0; i < runs; ++i)
|
|
{
|
|
int value = 0;
|
|
std::size_t count = 0;
|
|
in >> value >> count;
|
|
out.insert(out.end(), count, static_cast<uint8_t>(value));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** One map: compile grid, live grid, direction grids and the cached graphs per direction grid and limit. */
|
|
struct MapData
|
|
{
|
|
int32_t width = 0, height = 0;
|
|
double resolution = 0.0, origin_x = 0.0, origin_y = 0.0;
|
|
std::vector<uint8_t> static_cost; // No inflation, as the static layer provides.
|
|
std::vector<uint8_t> live_cost; // Master: with the inscribed ring and soft values.
|
|
std::vector<std::vector<uint8_t>> directions;
|
|
std::vector<uint16_t> clearance_mm;
|
|
double step1_ms = 0.0;
|
|
std::map<int, CoarseGraph> coarse;
|
|
std::map<std::pair<int, double>, std::shared_ptr<const CompiledGraph>> graphs;
|
|
};
|
|
|
|
std::shared_ptr<MapData> loadMap(const std::string& path, const robot_costmap_2d::ReferenceGraphParams& ridge_params)
|
|
{
|
|
std::ifstream in(path);
|
|
if (!in)
|
|
throw std::runtime_error("cannot open " + path);
|
|
auto map = std::make_shared<MapData>();
|
|
std::string key;
|
|
int version = 0, variants = 0;
|
|
in >> key >> version;
|
|
in >> key >> map->width >> map->height;
|
|
in >> key >> map->resolution;
|
|
in >> key >> map->origin_x >> map->origin_y;
|
|
map->live_cost = readRuns(in, "cost");
|
|
in >> key >> variants;
|
|
for (int i = 0; i < variants; ++i)
|
|
map->directions.push_back(readRuns(in, "direction"));
|
|
if (!in)
|
|
throw std::runtime_error("truncated grid " + path);
|
|
map->static_cost = map->live_cost;
|
|
for (uint8_t& value : map->static_cost)
|
|
if (value == kInscribedCost)
|
|
value = kFreeSpace;
|
|
|
|
GridRef grid;
|
|
grid.width = map->width;
|
|
grid.height = map->height;
|
|
grid.resolution = map->resolution;
|
|
grid.origin_x = map->origin_x;
|
|
grid.origin_y = map->origin_y;
|
|
grid.cost = map->static_cost.data();
|
|
const Clock::time_point began = Clock::now();
|
|
robot_costmap_2d::ReferenceGridView view;
|
|
view.cost = grid.cost;
|
|
view.geometry = geometryOf(grid);
|
|
map->clearance_mm = robot_costmap_2d::computeVoronoiSkeleton(view, ridge_params).clearance_mm;
|
|
map->step1_ms = msSince(began);
|
|
return map;
|
|
}
|
|
|
|
GridRef gridOf(const MapData& map, const std::vector<uint8_t>& cost, const uint16_t* clearance, int variant)
|
|
{
|
|
GridRef grid;
|
|
grid.width = map.width;
|
|
grid.height = map.height;
|
|
grid.resolution = map.resolution;
|
|
grid.origin_x = map.origin_x;
|
|
grid.origin_y = map.origin_y;
|
|
grid.cost = cost.data();
|
|
grid.clearance_mm = clearance;
|
|
grid.direction = map.directions[static_cast<std::size_t>(variant)].data();
|
|
return grid;
|
|
}
|
|
|
|
/** Offline tuning without a rebuild: LANE_ROUTE_EVAL_SET="name=value,name=value" overrides lattice, fallback and turn radius
|
|
* fields.
|
|
* Unknown names are an error, so a typo cannot silently run the defaults. */
|
|
void applyOverrides(PlannerConfig* config)
|
|
{
|
|
const char* text = std::getenv("LANE_ROUTE_EVAL_SET");
|
|
if (text == nullptr || *text == '\0')
|
|
return;
|
|
const std::map<std::string, double*> doubles{
|
|
{ "lattice_closed_resolution_m", &config->lattice_closed_resolution_m },
|
|
{ "zone_transition_m", &config->zone_transition_m },
|
|
{ "zone_deviation_weight", &config->zone_deviation_weight },
|
|
{ "lattice_preferred_gap_m", &config->lattice_preferred_gap_m },
|
|
{ "lattice_preferred_time_fraction", &config->lattice_preferred_time_fraction },
|
|
{ "lattice_heuristic_weight", &config->lattice_heuristic_weight },
|
|
{ "lattice_bucket_width_m", &config->lattice_bucket_width_m },
|
|
{ "lattice_field_clearance_weight_m", &config->lattice_field_clearance_weight_m },
|
|
{ "lattice_wall_weight_m", &config->lattice_wall_weight_m },
|
|
{ "lattice_switch_cost_m", &config->lattice_switch_cost_m },
|
|
{ "lattice_steer_cost_m", &config->lattice_steer_cost_m },
|
|
{ "lattice_analytic_radius_m", &config->lattice_analytic_radius_m },
|
|
{ "lattice_analytic_near_goal_m", &config->lattice_analytic_near_goal_m },
|
|
{ "lattice_field_time_fraction", &config->lattice_field_time_fraction },
|
|
{ "detour_upgrade_time_limit_s", &config->detour_upgrade_time_limit_s },
|
|
{ "detour_upgrade_max_extra_zone_deviation_m", &config->detour_upgrade_max_extra_zone_deviation_m },
|
|
{ "detour_upgrade_min_saving", &config->detour_upgrade_min_saving },
|
|
{ "fallback_time_limit_s", &config->fallback_time_limit_s },
|
|
{ "time_limit_s", &config->time_limit_s },
|
|
{ "lattice_end_relax_radius_m", &config->lattice_end_relax_radius_m },
|
|
{ "min_turn_radius_m", &config->min_turn_radius_m },
|
|
{ "max_kink_deg", &config->max_kink_deg },
|
|
{ "regular_turn_radius_m", &config->regular_turn_radius_m },
|
|
{ "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 },
|
|
{ "lattice_primitive_multiple_2", &config->lattice_primitive_multiple_2 },
|
|
{ "lattice_primitive_multiple_3", &config->lattice_primitive_multiple_3 },
|
|
{ "lattice_analytic_interval", &config->lattice_analytic_interval },
|
|
{ "lattice_analytic_goal_headings", &config->lattice_analytic_goal_headings },
|
|
{ "lattice_field_cell_factor", &config->lattice_field_cell_factor },
|
|
};
|
|
const std::map<std::string, int64_t*> longs{
|
|
{ "lattice_field_max_cells", &config->lattice_field_max_cells },
|
|
{ "lattice_max_memory_mb", &config->lattice_max_memory_mb },
|
|
{ "lattice_smooth_max_checks", &config->lattice_smooth_max_checks },
|
|
};
|
|
const std::map<std::string, bool*> bools{
|
|
{ "detour_upgrade_enabled", &config->detour_upgrade_enabled },
|
|
{ "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;
|
|
while (std::getline(stream, item, ','))
|
|
{
|
|
const std::size_t equals = item.find('=');
|
|
if (equals == std::string::npos)
|
|
throw std::runtime_error("bad LANE_ROUTE_EVAL_SET item: " + item);
|
|
const std::string key = item.substr(0, equals);
|
|
const std::string value = item.substr(equals + 1);
|
|
if (doubles.count(key) != 0)
|
|
*doubles.at(key) = std::stod(value);
|
|
else if (ints.count(key) != 0)
|
|
*ints.at(key) = std::stoi(value);
|
|
else if (longs.count(key) != 0)
|
|
*longs.at(key) = std::stoll(value);
|
|
else if (bools.count(key) != 0)
|
|
*bools.at(key) = std::stoi(value) != 0;
|
|
else if (key == "corner_radii_m")
|
|
{
|
|
// A list inside the comma-separated overrides uses '/', e.g. corner_radii_m=1.9/1.3/0.6, largest first.
|
|
std::stringstream radii(value);
|
|
std::string radius;
|
|
config->corner_radii_m.clear();
|
|
while (std::getline(radii, radius, '/'))
|
|
config->corner_radii_m.push_back(std::stod(radius));
|
|
}
|
|
else
|
|
throw std::runtime_error("unknown LANE_ROUTE_EVAL_SET key: " + key);
|
|
std::cerr << "override " << key << "=" << value << std::endl;
|
|
}
|
|
}
|
|
|
|
std::vector<double> parseList(const std::string& text)
|
|
{
|
|
std::vector<double> out;
|
|
if (text == "none")
|
|
return out;
|
|
std::stringstream stream(text);
|
|
std::string item;
|
|
while (std::getline(stream, item, ','))
|
|
out.push_back(std::stod(item));
|
|
return out;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
if (argc != 6 && argc != 7)
|
|
{
|
|
std::cerr << "usage: lane_route_eval <scenes.txt> <out.jsonl> <sehc|kit|hook_150> <deviations> <ladder|none> [graph|fallback|lattice]" << std::endl;
|
|
return 2;
|
|
}
|
|
const std::string mode = argc == 7 ? argv[6] : "graph";
|
|
if (mode != "graph" && mode != "fallback" && mode != "lattice")
|
|
{
|
|
std::cerr << "invalid tier mode: " << mode << std::endl;
|
|
return 2;
|
|
}
|
|
PlannerConfig config;
|
|
config.lattice_enabled = mode != "graph";
|
|
if (std::string(argv[3]) == "kit")
|
|
{
|
|
config.half_x_m = 0.325;
|
|
config.half_y_m = 0.345;
|
|
config.clearance_hard_m = 0.36;
|
|
config.clearance_min_m = 0.36;
|
|
config.clearance_preferred_m = 0.625;
|
|
config.collision_check_distance_m = 0.53;
|
|
}
|
|
else if (std::string(argv[3]) == "hook_150")
|
|
{
|
|
config.half_x_m = 0.511;
|
|
config.half_y_m = 0.1955;
|
|
config.clearance_hard_m = 0.2305;
|
|
config.clearance_min_m = 0.2305;
|
|
config.clearance_preferred_m = 0.4955;
|
|
config.collision_check_distance_m = 0.603;
|
|
}
|
|
robot_costmap_2d::ReferenceGraphParams ridge_params = ridgeParams(config);
|
|
if (std::string(argv[3]) == "hook_150")
|
|
{
|
|
ridge_params.min_clearance_m = 0.335;
|
|
ridge_params.ridge_min_separation_m = 0.7;
|
|
}
|
|
config.deviation_ladder_deg = parseList(argv[5]);
|
|
const std::vector<double> deviations = parseList(argv[4]);
|
|
applyOverrides(&config);
|
|
const RidgeBackend backend = makeRidgeBackend(config, ridge_params);
|
|
|
|
std::ifstream scenes(argv[1]);
|
|
std::ofstream out(argv[2]);
|
|
if (!scenes || !out)
|
|
{
|
|
std::cerr << "cannot open input or output" << std::endl;
|
|
return 2;
|
|
}
|
|
out.precision(17);
|
|
|
|
std::map<std::string, std::shared_ptr<MapData>> maps;
|
|
const char* graph_dump_dir = std::getenv("LANE_ROUTE_EVAL_DUMP_GRAPH");
|
|
const bool graph_dump_enabled = graph_dump_dir != nullptr && *graph_dump_dir != '\0';
|
|
std::size_t graph_dump_ordinal = 0;
|
|
std::string line;
|
|
while (std::getline(scenes, line))
|
|
{
|
|
if (line.empty())
|
|
continue;
|
|
std::istringstream row(line);
|
|
std::string id, grid_file, live_file;
|
|
int variant = 0, has_start = 0, has_goal = 0, boxes = 0;
|
|
RouteRequest request;
|
|
request.lattice_only = mode == "lattice";
|
|
row >> id >> grid_file >> variant >> live_file >> has_start >> request.start.x >> request.start.y >> request.start.yaw >> has_goal >>
|
|
request.goal.x >> request.goal.y >> request.goal.yaw >> boxes;
|
|
request.start.has_yaw = has_start != 0;
|
|
request.goal.has_yaw = has_goal != 0;
|
|
|
|
auto& map = maps[grid_file];
|
|
if (map == nullptr)
|
|
map = loadMap(grid_file, ridge_params);
|
|
|
|
// Live master: a recorded merged grid, or the map with boxes painted lethal.
|
|
std::vector<uint8_t> live_cost = map->live_cost;
|
|
if (live_file != "-")
|
|
{
|
|
std::ifstream live(live_file);
|
|
live_cost = readRuns(live, "cost");
|
|
}
|
|
const double resolution = map->resolution;
|
|
for (int b = 0; b < boxes; ++b)
|
|
{
|
|
double x_min, y_min, x_max, y_max;
|
|
row >> x_min >> y_min >> x_max >> y_max;
|
|
const int32_t x0 = std::max<int32_t>(0, static_cast<int32_t>(std::floor((x_min - map->origin_x) / resolution)));
|
|
const int32_t x1 = std::min<int32_t>(map->width, static_cast<int32_t>(std::ceil((x_max - map->origin_x) / resolution)));
|
|
const int32_t y0 = std::max<int32_t>(0, static_cast<int32_t>(std::floor((y_min - map->origin_y) / resolution)));
|
|
const int32_t y1 = std::min<int32_t>(map->height, static_cast<int32_t>(std::ceil((y_max - map->origin_y) / resolution)));
|
|
for (int32_t y = y0; y < y1; ++y)
|
|
for (int32_t x = x0; x < x1; ++x)
|
|
live_cost[static_cast<std::size_t>(y * map->width + x)] = kLethalCost;
|
|
}
|
|
|
|
const GridRef compile_grid = gridOf(*map, map->static_cost, map->clearance_mm.data(), variant);
|
|
const Clock::time_point update_began = Clock::now();
|
|
const GridRef unpatched = gridOf(*map, live_cost, map->clearance_mm.data(), variant);
|
|
request.changed_boxes = changedBoxes(map->static_cost.data(), unpatched);
|
|
std::vector<uint16_t> patched;
|
|
const uint16_t* live_clearance = map->clearance_mm.data();
|
|
if (!request.changed_boxes.empty())
|
|
{
|
|
patchClearance(map->static_cost.data(), map->clearance_mm.data(), unpatched, 1.5, &patched);
|
|
live_clearance = patched.data();
|
|
}
|
|
const double update_ms = msSince(update_began);
|
|
const GridRef live_grid = gridOf(*map, live_cost, live_clearance, variant);
|
|
|
|
double coarse_ms = 0.0;
|
|
const CoarseGraph* coarse = nullptr;
|
|
if (mode != "lattice")
|
|
{
|
|
auto found = map->coarse.find(variant);
|
|
if (found == map->coarse.end())
|
|
{
|
|
const Clock::time_point began = Clock::now();
|
|
found = map->coarse.emplace(variant, buildCoarseGraph(compile_grid, config, ridge_params)).first;
|
|
coarse_ms = msSince(began);
|
|
}
|
|
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;
|
|
GraphTrace graph_trace;
|
|
const GraphProvider provider = [&](double limit) -> std::shared_ptr<const CompiledGraph> {
|
|
if (coarse == nullptr) return {};
|
|
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, live_preferred ? live_cost.data() : compile_grid.cost, limit, config, backend));
|
|
return slot;
|
|
};
|
|
const PlanResult result = planRoute(provider, request, live_grid, config, backend, graph_dump_enabled ? &graph_trace : nullptr);
|
|
if (graph_dump_enabled) writeGraphDump(graph_dump_dir, graph_dump_ordinal++, id, deviation, request, graph_trace, result);
|
|
const PlanInfo& info = result.info;
|
|
ZoneDeviation zone_deviation;
|
|
for (std::size_t i = 1; i < result.path.size(); ++i)
|
|
{
|
|
const auto& a = result.path[i-1];
|
|
const auto& b = result.path[i];
|
|
const ZoneDeviation segment = lineZoneDeviation({a.x, a.y}, {b.x, b.y}, live_grid);
|
|
zone_deviation.deviated_length_m += segment.deviated_length_m;
|
|
zone_deviation.integral_deg_m += segment.integral_deg_m;
|
|
}
|
|
out << "{\"scene\":\"" << id << "\",\"dev\":" << deviation << ",\"ok\":" << (result.ok ? 1 : 0) << ",\"reason\":\"" << info.reason
|
|
<< "\",\"zone_dev_len_m\":" << zone_deviation.deviated_length_m << ",\"zone_dev_integral\":" << zone_deviation.integral_deg_m
|
|
<< ",\"query_ms\":" << info.query_ms << ",\"compile_ms\":" << info.compile_ms << ",\"coarse_ms\":" << coarse_ms
|
|
<< ",\"update_ms\":" << update_ms << ",\"search_ms\":" << info.search_ms << ",\"build_ms\":" << info.build_ms
|
|
<< ",\"verify_ms\":" << info.verify_ms << ",\"live_ridge_ms\":" << info.live_ridge_ms << ",\"live_ridge\":\"" << info.live_ridge
|
|
<< "\",\"rung\":" << info.rung_used_deg << ",\"retries\":" << info.retries << ",\"gap_failures\":" << info.gap_failures
|
|
<< ",\"soft_ignored\":" << (info.soft_cost_ignored ? 1 : 0) << ",\"raise\":" << info.gap_shortcut_raise_m
|
|
<< ",\"swing_windows\":" << info.swing_windows << ",\"swing_checks\":" << info.swing_line_checks
|
|
<< ",\"swing_builds\":" << info.swing_builds << ",\"swing_selected\":" << info.swing_selected;
|
|
if (result.ok)
|
|
{
|
|
out << ",\"min_gap\":" << info.min_gap_m << ",\"exempt\":" << (info.gap_exemption_used ? 1 : 0) << ",\"kinks\":" << result.check.kinks
|
|
<< ",\"corners\":" << result.check.corners << ",\"crossings\":" << result.check.self_crossings << ",\"zone_violations\":" << result.check.zone_violations
|
|
<< ",\"collisions\":" << result.check.footprint_collisions << ",\"path\":[";
|
|
for (std::size_t i = 0; i < result.path.size(); ++i)
|
|
{
|
|
const PathPoint& p = result.path[i];
|
|
out << (i > 0 ? "," : "") << "[" << p.x << "," << p.y << "," << p.yaw << "]";
|
|
}
|
|
out << "]";
|
|
}
|
|
out << ",\"detour_upgrade_outcome\":\"" << info.detour_upgrade_outcome
|
|
<< "\",\"detour_upgrade_graph_length_m\":" << info.detour_upgrade_graph_length_m
|
|
<< ",\"detour_upgrade_lattice_length_m\":" << info.detour_upgrade_lattice_length_m
|
|
<< ",\"detour_upgrade_graph_cost\":" << info.detour_upgrade_graph_cost
|
|
<< ",\"detour_upgrade_lattice_cost\":" << info.detour_upgrade_lattice_cost
|
|
<< ",\"detour_upgrade_graph_zone_deviation_m\":" << info.detour_upgrade_graph_zone_deviation_m
|
|
<< ",\"detour_upgrade_lattice_zone_deviation_m\":" << info.detour_upgrade_lattice_zone_deviation_m
|
|
<< ",\"detour_upgrade_time_ms\":" << info.detour_upgrade_time_ms
|
|
<< ",\"detour_upgrade_lattice_reason\":\"" << info.detour_upgrade_lattice_reason << "\""
|
|
<< ",\"tier\":\"" << info.tier_used << "\",\"graph_reason\":\"" << info.graph_reason
|
|
<< "\",\"lattice_reason\":\"" << info.lattice_reason << "\",\"lattice_states_expanded\":" << info.lattice_states_expanded
|
|
<< ",\"lattice_closed_pruned\":" << info.lattice_closed_pruned << ",\"lattice_refinement_attempts\":" << info.lattice_refinement_attempts
|
|
<< ",\"lattice_pose_checks\":" << info.lattice_pose_checks << ",\"lattice_field_cells\":" << info.lattice_field_cells
|
|
<< ",\"lattice_heading_field_states\":" << info.lattice_heading_field_states
|
|
<< ",\"lattice_tiles_built\":" << info.lattice_tiles_built << ",\"lattice_peak_bytes\":" << info.lattice_peak_bytes
|
|
<< ",\"lattice_crossing_rejects\":" << info.lattice_crossing_rejects
|
|
<< ",\"lattice_field_ms\":" << info.lattice_field_ms << ",\"lattice_search_ms\":" << info.lattice_search_ms
|
|
<< ",\"lattice_verify_ms\":" << info.lattice_verify_ms
|
|
<< ",\"lattice_ms\":" << info.lattice_ms
|
|
<< ",\"lattice_smooth_ms\":" << info.lattice_smooth_ms << ",\"lattice_smooth_checks\":" << info.lattice_smooth_checks
|
|
<< ",\"lattice_smoothed\":" << info.lattice_smoothed << ",\"curvature_sign_changes\":" << info.curvature_sign_changes
|
|
<< ",\"graph_radius_pass\":\"" << info.graph_radius_pass << "\"" << ",\"max_curvature\":" << result.check.max_curvature << ",\"path_length_m\":" << info.path_length_m << ",\"straight_share\":" << info.straight_share
|
|
<< ",\"lattice_smooth_connectors_tried\":" << info.lattice_smooth_connectors_tried
|
|
<< ",\"lattice_smooth_connectors_ok\":" << info.lattice_smooth_connectors_ok
|
|
<< ",\"lattice_smooth_candidates\":" << info.lattice_smooth_candidates
|
|
<< ",\"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;
|
|
else out << "null";
|
|
out << ",\"lattice_upgrade_ms\":" << info.lattice_upgrade_ms
|
|
<< ",\"graph_standability_tests\":" << info.graph_standability_tests
|
|
<< ",\"graph_standability_rejected\":" << info.graph_standability_rejected
|
|
<< ",\"lattice_standability_tests\":" << info.lattice_standability_tests
|
|
<< ",\"lattice_standability_rejected\":" << info.lattice_standability_rejected
|
|
<< ",\"graph_reference_length_m\":" << info.graph_reference_length_m << "}\n";
|
|
std::cout << id << " " << deviation << " " << info.reason << " " << info.query_ms << " ms" << std::endl;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|