java - A*算法的问题

标签 java algorithm artificial-intelligence a-star

<分区>

我正在尝试用 Java 实现 A* 算法。我遵循了本教程,特别是这个伪代码:http://theory.stanford.edu/~amitp/GameProgramming/ImplementationNotes.html

问题是我的代码不起作用。它进入无限循环。我真的不知道为什么会这样……我怀疑问题出在 Graph 构造函数中实现的 F = G + H 函数中。我怀疑我没有正确计算邻居 F。

这是我的代码:

List<Graph> open;
    List<Graph> close;

    private void createRouteAStar(Unit u)
    {
        open = new ArrayList<Graph>();
        close = new ArrayList<Graph>();

        u.ai_route_endX = 11;
        u.ai_route_endY = 5;

        List<Graph> neigh;

        int index;
        int i;
        boolean finish = false;

        Graph current;
        int cost;
        Graph start = new Graph(u.xMap, u.yMap, 0, ManhattanDistance(u.xMap, u.yMap, u.ai_route_endX, u.ai_route_endY));

        open.add(start);
        current = start;
        while(!finish)
        {
            index = findLowerF();
            current = new Graph(open, index);
            System.out.println(current.x);
            System.out.println(current.y);
            if (current.x == u.ai_route_endX && current.y == u.ai_route_endY)
            {
                finish = true;
            }
            else
            {
                close.add(current);
                open.remove(open.indexOf(current)); //EDITED LATER
                neigh = current.getNeighbors();
                for (i = 0; i < neigh.size(); i++)
                {
                    cost = current.g + ManhattanDistance(current.x, current.y, neigh.get(i).x, neigh.get(i).y);

                    if (open.contains(neigh.get(i)) && cost < neigh.get(i).g)
                    {
                        open.remove(open.indexOf(neigh));
                    } 
                    else if (close.contains(neigh.get(i)) && cost < neigh.get(i).g)
                    {
                        close.remove(close.indexOf(neigh));
                    }
                    else if (!open.contains(neigh.get(i)) && !close.contains(neigh.get(i)))
                    {
                        neigh.get(i).g = cost;
                        neigh.get(i).f = cost + ManhattanDistance(neigh.get(i).x, neigh.get(i).y, u.ai_route_endX, u.ai_route_endY);
                        neigh.get(i).setParent(current);
                        open.add(neigh.get(i));
                    }
                }
            }

        }

        System.out.println("step");
        for (i=0; i < close.size(); i++)
        {
            if (close.get(i).parent != null)
            {
                System.out.println(i);
                System.out.println(close.get(i).parent.x);
                System.out.println(close.get(i).parent.y);
            }
        }
    }

    private int findLowerF()
    {
        int i;
        int min = 10000;
        int minIndex = -1;
        for (i=0; i < open.size(); i++)
        {
            if (open.get(i).f < min)
            {
                min = open.get(i).f;
                minIndex = i;
                System.out.println("min");
                System.out.println(min);
            }
        }
        return minIndex;
    }


    private int ManhattanDistance(int ax, int ay, int bx, int by)
    {
        return Math.abs(ax-bx) + Math.abs(ay-by);
    }

而且,正如我所说的。我怀疑 Graph 类有主要问题。但是我一直无法检测和修复它。

public class Graph {

    int x, y;
    int f,g,h;
    Graph parent;

    public Graph(int x, int y, int g, int h)
    {
        this.x = x;
        this.y = y;
        this.g = g;
        this.h = h;
        this.f = g + h;
    }

    public Graph(List<Graph> list, int index)
    {
        this.x = list.get(index).x;
        this.y = list.get(index).y;
        this.g = list.get(index).g;
        this.h = list.get(index).h;
        this.f = list.get(index).f;
        this.parent = list.get(index).parent;
    }

    public Graph(Graph gp)
    {
        this.x = gp.x;
        this.y = gp.y;
        this.g = gp.g;
        this.h = gp.h;
        this.f = gp.f;
    }

    public Graph(Graph gp, Graph parent)
    {
        this.x = gp.x;
        this.y = gp.y;
        this.g = gp.g;
        this.h = gp.h;
        this.f = g + h;
        this.parent = parent;
    }

    public List<Graph> getNeighbors()
    {
        List<Graph> aux = new ArrayList<Graph>();
        aux.add(new Graph(x+1, y, g,h));
        aux.add(new Graph(x-1, y, g,h));
        aux.add(new Graph(x, y+1, g,h));
        aux.add(new Graph(x, y-1, g,h));
        return aux;
    }

    public void setParent(Graph g)
    {
        parent = g;
    }

    //Added later. Generated by Eclipse

    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + x;
    result = prime * result + y;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Graph other = (Graph) obj;
    if (x != other.x)
        return false;
    if (y != other.y)
        return false;
    return true;
}
}

小修改:

使用 System.out 和调试器我发现程序总是检查相同的“当前”图,(15,8) 这是 (u.xMap, u.yMap) 位置。看起来它永远保持在第一步中。

最佳答案

您不会从打开列表中删除您的起始节点(我的意思是,在每个循环之后,您需要删除刚刚排除的节点)。 另外,一些提示:

  1. java中有优先级队列。所以,你不需要自己在列表上做
  2. int f,g,h;尝试重命名变量。在代码中无法理解它们的含义。
  3. 然后创建邻居,检查它们是否在该区域内(即:x,y>0 且小于 maxX,maxY)

关于java - A*算法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25489130/

相关文章:

java - 命令提示符的输出返回 null

algorithm - 提高模块效率

java - 如何在c++/java中生成一组遵循一定分布的值?

machine-learning - 根据给定事件的持续时间,如何将人员聚集/分组在一起?

java - 当我需要一个 String 时,R.string.XXX 从 strings.xml 返回一个 int

java - 如何更改小部件启动器预览?

phpass 的自定义 base 64 编码器 : does it have a name/advantage over Base64?

design-patterns - AI 设计模式 'intelligent agents'

algorithm - 二维离散游戏中的建筑物放置

java - 我需要将一个对象转换为字符串,然后通过将其转换回我的对象​​来检索它