开发者

A Star Algorithm - Help on G and H part

开发者 https://www.devze.com 2023-03-04 22:03 出处:网络
I am having a bit of trouble getting the H and G to function properly. What is happening is when I run the program it sometimes finds the best path and sometimes goes way out of the way to get to the

I am having a bit of trouble getting the H and G to function properly. What is happening is when I run the program it sometimes finds the best path and sometimes goes way out of the way to get to the location.

Here are some screenshots of what is going on:

Good path find

Bad path find

This is my current setup for F, H and G:

public double f(Node current, Node adj, Node goal)
{
    double f = g(current, adj) + h(current, goal);
    return f;
}

public double h(Node current, Node goal)
    {
        double dx = goal.getX() - current.getX();
        double dy = goal.getY() - current.getY();

        double h = Math.sqrt(dx*dx + dy*dy);

        return h;
    }

public doub开发者_如何学Gole g(Node current, Node adj)
    {
        double dx = adj.getX() - current.getX();
        double dy = adj.getY() - current.getY();

        double g = Math.sqrt(Math.abs(dx) + Math.abs(dy));
        System.out.println("g " + g);
        return g;
    }

Thanks!


The G value is the cost from the start to the current node and not just to the adjacent node. At the moment you are more doing a greedy search, just going forward in the shortest direction and not looking back how far you have traveled already.

so you got "cost from start to current" + "(under)estimated cost from current to goal".

0

精彩评论

暂无评论...
验证码 换一张
取 消