Advertisement
dzocesrce

[VI] Put putujem

Apr 2nd, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. #lab 2 informed search task 1
  2.  
  3. from searching_framework import Problem, astar_search
  4.  
  5. class Table(Problem):
  6.  
  7.     def __init__(self,initial,goal):
  8.         super().__init__(initial,goal)
  9.         self.grid_size = (5,9)
  10.  
  11.     def goal_test(self, state):
  12.         man_x = state[0][0]
  13.         man_y = state[0][1]
  14.  
  15.         house_x = state[2][0]
  16.         house_y = state[2][1]
  17.  
  18.         return man_x == house_x and man_y == house_y
  19.  
  20.     def successor(self, state):
  21.         successors = dict()
  22.  
  23.         man_x = state[0][0]
  24.         man_y = state[0][1]
  25.  
  26.         house_x = state[2][0]
  27.         house_y = state[2][1]
  28.         house_way = state[2][2]
  29.  
  30.         allowed = state[1]
  31.         max_x = self.grid_size[0]
  32.         max_y = self.grid_size[1]
  33.  
  34.         if house_way == 'desno':
  35.             if house_x == max_x-1:
  36.                 house_way = 'levo'
  37.                 house_x -= 1
  38.             else:
  39.                 house_x += 1
  40.         else:
  41.             if house_x == 0:
  42.                 house_way = 'desno'
  43.                 house_x += 1
  44.             else:
  45.                 house_x -= 1
  46.  
  47.         if man_y <= max_y-2 and ((man_x,man_y+1) in allowed or (man_x,man_y+1) == (house_x,house_y)):
  48.             successors["Gore 1"] = ((man_x,man_y+1),allowed,(house_x,house_y,house_way))
  49.  
  50.         if man_y <= max_y-2 and man_x > 0 and ((man_x-1,man_y+1) in allowed or (man_x-1,man_y+1) == (house_x,house_y)):
  51.             successors["Gore-levo 1"] = ((man_x-1,man_y+1),allowed,(house_x,house_y,house_way))
  52.  
  53.         if man_y <= max_y-2 and man_x <= max_x-2 and ((man_x+1,man_y+1) in allowed or (man_x+1,man_y+1) == (house_x,house_y)):
  54.             successors["Gore-desno 1"] = ((man_x+1,man_y+1),allowed,(house_x,house_y,house_way))
  55.  
  56.         if man_y <= max_y - 3 and ((man_x, man_y + 2) in allowed or (man_x, man_y + 2) == (house_x,house_y)):
  57.             successors["Gore 2"] = ((man_x, man_y + 2), allowed, (house_x, house_y,house_way))
  58.  
  59.         if man_y <= max_y - 3 and man_x > 1 and ((man_x - 2, man_y + 2) in allowed or (man_x - 2, man_y + 2) == (house_x,house_y)):
  60.             successors["Gore-levo 2"] = ((man_x - 2, man_y + 2), allowed, (house_x, house_y,house_way))
  61.  
  62.         if man_y <= max_y - 3 and man_x <= max_x-3 and ((man_x + 2, man_y + 2) in allowed or (man_x + 2, man_y + 2) == (house_x,house_y)):
  63.             successors["Gore-desno 2"] = ((man_x + 2, man_y + 2), allowed, (house_x, house_y,house_way))
  64.  
  65.         successors["Stoj"] = ((man_x, man_y), allowed, (house_x, house_y,house_way))
  66.  
  67.         return successors
  68.  
  69.     def actions(self,state):
  70.         return self.successor(state).keys()
  71.  
  72.     def h(self,node):
  73.         man_x = node.state[0][0]
  74.         man_y = node.state[0][1]
  75.         house_x = node.state[2][0]
  76.         house_y = node.state[2][1]
  77.  
  78.         return (abs(man_x - house_x)/2 + abs(man_y - house_y))/4
  79.     def result(self, state, action):
  80.         possible = self.successor(state)
  81.         return possible[action]
  82.  
  83. if __name__ == '__main__':
  84.     allowed = ((1, 0), (2, 0), (3, 0), (1, 1), (2, 1), (0, 2), (2, 2), (4, 2), (1, 3), (3, 3), (4, 3), (0, 4), (2, 4),
  85.                (2, 5), (3, 5), (0, 6), (2, 6), (1, 7), (3, 7))
  86.  
  87.     man_coordinates = input().split(',')
  88.     man_x = int(man_coordinates[0])
  89.     man_y = int(man_coordinates[1])
  90.     man = (man_x, man_y)
  91.     house_coordinates = input().split(',')
  92.     house_x = int(house_coordinates[0])
  93.     house_y = int(house_coordinates[1])
  94.     way = input()
  95.     house = (house_x, house_y, way)
  96.  
  97.     table = Table((man,allowed,house),house)
  98.  
  99.     answer = astar_search(table)
  100.     if answer is None:
  101.         print("NONE")
  102.     else:
  103.         print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement