使用C ++使用A *算法解决难题

时间:2011-10-18 04:04:17

标签: c++ heuristics a-star 8-puzzle

我在C ++中实现A* algorithm来解决 n-puzzle问题
我试图在this链接中实现伪代码 总成本(F = H + G)计算是“成本取决于错位瓦片的数量(启发式)+从初始状态(G)开始的步骤”。下面给出AStar函数的算法。

问题是,我有一个无限循环的情况。我该如何解决这个问题?

PS:如果需要,我可以提供AStar中使用的其他功能的实现。

任何帮助都将不胜感激。

void AStar(const int size, int** puzzle)
{
int moveCount = 0;                                                                  // initialize G(n)
int**goalState = GoalState(size);                                                   // initialize  and assign goal state
int openListIndex = 0;                                                              // initialize open list index
vector<node> openList;                                                              // initialize open list
vector<node> closedList;                                                            // initialize closed list

node startNode;                                                                     // initialize start node
startNode.puzzleArray = puzzle;                                                     // assign start node's state
startNode.cost = moveCount + Heuristics(goalState,puzzle,size);                     // assign start node's cost

node goalNode;                                                                      // initialize goal node
goalNode.puzzleArray = goalState;                                                   // assign goal node's state

openList.push_back(startNode);                                                      // push start node to the open list

while (!openList.empty())                                                           // loop while open list is not empty
{
    node currentNode = CalculateLowestCost(&openList, &closedList);                 // initialize current node which has the lowest cost, pop it from open list, push it to the closed list
    int** currentState = currentNode.puzzleArray;                                   // initialize and assign current state array
    /*********************************************************************************************/
    if (GoalCheck(goalState, currentState, size)) break;                            // GOAL CHECK//
    /*********************************************************************************************/
    vector<char> successorDirectionList = CalculateSuccessor(size, currentState);   // initialize a char vector for the directions of the successors

    int**successor;                                                                 // initialize successor state
    node successorNode;                                                             // initialize successor node
    moveCount++;                                                                    // advance G(n)

    for (;!successorDirectionList.empty();)                                         // loop over the successor list
    {
        char direction = successorDirectionList.back();                             // take a direction from the list
        successorDirectionList.pop_back();                                          // remove that direction from the list
        successor = MoveBlank(currentState, size, direction);                       // assign successor state

        successorNode.puzzleArray = successor;                                      // assign successor node's state
        successorNode.cost = moveCount + Heuristics(goalState,currentState,size);   // assign successor node's cost

        //vector<node> stateCheckList = openList;                                   // copy the open list for the checking the nodes in that list

        bool flagOpen = false;
        bool flagClosed = false;
        int locationOpen = -1;
        int locationClosed = -1;

        for (int i=0; i<openList.size(); i++)
        {
            int** existing = openList[i].puzzleArray;
            int existingCost = openList[i].cost;

            if (StateCheck(successor, existing, size))
            {
                locationOpen = i;
                if (successorNode.cost > existingCost)
                {
                    flagOpen = true;
                    break;
                }
            }
        }
        if (flagOpen) continue;

        int** existingInOpen;
        if(locationOpen != -1) 
        {
            existingInOpen = openList[locationOpen].puzzleArray;
            openList.erase(openList.begin()+locationOpen);
        }

        for (int i=0; i<closedList.size(); i++)
        {
            int** existing = closedList[i].puzzleArray;
            int existingCost = closedList[i].cost;

            if (StateCheck(successor, existing, size))
            {
                locationClosed = i;
                if (successorNode.cost > existingCost)
                {
                    flagClosed = true;
                    break;
                }
            }
        }
        if (flagClosed) continue;

        int**existingInClosed;
        if(locationClosed != -1)
        {
            existingInClosed = closedList[locationClosed].puzzleArray;
            closedList.erase(closedList.begin()+locationClosed);
        }

        openList.push_back(successorNode);
    }    
}

}

3 个答案:

答案 0 :(得分:1)

由于循环的可能性,即一系列移动会将您带回到您已经访问过的状态,因此检查重复状态(显然不是树搜索的问题)非常重要。我无法完全按照您的检查方式进行检查,但这可能是问题所在。在编写Haskell实现时,我遇到了类似的问题(详细信息herehere),这归结为处理探索集的问题。做对了,一切正常。 (获得4x4拼图的解决方案仍然是一个昙花一现的事情,特别是如果你从状态空间的目标很远的一个州开始,但这主要是由于A *的缺陷和天真的方式我们正在处理可能的行动。)

答案 1 :(得分:1)

您是否删除了从打开列表中选择的状态?这是一个非常简单且写得很好的C#代码,它可以帮助你:http://geekbrothers.org/index.php/categories/computer/12-solve-8-puzzle-with-a 并且A *会自动避免循环,因为它采取了你考虑的动作

答案 2 :(得分:0)

我还实现了一个n-puzzle(深度优先和*)并且它进入了无限循环。这是因为以下循环:向上,向左,向下,向右,向上,向左... 我真的不知道是否有办法防止这样的事情(我能做的最大办法就是通过记住最后一次移动来阻止左右循环......)

不知道这是否是导致你的循环的原因,最好的方法是在每次迭代时打印电路板以查看实际发生的情况;)