You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Boost A*算法触发段错误的问题排查求助

问题:Boost A*算法触发段错误

这是此前关于Boost图相关问题的续问,成功读取图数据后,对随机选取的起始节点和目标节点应用Boost A*算法时触发了段错误(segmentation fault)。


图结构细节

using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties>;

struct VertexProperties {
    std::vector<double> joint_angles;
    VertexProperties() : joint_angles(3){}
    
};
struct EdgeProperties {
    double weight;
};

A*相关实现

参考Boost官方的A* Cities示例实现了距离启发式函数和astar_goal_visitor:

终止访问器

struct found_goal {}; // 用于终止的异常

// 找到目标节点时终止的访问器
template <typename Vertex>
class astar_goal_visitor : public boost::default_astar_visitor
{
public:
  astar_goal_visitor(Vertex goal) : m_goal(goal) {}
  template <class Graph>
  void examine_vertex(Vertex u, Graph& g) {
    if(u == m_goal)
      throw found_goal();
  }
private:
  Vertex m_goal;
};

欧氏距离启发式函数

// 欧氏距离启发式
template <class Graph>
class distance_heuristic : public boost::astar_heuristic<typename Graph::Graph, double>
{
public:
  typedef typename boost::graph_traits<typename Graph::Graph>::vertex_descriptor Vertex;
  distance_heuristic(Vertex goal, Graph &graph)
    :  m_goal(goal), m_graph(graph) {}
  double operator()(Vertex u)
  {
    double dx = m_graph.getGraph()[m_goal].joint_angles[0] - m_graph.getGraph()[u].joint_angles[0];
    double dy = m_graph.getGraph()[m_goal].joint_angles[1] - m_graph.getGraph()[u].joint_angles[1];
    double dz = m_graph.getGraph()[m_goal].joint_angles[2] - m_graph.getGraph()[u].joint_angles[2];
    
    return ::sqrt(dx * dx + dy * dy + dz * dz);
  }
private:
  Graph m_graph;
  Vertex m_goal;
};

前驱映射定义

typedef boost::property_map < Graph, boost::vertex_index_t >::type IndexMap;
typedef boost::iterator_property_map < Vertex*, IndexMap, Vertex, Vertex& > PredecessorMap;
BoostGraph::PredecessorMap BoostGraph::getPredecessorMap(){

    IndexMap indexMap = boost::get(boost::vertex_index, graph);
    std::vector<Vertex> p(boost::num_vertices(graph));
    PredecessorMap predecessorMap(&p[0], indexMap);

    return predecessorMap;
}

搜索执行代码

std::vector<double> d(boost::num_vertices(graph.getGraph()));

std::mt19937 gen(time(0));
BoostGraph::Vertex start = boost::random_vertex(graph.getGraph(), gen);
BoostGraph::Vertex goal = boost::random_vertex(graph.getGraph(), gen);

auto weightmap = boost::get(&EdgeProperties::weight, graph.getGraph());

try {
// 调用astar命名参数接口
boost::astar_search
  (graph.getGraph(), start,
   distance_heuristic<BoostGraph>
    (goal, graph),
   boost::predecessor_map(graph.getPredecessorMap()).distance_map(boost::make_iterator_property_map(d.begin(), boost::get(boost::vertex_index, graph.getGraph()))).
   weight_map(weightmap).
   visitor(astar_goal_visitor<BoostGraph::Vertex>(goal)));


} catch(found_goal fg) { // 找到通往目标的路径
  std::list<BoostGraph::Vertex> shortest_path;
  for(BoostGraph::Vertex v = goal;; v = p[v]) {
    shortest_path.push_front(v);
    if(p[v] == v)
      break;
  }
}

BoostGraph类的getGraph函数

protected:
    Graph graph;

const BoostGraph::Graph& BoostGraph::getGraph() const{
    return graph;
}

段错误发生在stl_tree.h中,不清楚问题所在,恳请提供帮助。


内容的提问来源于stack exchange,提问作者Samarth Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 03:21:10