A *アルゴリズム8パズル

デイブ・スミス

A *アルゴリズムの多くの擬似コードを読みましたが、どちらも実際にソリューションを出力する方法を説明していません。まだアクセスしていない場合は優先キューを使用し、探索する場合はテーブルを使用するという概念は理解していると思いますが、アルゴリズムを実行すると、どの時点で結果を出力するかわかりません。パスを出力する方法を実際に示す疑似コードを持っている人はいますか?

本当に感謝しています。私は8パズルの問題を実装するためにアルゴリズムを使用しようとしています

これが私のコードです:

public class Board{

private int[][] squares;
private int f;
private int g;
private int h;

private int size;
private Board parent;

public Board(Board current, Board parent)
{
    this(current);
    g = current.getG();
    h = current.getH();
    f = current.getF();
    this.parent = parent;

}

public void solveH1()
{

    while(!frontier.isEmpty())
    {
        board = frontier.poll();

        ArrayList<Board> successors = new ArrayList<Board>();
        Board b1 = new Board(board.moveDown(),board);
        Board b2 = new Board(board.moveUp(),board);
        Board b3 = new Board(board.moveLeft(),board);
        Board b4 = new Board(board.moveRight(),board);
        if(!b1.equals(board))
            successors.add(b1);
        if(!b2.equals(board))
            successors.add(b2);
        if(!b3.equals(board))
            successors.add(b3);
        if(!b4.equals(board))
            successors.add(b4);
        for(int i=0; i<successors.size(); i++)
        {
            if(successors.get(i).isGoal())
            {
                break;
            }
            int g = board.getG()+1;
            int h = successors.get(i).getH1Cost();
            successors.get(i).setG(g);
            successors.get(i).setH(h);
            successors.get(i).setF(g+h);

            if(frontier.contains(successors.get(i)))
            {
                Iterator<Board> iterator = frontier.iterator();
                Board b = null;
                while(iterator.hasNext())
                {
                    b = iterator.next();
                    if(b.equals(successors.get(i)))
                    {
                        break;
                    }
                }
                if(b.getG() < successors.get(i).getG())
                {
                    break;
                }
            }
            if(exploredSet.contains(successors.get(i)))
            {
                int index = exploredSet.indexOf(successors.get(i));
                if(exploredSet.get(index).getG() < successors.get(i).getG())
                    break;
            }
            else
            {
                frontier.add(successors.get(i));
            }
        }
        exploredSet.add(board);
    }
    printPath();
}
public void printPath()
{

    ArrayList<Board> path = new ArrayList<Board>();
    cursor = board;
    while(cursor.getParent()!=null)
    {
        path.add(cursor);
        cursor = cursor.getParent();
    }
    for(int i=0; i<path.size(); i++)
        System.out.println(path.get(i));
}

何らかの理由で、これは1つのノードを出力するだけであり、それはボットでさえ目標です。誰かが私が欠けているものを教えてもらえますか?

nullpotent

ノードをキューにプッシュすると、その親、つまり、ノードにアクセスしたノードも保存(プッシュ)されます。

class NodeWrapper {
 public float cost;
 public Node node;
 public Node parentNode;
 public NodeWrapper(Node node, Node parentNode) {
   this.node = node;
   this.parentNode = parentNode;
 }
}

その後

openQueue.push(new NodeWrapper(neihgbouringNode, currentNode));

そして、エンドノードに到達したら、そこからさかのぼります。

List<Node> out = new ArrayList<Node>();
while (currentNode != null) {
   out.add(currentNode.node);
   currentNode = currentNode.parentNode;
}
return out; 

これがA *パスファインダーのデモです

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事