Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from Queue import PriorityQueue
- class State(object):
- def __init__(self, value, parent,
- start = 0, goal = 0):
- self.children = []
- self.parent = parent
- self.value = value
- self.dist = 0
- if parent:
- self.path = parent.path[:] #copy list value
- 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(object):
- """docstring for ClassName"""
- def __init__(self, value, parent, start = 0, goal = 0):
- super(ClassName, 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 range(len(self.goal)-1):
- val = self.value
- val = val[:i] + val[i+1] + valu[i] + val[i+2]
- child = State_String(val, self)
- self.children.append(child)
- class AStart_Solver:
- """docstring for ClassName"""
- 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()): # while self.path is not null and priorityQueue has size
- closestChild = self.priorityQueue.get()[2]
- closestChild = CreateChildren()
- self.visitedQueue.append(closestChild.value)
- for x 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, )))
- if not self.path:
- print("Goal of " + self.goal + " is not possible")
- return sel.path
- """
- class ClassName(object):
- #docstring for ClassName
- 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, self)
- count = 0
- self.priorityQueue.put((o, count, startState))
- while(not self.path and self.priorityQueue.qsize):
- """
- ##==============================================
- ## Main
- if __name__ == '__main__':
- start1 = "hma"
- goal1 = "ham"
- print("starting...")
- a = AStart_Solver(star1, goal1)
- a.Solve()
- for i in range(len(a.path)):
- print("{0}) {1}".format(i, a.path[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement