I have a problem getting my a star implementation to work. Can you point the way? Thank you.
#!/usr/bin/env python
import sys, re, math
grid = []
g = []
h = []
width = int(sys.argv[1])
height = int(sys.argv[2])
open = []
close = []
startpos = (0,0) #(height,width)
endpos = (6,5) #(height,width)
#Functions
def findlowestcostinopen():
lowest = 9999
lowestpair = []
for q in open:
sum = int(g[q[0]][q[1]])+int(h[q[0]][q[1]])
#print sum,lowest
if sum<lowest:
lowestpair = q
lowest=sum
return lowestpair
# Init
for q in range(height):
temp = []
for w in range(width):
temp.append((0,0))
grid.append(temp)
for q in range(height):
temp = []
for w in range(width):
temp.append(0)
g.append(temp)
for q in range(height):
temp = []
for w in range(width):
temp.appe开发者_如何学JAVAnd(0)
h.append(temp)
for q in range(height):
for w in range(width):
h[q][w]=abs(endpos[0]-q)*10 + abs(endpos[1]-w)*10
open.append(startpos)
switch = True
while switch and open:
#Find the smallest cost
lowestcost = findlowestcostinopen()
print lowestcost,endpos
if lowestcost == endpos:
switch = False
print 'found',lowestcost
parentgcost=int(g[lowestcost[0]][lowestcost[1]])
#print parentgcost
#Check every directly connected node
for q in range(-1,2):
for w in range(-1,2):
currentnode = ((lowestcost[0]+q),(lowestcost[1]+w))
if q==0 and w==0:
''''''
elif(currentnode[0]<0 or currentnode[0]>(height-1)):
'''Vertical out'''
elif(currentnode[1]<0 or currentnode[1]>(width-1)):
'''Horizontal out'''
elif(grid[currentnode[0]][currentnode[1]]=='wall'):
'''WALL'''
elif open.count((currentnode[0],currentnode[1]))>0:
''''''
currentg = g[currentnode[0]][currentnode[1]]
if (q==0 and w==1) or (q==0 and w==-1) or (q==1 and w==0) or (q==-1 and w==0):
newsum = parentgcost+10
else: newsum = parentgcost+14
if newsum<currentg:
g[currentnode[0]][currentnode[1]]=newsum
grid[currentnode[0]][currentnode[1]]=lowestcost
elif close.count((currentnode[0],currentnode[1]))>0:
'''EXISTS IN CLOSE'''
else:
#Time to calculate g values
if q==0:
if w==-1 or w==1:
nodecost = parentgcost+10
elif q==1:
if w==0:
nodecost = parentgcost+10
else:
nodecost = parentgcost+14
elif q==-1:
if w==0:
nodecost = parentgcost+10
else:
nodecost = parentgcost+14
g[(currentnode[0])][(currentnode[1])]=nodecost
grid[(currentnode[0])][(currentnode[1])]=lowestcost
#print nodecost
open.append(currentnode)
Some problems:
You do this for comments:
'''some text'''
That is in fact not a comment, but a string. You just don't assign it to anything. se a comment instead:
# some text
This code is very hard to read:
if q==0: if w==-1 or w==1: nodecost = parentgcost+10 elif q==1: if w==0: nodecost = parentgcost+10 else: nodecost = parentgcost+14 elif q==-1: if w==0: nodecost = parentgcost+10 else: nodecost = parentgcost+14
Change it to:
if q==0 and (w==-1 or w==1): nodecost = parentgcost+10 elif q==1 and w==0: nodecost = parentgcost+10 elif q==1: nodecost = parentgcost+14 elif q==-1 and w==0: nodecost = parentgcost+10 elif q==-1: nodecost = parentgcost+14
and note how four spaces are used to indent, not just one.
The parenthesis here are not needed:
g[(currentnode[0])][(currentnode[1])]=nodecost
change to
g[currentnode[0]][currentnode[1]]=nodecost
You are to fond of indexing. It also makes it hard to read.
g[(currentnode[0])][(currentnode[1])]=nodecost
would be better as
height, width = currentnode g[height][width] = nodecost
None of this will fix your problem, since you didn't say what that was or even what the code is supposed to do.
This is pseudocode for A* search. It's easily transcribed into Python.
Wikipedia A* search page
精彩评论