Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bisect
- class Problem:
- def __init__(self, initial, goal=None):
- self.initial = initial
- self.goal = goal
- def successor(self, state):
- raise NotImplementedError
- def actions(self, state):
- raise NotImplementedError
- def result(self, state, action):
- raise NotImplementedError
- def goal_test(self, state):
- return state == self.goal
- def path_cost(self, c, state1, action, state2):
- return c + 1
- def value(self):
- raise NotImplementedError
- class Node:
- def __init__(self, state, parent=None, action=None, path_cost=0):
- self.state = state
- self.parent = parent
- self.action = action
- self.path_cost = path_cost
- self.depth = 0
- if parent:
- self.depth = parent.depth + 1
- def __repr__(self):
- return "<Node %s>" % (self.state,)
- def __lt__(self, node):
- return self.state < node.state
- def expand(self, problem):
- return [self.child_node(problem, action) for action in problem.actions(self.state)]
- def child_node(self, problem, action):
- next_state = problem.result(self.state, action)
- return Node(next_state, self, action, problem.path_cost(self.path_cost, self.state, action, next_state))
- def solution(self):
- return [node.action for node in self.path()[1:]]
- def solve(self):
- return [node.state for node in self.path()[0:]]
- def path(self):
- x, result = self, []
- while x:
- result.append(x)
- x = x.parent
- result.reverse()
- return result
- def __eq__(self, other):
- return isinstance(other, Node) and self.state == other.state
- def __hash__(self):
- return hash(self.state)
- class Queue:
- def __init__(self):
- raise NotImplementedError
- def append(self, item):
- raise NotImplementedError
- def extend(self, items):
- raise NotImplementedError
- def pop(self):
- raise NotImplementedError
- def __len__(self):
- raise NotImplementedError
- def __contains__(self, item):
- raise NotImplementedError
- class Stack(Queue):
- def __init__(self):
- self.data = []
- def append(self, item):
- self.data.append(item)
- def extend(self, items):
- self.data.extend(items)
- def pop(self):
- return self.data.pop()
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return item in self.data
- class FIFOQueue(Queue):
- def __init__(self):
- self.data = []
- def append(self, item):
- self.data.append(item)
- def extend(self, items):
- self.data.extend(items)
- def pop(self):
- return self.data.pop(0) # vo zagradite treba 0-tiot clen da se vrakja koga e lista!
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return item in self.data
- class PriorityQueue(Queue):
- def __init__(self, order=min, f=lambda x: x):
- assert order in (min, max)
- self.data = []
- self.order = order
- self.f = f
- def append(self, item):
- bisect.insort_right(self.data, (self.f(item), item))
- def extend(self, items):
- for item in items:
- bisect.insort_right(self.data, (self.f(item), item))
- def pop(self):
- if self.order == min:
- return self.data.pop(0)[1]
- return self.data.pop()[1]
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return any(item == pair[1] for pair in self.data)
- def __getitem__(self, key):
- for _, item in self.data:
- if item == key:
- return item
- def tree_search(problem, fringe):
- fringe.append(Node(problem.initial))
- while fringe:
- node = fringe.pop()
- print(node.state)
- if problem.goal_test(node.state):
- return node
- fringe.extend(node.expand(problem))
- return None
- def breath_first_tree_search(problem):
- return tree_search(problem, FIFOQueue())
- def depth_first_search(problem):
- return tree_search(problem, Stack())
- def graph_search(problem, fringe):
- closed = set()
- fringe.append(Node(problem.initial))
- while fringe:
- node = fringe.pop()
- if problem.goal_test(node.state):
- return node
- if node.state not in closed:
- closed.add(node.state)
- fringe.extend(node.expand(problem))
- return None
- def breadth_first_graph_search(problem):
- return graph_search(problem, FIFOQueue())
- def depth_first_graph_search(problem):
- return graph_search(problem, Stack())
- # moving forwars
- def move_forwards_Istok(paky_x, paky_y, obstaclesForPaky):
- if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
- paky_x += 1
- return paky_x, paky_y
- def move_forwards_Zapad(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
- paky_x -= 1
- return paky_x, paky_y
- def move_forwards_Sever(paky_x, paky_y, obstaclesForPaky):
- if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
- paky_y += 1
- return paky_x, paky_y
- def move_forwards_Jug(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
- paky_y -= 1
- return paky_x, paky_y
- # moving backwards
- def move_backwards_Istok(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
- paky_x -= 1
- return paky_x, paky_y
- def move_backwards_Zapad(paky_x, paky_y, obstaclesForPaky):
- if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
- paky_x += 1
- return paky_x, paky_y
- def move_backwards_Sever(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
- paky_y -= 1
- return paky_x, paky_y
- def move_backwards_Jug(paky_x, paky_y, obstaclesForPaky):
- if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
- paky_y += 1
- return paky_x, paky_y
- # moving left
- def move_left_Istok(paky_x, paky_y, obstaclesForPaky):
- if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
- paky_y += 1
- return paky_x, paky_y
- def move_left_Zapad(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
- paky_y -= 1
- return paky_x, paky_y
- def move_left_Sever(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
- paky_x -= 1
- return paky_x, paky_y
- def move_left_Jug(paky_x, paky_y, obstaclesForPaky):
- if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
- paky_x += 1
- return paky_x, paky_y
- # moving right
- def move_right_Istok(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_y and [paky_x, paky_y - 1] not in obstaclesForPaky:
- paky_y -= 1
- return paky_x, paky_y
- def move_right_Zapad(paky_x, paky_y, obstaclesForPaky):
- if paky_y < 9 and [paky_x, paky_y + 1] not in obstaclesForPaky:
- paky_y += 1
- return paky_x, paky_y
- def move_right_Sever(paky_x, paky_y, obstaclesForPaky):
- if paky_x < 9 and [paky_x + 1, paky_y] not in obstaclesForPaky:
- paky_x += 1
- return paky_x, paky_y
- def move_right_Jug(paky_x, paky_y, obstaclesForPaky):
- if 0 < paky_x and [paky_x - 1, paky_y] not in obstaclesForPaky:
- paky_x -= 1
- return paky_x, paky_y
- class Pacman(Problem):
- def __init__(self, ob, initial, goal=None):
- super().__init__(initial, goal)
- self.obstacles = ob
- def successor(self, state):
- successors = dict()
- paky_x, paky_y = state[0], state[1]
- foodForPaky = state[3]
- # Zavrten na Istok
- if state[2] == "istok":
- new_x, new_y = move_forwards_Istok(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiPravo'] = (new_x, new_y, "istok",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_backwards_Istok(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiNazad'] = (new_x, new_y, "zapad",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_left_Istok(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiLevo'] = (new_x, new_y, "sever",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_right_Istok(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiDesno'] = (new_x, new_y, "jug",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- # Zavrten na Zapad
- if state[2] == "zapad":
- new_x, new_y = move_forwards_Zapad(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiPravo'] = (new_x, new_y, "zapad",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_backwards_Zapad(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiNazad'] = (new_x, new_y, "istok",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_left_Zapad(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiLevo'] = (new_x, new_y, "jug",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_right_Zapad(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiDesno'] = (new_x, new_y, "sever",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- # Zavrten na Sever
- if state[2] == "sever":
- new_x, new_y = move_forwards_Sever(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiPravo'] = (new_x, new_y, "sever",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_backwards_Sever(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiNazad'] = (new_x, new_y, "jug",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_left_Sever(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiLevo'] = (new_x, new_y, "zapad",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_right_Sever(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiDesno'] = (new_x, new_y, "istok",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- # Svrten na Jug
- if state[2] == "jug":
- new_x, new_y = move_forwards_Jug(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiPravo'] = (new_x, new_y, "jug",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_backwards_Jug(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['ProdolzhiNazad'] = (new_x, new_y, "sever",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_left_Jug(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiLevo'] = (new_x, new_y, "istok",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- new_x, new_y = move_right_Jug(paky_x, paky_y, self.obstacles)
- if [new_x, new_y] != [paky_x, paky_y]:
- successors['SvrtiDesno'] = (new_x, new_y, "zapad",
- tuple([s for s in foodForPaky if new_x != s[0] or new_y != s[1]]))
- # print(successors)
- return successors
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- return self.successor(state)[action]
- def goal_test(self, state):
- return len(state[-1]) == 0
- if _name_ == '_main_':
- obstacles = [[6, 0], [4, 1], [5, 1], [6, 1], [8, 1], [1, 2], [6, 2], [1, 3], [1, 4], [8, 4], [9, 4],
- [4, 5], [0, 6], [3, 6], [4, 6], [5, 6], [4, 7], [8, 7], [9, 7], [0, 8], [8, 8], [9, 8],
- [0, 9], [1, 9], [2, 9], [3, 9], [6, 9]]
- paky_x = int(input())
- paky_y = int(input())
- paky_direction = input()
- howMuchFood = int(input())
- foodForPaky = []
- for i in range(howMuchFood):
- FoodCoordinates = input()
- newFood_x = int(FoodCoordinates.split(",")[0])
- newFood_y = int(FoodCoordinates.split(",")[1])
- foodForPaky.append((newFood_x, newFood_y))
- foodForPaky = tuple(foodForPaky)
- pacman = Pacman(obstacles, (paky_x, paky_y, paky_direction, foodForPaky))
- result = breadth_first_graph_search(pacman)
- print(result.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement