我一直在研究Mario AI竞赛中的成员正在做的事情,其中有些人利用A *(A星)路径算法构建了一些非常简洁的Mario机器人。
我的问题是,A-Star与Dijkstra相比如何?看着它们,它们看起来很相似。
为什么有人会使用另一个?尤其是在游戏中使用路径时?
我一直在研究Mario AI竞赛中的成员正在做的事情,其中有些人利用A *(A星)路径算法构建了一些非常简洁的Mario机器人。
我的问题是,A-Star与Dijkstra相比如何?看着它们,它们看起来很相似。
为什么有人会使用另一个?尤其是在游戏中使用路径时?
Answers:
Dijkstra是A *的特例(启发式为零时)。
它具有一个成本函数,它是从源到每个节点的实际成本值:f(x)=g(x)
。
它仅考虑实际成本即可找到从源到其他节点的最短路径。
它具有两个成本函数。
g(x)
:与Dijkstra相同。到达节点的实际成本x
。h(x)
:从节点x
到目标节点的大概成本。这是一种启发式功能。这种启发式功能绝不能高估成本。这意味着,从节点到达目标节点的实际成本x
应大于或等于h(x)
。这被称为可允许的启发式。每个节点的总成本由下式计算: f(x)=g(x)+h(x)
A *搜索仅在看起来有希望的情况下才扩展节点。它仅专注于从当前节点到达目标节点,而不是其他所有节点。如果允许启发式函数,则为最佳。
因此,如果您的启发式函数可以很好地估计未来成本,那么与Dijkstra相比,您需要探索的节点要少得多。
Dijkstra的算法永远不会用于寻路。如果您能想到一个体面的启发式方法(通常对于游戏来说特别容易,尤其是在2D世界中),那么使用A *毫无疑问。根据搜索空间的不同,有时最好使用迭代加深A *,因为它使用较少的内存。
Dijkstra是A *的特例。
Dijkstra找到从起始节点到所有其他节点的最低成本。A *查找从起始节点到目标节点的最低成本。
Dijkstra的算法永远不会用于路径查找。使用A *可以得出不错的启发式方法。根据搜索空间,迭代A *是更可取的,因为它使用较少的内存。
Dijkstra算法的代码为:
// A C / C++ program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
#include <stdio.h>
#include <limits.h>
// Number of vertices in the graph
#define V 9
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i
bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V-1; count++)
{
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in first iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
printSolution(dist, V);
}
// driver program to test above function
int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}
A *算法的代码为:
class Node:
def __init__(self,value,point):
self.value = value
self.point = point
self.parent = None
self.H = 0
self.G = 0
def move_cost(self,other):
return 0 if self.value == '.' else 1
def children(point,grid):
x,y = point.point
links = [grid[d[0]][d[1]] for d in [(x-1, y),(x,y - 1),(x,y + 1),(x+1,y)]]
return [link for link in links if link.value != '%']
def manhattan(point,point2):
return abs(point.point[0] - point2.point[0]) + abs(point.point[1]-point2.point[0])
def aStar(start, goal, grid):
#The open and closed sets
openset = set()
closedset = set()
#Current point is the starting point
current = start
#Add the starting point to the open set
openset.add(current)
#While the open set is not empty
while openset:
#Find the item in the open set with the lowest G + H score
current = min(openset, key=lambda o:o.G + o.H)
#If it is the item we want, retrace the path and return it
if current == goal:
path = []
while current.parent:
path.append(current)
current = current.parent
path.append(current)
return path[::-1]
#Remove the item from the open set
openset.remove(current)
#Add it to the closed set
closedset.add(current)
#Loop through the node's children/siblings
for node in children(current,grid):
#If it is already in the closed set, skip it
if node in closedset:
continue
#Otherwise if it is already in the open set
if node in openset:
#Check if we beat the G score
new_g = current.G + current.move_cost(node)
if node.G > new_g:
#If so, update the node to have a new parent
node.G = new_g
node.parent = current
else:
#If it isn't in the open set, calculate the G and H score for the node
node.G = current.G + current.move_cost(node)
node.H = manhattan(node, goal)
#Set the parent to our current item
node.parent = current
#Add it to the set
openset.add(node)
#Throw an exception if there is no path
raise ValueError('No Path Found')
def next_move(pacman,food,grid):
#Convert all the points to instances of Node
for x in xrange(len(grid)):
for y in xrange(len(grid[x])):
grid[x][y] = Node(grid[x][y],(x,y))
#Get the path
path = aStar(grid[pacman[0]][pacman[1]],grid[food[0]][food[1]],grid)
#Output the path
print len(path) - 1
for node in path:
x, y = node.point
print x, y
pacman_x, pacman_y = [ int(i) for i in raw_input().strip().split() ]
food_x, food_y = [ int(i) for i in raw_input().strip().split() ]
x,y = [ int(i) for i in raw_input().strip().split() ]
grid = []
for i in xrange(0, x):
grid.append(list(raw_input().strip()))
next_move((pacman_x, pacman_y),(food_x, food_y), grid)
Dijkstra找到从起始节点到所有其他节点的最低成本。A *查找从起始节点到目标节点的最低成本。
因此,当您需要的只是从一个节点到另一个节点的最小距离时,Dijkstra似乎效率会降低。
如果您查看Astar 的伪代码:
foreach y in neighbor_nodes(x)
if y in closedset
continue
而如果您与Dijkstra相同的话:
for each neighbor v of u:
alt := dist[u] + dist_between(u, v) ;
因此,重点是,Astar不会对节点进行多次评估,
因为它认为一次查看一个节点就足够了,因为
它具有启发性。
OTOH,Dijkstra的算法很容易自行纠正,以防万一
节点再次弹出。
这应该使Astar更快,更适合于路径查找。
在A *中,对于每个节点,请检查它们的传出连接。
对于每个新节点,您都需要根据与该节点的连接权重以及到达前一个节点所需的成本来计算到目前为止的最低成本(csf)。
另外,您估计从新节点到目标节点的成本,并将其添加到csf中。现在,您有了估算的总成本(等)。(等= csf +到目标的估计距离)接下来,从新节点中选择具有最低
等值的一个。与之前相同,直到新节点之一成为目标。
Dijkstra的工作原理几乎相同。除了估计的到目标的距离始终为0,并且当目标不仅是新节点之一,而且是csf最低的目标时,算法首先停止。
A *通常比dijstra更快,尽管并非总是如此。在视频游戏中,您通常会提倡“足够接近游戏”的方法。因此,从A *开始的“足够接近”的最佳路径通常就足够了。