你应该改变你的得分部分。现在你计算绝对距离。而是计算最小移动距离。如果你把每一步都算作一次,那么如果你在 (x,y) 并去 (dX,dY) 那将是
distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
较低的值被认为是较高的分数。
这种启发式是猜测如果没有任何阻碍,它将采取多少步。
启发式的好处是您可以更改它以获得您想要的结果,例如,如果您喜欢按照您的建议沿直线移动,那么您可以进行此更改:
= distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
+ (1 if this is a turn from the last move)
这将使您“找到”趋向于同一方向的解决方案。
如果您想强制尽可能少的转弯:
= distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
+ (1 times the number of turns made)
这就是 A* 的优点——启发式会通知搜索——你仍然会找到一个解决方案,但如果有多个解决方案,你可以影响你首先看的地方——这有助于模拟 AI 行为.
疑问:第一种和第二种计算方式有何不同?
第一个将较低的优先级放在转弯的移动上。第二个将较低优先级放在转弯较多的路径上。在某些情况下(例如,第一个转弯),该值将是相同的,但总的来说,第二个将选择具有尽可能少转弯的路径,而第一个可能没有。
Also, 1 if this is a turn from the last move , for this,
say i have source at top left and destination at bottom right, now my
path normally would be, left,left,left...down,down,down.... Now, 1 if
this is a turn from the last move, according to this, when I change
from left to down, will I add 1?
Yes
Wont it make the total value more and the priority for down will decrease.
Yes, exactly. You want to not look at choices that have a turn in them first. This will make them lower priority and your algorithm will investigate other options with a higher priority -- exactly what you want.
Or 1 if this is a turn from the last move is when I move to a cell, that is not abutting the cell previously worked upon? Thnks –</p>
No, I don't understand this question -- I don't think it makes sense in this context -- all moves have to abut the previous cell, diagonal moves are not allowed.
Though, I'd really appreciate if you could tell me one instance where the first and second methods will give different answers. If you could. Thanks alot. :)
Not so easy without seeing the details of your algorithm but the following might work:
The red are blocks. The green is what I would expect the first one to do, it locally tries to find the least turn. The blue is the least turn solution. Note, how far the red areas are from each other and the details of how your algorithm influence if this will work. As I have it above -- having an extra turn only costs 1 in the heuristic. SO, if you want to be sure this will work change the heuristic like this:
= distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
+ (25 times the number of turns made)
Where 25 is bigger than the distance to get past the 2nd turn in the green path. (Thus after the 2nd turn the blue path will be searched.)