首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与A*算法有关的问题

与A*算法有关的问题
EN

Stack Overflow用户
提问于 2014-08-25 15:17:43
回答 1查看 147关注 0票数 0

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

问题是我的代码不起作用。它进入一个无限的循环。我真的不知道为什么会这样..。我怀疑这个问题是在图构造函数中实现的F=G+H函数中。我怀疑我不是在计算邻居F的相关性。

这是我的密码:

代码语言:javascript
复制
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);
    }

就像我说过的。我怀疑图类有主要的问题。不过,我还没能发现并修复它。

代码语言:javascript
复制
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)位置。看上去它在第一步就会永远保持下去。

EN

回答 1

Stack Overflow用户

发布于 2014-08-25 16:16:48

您不会从打开列表中删除开始节点(我的意思是,在每个循环之后,当您需要删除刚刚退出的节点时)。此外,还有一些提示:

  1. java中有优先级队列。所以,你不需要自己去做
  2. int f,g,h;尝试重命名变量在代码中不可能理解它们的含义。
  3. 然后创建邻居,检查它们是否位于该区域(即: x、y>0,而不是maxX、maxY)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25489130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档