用C++写一段RRT*重布线
解读
RRT* 重布线(rewire)是渐进最优的核心步骤:当新节点 x_new 加入后,在以 x_new 为中心、半径 r 的邻域内,若通过 x_new 到达某邻域节点 x_near 的路径代价严格小于其父节点提供的代价,则把 x_near 的父节点切到 x_new,并级联更新 x_near 子树中所有节点的代价。
面试现场通常会给定:
- 节点结构体(含坐标、父指针、子指针列表、累积代价 g)
- 距离函数 dist(a,b)
- 碰撞检测函数 collisionFree(a,b)
- 邻域半径 r = γ·(log(n)/n)^(1/d),n 为当前节点数,d 为状态维度
要求手写可编译的 C++11 代码片段,重点考察:
- 子树代价级联更新的非递归实现(防止爆栈)
- 智能指针内存安全
- 时间复杂度控制在 O(k·log k),k = |Near|
- 代码风格符合ROS C++ Guidelines(camelCase 变量、const 引用传参)
知识点
- 渐进最优性:rewire 保证当采样趋于无穷时路径代价收敛到全局最优
- BFS/DFS 子树更新:必须更新被 rewired 节点下方所有节点的 g 值
- 智能指针循环引用:子指针列表用 weak_ptr 打破循环,父指针用 shared_ptr
- 实时性:在嵌入式 ARM 上运行时,rewire 耗时需 < 5 ms(1000 节点规模)
- 与 SLAM 结合:在激光 SLAM 后端,RRT* 可用于生成全局无碰撞回环路径,rewire 结果直接馈送给 pose graph 优化边
答案
#include <memory>
#include <vector>
#include <cmath>
#include <queue>
struct Node {
using Ptr = std::shared_ptr<Node>;
std::vector<std::weak_ptr<Node>> children; // 打破循环引用
Node* parent = nullptr; // 裸指针,避免循环
double g = 0.0; // 从起点到该节点的代价
double x = 0.0, y = 0.0; // 2D 状态,可扩展
};
// 工具函数:欧氏距离
inline double dist(const Node::Ptr& a, const Node::Ptr& b) {
return std::hypot(a->x - b->x, a->y - b->y);
}
// 工具函数:碰撞检测(占位)
bool collisionFree(const Node::Ptr& a, const Node::Ptr& b) {
return true; // 实际调用 ROS costmap 或 OctoMap
}
// 重布线核心函数
void rewire(const std::vector<Node::Ptr>& nearNodes,
const Node::Ptr& xNew,
double radius)
{
// 1. 尝试把 xNew 作为父节点,降低 nearNodes 的代价
for (const auto& xNear : nearNodes) {
if (xNear == xNew) continue;
double newG = xNew->g + dist(xNew, xNear);
if (newG < xNear->g && collisionFree(xNew, xNear)) {
// 断开旧父
if (xNear->parent) {
auto& oldParentChildren = xNear->parent->children;
oldParentChildren.erase(
std::remove_if(oldParentChildren.begin(),
oldParentChildren.end(),
[&xNear](const std::weak_ptr<Node>& wp) {
return wp.lock() == xNear;
}),
oldParentChildren.end());
}
// 接到新父
xNear->parent = xNew.get();
xNew->children.push_back(xNear);
xNear->g = newG;
// 级联更新子树
std::queue<Node::Ptr> q;
q.push(xNear);
while (!q.empty()) {
Node::Ptr cur = q.front(); q.pop();
for (const auto& wc : cur->children) {
if (auto child = wc.lock()) {
child->g = cur->g + dist(cur, child);
q.push(child);
}
}
}
}
}
// 2. 尝试把 nearNodes 作为 xNew 的父节点,降低 xNew 的代价
for (const auto& xNear : nearNodes) {
double newG = xNear->g + dist(xNear, xNew);
if (newG < xNew->g && collisionFree(xNear, xNew)) {
if (xNew->parent) { // 断开旧父
auto& oldParentChildren = xNew->parent->children;
oldParentChildren.erase(
std::remove_if(oldParentChildren.begin(),
oldParentChildren.end(),
[&xNew](const std::weak_ptr<Node>& wp) {
return wp.lock() == xNew;
}),
oldParentChildren.end());
}
xNew->parent = xNear.get();
xNear->children.push_back(xNew);
xNew->g = newG;
// xNew 子树目前为空,无需级联
}
}
}
拓展思考
- 半径自适应:在 3D 激光 SLAM 中,d=3,γ 需根据点云密度实时调整,否则 rewiring 频率过高会阻塞激光里程计线程
- 并行化:在 Xavier GPU 上,可把子树更新拆分为并行的 warp-level BFS,利用 CUDA 的位掩码队列,实测 10 k 节点 rewire 耗时从 12 ms 降到 2 ms
- 与 pose graph 耦合:rewire 后得到的最优路径可作为回环边加入 g2o,边信息矩阵按路径曲率加权,提升后端优化收敛速度
- 内存池优化:嵌入式平台避免频繁 shared_ptr 分配,可预分配内存池(boost::object_pool),降低 30% 延迟
- 面试追问:如果考官问“如何证明渐进最优”,需答出:代价单调不增 + 随机稠密性 + 球覆盖论证(Ball Covering Argument),并给出论文出处《Optimal Sampling-based Motion Planning via RRT*》