Advertisement Jump to content
Sign in to follow this  
GuiTeK

A* Bad heuristic (zigzag)

This topic is 1794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I'm new to AI programming and I need a pathfinding algorithm for my game which uses isometric tile maps (see below the screenshot in attachment). Player can move in the 8 cardinal directions (North, North East, East, etc.).

 

I successfully implemented the A* algorithm, however I think my heuristic is bad:

[attachment=19969:heuristic_bad.PNG]

As you can see, the algorithm doesn't keep a straight diagonal to the goal. It's not natural at all, I can't let the player move this way.

 

I used this heuristic which is described as the best algorithm for grids that allow 8 directions of movement.

Here is how I implemented it in Java:

public final int ORTHOGONAL_COST = 14;
public final int DIAGONAL_COST = 10;

public float getHeuristicCost(final Point2D.Int source, final Point2D.Int target)
{
	int dx = Math.abs(source.x - target.x);
	int dy = Math.abs(source.y - target.y) / 2;
	
	return ORTHOGONAL_COST * (dx + dy) + (DIAGONAL_COST - 2 * ORTHOGONAL_COST) * Math.min(dx, dy);
}

I divided dy by 2 because of the map's style, it gives a path even weirder if I don't divide by 2.

 

I can understand the path isn't straight because this heuristic isn't fit for my map style, but which one should I use then?

 

Thank you.

Share this post


Link to post
Share on other sites
Advertisement

Are you sure the heuristic is what's wrong and not your overall traversal cost computation?

Hmm... what do you mean? I think I didn't implement such a thing blink.png ...

Sorry but I'm new to AI world...

Share this post


Link to post
Share on other sites
The diagonal cost is supposed to be greater than the orthogonal cost. Of course your pathfinder will choose to use all diagonals if those are cheaper. They cover more ground.

Also, if you have to throw in a divide by 2 for no apparent reason other than style, you have other problems too. Your pathfinding shouldn't have to be any different than if you draw the map with no skew. Edited by Pink Horror

Share this post


Link to post
Share on other sites

I'd first try with the simplest implementation of your cost function and that is simple geometric distance between the two points. That is Math.sqrt(dx * dx + dy * dy). Get rid of that /2 in dy, just as the others said. If this doesn't work, then your problem is elsewhere :) Just then, when it works, you might go on with optimising the function to use integers only (for performance).

Share this post


Link to post
Share on other sites
As a very general rule, the heuristic is not to blame for bad quality of the results (as long as it's admissible). If your algorithm is picking a result you don't like among many possible results with the same cost, it means your cost function doesn't accurately reflect your preferences.

Share this post


Link to post
Share on other sites

The diagonal cost is supposed to be greater than the orthogonal cost. Of course your pathfinder will choose to use all diagonals if those are cheaper. They cover more ground.

Also, if you have to throw in a divide by 2 for no apparent reason other than style, you have other problems too. Your pathfinding shouldn't have to be any different than if you draw the map with no skew.

I've taken into account your tips. Here is the code and the results:

public final int ORTHOGONAL_COST = 10;
public final int DIAGONAL_COST = 14;

public float getHeuristicCost(final Point2D.Int source, final Point2D.Int target)
{
	int dx = Math.abs(source.x - target.x);
	int dy = Math.abs(source.y - target.y);
	
	return ORTHOGONAL_COST * (dx + dy) + (DIAGONAL_COST - 2 * ORTHOGONAL_COST) * Math.min(dx, dy);
}

[attachment=20008:diagonal_distance.PNG]

 

Maybe I don't get the expected result (straight diagonal line) because I don't handle the coordinates the right way? Below you can see how I do it:

[attachment=20009:grid_blank.PNG]

Is it right?

 

 

 

I'd first try with the simplest implementation of your cost function and that is simple geometric distance between the two points. That is Math.sqrt(dx * dx + dy * dy). Get rid of that /2 in dy, just as the others said. If this doesn't work, then your problem is elsewhere smile.png Just then, when it works, you might go on with optimising the function to use integers only (for performance).

Tried that too. Code & results:

public float getHeuristicCost(final Point2D.Int source, final Point2D.Int target)
{
	int dx = Math.abs(source.x - target.x);
	int dy = Math.abs(source.y - target.y);
	
	return (float)Math.sqrt(dx * dx + dy * dy);
}

[attachment=20007:euclidean_distance.PNG]

 

 

As a very general rule, the heuristic is not to blame for bad quality of the results (as long as it's admissible). If your algorithm is picking a result you don't like among many possible results with the same cost, it means your cost function doesn't accurately reflect your preferences.

How can I adjust my cost function? I just try again and again until I find something that matches with what I want?

Share this post


Link to post
Share on other sites

Look carefully at the coordinates you provided and the pattern in traversal.

 

Your "horizontal" cost (e.g. 0,0 to 1,0) should be higher than your "diagonal" cost (e.g. 0,0 to 0,1). The reason dividing dy by 2 in your first attempt appeared to help is that you made the relative cost of diagonals cheaper.

 

Don't just "try again and again." Look at your numbers and think about how to represent the costs that you want, and then write code that reflects that.

 

 

Also, it sounds like you're not actually implementing A* correctly, or don't fully understand the algorithm. There are two costs: the heuristic (which as Alvaro said is generally not the problem provided it is admissible) and the actual traversal cost. Chances are your problem is in your A* implementation in how you compute the actual traversal cost versus the values provided by your heuristic.

Edited by ApochPiQ

Share this post


Link to post
Share on other sites

How can I adjust my cost function? I just try again and again until I find something that matches with what I want?


Why don't you share with the forum the actual cost function that you use when you're building the path? The heuristic and the real cost have to be fixed together.

Also, because you're having trouble with an 8-way system, I think you should try to get 4 directions working correctly first. I'd like to know how that works out.

Another interesting experiment would be using a heuristic that always returned 0. You would have a slower path-finding function but it should work and rule out any heuristic issues.

Share this post


Link to post
Share on other sites

Yeah, I'm with ApochPiQ, I suspect the problem is that there are two functions to determine the next move, the cost to move to that new tile, and the heuristic to determine how close that move will get you to the goal.

 

For your case, you want to take the path that costs the least to move to (diagonals cost more) and yet yields the best H value, in your case the least distance to the goal.

Edited by ferrous

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!