C++ 使用Boost图形库进行路径查找(在网格中)

C++ 使用Boost图形库进行路径查找(在网格中),c++,boost,graph,path-finding,boost-graph,C++,Boost,Graph,Path Finding,Boost Graph,我将我的谷歌AI挑战从Python改写成C++,我想使用Boost的图形库来处理路径发现,而不是像Python那样滚动我自己的图形和搜索代码。 该地图是一个简单的方形网格,环绕边缘 我还没有使用Boost或C++之前,我确实知道C,而且我发现Boost图文档很难理解,所以我需要一点帮助。 我遇到问题的特定文档: 等,目前仅限于两个环节: 下面是一段正在工作的python代码: def do_turn(self, ants): # update path-finding graph

我将我的谷歌AI挑战从Python改写成C++,我想使用Boost的图形库来处理路径发现,而不是像Python那样滚动我自己的图形和搜索代码。 该地图是一个简单的方形网格,环绕边缘

我还没有使用Boost或C++之前,我确实知道C,而且我发现Boost图文档很难理解,所以我需要一点帮助。 我遇到问题的特定文档:

等,目前仅限于两个环节: 下面是一段正在工作的python代码:

def do_turn(self, ants):
    # update path-finding graph
    for row in range(ants.rows):
        for col in range(ants.cols):
            key = (row, col)
            self.graph[key] = []

            def append_if_unoccupied(coord):
                if ants.passable(coord) and coord not in ants.my_hills():
                    self.graph[key].append(coord)

            if row - 1 >= 0:
                append_if_unoccupied((row - 1, col))
            else:
                append_if_unoccupied((ants.rows + (row - 1), col))

            if col - 1 >= 0:
                append_if_unoccupied((row, col - 1))
            else:
                append_if_unoccupied((row, ants.cols + (col - 1)))

            if row + 1 < ants.rows:
                append_if_unoccupied((row + 1, col))
            else:
                append_if_unoccupied((row + 1 - ants.rows, col))

            if col + 1 < ants.cols:
                append_if_unoccupied((row, col + 1))
            else:
                append_if_unoccupied((row, col + 1 - ants.cols))

不需要切换到C++来开发BGL;python有一个非常好的包装,形式为。当然包括。

应该有助于:

    boost::astar_search
    (
        m_navmesh, start,
        distance_heuristic(m_navmesh, goal),
        boost::predecessor_map(&p[0])
        .distance_map(&d[0])
        .visitor(astar_goal_visitor(goal))
        .weight_map(get(&NavMeshConnection::dist, m_navmesh))
    );
此函数采用距离启发法,这是您必须自己创建的函子:

// euclidean distance heuristic
class distance_heuristic : public boost::astar_heuristic <NavMeshGraph, float> {
public:
    distance_heuristic(const NavMeshGraph & l, TriangleDescriptor goal)
    : m_graph(l), m_goal(goal)
    {}

    float operator()(TriangleDescriptor u) {
        const NavMeshTriangle & U = m_graph[u];
        const NavMeshTriangle & V = m_graph[m_goal];
        float dx = U.pos.x - V.pos.x;
        float dy = U.pos.y - V.pos.y;
        return ::sqrt(dx * dx + dy * dy);
    }

private:
    const NavMeshGraph & m_graph;
    TriangleDescriptor m_goal;
};
found_gloal可以是简单的结构,也可以是更复杂的结构,具体取决于要返回的内容:

struct found_goal {};
这样一个对象被抛出到访问者中,因此您必须将boost::astar_搜索包装在try/catch块中:

try {

    boost::astar_search
    (
      ...
     );


} catch (found_goal fg) { // found a path to the goal
    std::list<TriangleDescriptor> shortest_path;
    for (TriangleDescriptor v = goal;; v = p[v]) {
        shortest_path.push_front(v);
        if (p[v] == v)
            break;
    }
然后在catch块中检索最佳方式:

try {

    boost::astar_search
    (
      ...
     );


} catch (found_goal fg) { // found a path to the goal
    std::list<TriangleDescriptor> shortest_path;
    for (TriangleDescriptor v = goal;; v = p[v]) {
        shortest_path.push_front(v);
        if (p[v] == v)
            break;
    }
你将需要大量的适应,但至少这应该足以让你的方式得到提升

顺便说一下,Djikstra并不完全相似。它返回来自其他每个节点的所有可能路径。这对速度不好,但对预处理很好:如果你的世界是静态的,你可以预先构建一个寻路矩阵,它将返回O1中最好的下一个节点