Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Oct 19 21:47:36 2018
  4.  
  5. @author: User
  6. """
  7.  
  8. import Queue as Q
  9.  
  10. import time
  11.  
  12. import resource
  13.  
  14. import sys
  15.  
  16. import math
  17.  
  18. #### SKELETON CODE ####
  19.  
  20. ## The Class that Represents the Puzzle
  21.  
  22. class PuzzleState(object):
  23.  
  24. """docstring for PuzzleState"""
  25.  
  26. def __init__(self, config, n, parent=None, action="Initial", cost=0):
  27.  
  28. if n*n != len(config) or n < 2:
  29.  
  30. raise Exception("the length of config is not correct!")
  31.  
  32. self.n = n
  33.  
  34. self.cost = cost
  35.  
  36. self.parent = parent
  37.  
  38. self.action = action
  39.  
  40. self.dimension = n
  41.  
  42. self.config = config
  43.  
  44. self.children = []
  45.  
  46. for i, item in enumerate(self.config):
  47.  
  48. if item == 0:
  49.  
  50. self.blank_row = i / self.n
  51.  
  52. self.blank_col = i % self.n
  53.  
  54. break
  55.  
  56. def display(self):
  57.  
  58. for i in range(self.n):
  59.  
  60. line = []
  61.  
  62. offset = i * self.n
  63.  
  64. for j in range(self.n):
  65.  
  66. line.append(self.config[offset + j])
  67.  
  68. print (line)
  69.  
  70. def move_left(self):
  71.  
  72. if self.blank_col == 0:
  73.  
  74. return None
  75.  
  76. else:
  77.  
  78. blank_index = self.blank_row * self.n + self.blank_col
  79.  
  80. target = blank_index - 1
  81.  
  82. new_config = list(self.config)
  83.  
  84. new_config[blank_index], new_config[target] = new_config[target], new_config[blank_index]
  85.  
  86. return PuzzleState(tuple(new_config), self.n, parent=self, action="Left", cost=self.cost + 1)
  87.  
  88. def move_right(self):
  89.  
  90. if self.blank_col == self.n - 1:
  91.  
  92. return None
  93.  
  94. else:
  95.  
  96. blank_index = self.blank_row * self.n + self.blank_col
  97.  
  98. target = blank_index + 1
  99.  
  100. new_config = list(self.config)
  101.  
  102. new_config[blank_index], new_config[target] = new_config[target], new_config[blank_index]
  103.  
  104. return PuzzleState(tuple(new_config), self.n, parent=self, action="Right", cost=self.cost + 1)
  105.  
  106. def move_up(self):
  107.  
  108. if self.blank_row == 0:
  109.  
  110. return None
  111.  
  112. else:
  113.  
  114. blank_index = self.blank_row * self.n + self.blank_col
  115.  
  116. target = blank_index - self.n
  117.  
  118. new_config = list(self.config)
  119.  
  120. new_config[blank_index], new_config[target] = new_config[target], new_config[blank_index]
  121.  
  122. return PuzzleState(tuple(new_config), self.n, parent=self, action="Up", cost=self.cost + 1)
  123.  
  124. def move_down(self):
  125.  
  126. if self.blank_row == self.n - 1:
  127.  
  128. return None
  129.  
  130. else:
  131.  
  132. blank_index = self.blank_row * self.n + self.blank_col
  133.  
  134. target = blank_index + self.n
  135.  
  136. new_config = list(self.config)
  137.  
  138. new_config[blank_index], new_config[target] = new_config[target], new_config[blank_index]
  139.  
  140. return PuzzleState(tuple(new_config), self.n, parent=self, action="Down", cost=self.cost + 1)
  141.  
  142. def expand(self):
  143.  
  144. """expand the node"""
  145.  
  146. # add child nodes in order of UDLR
  147.  
  148. if len(self.children) == 0:
  149.  
  150. up_child = self.move_up()
  151.  
  152. if up_child is not None:
  153.  
  154. self.children.append(up_child)
  155.  
  156. down_child = self.move_down()
  157.  
  158. if down_child is not None:
  159.  
  160. self.children.append(down_child)
  161.  
  162. left_child = self.move_left()
  163.  
  164. if left_child is not None:
  165.  
  166. self.children.append(left_child)
  167.  
  168. right_child = self.move_right()
  169.  
  170. if right_child is not None:
  171.  
  172. self.children.append(right_child)
  173.  
  174. return self.children
  175.  
  176. # Function that Writes to output.txt
  177.  
  178. ### Students need to change the method to have the corresponding parameters
  179.  
  180. def writeOutput():
  181.  
  182. ### Student Code Goes here
  183.  
  184. def bfs_search(initial_state):
  185.  
  186. """BFS search"""
  187.  
  188. ### STUDENT CODE GOES HERE ###
  189.  
  190. def dfs_search(initial_state):
  191.  
  192. """DFS search"""
  193.  
  194. ### STUDENT CODE GOES HERE ###
  195.  
  196. def A_star_search(initial_state):
  197.  
  198. """A * search"""
  199.  
  200. ### STUDENT CODE GOES HERE ###
  201.  
  202. def calculate_total_cost(state):
  203.  
  204. """calculate the total estimated cost of a state"""
  205.  
  206. ### STUDENT CODE GOES HERE ###
  207.  
  208. def calculate_manhattan_dist(idx, value, n):
  209.  
  210. """calculate the manhattan distance of a tile"""
  211.  
  212. ### STUDENT CODE GOES HERE ###
  213.  
  214. def test_goal(puzzle_state):
  215.  
  216. """test the state is the goal state or not"""
  217.  
  218. ### STUDENT CODE GOES HERE ###
  219.  
  220. # Main Function that reads in Input and Runs corresponding Algorithm
  221.  
  222. def main():
  223.  
  224. sm = sys.argv[1].lower()
  225.  
  226. begin_state = sys.argv[2].split(",")
  227.  
  228. begin_state = tuple(map(int, begin_state))
  229.  
  230. size = int(math.sqrt(len(begin_state)))
  231.  
  232. hard_state = PuzzleState(begin_state, size)
  233.  
  234. if sm == "bfs":
  235.  
  236. bfs_search(hard_state)
  237.  
  238. elif sm == "dfs":
  239.  
  240. dfs_search(hard_state)
  241.  
  242. elif sm == "ast":
  243.  
  244. A_star_search(hard_state)
  245.  
  246. else:
  247.  
  248. print("Enter valid command arguments !")
  249.  
  250. if __name__ == '__main__':
  251.  
  252. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement