在对象的ArrayList中找到最小值

时间:2018-07-20 17:04:39

标签: java a-star

我在A * Star算法的ArrayList中找到最小节点时遇到问题。我正在使用此算法来查找从起点到终点的最佳和最快路径。目前,我没有找到正确的路径,但不知道出了什么问题。

下面是我的Node类的一个片段

import java.awt.Point;

public class Node {

    private Point point;
    private Node parent;
    private int h_cost;
    private int g_cost;

    // Constructor
    public Node(Point point) {
        this.point = point;
    }
}

A *算法的实现

public ArrayList<Node> AStarAlgorithm(Node start, Node goal) {
    ArrayList<Node> open = new ArrayList<>();
    ArrayList<Node> closed = new ArrayList<>();
    start.setGCost(0);
    start.setHCost(hueristic(start, goal));
    open.add(start);

    while(!open.isEmpty()) {
        int min = findMinIndex(open);
        Node current = open.get(0);
        if(current.equals(goal)) { return open;}
        if(world != null) { current.setWorld(world); }
        open.remove(0);
        closed.add(current);
        ArrayList<Node> children = current.getChildren();
        for(int i = 0;i < children.size();++i) {
            Node child = children.get(i);
            if(closed.contains(child)) { continue; }
            if(open.contains(child) && current.getTotalCost() < child.getGCost()) {
                open.remove(child);
            }
            if(closed.contains(child) && current.getTotalCost() < child.getGCost()) {
                closed.remove(child);
            }
            if(!open.contains(child) && !closed.contains(child)) {
                child.setHCost(hueristic(child, goal));
                open.add(child);
            }
        }
    }
    return open;
}

我的幽默感是这样的:

private int hueristic(Node start, Node goal) {
    int xAbs = Math.abs(start.getPoint().x - goal.getPoint().x);
    int yAbs = Math.abs(start.getPoint().y - goal.getPoint().y);
    return xAbs + yAbs;
}

0 个答案:

没有答案