Star Algorithm - Help for Parts G and H

I'm having trouble getting the functions H and G to function properly. What happens when I run a program, it sometimes finds a better way and sometimes it goes out of the way to get to the place.

Here are some screenshots of what is happening:

Good way finding

Poor path finding

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 double 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;
    }

Thank!

+3
source share
1 answer

The value of G is the cost from the beginning to the current node, not just the neighboring node. At the moment, you are doing a greedy search more, just go on the shortest path and do not look back how far you've traveled.

" " + "() ".

+1

All Articles