A * bug(星搜索算法)

时间:2011-07-26 04:43:38

标签: c++ algorithm a-star

我已经开始研究算法和软件开发,作为一个小型的自我评估项目,我决定用C ++编写A *搜索算法。它使用Qt和OpenGL作为视觉部分(但这并不重要)。

主要使用此来源: A* Pathfinding for Beginners

我已经写了一个小应用程序,但是我发现了一个我无法修复的错误。看起来由于某种原因,靠近墙的节点的父节点被设置为墙。(?)并且由于我存储信息的方式,墙的父节点被设置为起点(?)。

我使用了stateMatrix [] [] where

1 = entrance green;
2 = exit;
3 = wall and;
4 = path;

我还使用矩阵来表示openNodes和closedNode。 closedNodes矩阵是bool矩阵,openNode矩阵也存储一些信息:

openNodes指令是:

openNodes[100][100][6];
0 - bool open or closed
1 - F
2 - G
3 - H
4 - parentX
5 - parentY

我知道有更好的方法可以对此进行编码,但我还没有完成这一课;)

以下是astar文件的代码:

#include <math.h>
#include "apath.h"

aPath::aPath()
{
    gridSize = 100;

    int i, j, k;

    for(i = 0; i < gridSize; i++)
        for(j = 0; j < gridSize; j++)
        {
            stateMatrix[i][j] = 0;
            for(int k = 0; k < 6; k++) openNodes[i][j][k] = 0;
            closedNodes[i][j] = 0;
        }

    targetX = targetY =
            openX = openY = entranceX = entranceY = 0;
}

void aPath::start()
{
    bool testOK = false;
    int G = 0;

    openNodes[entranceX][entranceY][0] = 1;
    openNodes[entranceX][entranceY][2] = 14;
    openNodes[entranceX][entranceY][3] = euclidean(entranceX,
                                                   entranceY);
    openNodes[entranceX][entranceY][1] =
            openNodes[entranceX][entranceY][2] +
            openNodes[entranceX][entranceY][3];
    openNodes[entranceX][entranceY][4] = entranceX;
    openNodes[entranceX][entranceY][5] = entranceY;

    int i, j, x, y;

    while(closedNodes[targetX][targetY] == 0)
    {
        searchLessOpen();
        closedNodes[openX][openY] = 1;
        openNodes[openX][openY][0] = 0;
        //Check the 8 squares around
        for(i = openX - 1; i <= openX + 1; i++)
            for(j = openY - 1; j <= openY + 1; j++)
            {
                //check if the square is in the limits,
                //is not a wall and is not in the closed list
                if((i  >= 0) && (j >= 0)  &&
                        (i < gridSize) && (j < gridSize) &&
                        (stateMatrix[i][j] != 3) &&
                        (closedNodes[i][j] == 0))
                {
                    //G calculus. If it is in the edge it costs more
                    x = i - openX + 1;
                    y = j - openY + 1;
                    if((x == 0 && y == 0) ||
                            (x == 0 && y == 2) ||
                            (x == 2 && y == 0) ||
                            (x == 2 && y == 2))
                    {
                        G = 14;
                    }
                    else G = 10;

                    //check if node is already open
                    if(openNodes[i][j][0] == 0)
                    {
                        openNodes[i][j][0] = 1;
                        openNodes[i][j][2] = G;
                        openNodes[i][j][3] = euclidean(i,j);
                        openNodes[i][j][1] = openNodes[i][j][2] + openNodes[i][j][3];
                        openNodes[i][j][4] = openX;
                        openNodes[i][j][5] = openY;
                    }
                    else //if node is open, check if this path is better
                    {
                        if(G < openNodes[i][j][2])
                        {
                            openNodes[i][j][2] = G;
                            openNodes[i][j][1] = openNodes[i][j][2] + openNodes[i][j][3];
                            openNodes[i][j][4] = openX;
                            openNodes[i][j][5] = openY;
                        }
                    }
                }
            }
    }

    reconstruct();
}

void aPath::reconstruct()
{
    bool test = false;

    int x = openNodes[targetX][targetY][4];
    int y = openNodes[targetX][targetY][5];

    do
    {
        stateMatrix[x][y] = 4;
        x = openNodes[x][y][4];
        y = openNodes[x][y][5];

        if(x == entranceX && y == entranceY) test = true;

    } while(test == false);
}

int aPath::euclidean(int currentX, int currentY)
{
    int dx = targetX - currentX;
    int dy = targetY - currentY;

    return 10*sqrt((dx*dx)+(dy*dy));
}

void aPath::searchLessOpen()
{
    int F = 1000000;
    int i, j;

    for(i = 0; i < gridSize; i++)
        for(j = 0; j < gridSize; j++)
        {
            if(openNodes[i][j][0] == 1)
            {
                if(openNodes[i][j][1] <= F)
                {
                    F = openNodes[i][j][1];
                    openX = i;
                    openY = j;
                }
            }
        }
}

有谁知道我做错了什么?

感谢。 编辑:这是一些图片:

1 个答案:

答案 0 :(得分:5)

aPath::start()中,您有:

openNodes[entranceX][entranceY][0] = 1;
openNodes[entranceX][entranceY][2] = 14;
openNodes[entranceX][entranceY][3] = euclidean(entranceX,
                                               entranceY);
openNodes[entranceX][entranceY][3] =
        openNodes[entranceX][entranceY][2] +
        openNodes[entranceX][entranceY][3];
openNodes[entranceX][entranceY][4] = entranceX;
openNodes[entranceX][entranceY][5] = entranceY;

为什么下标[1]没有值?为什么要为下标[3]分配两个不同的值?另外,说实话,entranceXentranceY名称对于他们正在做的工作来说太长了;它们使代码的可读性降低(尽管我确信你被告知要使用有意义的名字)。对于这些数组索引,我可能只使用xy

代码:

    //Check the 8 squares around
    for(i = openX - 1; i <= openX + 1; i++)
        for(j = openY - 1; j <= openY + 1; j++)
        {

我可能会确保ij都没有使用以下代码来处理无效值:

    //Check the 8 squares around (openX, openY)
    int minX = max(openX - 1, 0);
    int maxX = min(openX + 1, gridsize);
    int minY = max(openY - 1, 0);
    int maxY = min(openY + 1, gridsize);
    for (i = minX; i <= maxX; i++)
        for (j = minY; j <= maxY; j++)
        {

我不确定您是否需要明确检查i == openX && j == openY(当前单元格)的情况;它不是当前单元格周围的8个单元格之一(因为它当前单元格),但其他条件可能已经处理过。如果不是:

            if (i == openX && j == openY)
                continue;

我注意到我们没有openXopenY或许多其他非局部变量的定义。这使得很难确定它们是类成员变量还是某种类型的全局变量。我们也看不到它们是如何初始化的,也不能看到它们所代表的文档。

最合理的麻烦来源

aPath::SearchLessOpen()中,您有:

        if(openNodes[i][j][0] == 1)
        {
            if(openNodes[i][j][6] <= F)
            {
                F = openNodes[i][j][7];

您在说明中指出,最后一个地方openNodes上的下标范围超过0..5;但是,您的代码正在访问下标6和7.这很容易导致您描述的那种混乱 - 您正在访问数据超出范围。我认为这可能很容易成为你麻烦的根源。当您访问openNodes[i][j][6]时,这是技术上未定义的行为,但最有可能的结果是它正在读取与您编写openNodes[i][j+1][0]j < gridsize - 1时)相同的数据。同样,openNodes[i][j][7]相当于访问openNodes[i][j+1][1],具有相同的警告。