Java 带图像映射的星形算法(android)
我使用算法a开始查找带有数组节点的路径。我有这样的图像映射和节点:Java 带图像映射的星形算法(android),java,algorithm,path-finding,a-star,Java,Algorithm,Path Finding,A Star,我使用算法a开始查找带有数组节点的路径。我有这样的图像映射和节点: if(fabs(currentDistX)<=1e-6) 红色节点是障碍物,黑色用于查找路径。我不知道为什么这条路是弯曲的。我使用了这个库,我被函数注册表项改变了: private void registerEdges(ArrayList<Node> nodes) { float currentDistX = 0; float currentDistY = 0;
if(fabs(currentDistX)<=1e-6)
红色节点是障碍物,黑色用于查找路径。我不知道为什么这条路是弯曲的。我使用了这个库,我被函数注册表项改变了:
private void registerEdges(ArrayList<Node> nodes)
{
float currentDistX = 0;
float currentDistY = 0;
float distance = 0;
for(int l = 0 ; l < nodes.size(); l++)
{
MINDISTN = Integer.MIN_VALUE;
MINDISTS = Integer.MAX_VALUE;
MINDISTE = Integer.MIN_VALUE;
MINDISTW = Integer.MAX_VALUE;
MINDISTNE = Integer.MAX_VALUE;
MINDISTNW = Integer.MAX_VALUE;
MINDISTSE = Integer.MAX_VALUE;
MINDISTSW = Integer.MAX_VALUE;
Node node = null;
currentDistX = 0;
currentDistY = 0;
//System.out.println("current " + node.x + " " + node.y);
for(int j = 0 ; j < map.size() ; j++)
{
if(l != j)
{
node = map.get(l);
currentDistX = map.get(j).x - node.x;
currentDistY = map.get(j).y - node.y;
if(currentDistX == 0)
{
if(currentDistY < 0)
{
if(currentDistY > MINDISTN)
{
MINDISTN = currentDistY;
node.setNorth(map.get(j));
//System.out.println(currentDist + " n " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistY > 0)
{
if(currentDistY < MINDISTS)
{
//System.out.println(currentDist + " south " + map.get(j).x + " " + map.get(j).y);
MINDISTS = currentDistY;
node.setSouth(map.get(j));
}
}
}
if(currentDistY == 0)
{
if(currentDistX < 0)
{
if(currentDistX > MINDISTE)
{
MINDISTE = currentDistX;
node.setEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX > 0)
{
//System.out.print("m " + MINDISTRIGHT);
if(currentDistX < MINDISTW)
{
MINDISTW = currentDistX;
node.setWest(map.get(j));
//System.out.println(currentDist + " w " + map.get(j).x + " " + map.get(j).y);
}
}
}
if(currentDistY != 0 && currentDistX != 0)
{
if(currentDistX > 0 && currentDistY > 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTNE)
{
MINDISTNE = distance;
node.setNorthEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX < 0 && currentDistY > 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTNW)
{
MINDISTNW = distance;
node.setNorthWest(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX <= 0 && currentDistY <= 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTSW)
{
MINDISTSW = distance;
node.setSouthWest(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX > 0 && currentDistY < 0)
{
distance = node.calculateDistanceBetweenNods(map.get(j));
if(distance < MINDISTSE)
{
MINDISTSE = distance;
node.setSouthEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
}
}
}
}
当前节点与邻居节点的连接不正确例如: 某些节点具有单向连接(镜像向上)
节点2有邻居节点1,节点1没有邻居节点2
节点.calculatedInstanceBetweenNodes()
函数或将节点设置为无缝网格(添加空节点)来更正此问题。但没有这种自由的经验,我只能推测float
表示当前距离,使用int
表示与之比较的常数。如果所有的都不处理浮点值例如:
if(currentDistX == 0)
应该是这样的:
if(fabs(currentDistX)<=1e-6)
正如我以前在许多编译器中多次被这个问题所困扰一样
我发现了一个错误。但是,这并不是唯一必要的:
if(currentDistY == 0)
{
if(currentDistX < 0) <===== here, change to currentDistX > 0
{
if(currentDistX > MINDISTE)
{
MINDISTE = currentDistX;
node.setEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX > 0) <======= here, change to currentDistX < 0
{
//System.out.print("m " + MINDISTRIGHT);
if(currentDistX < MINDISTW)
{
MINDISTW = currentDistX;
node.setWest(map.get(j));
//System.out.println(currentDist + " w " + map.get(j).x + " " + map.get(j).y);
}
}
}
if(currentDistY==0)
{
如果(currentDistX<0)0
{
如果(currentDistX>MINDISTE)
{
MINDISTE=currentDistX;
node.setEast(map.get(j));
//System.out.println(currentDist+“e”+map.get(j.x+“”+map.get(j.y));
}
}
否则,如果(currentDistX>0)您粘贴在答案中的代码对于二维路径搜索来说似乎有点复杂
如果我很了解你,你打算在一个8连接的电网中搜索图像中的最短路径,你正试图使用*来解决这个问题。我建议你使用一个搜索库,它可以更清楚地组织搜索算法的不同部分
在此基础上,如果有*的实现,则只需定义:
- 成本函数(根据问题描述,可能是点之间的欧几里德距离)
- 过渡函数(给定一个
点
检索8个邻居,过滤由于地图图像中的障碍物而无法访问的邻居)
- 一个启发式函数,已在
getEstimatedInstanceTogoal
中实现
您可以看看,它除了具有更清晰的结构外,还具有以动态方式生成图的节点的优点。这允许您节省大量内存,因为您正在生成的大多数节点不会在搜索过程中使用
您可以找到一个使用迷宫进行2D搜索的代码示例。您需要使该示例适合您的情况的唯一一件事是实现一个转换函数来使用地图图像。该示例使用以下代码生成给定当前节点的相邻节点(函数Maze2D.validLocationsFrom(Point)
):
public Collection validLocationsFrom(点位置){
Collection validMoves=new HashSet();
//检查所有有效的移动
对于(int row=-1;row我不明白它看起来工作正常。你有什么问题?如何搜索无障碍的封闭集邻居?您好,感谢您的回复时间。我的节点形成一个图,函数registerEdges只搜索8个邻居。有时节点与其他节点没有连接。方法registerEdges应该搜索所有连接对于当前节点和搜索路径,应避免障碍。您好,感谢您的回复时间。函数EstimateDistanceToGoal和CalculatedInstanceBetweenNode正文:float dx=goalX-startX;float dy=goalY-startY;float result=(float)(dxdx)+(dydy);您好,谢谢您的回复。您的回答将对我有所帮助。现在我创建了一个xml文件,其结构为:……在该文件中,我描述了每个节点的邻居。在下一步中,将该xml文件解析为ArrayList映射(每个节点都有AraryList with Neights).A star使用数组映射来搜索路径。你知道我的意思吗?嗨@michael,我想我理解你想法的基本原理。如果你最终决定将库用于你的项目,你可以实现TransitionFunction
,如下所示:1)在构造函数中,解析XML文件并将信息存储在map
中方法TransitionFunction.successorsOf(Point)
,将只从您在1)中生成的映射中获取后续者的信息。它应该可以非常有效地工作。
if(currentDistY == 0)
{
if(currentDistX < 0) <===== here, change to currentDistX > 0
{
if(currentDistX > MINDISTE)
{
MINDISTE = currentDistX;
node.setEast(map.get(j));
//System.out.println(currentDist + " e " + map.get(j).x + " " + map.get(j).y);
}
}
else if(currentDistX > 0) <======= here, change to currentDistX < 0
{
//System.out.print("m " + MINDISTRIGHT);
if(currentDistX < MINDISTW)
{
MINDISTW = currentDistX;
node.setWest(map.get(j));
//System.out.println(currentDist + " w " + map.get(j).x + " " + map.get(j).y);
}
}
}
public Collection<Point> validLocationsFrom(Point loc) {
Collection<Point> validMoves = new HashSet<Point>();
// Check for all valid movements
for (int row = -1; row <= 1; row++) {
for (int column = -1; column <= 1; column++) {
try {
//
//TODO: Implement isFree(Point) to check in your map image
//if a node is occupied or not.
//
if (isFree(new Point(loc.x + column, loc.y + row))) {
validMoves.add(new Point(loc.x + column, loc.y + row));
}
} catch (ArrayIndexOutOfBoundsException ex) {
// Invalid move!
}
}
}
validMoves.remove(loc);
return validMoves;
}