简体   繁体   English

如何在A *启发式中添加比仅距离更多的参数?

[英]How to add more parameters to A* heuristic than only distance?

For pathfinding in a small 2D game I use an A* algorithm, by now based on a simple euclidean distance heuristic. 对于小型2D游戏中的寻路,我现在使用A *算法,该算法基于简单的欧氏距离启发式算法。 My game world is represented as a grid of cells, either occuppied by an obstacle or not. 我的游戏世界被表示为由障碍物占据或未被障碍物占据的网格。 The character for which a path has to be calculated using A* can move in any direction (if not blocked), not only N/E/S/W. 必须使用A *计算路径的字符可以朝任何方向移动(如果未被阻止),而不仅限于N / E / S / W。

Ok, so that basically works fine. 好的,这样基本上可以正常工作。 Now I need to add another parameter to the A* heuristic function, a cost value tied to each grid cell. 现在,我需要向A *启发式函数添加另一个参数,该参数值绑定到每个网格单元。 The higher this cost value, the more our character should try to avoid that cell. 该成本值越高,我们的角色越应该尝试避免使用该单元格。

However, I can't change the heuristic function to solely use this cost value per grid cell, because the cell's distance to the A* target location is still important. 但是,我无法更改试探函数以仅使用每个网格单元的成本值,因为单元到A *目标位置的距离仍然很重要。 The character should try to avoid any high cost cells, but at the same time it also must not get too far away from the target position. 角色应尽量避免使用任何高成本的格,但同时也不得距离目标位置太远。 I therefore need kind of a "trade off" between the cell's distance and its cost value. 因此,我需要在单元格的距离与其成本值之间进行某种“权衡”。

Ideally, I would like to find a solution that allows me to easilly adjust/optimize such a relation between the cell's distance and its cost value, so that I can fine tune the heuristic. 理想情况下,我想找到一种解决方案,使我能够轻松地调整/优化单元格距离与其成本值之间的这种关系,以便微调启发式方法。

Any ideas how to achieve this? 任何想法如何实现这一目标?

When you use any shortest path algorithm (A* or Dijkstrs) you need one cost value for each node. 使用任何最短路径算法(A *或Dijkstrs)时,每个节点都需要一个成本值。
So you must think yourself a formula how to combine distance (your cells) with obstacle costs. 因此,您必须为自己想一个公式,该公式如何结合距离(您的单元格)和障碍物成本。
You could create a cost() function which takes the length cost plus adds obstacle costs. 您可以创建一个cost()函数,该函数采用长度成本加上障碍成本。

You need to use weights (values between 0 and 1), lets say w is the distanse weight and 1-w is the cost weight so your new huristic will be something like: H= w*distance+(1-w)*cost 您需要使用权重(0到1之间的值),假设w是离散权重,而1-w是成本权重,那么您的新指标将是:H = w *距离+(1-w)*成本

now you need to optimize the W according to your domain - ampiricly. 现在,您需要根据域来优化W。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM