星号问题

时间:2011-01-11 09:55:08

标签: python algorithm graph

让我的星形实现工作时遇到问题。你能指明方向吗?谢谢。

#!/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)  

2 个答案:

答案 0 :(得分:4)

一些问题:

  1. 您这样做是为了评论:

    '''some text'''
    

    实际上不是一个评论,而是一个字符串。你只是不把它分配给任何东西。请改为评论:

    # some text
    
  2. 此代码很难阅读:

            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  
    

    并注意如何使用四个空格来缩进,而不只是一个。

  3. 此处不需要括号:

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

    更改为

            g[currentnode[0]][currentnode[1]]=nodecost   
    
  4. 你喜欢索引。这也让人难以阅读。

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

    会更好

            height, width = currentnode
            g[height][width] = nodecost
    
  5. 这些都无法解决您的问题,因为您没有说出那是什么,甚至没有说代码应该做什么。

答案 1 :(得分:0)

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

Wikipedia A* search page