Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #graph class
  2. class Puzzle:
  3.  
  4. #init constructor
  5. def __init__(self, list, finish, G):
  6. assert(len(list) == 9)
  7. self.list = list
  8. self.finish = finish
  9. self.graph = [[list[0], list[1], list[2]], [list[3], list[4], list[5]],[list[6], list[7], list[8]]]
  10. self.goal = [[finish[0], finish[1], finish[2]], [finish[3], finish[4], finish[5]],[finish[6], finish[7], finish[8]]]
  11. self.G = G
  12. self.H = self.getH()
  13. self.F = self.H + self.G
  14. def __lt__(self, other):
  15. return self.F < other.F
  16.  
  17. #get summation hueristic values of current graph
  18. def getH(self):
  19. totalDist = 0
  20. for ya in range(len(self.graph)):
  21. for xa in range(len(self.graph[ya])):
  22. value = self.graph[ya][xa]
  23. location = self.getLocation(self.goal, value)
  24. xb = location[0]
  25. yb = location[1]
  26. deltaX = abs(xa-xb)
  27. deltaY = abs(ya-yb)
  28. dist = deltaX + deltaY
  29. totalDist += dist
  30. ##print("(", xa, ",", ya, "), (", xb, ",", yb, ") Dist: ", dist, sep= '')
  31. ##print("Total Dist: ", totalDist)
  32. return totalDist
  33.  
  34. #return of list of graphs of possible moves from current graph
  35. def getChildren(self):
  36.  
  37. x = self.getBlankLocation()[0]
  38. y = self.getBlankLocation()[1]
  39. print("Blank Tile: (", x, ",", y, ")", sep = '')
  40. children = []
  41.  
  42. if x != 2: ##
  43. newGraph = Puzzle(self.list, self.finish, self.G+1)
  44. children.append(newGraph.moveBlank("right"))
  45. if x != 0:
  46. newGraph = Puzzle(self.list, self.finish, self.G+1)
  47. children.append(newGraph.moveBlank("left"))
  48. if y != 2:
  49. newGraph = Puzzle(self.list, self.finish, self.G+1)
  50. children.append(newGraph.moveBlank("down"))
  51. if y != 0:
  52. newGraph = Puzzle(self.list, self.finish, self.G+1)
  53. children.append(newGraph.moveBlank("up"))
  54. return children
  55.  
  56. def moveBlank(self, direction):
  57. blankLocation = self.getBlankLocation()
  58. x = blankLocation[0]
  59. y = blankLocation[1]
  60.  
  61. if(direction == "right"):
  62. newLocation = (x+1, y)
  63. newValue = self.getValue(newLocation)
  64. self.setValue(blankLocation, newValue)
  65. self.setValue(newLocation, 0)
  66. if(direction == "left"):
  67. newLocation = (x-1, y)
  68. newValue = self.getValue(newLocation)
  69. self.setValue(blankLocation, newValue)
  70. self.setValue(newLocation, 0)
  71. if(direction == "up"):
  72. newLocation = (x, y-1)
  73. newValue = self.getValue(newLocation)
  74. self.setValue(blankLocation, newValue)
  75. self.setValue(newLocation, 0)
  76. if(direction == "down"):
  77. newLocation = (x, y+1)
  78. newValue = self.getValue(newLocation)
  79. self.setValue(blankLocation, newValue)
  80. self.setValue(newLocation, 0)
  81.  
  82. def getValue(self, location):
  83. return self.graph[location[1]][location[0]]
  84.  
  85. def setValue(self, location, value):
  86. self.graph[location[1]][location[0]] = value
  87.  
  88. def getLocation(self, graph, value):
  89. for y in range(len(graph)):
  90. for x in range(len(graph[y])):
  91. if graph[y][x] == value:
  92. return (x,y)
  93. def getBlankLocation(self):
  94. for y in range(len(self.graph)):
  95. for x in range(len(self.graph[y])):
  96. if self.graph[y][x] == 0:
  97. return (x,y)
  98.  
  99. #prints the graphs, replaces the 0 to blank
  100. def printGraphs(self, counter):
  101. a = self.graph[0]
  102. b = self.graph[1]
  103. c = self.graph[2]
  104. x = self.goal[0]
  105. y = self.goal[1]
  106. z = self.goal[2]
  107.  
  108. output = []
  109. if counter == 1:
  110. print("=Initial= =Goal=")
  111. else:
  112. print("=Current= =Goal=")
  113. print(str(a).replace("0", " "), " ", str(x).replace("0", " "), " Step: ", counter)
  114. print(str(b).replace("0", " "), " ", str(y).replace("0", " "), " H: ", self.H )
  115. print(str(c).replace("0", " "), " ", str(z).replace("0", " "), " G: ", self.G)
  116.  
  117. #graphs (0 is the blank tile)
  118. start = [2,8,3,1,6,4,7,0,5]
  119. goal = [1,2,3,8,0,4,7,6,5]
  120. puzzle = Puzzle(start, goal, 0)
  121. from queue import PriorityQueue
  122. q = PriorityQueue()
  123. q.put(puzzle)
  124.  
  125. puzzle.printGraphs(0)
  126.  
  127. children = puzzle.getChildren()
  128. children[0].printGraphs(0)
  129. #children2 = children[0].getChildren()
  130. #for child in children2:
  131. # child.printGraphs(0)
  132.  
  133. def main():
  134. step = 0
  135. while not q.empty():
  136. step += 1
  137. if step > 4:
  138. break
  139. current = q.get()
  140. current.printGraphs(step)
  141. children = current.getChildren()
  142. for child in children:
  143. if child.H == 0:
  144. return
  145. q.put(child)
  146.  
  147. ##main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement