Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.29 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.         raise NotImplementedError
  14.  
  15.     def result(self, state, action):
  16.         raise NotImplementedError
  17.  
  18.     def goal_test(self, state):
  19.         return state == self.goal
  20.  
  21.     def path_cost(self, c, state1, action, state2):
  22.         return c + 1
  23.  
  24.     def value(self):
  25.         raise NotImplementedError
  26.  
  27.  
  28. class Node:
  29.     def __init__(self, state, parent=None, action=None, path_cost=0):
  30.         self.state = state
  31.         self.parent = parent
  32.         self.action = action
  33.         self.path_cost = path_cost
  34.         self.depth = 0
  35.         if parent:
  36.             self.depth = parent.depth + 1
  37.  
  38.     def __repr__(self):
  39.         return "<Node %s>" % (self.state,)
  40.  
  41.     def __lt__(self, node):
  42.         return self.state < node.state
  43.  
  44.     def expand(self, problem):
  45.         return [self.child_node(problem, action) for action in problem.actions(self.state)]
  46.  
  47.     def child_node(self, problem, action):
  48.         next_state = problem.result(self.state, action)
  49.         return Node(next_state, self, action, problem.path_cost(self.path_cost, self.state, action, next_state))
  50.  
  51.     def solution(self):
  52.         return [node.action for node in self.path()[1:]]
  53.  
  54.     def solve(self):
  55.         return [node.state for node in self.path()[0:]]
  56.  
  57.     def path(self):
  58.         x, result = self, []
  59.         while x:
  60.             result.append(x)
  61.             x = x.parent
  62.         result.reverse()
  63.         return result
  64.  
  65.     def __eq__(self, other):
  66.         return isinstance(other, Node) and self.state == other.state
  67.  
  68.     def __hash__(self):
  69.         return hash(self.state)
  70.  
  71.  
  72. class Queue:
  73.     def __init__(self):
  74.         raise NotImplementedError
  75.  
  76.     def append(self, item):
  77.         raise NotImplementedError
  78.  
  79.     def extend(self, items):
  80.         raise NotImplementedError
  81.  
  82.     def pop(self):
  83.         raise NotImplementedError
  84.  
  85.     def __len__(self):
  86.         raise NotImplementedError
  87.  
  88.     def __contains__(self, item):
  89.         raise NotImplementedError
  90.  
  91.  
  92. class Stack(Queue):
  93.     def __init__(self):
  94.         self.data = []
  95.  
  96.     def append(self, item):
  97.         self.data.append(item)
  98.  
  99.     def extend(self, items):
  100.         self.data.extend(items)
  101.  
  102.     def pop(self):
  103.         return self.data.pop()
  104.  
  105.     def __len__(self):
  106.         return len(self.data)
  107.  
  108.     def __contains__(self, item):
  109.         return item in self.data
  110.  
  111.  
  112. class FIFOQueue(Queue):
  113.     def __init__(self):
  114.         self.data = []
  115.  
  116.     def append(self, item):
  117.         self.data.append(item)
  118.  
  119.     def extend(self, items):
  120.         self.data.extend(items)
  121.  
  122.     def pop(self):
  123.         return self.data.pop(0)  # vo zagradite treba 0-tiot clen da se vrakja koga e lista!
  124.  
  125.     def __len__(self):
  126.         return len(self.data)
  127.  
  128.     def __contains__(self, item):
  129.         return item in self.data
  130.  
  131.  
  132. class PriorityQueue(Queue):
  133.     def __init__(self, order=min, f=lambda x: x):
  134.         assert order in (min, max)
  135.         self.data = []
  136.         self.order = order
  137.         self.f = f
  138.  
  139.     def append(self, item):
  140.         bisect.insort_right(self.data, (self.f(item), item))
  141.  
  142.     def extend(self, items):
  143.         for item in items:
  144.             bisect.insort_right(self.data, (self.f(item), item))
  145.  
  146.     def pop(self):
  147.         if self.order == min:
  148.             return self.data.pop(0)[1]
  149.         return self.data.pop()[1]
  150.  
  151.     def __len__(self):
  152.         return len(self.data)
  153.  
  154.     def __contains__(self, item):
  155.         return any(item == pair[1] for pair in self.data)
  156.  
  157.     def __getitem__(self, key):
  158.         for _, item in self.data:
  159.             if item == key:
  160.                 return item
  161.  
  162.            
  163.            
  164.  
  165.  
  166. def tree_search(problem, fringe):
  167.  
  168.     fringe.append(Node(problem.initial))
  169.     while fringe:
  170.         node = fringe.pop()
  171.         print(node.state)
  172.         if problem.goal_test(node.state):
  173.             return node
  174.         fringe.extend(node.expand(problem))
  175.     return None
  176.  
  177.  
  178. def breath_first_tree_search(problem):
  179.     return tree_search(problem, FIFOQueue())
  180.  
  181.  
  182. def depth_first_search(problem):
  183.     return tree_search(problem, Stack())
  184.  
  185.  
  186. def graph_search(problem, fringe):
  187.     closed = set()
  188.     fringe.append(Node(problem.initial))
  189.     while fringe:
  190.         node = fringe.pop()
  191.         if problem.goal_test(node.state):
  192.             return node
  193.         if node.state not in closed:
  194.             closed.add(node.state)
  195.             fringe.extend(node.expand(problem))
  196.     return None
  197.  
  198.  
  199. def breadth_first_graph_search(problem):
  200.     return graph_search(problem, FIFOQueue())
  201.  
  202.  
  203. def depth_first_graph_search(problem):
  204.     return graph_search(problem, Stack())
  205.  
  206.  
  207.  
  208. # moving forwars
  209.  
  210. def move_forwards_Istok(paky_x, paky_y, obstaclesForPaky):
  211.     if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
  212.         paky_x += 1
  213.     return paky_x, paky_y
  214.  
  215.  
  216. def move_forwards_Zapad(paky_x, paky_y, obstaclesForPaky):
  217.     if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
  218.         paky_x -= 1
  219.     return paky_x, paky_y
  220.  
  221.  
  222. def move_forwards_Sever(paky_x, paky_y, obstaclesForPaky):
  223.     if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
  224.         paky_y += 1
  225.     return paky_x, paky_y
  226.  
  227.  
  228. def move_forwards_Jug(paky_x, paky_y, obstaclesForPaky):
  229.     if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
  230.         paky_y -= 1
  231.     return paky_x, paky_y
  232.  
  233.  
  234. # moving backwards
  235.  
  236.  
  237. def move_backwards_Istok(paky_x, paky_y, obstaclesForPaky):
  238.     if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
  239.         paky_x -= 1
  240.     return paky_x, paky_y
  241.  
  242.  
  243. def move_backwards_Zapad(paky_x, paky_y, obstaclesForPaky):
  244.     if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
  245.         paky_x += 1
  246.     return paky_x, paky_y
  247.  
  248.  
  249. def move_backwards_Sever(paky_x, paky_y, obstaclesForPaky):
  250.     if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
  251.         paky_y -= 1
  252.     return paky_x, paky_y
  253.  
  254.  
  255. def move_backwards_Jug(paky_x, paky_y, obstaclesForPaky):
  256.     if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
  257.         paky_y += 1
  258.     return paky_x, paky_y
  259.  
  260.  
  261. #  moving left
  262.  
  263. def move_left_Istok(paky_x, paky_y, obstaclesForPaky):
  264.     if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
  265.         paky_y += 1
  266.     return paky_x, paky_y
  267.  
  268.  
  269. def move_left_Zapad(paky_x, paky_y, obstaclesForPaky):
  270.     if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
  271.         paky_y -= 1
  272.     return paky_x, paky_y
  273.  
  274.  
  275. def move_left_Sever(paky_x, paky_y, obstaclesForPaky):
  276.     if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
  277.         paky_x -= 1
  278.     return paky_x, paky_y
  279.  
  280.  
  281. def move_left_Jug(paky_x, paky_y, obstaclesForPaky):
  282.     if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
  283.         paky_x += 1
  284.     return paky_x, paky_y
  285.  
  286.  
  287. # moving right
  288.  
  289. def move_right_Istok(paky_x, paky_y, obstaclesForPaky):
  290.     if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
  291.         paky_y -= 1
  292.     return paky_x, paky_y
  293.  
  294.  
  295. def move_right_Zapad(paky_x, paky_y, obstaclesForPaky):
  296.     if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
  297.         paky_y += 1
  298.     return paky_x, paky_y
  299.  
  300.  
  301. def move_right_Sever(paky_x, paky_y, obstaclesForPaky):
  302.     if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
  303.         paky_x += 1
  304.     return paky_x, paky_y
  305.  
  306.  
  307. def move_right_Jug(paky_x, paky_y, obstaclesForPaky):
  308.     if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
  309.         paky_x -= 1
  310.     return paky_x, paky_y
  311.  
  312.  
  313. class Pacman(Problem):
  314.     def __init__(self, ob, initial, goal=None):
  315.         super().__init__(initial, goal)
  316.         self.obstacles = ob
  317.  
  318.     def successor(self, state):
  319.         successors = dict()
  320.  
  321.         paky_x, paky_y = state[0], state[1]
  322.  
  323.         foodForPaky = state[3]
  324.  
  325.         # Zavrten na Istok
  326.         if state[2] == "istok":
  327.             new_x, new_y = move_forwards_Istok(paky_x, paky_y, self.obstacles)
  328.             if [new_x, new_y] != [paky_x, paky_y]:
  329.                 successors['ProdolzhiPravo'] = (new_x, new_y, "istok",
  330.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  331.  
  332.             new_x, new_y = move_backwards_Istok(paky_x, paky_y, self.obstacles)
  333.             if [new_x, new_y] != [paky_x, paky_y]:
  334.                 successors['ProdolzhiNazad'] = (new_x, new_y, "zapad",
  335.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  336.  
  337.             new_x, new_y = move_left_Istok(paky_x, paky_y, self.obstacles)
  338.             if [new_x, new_y] != [paky_x, paky_y]:
  339.                 successors['SvrtiLevo'] = (new_x, new_y, "sever",
  340.                                            tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  341.  
  342.             new_x, new_y = move_right_Istok(paky_x, paky_y, self.obstacles)
  343.             if [new_x, new_y] != [paky_x, paky_y]:
  344.                 successors['SvrtiDesno'] = (new_x, new_y, "jug",
  345.                                             tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  346.  
  347.         # Zavrten na Zapad
  348.  
  349.         if state[2] == "zapad":
  350.             new_x, new_y = move_forwards_Zapad(paky_x, paky_y, self.obstacles)
  351.             if [new_x, new_y] != [paky_x, paky_y]:
  352.                 successors['ProdolzhiPravo'] = (new_x, new_y, "zapad",
  353.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  354.  
  355.             new_x, new_y = move_backwards_Zapad(paky_x, paky_y, self.obstacles)
  356.             if [new_x, new_y] != [paky_x, paky_y]:
  357.                 successors['ProdolzhiNazad'] = (new_x, new_y, "istok",
  358.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  359.  
  360.             new_x, new_y = move_left_Zapad(paky_x, paky_y, self.obstacles)
  361.             if [new_x, new_y] != [paky_x, paky_y]:
  362.                 successors['SvrtiLevo'] = (new_x, new_y, "jug",
  363.                                            tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  364.  
  365.             new_x, new_y = move_right_Zapad(paky_x, paky_y, self.obstacles)
  366.             if [new_x, new_y] != [paky_x, paky_y]:
  367.                 successors['SvrtiDesno'] = (new_x, new_y, "sever",
  368.                                             tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  369.  
  370.  
  371.         # Zavrten na Sever
  372.  
  373.         if state[2] == "sever":
  374.             new_x, new_y = move_forwards_Sever(paky_x, paky_y, self.obstacles)
  375.             if [new_x, new_y] != [paky_x, paky_y]:
  376.                 successors['ProdolzhiPravo'] = (new_x, new_y, "sever",
  377.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  378.  
  379.             new_x, new_y = move_backwards_Sever(paky_x, paky_y, self.obstacles)
  380.             if [new_x, new_y] != [paky_x, paky_y]:
  381.                 successors['ProdolzhiNazad'] = (new_x, new_y, "jug",
  382.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  383.  
  384.             new_x, new_y = move_left_Sever(paky_x, paky_y, self.obstacles)
  385.             if [new_x, new_y] != [paky_x, paky_y]:
  386.                 successors['SvrtiLevo'] = (new_x, new_y, "zapad",
  387.                                            tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  388.  
  389.             new_x, new_y = move_right_Sever(paky_x, paky_y, self.obstacles)
  390.             if [new_x, new_y] != [paky_x, paky_y]:
  391.                 successors['SvrtiDesno'] = (new_x, new_y, "istok",
  392.                                             tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  393.  
  394.         # Svrten na Jug
  395.  
  396.         if state[2] == "jug":
  397.             new_x, new_y = move_forwards_Jug(paky_x, paky_y, self.obstacles)
  398.             if [new_x, new_y] != [paky_x, paky_y]:
  399.                 successors['ProdolzhiPravo'] = (new_x, new_y, "jug",
  400.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  401.  
  402.             new_x, new_y = move_backwards_Jug(paky_x, paky_y, self.obstacles)
  403.             if [new_x, new_y] != [paky_x, paky_y]:
  404.                 successors['ProdolzhiNazad'] = (new_x, new_y, "sever",
  405.                                                 tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  406.  
  407.             new_x, new_y = move_left_Jug(paky_x, paky_y, self.obstacles)
  408.             if [new_x, new_y] != [paky_x, paky_y]:
  409.                 successors['SvrtiLevo'] = (new_x, new_y, "istok",
  410.                                            tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  411.  
  412.             new_x, new_y = move_right_Jug(paky_x, paky_y, self.obstacles)
  413.             if [new_x, new_y] != [paky_x, paky_y]:
  414.                 successors['SvrtiDesno'] = (new_x, new_y, "zapad",
  415.                                             tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
  416.  
  417.         # print(successors)
  418.  
  419.         return successors
  420.  
  421.     def actions(self, state):
  422.         return self.successor(state).keys()
  423.  
  424.     def result(self, state, action):
  425.         return self.successor(state)[action]
  426.  
  427.     def goal_test(self, state):
  428.         return len(state[-1]) == 0
  429.  
  430.  
  431. if _name_ == '_main_':
  432.     obstacles = [[6, 0], [4, 1], [5, 1], [6, 1], [8, 1], [1, 2], [6, 2], [1, 3], [1, 4], [8, 4], [9, 4],
  433.                  [4, 5], [0, 6], [3, 6], [4, 6], [5, 6], [4, 7], [8, 7], [9, 7], [0, 8], [8, 8], [9, 8],
  434.                  [0, 9], [1, 9], [2, 9], [3, 9], [6, 9]]
  435.  
  436.     paky_x = int(input())
  437.     paky_y = int(input())
  438.  
  439.     paky_direction = input()
  440.  
  441.     howMuchFood = int(input())
  442.  
  443.     foodForPaky = []
  444.  
  445.     for i in range(howMuchFood):
  446.         FoodCoordinates = input()
  447.         newFood_x = int(FoodCoordinates.split(",")[0])
  448.         newFood_y = int(FoodCoordinates.split(",")[1])
  449.         foodForPaky.append((newFood_x, newFood_y))
  450.  
  451.     foodForPaky = tuple(foodForPaky)
  452.  
  453.     pacman = Pacman(obstacles, (paky_x, paky_y, paky_direction, foodForPaky))
  454.  
  455.     result = breadth_first_graph_search(pacman)
  456.  
  457.     print(result.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement