Advertisement
Petrush

[AI] Lab2_1 Moving obstacles

Apr 26th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. from utils import Problem
  2. from uninformed_search import breadth_first_graph_search
  3.  
  4.  
  5. # write class definitions after this comment line
  6.  
  7. def nextO1(o1):
  8.     x, y, direction = o1
  9.     if direction == 1:
  10.         if x != 4:
  11.             return x + 1, y, direction
  12.         return x - 1, y, direction * -1
  13.     if direction == -1:
  14.         if x != 0:
  15.             return x - 1, y, direction
  16.         return x + 1, y, direction * -1
  17.  
  18.  
  19. def nextO2(o2):
  20.     x, y, direction = o2
  21.     if direction == 1:
  22.         if x != 4:
  23.             return x + 1, y - 1, direction
  24.         return x - 1, y + 1, direction * -1
  25.     if direction == -1:
  26.         if x != 0:
  27.             return x - 1, y + 1, direction
  28.         return x + 1, y - 1, direction * -1
  29.  
  30.  
  31. def nextO3(o3):
  32.     x, y, direction = o3
  33.     if direction == 1:
  34.         if y != 5:
  35.             return x, y - 1, direction
  36.         return x, y + 1, direction * -1
  37.     if direction == -1:
  38.         if y != 9:
  39.             return x, y + 1, direction
  40.         return x, y - 1, direction * -1
  41.  
  42.  
  43. def is_valid(x, y, o1, o2, o3):
  44.     if x < 0 or x > 10 or y > 10 or y < 0:
  45.         return False
  46.     if x > 5 and y < 5:
  47.         return False
  48.     if (x == o1[0] and y == o1[1]) or (x == o1[0] + 1 and y == o1[1]):
  49.         return False
  50.     if (x == o2[0] and y == o2[1]) or (x == o2[0] + 1 and y == o2[1]) or (x == o2[0] and y == o2[1] + 1) \
  51.             or (x == o2[0] + 1 and y == o2[1] + 1):
  52.         return False
  53.     if (x == o3[0] and y == o3[1]) or (x == o3[0] and y == o3[1] + 1):
  54.         return False
  55.  
  56.     return True
  57.  
  58.  
  59. class MovingObstacles(Problem):
  60.     def __init__(self, initial, goal):
  61.         super().__init__(initial, goal)
  62.  
  63.     def goal_test(self, state):
  64.         return state[0] == self.goal
  65.  
  66.     def successor(self, state):
  67.         successors = dict()
  68.         x, y = state[0]
  69.         o1 = state[1]
  70.         o2 = state[2]
  71.         o3 = state[3]
  72.         o1_new = nextO1(o1)
  73.         o2_new = nextO2(o2)
  74.         o3_new = nextO3(o3)
  75.  
  76.         x_new = x + 1
  77.         if is_valid(x_new, y, o1_new, o2_new, o3_new):
  78.             state_new = ((x_new, y), o1_new, o2_new, o3_new)
  79.             successors['Desno'] = state_new
  80.  
  81.         x_new = x - 1
  82.         if is_valid(x_new, y, o1_new, o2_new, o3_new):
  83.             state_new = ((x_new, y), o1_new, o2_new, o3_new)
  84.             successors['Levo'] = state_new
  85.  
  86.         y_new = y - 1
  87.         if is_valid(x, y_new, o1_new, o2_new, o3_new):
  88.             state_new = ((x, y_new), o1_new, o2_new, o3_new)
  89.             successors['Gore'] = state_new
  90.  
  91.         y_new = y + 1
  92.         if is_valid(x, y_new, o1_new, o2_new, o3_new):
  93.             state_new = ((x, y_new), o1_new, o2_new, o3_new)
  94.             successors['Dolu'] = state_new
  95.  
  96.         return successors
  97.  
  98.     def actions(self, state):
  99.         return self.successor(state).keys()
  100.  
  101.     def result(self, state, action):
  102.         return self.successor(state)[action]
  103.  
  104.  
  105. if __name__ == '__main__':
  106.     man_row = int(input())
  107.     man_column = int(input())
  108.     house_row = int(input())
  109.     house_column = int(input())
  110.     # continue the code for solving the problem after this comment line
  111.     o1 = (2, 2, -1)
  112.     o2 = (2, 7, 1)
  113.     o3 = (8, 7, -1)
  114.     ini = ((man_column, man_row), o1, o2, o3)
  115.     g = (house_column, house_row)
  116.     problem = MovingObstacles(ini, g)
  117.     answer = breadth_first_graph_search(problem)
  118.     print(answer.solution())
  119.  
  120.  
  121. '''
  122. Moving obstacles Problem 1 (0 / 0)
  123. Implement in Python representation of the state about the problem for which, one of the possible initial states is depicted in Figure 1:
  124.  
  125. enter image description here
  126.  
  127. Figure 1
  128.  
  129. "The stick-man needs to arrive safely to his house. The stick-man can move to any adjacent field horizontally, vertically or diagonally. Obstacles 1, 2 and 3 are moving, with Obstacle 1 moving horizontally, Obstacle 2 moving diagonally, and Obstacle 3 moving vertically. When the stick-man doesn't move the obstacles don't move. With every step (move) the stick-man takes, each of the obstacles also makes a single field movement following its corresponding course and direction. Initially (before any move is made by the stick-man) the movement direction of Obstacle 1 is to the left, of Obstacle 2 is upward-right, and of Obstacle 3 is downward (Figure 2 gives the configuration of the table after the stick-man has moved down from his initial position). When an obstacle hits a wall (the frontier of the table) and can no longer move in its current direction, for the next movement the direction of the obstacle is switched (for example the direction of Obstacle 1 changes from left to right or vice versa). If the stick-man and an obstacle occupy the same field the stick-man is destroyed."
  130.  
  131. enter image description here
  132.  
  133. Figure 2
  134.  
  135. For all test examples, the look and size of the board are the same as in the images. All test cases have the same initial position and movement direction for the obstacles. Each test case has a different starting position of the man and a different position of the house.
  136.  
  137. The initial code reads the input arguments for each test case. man_row and man_column contain the row and column of the man, house_row and house_column contain the row and column of the house.
  138.  
  139. The movements need to be defined as follows:
  140.  
  141. Desno - move the man right
  142. Levo - move the man left
  143. Gore - move the man up
  144. Dolu - move the man down
  145. Your code should have one call of a function that prints the output on the standard screen output that will print the movement sequence of the man. The movement sequence is the sequence of moves that will allow the man to reach the position of the house. Use uninformed search. Use the test examples to decide which variant of uninformed search to use.
  146. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement