2015-10-15 86 views
2

這是python文件即時嘗試使A *算法,但無法得到它的工作,我需要一些幫助,它是一個很棒的代碼,它已被運行在最新的Python版本的Windows錯誤:對象()不採取任何參數,無法解決它

from queue import PriorityQueue 
    class State(object): 
     def _init_(self,value,parent,start = 0,goal = 0): 
      self.children = [] 
      self.value = value 
      self.parent = parent 
      self.dist = 0 
      if parent: 
       self.path = parent.path[:] 
       self.path.append(value) 
       self.start = parent.start 
       self.goal = parent.goal 
      else: 
       self.path = [value] 
       self.start = start 
       self.goal = goal 


     def GetDist(self): 
      pass 

     def CreateChildren(self): 
      pass 


    class State_String(State): 
     def _init_(self,value,parent,start = 0,goal = 0): 
      super(State_String,self).__init__(value,parent,start,goal) 
      self.dist = self.GetDist() 
     def GetDist(self): 
      if self.value == self.goal: 
       return 0 
      dist = 0 
      for i in range(len(self.goal)): 
       letter = self.goal[i] 
       dist += abs(i - self.value.index(letter)) 
      return dist 

     def CreateChildren(self): 
      if not self.children: 
       for i in xrange(len(self.goal)-1): 
        val = self.value 
        val = val[:i] + val[i+1] + val[i] + val[i+2:] 
        child = State_String(val,self) 
        self.children.append(child) 


    class AStar_Solver: 
     def _init_(self,start,goal): 
      self.path = [] 
      self.visitedQueue = [] 
      self.priorityQueue = PriorityQueue() 
      self.start = start 
      self.goal = goal 

     def Solve(self): 
      startState = State_String(self.start,0,self.start,self.goal) 
      count = 0 
      self.priorityQueue.put((0,count,startState)) 
      while(not self.path and self.priorityQueue.qsize()): 
       closestChild = self.priorityQueue.get()[2] 
       closestChild.CreateChildren() 
       self.visitedQueue.append(closestChild.value) 
       for child in closestChild.children: 
        if child.value not in self.visitedQueue: 
         count +=1 
         if not child.dist: 
          self.path = child.path 
          break 
         self.priorityQueue.put((child.dist,count,child)) 

      if not self.path: 
       print "Goal of " + self.goal + " is not possible!" 
      return self.path 

    if _name_ == '__main__': 
     start1 = "hma" 
     goal1 = "ham" 
     a = AStar_Solver(start1,goal1) 
     a.Solve() 
     for i in xrange(len(a.path)): 
      print " %d)" %i + a.path[i] 

getting these errors: 

    Traceback (most recent call last): 
     File "C:/Users/Herby/Desktop/untitled/Astar.py", line 82, in <module> 
     a.Solve() 
     File "C:/Users/Herby/Desktop/untitled/Astar.py", line 59, in Solve 
     startState = State_String(self.start,0,self.start,self.goal) 
    TypeError: object() takes no parameters 

我需要知道它是如何可以固定

+1

在init方法前後需要兩個下劃線,如下所示:'__init__'。 – metatoaster

回答

1

所有你在你的類init寫單下劃線代替雙下劃線:

改變所有的init寫爲: _init___init__

而且,這條線是不正確以及:

if _name_ == '__main__': 

它需要進行名稱雙下劃線以及

if __name__ == '__main__': 

如果你有興趣,這裏是爲什麼越來越多的信息這是需要:

https://www.python.org/dev/peps/pep-0008/

在particula r請參閱以下說明:「double_leading_and_trailing_underscore

相關問題