Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. import bisect
  2.  
  3.  
  4. class Problem:
  5. def __init__(self, initial, goal=None):
  6. self.initial = initial
  7. self.goal = goal
  8.  
  9. def successor(self, state):
  10. raise NotImplementedError
  11.  
  12. def actions(self, state):
  13. return self.successor(state).keys()
  14.  
  15. def result(self, state, action):
  16. possible = self.successor(state)
  17. return possible[action]
  18.  
  19. def goal_test(self, state):
  20. return state == self.goal
  21.  
  22. def path_cost(self, c, state1, action, state2):
  23. return c + 1
  24.  
  25. def value(self):
  26. raise NotImplementedError
  27.  
  28.  
  29. class Node:
  30. def __init__(self, state, parent=None, action=None, path_cost=0):
  31.  
  32. self.state = state
  33. self.parent = parent
  34. self.action = action
  35. self.path_cost = path_cost
  36. self.depth = 0 # search depth
  37. if parent:
  38. self.depth = parent.depth + 1
  39.  
  40. def __repr__(self):
  41. return "<Node %s>" % (self.state,)
  42.  
  43. def __lt__(self, node):
  44. return self.state < node.state
  45.  
  46. def expand(self, problem):
  47.  
  48. return [self.child_node(problem, action)
  49. for action in problem.actions(self.state)]
  50.  
  51. def child_node(self, problem, action):
  52.  
  53. next_state = problem.result(self.state, action)
  54. return Node(next_state, self, action,
  55. problem.path_cost(self.path_cost, self.state,
  56. action, next_state))
  57.  
  58. def path_cost(self, c, state1, action, state2):
  59. return c + 1
  60.  
  61. def solution(self):
  62.  
  63. return [node.action for node in self.path()[1:]]
  64.  
  65. def solve(self):
  66.  
  67. return [node.state for node in self.path()[0:]]
  68.  
  69. def path(self):
  70.  
  71. x, result = self, []
  72. while x:
  73. result.append(x)
  74. x = x.parent
  75. result.reverse()
  76. return result
  77.  
  78. def __eq__(self, other):
  79. return isinstance(other, Node) and self.state == other.state
  80.  
  81. def __hash__(self):
  82. return hash(self.state)
  83.  
  84.  
  85. class Queue:
  86.  
  87. def __init__(self):
  88. raise NotImplementedError
  89.  
  90. def append(self, item):
  91. raise NotImplementedError
  92.  
  93. def extend(self, items):
  94. raise NotImplementedError
  95.  
  96. def pop(self):
  97. raise NotImplementedError
  98.  
  99. def __len__(self):
  100. raise NotImplementedError
  101.  
  102. def __contains__(self, item):
  103. raise NotImplementedError
  104.  
  105.  
  106. class Stack(Queue):
  107.  
  108. def __init__(self):
  109. self.data = []
  110.  
  111. def append(self, item):
  112. self.data.append(item)
  113.  
  114. def extend(self, items):
  115. self.data.extend(items)
  116.  
  117. def pop(self):
  118. return self.data.pop()
  119.  
  120. def __len__(self):
  121. return len(self.data)
  122.  
  123. def __contains__(self, item):
  124. return item in self.data
  125.  
  126.  
  127. class FIFOQueue(Queue):
  128.  
  129. def __init__(self):
  130. self.data = []
  131.  
  132. def append(self, item):
  133. self.data.append(item)
  134.  
  135. def extend(self, items):
  136. self.data.extend(items)
  137.  
  138. def pop(self):
  139. return self.data.pop(0)
  140.  
  141. def __len__(self):
  142. return len(self.data)
  143.  
  144. def __contains__(self, item):
  145. return item in self.data
  146.  
  147.  
  148. class PriorityQueue(Queue):
  149.  
  150. def __init__(self, order=min, f=lambda x: x):
  151.  
  152. assert order in [min, max]
  153. self.data = []
  154. self.order = order
  155. self.f = f
  156.  
  157. def append(self, item):
  158. bisect.insort_right(self.data, (self.f(item), item))
  159.  
  160. def extend(self, items):
  161. for item in items:
  162. bisect.insort_right(self.data, (self.f(item), item))
  163.  
  164. def pop(self):
  165. if self.order == min:
  166. return self.data.pop(0)[1]
  167. return self.data.pop()[1]
  168.  
  169. def __len__(self):
  170. return len(self.data)
  171.  
  172. def __contains__(self, item):
  173. return any(item == pair[1] for pair in self.data)
  174.  
  175. def __getitem__(self, key):
  176. for _, item in self.data:
  177. if item == key:
  178. return item
  179.  
  180. def __delitem__(self, key):
  181. for i, (value, item) in enumerate(self.data):
  182. if item == key:
  183. self.data.pop(i)
  184.  
  185.  
  186. def tree_search(problem, fringe):
  187. fringe.append(Node(problem.initial))
  188. while fringe:
  189. node = fringe.pop()
  190. if problem.goal_test(node.state):
  191. return node
  192. fringe.extend(node.expand(problem))
  193. return None
  194.  
  195.  
  196. def breadth_first_tree_search(problem):
  197. return tree_search(problem, FIFOQueue())
  198.  
  199.  
  200. class Kocki(Problem):
  201. def __init__(self, n, initial):
  202. self.n = n
  203. self.initial = dict()
  204. x = 0
  205. for i in range(0, self.n):
  206. for j in range(0, self.n):
  207. self.initial[(i, j)] = initial[x]
  208. x += 1
  209.  
  210. self.goal = dict()
  211. for i in range(0, self.n):
  212. for j in range(0, self.n):
  213. self.goal[(i, j)] = 1
  214.  
  215. def successor(self, state):
  216. succ = dict()
  217. for action in actions(state):
  218. succ[action] = result(state, action)
  219. return succ
  220.  
  221. def result(self, state, action):
  222. i, j = action
  223. stateCopy = dict(state)
  224. stateCopy[(i, j)] = abs(stateCopy[(i, j)] - 1)
  225. if (i - 1 >= 0):
  226. stateCopy[(i - 1, j)] = abs(stateCopy[(i - 1, j)] - 1)
  227. if (i + 1 < self.n):
  228. stateCopy[(i + 1, j)] = abs(stateCopy[(i + 1, j)] - 1)
  229. if (j - 1 >= 0):
  230. stateCopy[(i, j - 1)] = abs(stateCopy[(i, j - 1)] - 1)
  231. if (j + 1 < self.n):
  232. stateCopy[(i, j + 1)] = abs(stateCopy[(i, j + 1)] - 1)
  233.  
  234. return stateCopy
  235.  
  236. def actions(self, state):
  237. actions = list()
  238. for i in range(0, self.n):
  239. for j in range(0, self.n):
  240. actions.append((i, j))
  241.  
  242. return actions
  243.  
  244. def goal_test(self, state):
  245. for i in range(0, self.n):
  246. for j in range(0, self.n):
  247. if (state[(i, j)] != self.goal[(i, j)]):
  248. return False
  249. return True
  250.  
  251.  
  252. if __name__ == "__main__":
  253. n = int(input())
  254. polinja = list(map(int, input().split(',')))
  255. k = Kocki(n, polinja)
  256. x = breadth_first_tree_search(k)
  257. lists=x.solution()
  258. empty=list()
  259. for (x,y) in lists:
  260. empty.append('x: '+str(x)+', y: '+str(y))
  261. print(empty)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement