Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在java中实现Star_Java_Path Finding_A Star - Fatal编程技术网

无法在java中实现Star

无法在java中实现Star,java,path-finding,a-star,Java,Path Finding,A Star,我一整天都在试着让这个算法运行起来,但我一辈子都做不到。我在网上阅读了很多教程,AS3、JavaScript和C++中的源代码;但我无法将我看到的内容改编成我自己的代码 我创建了一个AStar类,它有一个名为Node的嵌套类。该贴图是一个名为map的二维数组 我遇到的最大问题是拉取pathfind函数中的F值 我已经实现了F=G+H,我的问题是实际的AStar算法。有人能帮忙吗,这是我到目前为止所取得的成绩: import java.util.ArrayList; public class A

我一整天都在试着让这个算法运行起来,但我一辈子都做不到。我在网上阅读了很多教程,AS3、JavaScript和C++中的源代码;但我无法将我看到的内容改编成我自己的代码

我创建了一个AStar类,它有一个名为Node的嵌套类。该贴图是一个名为map的二维数组

我遇到的最大问题是拉取pathfind函数中的F值

我已经实现了F=G+H,我的问题是实际的AStar算法。有人能帮忙吗,这是我到目前为止所取得的成绩:

import java.util.ArrayList;

public class AStar
{
    int MAP[][];

    Node startNode, endNode;

    public AStar(int MAP[][], int startXNode, int startYNode,
                              int endXNode, int endYNode)
    {
        this.MAP = MAP;
        startNode = new Node(startXNode, startYNode);
        endNode = new Node(endXNode, endYNode);
    }

    public void pathfinder()
    {
        ArrayList openList = new ArrayList();
        ArrayList closedList = new ArrayList();



    }

    public int F(Node startNode, Node endNode)
    {
        return (H(startNode, endNode) + G(startNode));
    }

    //H or Heuristic part of A* algorithm
    public int H(Node startNode, Node endNode)
    {
        int WEIGHT = 10;
        int distance = (Math.abs(startNode.getX() - endNode.getX()) + Math.abs(startNode.getY() - endNode.getY()));

        return (distance * WEIGHT);
    }

    public int G(Node startNode)
    {
        if(MAP[startNode.getX() - 1][startNode.getY()] != 1)
        {
            return 10;
        }

        if(MAP[startNode.getX() + 1][startNode.getY()] != 1)
        {
            return 10;
        }

        if(MAP[startNode.getX()][startNode.getY() -1] != 1)
        {
            return 10;
        }

        if(MAP[startNode.getX()][startNode.getY() + 1] != 1)
        {
            return 0;
        }

        return 0;
    }

    public class Node
    {
        private int NodeX;
        private int NodeY;

        private int gScore;
        private int hScore;
        private int fScore;

        public Node(int NodeX, int NodeY)
        {
            this.NodeX = NodeX;
            this.NodeY = NodeY;
        }

        public int getX()
        {
            return NodeX;
        }

        public int getY()
        {
            return NodeY;
        }

        public int getG()
        {
            return gScore;
        }

        public void setG(int gScore)
        {
            this.gScore = gScore;
        }

        public int getH()
        {
            return hScore;
        }

        public void setH(int hScore)
        {
            this.hScore = hScore;
        }

        public int getF()
        {
            return fScore;
        }

        public void setF(int fScore)
        {
            this.fScore = fScore;
        }
    }
}
这是使用pathfinder功能所能达到的最远距离:

   public void pathfinder()
    {
        LinkedList<Node> openList = new LinkedList();
        LinkedList<Node> closedList = new LinkedList();

        Node currentNode;

        openList.add(startNode);

        while(openList.size() > 0)
        {
            currentNode = (Node) openList.get(0);
            closedList.add(currentNode);


            for(int i = 0; i < openList.size(); i++)
            {
                int cost = F(currentNode, endNode);

            }
        }

    }
public void pathfinder()
{
LinkedList openList=新建LinkedList();
LinkedList closedList=新建LinkedList();
节点当前节点;
添加(startNode);
while(openList.size()>0)
{
currentNode=(节点)openList.get(0);
closedList.add(当前节点);
对于(int i=0;i
我最近把这个A*代码放在一起解决了一个问题。您必须填写
节点
对象矩阵的详细信息。使用它的风险自负,但我可以说它解决了问题:)

公共类节点{
列表邻居=新的ArrayList();
节点父节点;
int f;
int g;
int-h;
int x;
int-y;
国际成本;
}
公共列表aStar(节点开始、节点目标){
Set open=new HashSet();
Set closed=新的HashSet();
start.g=0;
start.h=估计距离(起点、目标);
start.f=start.h;
打开。添加(开始);
while(true){
节点电流=零;
if(open.size()==0){
抛出新的运行时异常(“无路由”);
}
用于(节点:打开){
if(current==null | | node.f
我不知道您是在尝试只使用简单类型,还是只是没有考虑过,但您需要有一个优先级队列才能让a*正常工作

一个好的思考方法是,将起始点放入距离为0的优先级队列中,然后启动一个循环,该循环仅在优先级队列为空时停止

在循环中,您将min节点取出,并检查它是否以前没有打开过,或者是否打开过,现在是否找到了一种较短的方法来打开它。 如果其中一个为真,则将距离添加到新节点,将边/从正方形添加到贴图,然后将距离+启发式添加到优先级队列

我写这篇文章是为了在布尔网格上工作,并在一维和二维数组之间进行恒定转换,但我希望它可读:

public void AStarRoute()
{
    gridDist = new double[rows][cols];
    System.out.println("Start of AStarRoute");
    MinPriorityQueue pq = new MinPriorityQueue(rows * cols);
    edgeTo = new HashMap<Integer, Integer>();

    gridDist[x1Dto2D(start)][y1Dto2D(start)] = 0;
    pq.insert(start, 0);
    int from;
    while (!pq.isEmpty()) {
        from = pq.delMin();
        int x = x1Dto2D(from);
        int y = y1Dto2D(from);
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int newX = x + i;
                int newY = y + j;
                if (newX >= 0 && newY >= 0 && newX < cols && newY < rows && !(i == 0 && j == 0)) {
                    if (grid[newX][newY]) {
                        //System.out.println("NewDist: " + gridDist[newX][newY] + " - OldDist+dist: " + (gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 1.4 : 1.0)) + ":" + (int)(gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 1.4 : 1.0)));
                        if (!edgeTo.containsKey(convert2Dto1D(newX, newY)) || gridDist[newX][newY] > (gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 14 : 10))) {
                            gridDist[newX][newY] = (int)(gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 14 : 10));
                            maxDistToEnd = (int)Math.max(maxDistToEnd, gridDist[newX][newY]);
                            edgeTo.put(convert2Dto1D(newX, newY), convert2Dto1D(x, y));
                            pq.insert(convert2Dto1D(newX, newY), gridDist[newX][newY] + (int)Math.sqrt(Math.pow((newX - x1Dto2D(end))*10, 2) + Math.pow((newY - y1Dto2D(end))*10, 2)));
                            if(convert2Dto1D(newX, newY) == end){
                                System.out.println("End found at (" + newX + ", " + newY + ")");
                                paintGridDist = true;

                                route = new ArrayList<Integer>();
                                int n = convert2Dto1D(newX, newY);
                                route.add(n);
                                do{
                                    n = edgeTo.get(n);
                                    route.add(n);
                                }while(start != n);

                                repaint();
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    paintGridDist = true;
    repaint();
}
public void AStarRoute()
{
gridDist=新的双[行][cols];
System.out.println(“启动一个箭头”);
MinPriorityQueue pq=新的MinPriorityQueue(行*列);
edgeTo=新的HashMap();
gridDist[x1Dto2D(开始)][y1Dto2D(开始)]=0;
pq.插入(开始,0);
int from;
而(!pq.isEmpty()){
from=pq.delMin();
int x=x1Dto2D(从);
int y=y1Dto2D(从);
对于(int i=-1;i=0&&newX(gridDist[x][y]+((Math.abs(i)=Math.abs(j))?14:10))){
gridDist[newX][newY]=(int)(gridDist[x][y]+((Math.abs(i)=Math.abs(j))?14:10);
maxDistToEnd=(int)Math.max(maxDistToEnd,gridDist[newX][newY]);
put(convert2dtoad(newX,newY),convert2dtoad(x,y));
pq.insert(convert2dtoad(newX,newY),gridDist[newX][newY]+(int)Math.sqrt(Math.pow((newX-x1Dto2D(end))*10,2)+Math.pow((newY-y1Dto2D(end))*10,2));
if(convert2dtoad(newX,newY)==end){
System.out.println(“在(“+newX+”,“+newY+”)处找到的端点”);
paintGridDist=true;
route=newarraylist();
int n=convert2dtoad(newX,newY);
路线。添加(n);
做{
n=边缘获取(n);
路线。添加(n);
}while(start!=n);
重新油漆();
public void AStarRoute()
{
    gridDist = new double[rows][cols];
    System.out.println("Start of AStarRoute");
    MinPriorityQueue pq = new MinPriorityQueue(rows * cols);
    edgeTo = new HashMap<Integer, Integer>();

    gridDist[x1Dto2D(start)][y1Dto2D(start)] = 0;
    pq.insert(start, 0);
    int from;
    while (!pq.isEmpty()) {
        from = pq.delMin();
        int x = x1Dto2D(from);
        int y = y1Dto2D(from);
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int newX = x + i;
                int newY = y + j;
                if (newX >= 0 && newY >= 0 && newX < cols && newY < rows && !(i == 0 && j == 0)) {
                    if (grid[newX][newY]) {
                        //System.out.println("NewDist: " + gridDist[newX][newY] + " - OldDist+dist: " + (gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 1.4 : 1.0)) + ":" + (int)(gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 1.4 : 1.0)));
                        if (!edgeTo.containsKey(convert2Dto1D(newX, newY)) || gridDist[newX][newY] > (gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 14 : 10))) {
                            gridDist[newX][newY] = (int)(gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 14 : 10));
                            maxDistToEnd = (int)Math.max(maxDistToEnd, gridDist[newX][newY]);
                            edgeTo.put(convert2Dto1D(newX, newY), convert2Dto1D(x, y));
                            pq.insert(convert2Dto1D(newX, newY), gridDist[newX][newY] + (int)Math.sqrt(Math.pow((newX - x1Dto2D(end))*10, 2) + Math.pow((newY - y1Dto2D(end))*10, 2)));
                            if(convert2Dto1D(newX, newY) == end){
                                System.out.println("End found at (" + newX + ", " + newY + ")");
                                paintGridDist = true;

                                route = new ArrayList<Integer>();
                                int n = convert2Dto1D(newX, newY);
                                route.add(n);
                                do{
                                    n = edgeTo.get(n);
                                    route.add(n);
                                }while(start != n);

                                repaint();
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    paintGridDist = true;
    repaint();
}