我目前正在进行A*寻路,但我遇到了一些问题.在走最好的路径之前,它走错路.我究竟做错了什么?
源代码:http://basic.apayostudios.com/AStar.zip
线上:
Game.cs http://pastie.org/1656955
Node.cs http://pastie.org/1656956
枚举:
public enum NodeType
{
None,
Solid,
Start,
End
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
所以这就是我"喜欢"能够写的东西:
cur_loc = min(open_set,key=lambda x:costs[x])
Run Code Online (Sandbox Code Playgroud)
cur_loc
是一个元组,目标是将其设置为等于open_set
具有最低成本的元组.(你会发现成本x
与costs[x]
)
我怎么能这样做?我尝试过Python.org的文档min()
,但我似乎没有找到太多帮助.
谢谢!
编辑: 我解决了自己的问题.
我很迟钝,没有初始化费用字典.我实际上是复制并粘贴其他人的python代码以测试他们正在做什么,但显然他们创建的代码片段不包括初始化部分.Woops.如果有人有兴趣:
for row in range(self.rows):
for col in range(self.cols):
myloc = (row,col)
if (myloc) not in closed_set:
costs[myloc] = (abs(end_row-row)+abs(end_col - col))*10
if (myloc) not in open_set:
open_set.add(myloc)
parents[myloc] = cur_loc
cur_loc = min(open_set,key=lambda x:costs[x])
Run Code Online (Sandbox Code Playgroud) 我正在用Java实现A*算法,以找到两点(不同城市的机场)之间的最短路径.为此目的,我使用无向和加权图,其中每条边代表两个节点(机场)之间的距离.启发式计算通过欧几里德距离完成.这是我的启发式函数的代码
double Sum = 0;
Sum = Math.pow((destination.getG()-currentNode.getG()),2.0);
return Math.sqrt(Sum);
Run Code Online (Sandbox Code Playgroud)
我用G值i计算启发式,即节点之间的边缘.这是对的吗?请帮助.启发式函数获取源节点和目标节点.我希望它清楚.
我有两个线程使用两个不同的功能.第一个从头到尾搜索,第二个从头到尾搜索.
现在我正在使用Thread.Sleep(10)
同步,但它需要花费太多时间,并且在这种情况下无法进行测试.
知道如何同步两个具有不同功能的线程?
我遵循维基百科上的伪代码来实现具有优先级队列的A*算法,以找到从一个源到达无限大网格中的另一个目的地的最短路径.当目标无法到达时问题出现,例如,被墙壁包围(没有无限长的墙壁),算法将不会停止,因此它会陷入无限循环.我对该解决方案的直观思考是进行一些预处理工作,例如在源和目的地之间添加边界,或者在搜索之前检查目的地是否被包围(这并不容易).有关无法到达的早期停止的建议吗?谢谢.
我有一个struct
包含一些int
和bool
成员的,我希望从列表中获得最低值(实际上是基于A*搜索的路径查找器).
基本上,我的对象看起来像这样:
public struct Tile
{
public int id;
public int x;
public int y;
public int cost;
public bool walkable;
public int distanceLeft;
public int parentid;
}
Run Code Online (Sandbox Code Playgroud)
我想得到距离最低的物品.列表声明如下:
List<Structs.Tile> openList = new List<Structs.Tile>();
Run Code Online (Sandbox Code Playgroud)
并以这种方式分配值:
while (pathFound == null)
{
foreach (Structs.Tile tile in map)
{
foreach (Structs.Tile tile1 in getSurroundingTiles(Current))
{
if (tile1.x == tile.x && tile1.y == tile.y)
{
Structs.Tile curTile = tile1;
curTile.parentid = Current.id;
curTile.distanceLeft = (Math.Abs(tile.x - goalx) + …
Run Code Online (Sandbox Code Playgroud) a-star ×6
c# ×3
algorithm ×2
path-finding ×2
.net ×1
dictionary ×1
heuristics ×1
java ×1
python ×1
search ×1
xna ×1