Advertisement
ivandrofly

A_Start

Mar 25th, 2015
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. #!/usr/bin/python
  2. from Queue import PriorityQueue
  3.  
  4. class State(object):
  5.     def __init__(self, value, parent,
  6.                      start = 0, goal = 0):
  7.         self.children = []
  8.         self.parent = parent
  9.         self.value = value
  10.         self.dist = 0
  11.         if parent:
  12.             self.path = parent.path[:] #copy list value
  13.             self.path.append(value)
  14.             self.start = parent.start
  15.             self.goal = parent.goal
  16.         else:
  17.             self.path = [value]
  18.             self.start = start
  19.             self.goal = goal
  20.  
  21.     def GetDist(self):
  22.         pass
  23.     def CreateChildren(self):
  24.         pass
  25.  
  26. class State_String(object):
  27.     """docstring for ClassName"""
  28.     def __init__(self, value, parent, start = 0, goal = 0):
  29.         super(ClassName, self).__init__(value, parent, start, goal)
  30.         self.dist = self.GetDist()
  31.  
  32.     def GetDist(self):
  33.         if self.value == self.goal:
  34.             return 0
  35.         dist = 0
  36.         for i in range(len(self.goal)):
  37.             letter = self.goal[i]
  38.             dist += abs(i - self.value.index(letter))
  39.         return dist
  40.  
  41.     def CreateChildren(self):
  42.         if not self.children:
  43.             for i in range(len(self.goal)-1):
  44.                 val = self.value
  45.                 val = val[:i] + val[i+1] + valu[i] + val[i+2]
  46.                 child = State_String(val, self)
  47.                 self.children.append(child)
  48.  
  49. class AStart_Solver:
  50.     """docstring for ClassName"""
  51.     def __init__(self, start, goal):
  52.         self.path = []
  53.         self.visitedQueue = []
  54.         self.priorityQueue = PriorityQueue()
  55.         self.start = start
  56.         self.goal = goal
  57.  
  58.     def Solve(self):
  59.         startState = State_String(self.start, 0, self.start, self.goal)
  60.         count = 0
  61.         self.priorityQueue.put((0, count, startState))
  62.         while(not self.path and self.priorityQueue.qsize()): # while self.path is not null and priorityQueue has size
  63.             closestChild = self.priorityQueue.get()[2]
  64.             closestChild = CreateChildren()
  65.             self.visitedQueue.append(closestChild.value)
  66.             for x in closestChild.children:
  67.                 if child.value not in self.visitedQueue:
  68.                     count += 1
  69.                     if not child.dist:
  70.                         self.path = child.path
  71.                         break
  72.                     self.priorityQueue.put(((child.dist, )))
  73.         if not self.path:
  74.             print("Goal of " + self.goal + " is not possible")
  75.         return sel.path
  76.  
  77. """
  78. class ClassName(object):
  79.    #docstring for ClassName
  80.    def __init__(self, start, goal):
  81.        self.path = []
  82.        self.visitedQueue = []
  83.        self.priorityQueue = PriorityQueue()
  84.        self.start = start
  85.        self.goal = goal
  86.    def Solve(self):
  87.        startState = State_String(self.start, 0, self.start, self.goal, self)
  88.        count = 0
  89.        self.priorityQueue.put((o, count, startState))
  90.        while(not self.path and self.priorityQueue.qsize):
  91. """
  92. ##==============================================
  93. ## Main
  94.  
  95. if __name__ == '__main__':
  96.     start1 = "hma"
  97.     goal1 = "ham"
  98.     print("starting...")
  99.     a = AStart_Solver(star1, goal1)
  100.     a.Solve()
  101.     for i in range(len(a.path)):
  102.         print("{0}) {1}".format(i, a.path[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement