Python 星码问题

Python 星码问题,python,algorithm,graph,Python,Algorithm,Graph,我无法让我的a星实现正常工作。你能指点路吗?多谢各位 #!/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) #Function

我无法让我的a星实现正常工作。你能指点路吗?多谢各位

#!/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.append(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)  
#/usr/bin/env python
导入系统、re、数学
网格=[]
g=[]
h=[]
宽度=int(sys.argv[1])
高度=整数(sys.argv[2])
开放=[]
关闭=[]
startpos=(0,0)#(高度、宽度)
endpos=(6,5)#(高度、宽度)
#功能
def findlowestcostinopen():
最低=9999
最低空气温度=[]
对于开放式的q:
sum=int(g[q[0]][q[1]])+int(h[q[0]][q[1]]))
#打印金额,最低
如果sum0:
''''''  
currentg=g[currentnode[0]][currentnode[1]]
如果(q==0和w==1)或(q==0和w==1)或(q==1和w==0)或(q==1和w==0):
newsum=parentgcost+10
其他:newsum=parentgcost+14
如果是newsum0:
“存在于关闭状态”
其他:
#计算g值的时间
如果q==0:
如果w==-1或w==1:
nodecost=parentgcost+10
elif q==1:
如果w==0:
nodecost=parentgcost+10
其他:
nodecost=parentgcost+14
elif q==-1:
如果w==0:
nodecost=parentgcost+10
其他:
nodecost=parentgcost+14
g[(currentnode[0])][(currentnode[1])]=nodecost
网格[(currentnode[0])][(currentnode[1])]=最低成本
#打印节点成本
open.append(currentnode)
一些问题:

  • 您这样做是为了获得评论:

    '''some text'''
    
    这实际上不是一个注释,而是一个字符串。你只是不把它分配给任何东西。请发表评论:

    # some text
    
  • 这段代码很难读懂:

            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  
    
    将其更改为:

            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  
    
    请注意如何使用四个空格来缩进,而不仅仅是一个空格

  • 此处不需要括号:

            g[(currentnode[0])][(currentnode[1])]=nodecost   
    
    改为

            g[currentnode[0]][currentnode[1]]=nodecost   
    
  • 你太喜欢索引了。这也使得它很难阅读

            g[(currentnode[0])][(currentnode[1])]=nodecost
    
    不如

            height, width = currentnode
            g[height][width] = nodecost
    
  • 这些都不能解决您的问题,因为您没有说明这是什么,甚至没有说明代码应该做什么。

    一些问题:

  • 您这样做是为了获得评论:

    '''some text'''
    
    这实际上不是一个注释,而是一个字符串。你只是不把它分配给任何东西。请发表评论:

    # some text
    
  • 这段代码很难读懂:

            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  
    
    将其更改为:

            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  
    
    请注意如何使用四个空格来缩进,而不仅仅是一个空格

  • 此处不需要括号:

            g[(currentnode[0])][(currentnode[1])]=nodecost   
    
    改为

            g[currentnode[0]][currentnode[1]]=nodecost   
    
  • 你太喜欢索引了。这也使得它很难阅读

            g[(currentnode[0])][(currentnode[1])]=nodecost
    
    不如

            height, width = currentnode
            g[height][width] = nodecost
    

  • 这些都不能解决您的问题,因为您没有说明这是什么,甚至没有说明代码应该做什么。

    这是用于*搜索的伪代码。它很容易被转录成Python


    这是*搜索的伪代码。它很容易被转录成Python


    您能不能请您将代码正确格式化?否则您将得不到答案。如“选择代码文本并单击
    {}
    按钮”中所述,您的ifs是否都嵌套正确?看起来很可怕。除非你能先帮我们,否则没有人会帮你。另外(作为帮助的象征)在“if”或“else”条件后使用空语句的pythonic范例是使用no-op“pass”语句,而不是带有六个引号的空字符串!你没有描述你的问题,也没有问题。这将使帮助你成为问题。你能,请,请,正确格式化代码吗?否则您将得不到答案。如“选择代码文本并单击
    {}
    按钮”中所述,您的ifs是否都嵌套正确?看起来很可怕。除非你能先帮我们,否则没有人会帮你。另外(作为帮助的象征)在“if”或“else”条件后使用空语句的pythonic范例是使用no-op“pass”语句,而不是带有六个引号的空字符串!你没有描述你的问题,也没有问题。这将使帮助你成问题。