Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lab 2 informed search task 1
- from searching_framework import Problem, astar_search
- class Table(Problem):
- def __init__(self,initial,goal):
- super().__init__(initial,goal)
- self.grid_size = (5,9)
- def goal_test(self, state):
- man_x = state[0][0]
- man_y = state[0][1]
- house_x = state[2][0]
- house_y = state[2][1]
- return man_x == house_x and man_y == house_y
- def successor(self, state):
- successors = dict()
- man_x = state[0][0]
- man_y = state[0][1]
- house_x = state[2][0]
- house_y = state[2][1]
- house_way = state[2][2]
- allowed = state[1]
- max_x = self.grid_size[0]
- max_y = self.grid_size[1]
- if house_way == 'desno':
- if house_x == max_x-1:
- house_way = 'levo'
- house_x -= 1
- else:
- house_x += 1
- else:
- if house_x == 0:
- house_way = 'desno'
- house_x += 1
- else:
- house_x -= 1
- if man_y <= max_y-2 and ((man_x,man_y+1) in allowed or (man_x,man_y+1) == (house_x,house_y)):
- successors["Gore 1"] = ((man_x,man_y+1),allowed,(house_x,house_y,house_way))
- 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)):
- successors["Gore-levo 1"] = ((man_x-1,man_y+1),allowed,(house_x,house_y,house_way))
- 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)):
- successors["Gore-desno 1"] = ((man_x+1,man_y+1),allowed,(house_x,house_y,house_way))
- if man_y <= max_y - 3 and ((man_x, man_y + 2) in allowed or (man_x, man_y + 2) == (house_x,house_y)):
- successors["Gore 2"] = ((man_x, man_y + 2), allowed, (house_x, house_y,house_way))
- 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)):
- successors["Gore-levo 2"] = ((man_x - 2, man_y + 2), allowed, (house_x, house_y,house_way))
- 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)):
- successors["Gore-desno 2"] = ((man_x + 2, man_y + 2), allowed, (house_x, house_y,house_way))
- successors["Stoj"] = ((man_x, man_y), allowed, (house_x, house_y,house_way))
- return successors
- def actions(self,state):
- return self.successor(state).keys()
- def h(self,node):
- man_x = node.state[0][0]
- man_y = node.state[0][1]
- house_x = node.state[2][0]
- house_y = node.state[2][1]
- return (abs(man_x - house_x)/2 + abs(man_y - house_y))/4
- def result(self, state, action):
- possible = self.successor(state)
- return possible[action]
- if __name__ == '__main__':
- 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),
- (2, 5), (3, 5), (0, 6), (2, 6), (1, 7), (3, 7))
- man_coordinates = input().split(',')
- man_x = int(man_coordinates[0])
- man_y = int(man_coordinates[1])
- man = (man_x, man_y)
- house_coordinates = input().split(',')
- house_x = int(house_coordinates[0])
- house_y = int(house_coordinates[1])
- way = input()
- house = (house_x, house_y, way)
- table = Table((man,allowed,house),house)
- answer = astar_search(table)
- if answer is None:
- print("NONE")
- else:
- print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement