Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. m = 3
  2. n = 3
  3.  
  4. def swap(board,i,j,k,l):
  5. tmp = board[i][j]
  6. board[i][j] = board[k][l]
  7. board[k][l] = tmp
  8. return board
  9.  
  10. def copy(board):
  11. ret = [[0 for x in range(n)] for y in range(m)]
  12. for i in range(m):
  13. for j in range(n):
  14. ret[i][j] = board[i][j]
  15. return ret
  16.  
  17. def unique_string(board):
  18. if (board == None):
  19. return ""
  20. s = ""
  21. if (board == None):
  22. return s
  23. for i in range(m):
  24. for j in range(n):
  25. s += str(board[i][j])
  26. return s
  27.  
  28. def find_zero(board):
  29. for i in range (m):
  30. for j in range (n):
  31. if (board[i][j] == 0):
  32. return (i, j)
  33.  
  34. def verify_goal_state(array):
  35. goal = 0
  36. for i in range (m):
  37. for j in range (n):
  38. if (array[i][j] == goal):
  39. goal = goal + 1
  40. else: return False
  41. return True
  42.  
  43. def move_up(original_board):
  44. board = copy(original_board)
  45. zero_position = find_zero(board)
  46. if (zero_position[0] == 0):
  47. return None
  48.  
  49. else:
  50. i = zero_position[0]
  51. j = zero_position[1]
  52. k = i - 1
  53. l = j
  54. swap(board,i,j,k,l)
  55. return board
  56.  
  57. #array = [[3,1,2],[0,4,5],[6,7,8]]
  58. #move_up(array)
  59.  
  60. def move_down(original_board):
  61. board = copy(original_board)
  62. zero_position = find_zero(board)
  63. if (zero_position[0] == 2):
  64. return None
  65.  
  66. else:
  67. i = zero_position[0]
  68. j = zero_position[1]
  69. k = i + 1
  70. l = j
  71. swap(board,i,j,k,l)
  72. return board
  73.  
  74. array = [[0,1,2],[3,4,5],[6,7,8]]
  75. move_down(array)
  76.  
  77. def move_right(original_board):
  78. board = copy(original_board)
  79. zero_position = find_zero(board)
  80. if (zero_position[1] == 2):
  81. return None
  82.  
  83. else:
  84. i = zero_position[0]
  85. j = zero_position[1]
  86. k = i
  87. l = j + 1
  88. swap(board,i,j,k,l)
  89. return board
  90.  
  91. #array = [[2,0,1],[3,4,5],[6,7,8]]
  92. #move_Right(array)
  93.  
  94. def move_left(original_board):
  95. board = copy(original_board)
  96. zero_position = find_zero(board)
  97. if (zero_position[1] == 0):
  98. return None
  99.  
  100. else:
  101. i = zero_position[0]
  102. j = zero_position[1]
  103. k = i
  104. l = j - 1
  105. swap(board,i,j,k,l)
  106. return board
  107.  
  108. visited_state = set("")
  109. goal_found = False
  110. def ids(board):
  111. global visited_state
  112. global goal_found
  113.  
  114. queue = []
  115. max_depth = 0
  116.  
  117. queue.append(board)
  118. while (len(queue) > 0 and goal_found != True):
  119. for current_board in queue:
  120. current_board = queue.pop(0)
  121. current_state = unique_string(current_board)
  122. if (current_state not in visited_state):
  123. result = dfs(current_board, 0, max_depth, queue)
  124. max_depth += 1
  125. if (result != None):
  126. visited_state = set()
  127. goal_found = False
  128. return result
  129.  
  130.  
  131. def dfs(board, current_depth, max_depth, queue):
  132.  
  133. result = None
  134. if (current_depth <= max_depth):
  135. global visited_state
  136. global goal_found
  137. state = unique_string(board)
  138. if (goal_found == False and board != None and state not in visited_state):
  139. # print_result(board)
  140.  
  141. if (verify_goal_state(board)):
  142. goal_found = True
  143. return board
  144.  
  145. visited_state.add(state)
  146.  
  147. state1 = move_up(board)
  148. state2 = move_down(board)
  149. state3 = move_left(board)
  150. state4 = move_right(board)
  151.  
  152. result = dfs(state1, current_depth + 1, max_depth, queue)
  153. if (result == None):
  154. result = dfs(state2, current_depth + 1, max_depth, queue)
  155. if (result == None):
  156. result = dfs(state3, current_depth + 1, max_depth, queue)
  157. if (result == None):
  158. result = dfs(state4, current_depth + 1, max_depth, queue)
  159.  
  160. queue.append(board)
  161. return result
  162.  
  163. class MyHeap:
  164. def __init__(self):
  165. self.heapList = [[]]
  166. self.currentSize = 0
  167.  
  168. def percUp(self,i):
  169. while i // 2 > 0:
  170. if self.heapList[i].h < self.heapList[i // 2].h:
  171. tmp = self.heapList[i // 2]
  172. self.heapList[i // 2] = self.heapList[i]
  173. self.heapList[i] = tmp
  174. i = i // 2
  175.  
  176. def insert(self,k):
  177. self.heapList.append(k)
  178. self.currentSize = self.currentSize + 1
  179. self.percUp(self.currentSize)
  180.  
  181. def percDown(self,i):
  182. while (i * 2) <= self.currentSize:
  183. mc = self.minChild(i)
  184. if self.heapList[i].h > self.heapList[mc].h:
  185. tmp = self.heapList[i]
  186. self.heapList[i] = self.heapList[mc]
  187. self.heapList[mc] = tmp
  188. i = mc
  189.  
  190. def minChild(self,i):
  191. if i * 2 + 1 > self.currentSize:
  192. return i * 2
  193. else:
  194. if self.heapList[i*2].h < self.heapList[i*2+1].h:
  195. return i * 2
  196. else:
  197. return i * 2 + 1
  198.  
  199. def delMin(self):
  200. retval = self.heapList[1]
  201. self.heapList[1] = self.heapList[self.currentSize]
  202. self.currentSize = self.currentSize - 1
  203. self.heapList.pop()
  204. self.percDown(1)
  205. return retval
  206.  
  207. def buildHeap(self,alist):
  208. i = len(alist) // 2
  209. self.currentSize = len(alist)
  210. self.heapList = [0] + alist[:]
  211. while (i > 0):
  212. self.percDown(i)
  213. i = i - 1
  214.  
  215. class MyBoard:
  216. def __init__(self, board, value):
  217. self.board = board
  218. self.h = value
  219.  
  220.  
  221. def h(board):
  222. result = 0
  223. goal = 0
  224. for i in range(m):
  225. for j in range(n):
  226. result += abs(goal - board[i][j])
  227. goal += 1
  228. return result
  229. # array = [[2,0,1],[3,4,5],[6,7,8]]
  230. # print(h(array))
  231.  
  232. def A_Star_Search(original_board):
  233. heuristic_function = h
  234.  
  235. goal_found = False
  236. visited_states = set("")
  237. working_states = set("")
  238. next_states = MyHeap()
  239.  
  240. first_board = MyBoard(original_board, heuristic_function(original_board))
  241. next_states.insert(first_board)
  242.  
  243. number_of_interations = 0;
  244. while(next_states.currentSize > 0 and goal_found == False):
  245. number_of_interations += 1
  246. current_state = next_states.delMin()
  247. current_state_string = unique_string(current_state.board)
  248.  
  249. if (verify_goal_state(current_state.board)):
  250. goal_found = True
  251. print("Goal has been found:")
  252. print(current_state.board)
  253. print("number Of interation: " + str(number_of_interations))
  254. return
  255.  
  256. if (current_state_string not in visited_states):
  257. state1 = move_up(current_state.board)
  258. state2 = move_down(current_state.board)
  259. state3 = move_left(current_state.board)
  260. state4 = move_right(current_state.board)
  261.  
  262. # print("possible next states:")
  263. # print(state1)
  264. # print(state2)
  265. # print(state3)
  266. # print(state4)
  267.  
  268. state1_string = unique_string(state1)
  269. if (state1 != None and state1_string not in working_states):
  270. next_states.insert(MyBoard(state1, heuristic_function(state1)))
  271. working_states.add(state1_string)
  272.  
  273. state2_string = unique_string(state2)
  274. if (state2 != None and state2_string not in working_states):
  275. next_states.insert(MyBoard(state2, heuristic_function(state2)))
  276. working_states.add(state2_string)
  277.  
  278. state3_string = unique_string(state3)
  279. if (state3 != None and state3_string not in working_states):
  280. next_states.insert(MyBoard(state3, heuristic_function(state3)))
  281. working_states.add(state3_string)
  282.  
  283. state4_string = unique_string(state4)
  284. if (state4 != None and state4_string not in working_states):
  285. next_states.insert(MyBoard(state4, heuristic_function(state4)))
  286. working_states.add(state4_string)
  287.  
  288. visited_states.add(current_state_string)
  289.  
  290.  
  291. board1 = [[1,2,0],[3,4,5],[6,7,8]]
  292. board2 = [[4, 2, 5], [7, 3, 1], [6, 8, 0]]
  293. board3 = [[4, 2, 5], [7, 3, 1], [6, 0, 8]]
  294.  
  295. import time
  296. current_milli_time = lambda: int(round(time.time() * 1000))
  297.  
  298. start_time = current_milli_time()
  299. A_Star_Search(board3)
  300. end_time = current_milli_time()
  301. print("A Star question 2 Run Time: " + str(end_time - start_time) + "(ms)")
  302.  
  303. start_time = current_milli_time()
  304. ids(board3)
  305. end_time = current_milli_time()
  306. print("IDS Run Time: " + str(end_time - start_time) + "(ms)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement