Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.47 KB | None | 0 0
  1. import bisect
  2.  
  3.  
  4. def tree_search(problem, fringe):
  5. fringe.append(Node(problem.initial))
  6. while fringe:
  7. node = fringe.pop()
  8. print(node.state)
  9. if problem.goal_test(node.state):
  10. return node
  11. fringe.extend(node.expand(problem))
  12. return None
  13.  
  14.  
  15. def breadth_first_tree_search(problem):
  16. return tree_search(problem, FIFOQueue())
  17.  
  18.  
  19. def depth_first_tree_search(problem):
  20.  
  21. return tree_search(problem, Stack())
  22.  
  23.  
  24.  
  25. def graph_search(problem, fringe):
  26. closed = set()
  27. fringe.append(Node(problem.initial))
  28. while fringe:
  29. node = fringe.pop()
  30. if problem.goal_test(node.state):
  31. return node
  32. if node.state not in closed:
  33. closed.add(node.state)
  34. fringe.extend(node.expand(problem))
  35. return None
  36.  
  37.  
  38. def breadth_first_graph_search(problem):
  39. return graph_search(problem, FIFOQueue())
  40.  
  41.  
  42. def depth_first_graph_search(problem):
  43. return graph_search(problem, Stack())
  44.  
  45.  
  46. def depth_limited_search(problem, limit=50):
  47. def recursive_dls(node, problem, limit):
  48. cutoff_occurred = False
  49. if problem.goal_test(node.state):
  50. return node
  51. elif node.depth == limit:
  52. return 'cutoff'
  53. else:
  54. for successor in node.expand(problem):
  55. result = recursive_dls(successor, problem, limit)
  56. if result == 'cutoff':
  57. cutoff_occurred = True
  58. elif result is not None:
  59. return result
  60. if cutoff_occurred:
  61. return 'cutoff'
  62. return None
  63.  
  64. return recursive_dls(Node(problem.initial), problem, limit)
  65.  
  66.  
  67. def iterative_deepening_search(problem):
  68. for depth in range(sys.maxsize):
  69. result = depth_limited_search(problem, depth)
  70. if result is not 'cutoff':
  71. return result
  72.  
  73.  
  74. def uniform_cost_search(problem):
  75. return graph_search(problem, PriorityQueue(min, lambda a: a.path_cost))
  76.  
  77.  
  78. class Problem:
  79. def __init__(self, initial, goal=None):
  80. self.initial = initial
  81. self.goal = goal
  82.  
  83. def successor(self, state):
  84.  
  85. raise NotImplementedError
  86.  
  87. def actions(self, state):
  88. return self.successor(state).keys()
  89.  
  90. def result(self, state, action):
  91. possible = self.successor(state)
  92. return possible[action]
  93.  
  94.  
  95. def goal_test(self, state):
  96. return state == self.goal
  97.  
  98. def path_cost(self, c, state1, action, state2):
  99.  
  100. return c + 1
  101.  
  102. def value(self):
  103.  
  104. raise NotImplementedError
  105.  
  106.  
  107.  
  108. class Node:
  109. def __init__(self, state, parent=None, action=None, path_cost=0):
  110.  
  111. self.state = state
  112. self.parent = parent
  113. self.action = action
  114. self.path_cost = path_cost
  115. self.depth = 0 # search depth
  116. if parent:
  117. self.depth = parent.depth + 1
  118.  
  119. def __repr__(self):
  120. return "<Node %s>" % (self.state,)
  121.  
  122. def __lt__(self, node):
  123. return self.state < node.state
  124.  
  125. def expand(self, problem):
  126.  
  127. return [self.child_node(problem, action)
  128. for action in problem.actions(self.state)]
  129.  
  130. def child_node(self, problem, action):
  131.  
  132. next_state = problem.result(self.state, action)
  133. return Node(next_state, self, action,
  134. problem.path_cost(self.path_cost, self.state,
  135. action, next_state))
  136.  
  137. def solution(self):
  138. return [node.action for node in self.path()[1:]]
  139.  
  140. def solve(self):
  141.  
  142. return [node.state for node in self.path()[0:]]
  143.  
  144. def path(self):
  145.  
  146. x, result = self, []
  147. while x:
  148. result.append(x)
  149. x = x.parent
  150. result.reverse()
  151. return result
  152.  
  153.  
  154. def __eq__(self, other):
  155. return isinstance(other, Node) and self.state == other.state
  156.  
  157. def __hash__(self):
  158. return hash(self.state)
  159.  
  160.  
  161.  
  162. class Queue:
  163.  
  164.  
  165. def __init__(self):
  166. raise NotImplementedError
  167.  
  168. def append(self, item):
  169.  
  170. raise NotImplementedError
  171.  
  172. def extend(self, items):
  173. raise NotImplementedError
  174.  
  175. def pop(self):
  176.  
  177. raise NotImplementedError
  178.  
  179. def __len__(self):
  180. raise NotImplementedError
  181.  
  182. def __contains__(self, item):
  183. raise NotImplementedError
  184.  
  185.  
  186. class Stack(Queue):
  187. """Last-In-First-Out Queue."""
  188.  
  189. def __init__(self):
  190. self.data = []
  191.  
  192. def append(self, item):
  193. self.data.append(item)
  194.  
  195. def extend(self, items):
  196. self.data.extend(items)
  197.  
  198. def pop(self):
  199. return self.data.pop()
  200.  
  201. def __len__(self):
  202. return len(self.data)
  203.  
  204. def __contains__(self, item):
  205. return item in self.data
  206.  
  207.  
  208. class FIFOQueue(Queue):
  209. """First-In-First-Out Queue."""
  210.  
  211. def __init__(self):
  212. self.data = []
  213.  
  214. def append(self, item):
  215. self.data.append(item)
  216.  
  217. def extend(self, items):
  218. self.data.extend(items)
  219.  
  220. def pop(self):
  221. return self.data.pop(0)
  222.  
  223. def __len__(self):
  224. return len(self.data)
  225.  
  226. def __contains__(self, item):
  227. return item in self.data
  228.  
  229.  
  230. class PriorityQueue(Queue):
  231.  
  232. def __init__(self, order=min, f=lambda x: x):
  233.  
  234. assert order in [min, max]
  235. self.data = []
  236. self.order = order
  237. self.f = f
  238.  
  239. def append(self, item):
  240. bisect.insort_right(self.data, (self.f(item), item))
  241.  
  242. def extend(self, items):
  243. for item in items:
  244. bisect.insort_right(self.data, (self.f(item), item))
  245.  
  246. def pop(self):
  247. if self.order == min:
  248. return self.data.pop(0)[1]
  249. return self.data.pop()[1]
  250.  
  251. def __len__(self):
  252. return len(self.data)
  253.  
  254. def __contains__(self, item):
  255. return any(item == pair[1] for pair in self.data)
  256.  
  257. def __getitem__(self, key):
  258. for _, item in self.data:
  259. if item == key:
  260. return item
  261.  
  262. def __delitem__(self, key):
  263. for i, (value, item) in enumerate(self.data):
  264. if item == key:
  265. self.data.pop(i)
  266.  
  267. def move_up(state):
  268. man_row = state[0]
  269. man_column = state[1]
  270. b1_row = state[2]
  271. b1_column = state[3]
  272. b2_row = state[4]
  273. b2_column = state[5]
  274. b3_row = state[6]
  275. b3_column = state[7]
  276.  
  277. man=(man_row-1,man_column)
  278. b1=(b1_row,b1_column)
  279. b2=(b2_row,b2_column)
  280. b3=(b3_row,b3_column)
  281. state_new=None
  282. x=man_row-1
  283. y=man_column
  284. if x>=0 and (man not in [b1,b2,b3]):
  285. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  286.  
  287. return state_new
  288.  
  289. def move_down(state):
  290. man_row = state[0]
  291. man_column = state[1]
  292. b1_row = state[2]
  293. b1_column = state[3]
  294. b2_row = state[4]
  295. b2_column = state[5]
  296. b3_row = state[6]
  297. b3_column = state[7]
  298.  
  299. man=(man_row+1,man_column)
  300. b1=(b1_row,b1_column)
  301. b2=(b2_row,b2_column)
  302. b3=(b3_row,b3_column)
  303. state_new=None
  304. x=man_row+1
  305. y=man_column
  306. if x<8 and (man not in [b1,b2,b3]):
  307. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  308.  
  309. return state_new
  310.  
  311. def move_left(state):
  312. man_row = state[0]
  313. man_column = state[1]
  314. b1_row = state[2]
  315. b1_column = state[3]
  316. b2_row = state[4]
  317. b2_column = state[5]
  318. b3_row = state[6]
  319. b3_column = state[7]
  320.  
  321. man=(man_row,man_column-1)
  322. b1=(b1_row,b1_column)
  323. b2=(b2_row,b2_column)
  324. b3=(b3_row,b3_column)
  325. state_new=None
  326. x=man_row
  327. y=man_column-1
  328. if y>=0 and (man not in [b1,b2,b3]):
  329. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  330.  
  331. return state_new
  332.  
  333. def move_right(state):
  334. man_row = state[0]
  335. man_column = state[1]
  336. b1_row = state[2]
  337. b1_column = state[3]
  338. b2_row = state[4]
  339. b2_column = state[5]
  340. b3_row = state[6]
  341. b3_column = state[7]
  342.  
  343. man=(man_row,man_column+1)
  344. b1=(b1_row,b1_column)
  345. b2=(b2_row,b2_column)
  346. b3=(b3_row,b3_column)
  347. state_new=None
  348. x=man_row
  349. y=man_column+1
  350. if y<10 and (man not in [b1,b2,b3]):
  351. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  352.  
  353. return state_new
  354.  
  355. def moveK_up(state):
  356. man_row = state[0]
  357. man_column = state[1]
  358. b1_row = state[2]
  359. b1_column = state[3]
  360. b2_row = state[4]
  361. b2_column = state[5]
  362. b3_row = state[6]
  363. b3_column = state[7]
  364.  
  365. man=(man_row-1,man_column)
  366. b1=(b1_row,b1_column)
  367. b2=(b2_row,b2_column)
  368. b3=(b3_row,b3_column)
  369. state_new=None
  370. x=man_row-1
  371. y=man_column
  372.  
  373. if man==b1 and ((b1_row-1,b1_column) not in[b2,b3]) and x>=0:
  374. b1_row=b1_row-1
  375. elif man==b2 and ((b2_row-1,b2_column) not in[b1,b3]) and x>=0:
  376. b2_row=b2_row-1
  377. elif man==b3 and ((b3_row-1,b3_column) not in[b2,b1]) and x>=0:
  378. b3_row=b3_row-1
  379. else:
  380. return state_new
  381.  
  382. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  383.  
  384. return state_new
  385.  
  386. def moveK_down(state):
  387. man_row = state[0]
  388. man_column = state[1]
  389. b1_row = state[2]
  390. b1_column = state[3]
  391. b2_row = state[4]
  392. b2_column = state[5]
  393. b3_row = state[6]
  394. b3_column = state[7]
  395.  
  396. man=(man_row+1,man_column)
  397. b1=(b1_row,b1_column)
  398. b2=(b2_row,b2_column)
  399. b3=(b3_row,b3_column)
  400. state_new=None
  401. x=man_row+1
  402. y=man_column
  403.  
  404. if man==b1 and ((b1_row+1,b1_column) not in[b2,b3]) and x<8:
  405. b1_row=b1_row+1
  406. elif man==b2 and ((b2_row+1,b2_column) not in[b1,b3]) and x<8:
  407. b2_row=b2_row+1
  408. elif man==b3 and ((b3_row+1,b3_column) not in[b2,b1]) and x<8:
  409. b3_row=b3_row+1
  410. else:
  411. return state_new
  412.  
  413. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  414.  
  415. return state_new
  416.  
  417. def moveK_up(state):
  418. man_row = state[0]
  419. man_column = state[1]
  420. b1_row = state[2]
  421. b1_column = state[3]
  422. b2_row = state[4]
  423. b2_column = state[5]
  424. b3_row = state[6]
  425. b3_column = state[7]
  426.  
  427. man=(man_row-1,man_column)
  428. b1=(b1_row,b1_column)
  429. b2=(b2_row,b2_column)
  430. b3=(b3_row,b3_column)
  431. state_new=None
  432. x=man_row-1
  433. y=man_column
  434.  
  435. if man==b1 and ((b1_row-1,b1_column) not in[b2,b3]) and x>=0:
  436. b1_row=b1_row-1
  437. elif man==b2 and ((b2_row-1,b2_column) not in[b1,b3]) and x>=0:
  438. b2_row=b2_row-1
  439. elif man==b3 and ((b3_row-1,b3_column) not in[b2,b1]) and x>=0:
  440. b3_row=b3_row-1
  441. else:
  442. return state_new
  443.  
  444. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  445.  
  446. return state_new
  447.  
  448. def moveK_left(state):
  449. man_row = state[0]
  450. man_column = state[1]
  451. b1_row = state[2]
  452. b1_column = state[3]
  453. b2_row = state[4]
  454. b2_column = state[5]
  455. b3_row = state[6]
  456. b3_column = state[7]
  457.  
  458. man=(man_row,man_column-1)
  459. b1=(b1_row,b1_column)
  460. b2=(b2_row,b2_column)
  461. b3=(b3_row,b3_column)
  462. state_new=None
  463. x=man_row
  464. y=man_column-1
  465.  
  466. if man==b1 and ((b1_row,b1_column-1) not in[b2,b3]) and y>=0:
  467. b1_column=b1_column-1
  468. elif man==b2 and ((b2_row,b2_column-1) not in[b1,b3]) and y>=0:
  469. b2_column=b2_column-1
  470. elif man==b3 and ((b3_row,b3_column-1) not in[b2,b1]) and y>=0:
  471. b3_column=b3_column-1
  472. else:
  473. return state_new
  474.  
  475.  
  476. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  477.  
  478. return state_new
  479.  
  480. def moveK_right(state):
  481. man_row = state[0]
  482. man_column = state[1]
  483. b1_row = state[2]
  484. b1_column = state[3]
  485. b2_row = state[4]
  486. b2_column = state[5]
  487. b3_row = state[6]
  488. b3_column = state[7]
  489.  
  490. man=(man_row,man_column+1)
  491. b1=(b1_row,b1_column)
  492. b2=(b2_row,b2_column)
  493. b3=(b3_row,b3_column)
  494. state_new=None
  495. x=man_row
  496. y=man_column+1
  497.  
  498. if man==b1 and ((b1_row,b1_column+1) not in[b2,b3]) and y<10:
  499. b1_column=b1_column+1
  500. elif man==b2 and ((b2_row,b2_column+1) not in[b1,b3]) and y<10:
  501. b2_column=b2_column+1
  502. elif man==b3 and ((b3_row,b3_column+1) not in[b2,b1]) and y<10:
  503. b3_column=b3_column+1
  504. else:
  505. return state_new
  506.  
  507. state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
  508.  
  509. return state_new
  510.  
  511.  
  512.  
  513. class Kutija(Problem):
  514. def __init__(self,initial,goal=None):
  515. self.initial=initial
  516. self.goal=goal
  517. def goal_test(self, state):
  518. b1=(state[2],state[3])
  519. b2=(state[4],state[5])
  520. b3=(state[6],state[7])
  521.  
  522. return (b1==(2,3) and b2==(2,5) and b3==(2,7)) or\
  523. (b1==(2,3) and b3==(2,5) and b2==(2,7)) or\
  524. (b2==(2,3) and b1==(2,5) and b3==(2,7)) or\
  525. (b2==(2,3) and b3==(2,5) and b1==(2,7)) or\
  526. (b3==(2,3) and b1==(2,5) and b2==(2,7)) or\
  527. (b3==(2,3) and b2==(2,5) and b1==(2,7))
  528.  
  529. def successor(self, state):
  530. succs={}
  531.  
  532. #GoreCovece
  533. state_new=move_up(state)
  534. if state_new!=None: succs['GoreC']=state_new
  535.  
  536. #GoreCoveceKutija
  537. state_new=moveK_up(state)
  538. if state_new!=None: succs['GoreCK']=state_new
  539.  
  540. #DoluCovece
  541. state_new=move_down(state)
  542. if state_new!=None: succs['DoluC']=state_new
  543.  
  544. #DoluCoveceKutija
  545. state_new=moveK_down(state)
  546. if state_new!=None: succs['DoluCK']=state_new
  547.  
  548. #LevoCovece
  549. state_new=move_left(state)
  550. if state_new!=None: succs['LevoC']=state_new
  551.  
  552. #LevoCoveceKutija
  553. state_new=moveK_left(state)
  554. if state_new!=None: succs['LevoCK']=state_new
  555.  
  556. #DesnoCovece
  557. state_new=move_right(state)
  558. if state_new!=None: succs['DesnoC']=state_new
  559.  
  560. #DesnoCoveceKutija
  561. state_new=moveK_right(state)
  562. if state_new!=None: succs['DesnoCK']=state_new
  563.  
  564. return succs
  565.  
  566.  
  567. def actions(self, state):
  568. return self.successor(state).keys()
  569. def result(self, state, action):
  570. return self.successor(state)[action]
  571.  
  572. if __name__ == '__main__':
  573. man_row = int(input())
  574. man_column = int(input())
  575. b1_row = int(input())
  576. b1_column = int(input())
  577. b2_row = int(input())
  578. b2_column = int(input())
  579. b3_row = int(input())
  580. b3_column = int(input())
  581.  
  582. kutii=Kutija((man_row,man_column,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column))
  583.  
  584. result=breadth_first_graph_search(kutii)
  585. print(result.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement