Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from utils import Problem
- from uninformed_search import breadth_first_graph_search
- # write class definitions after this comment line
- def nextO1(o1):
- x, y, direction = o1
- if direction == 1:
- if x != 4:
- return x + 1, y, direction
- return x - 1, y, direction * -1
- if direction == -1:
- if x != 0:
- return x - 1, y, direction
- return x + 1, y, direction * -1
- def nextO2(o2):
- x, y, direction = o2
- if direction == 1:
- if x != 4:
- return x + 1, y - 1, direction
- return x - 1, y + 1, direction * -1
- if direction == -1:
- if x != 0:
- return x - 1, y + 1, direction
- return x + 1, y - 1, direction * -1
- def nextO3(o3):
- x, y, direction = o3
- if direction == 1:
- if y != 5:
- return x, y - 1, direction
- return x, y + 1, direction * -1
- if direction == -1:
- if y != 9:
- return x, y + 1, direction
- return x, y - 1, direction * -1
- def is_valid(x, y, o1, o2, o3):
- if x < 0 or x > 10 or y > 10 or y < 0:
- return False
- if x > 5 and y < 5:
- return False
- if (x == o1[0] and y == o1[1]) or (x == o1[0] + 1 and y == o1[1]):
- return False
- 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) \
- or (x == o2[0] + 1 and y == o2[1] + 1):
- return False
- if (x == o3[0] and y == o3[1]) or (x == o3[0] and y == o3[1] + 1):
- return False
- return True
- class MovingObstacles(Problem):
- def __init__(self, initial, goal):
- super().__init__(initial, goal)
- def goal_test(self, state):
- return state[0] == self.goal
- def successor(self, state):
- successors = dict()
- x, y = state[0]
- o1 = state[1]
- o2 = state[2]
- o3 = state[3]
- o1_new = nextO1(o1)
- o2_new = nextO2(o2)
- o3_new = nextO3(o3)
- x_new = x + 1
- if is_valid(x_new, y, o1_new, o2_new, o3_new):
- state_new = ((x_new, y), o1_new, o2_new, o3_new)
- successors['Desno'] = state_new
- x_new = x - 1
- if is_valid(x_new, y, o1_new, o2_new, o3_new):
- state_new = ((x_new, y), o1_new, o2_new, o3_new)
- successors['Levo'] = state_new
- y_new = y - 1
- if is_valid(x, y_new, o1_new, o2_new, o3_new):
- state_new = ((x, y_new), o1_new, o2_new, o3_new)
- successors['Gore'] = state_new
- y_new = y + 1
- if is_valid(x, y_new, o1_new, o2_new, o3_new):
- state_new = ((x, y_new), o1_new, o2_new, o3_new)
- successors['Dolu'] = state_new
- return successors
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- return self.successor(state)[action]
- if __name__ == '__main__':
- man_row = int(input())
- man_column = int(input())
- house_row = int(input())
- house_column = int(input())
- # continue the code for solving the problem after this comment line
- o1 = (2, 2, -1)
- o2 = (2, 7, 1)
- o3 = (8, 7, -1)
- ini = ((man_column, man_row), o1, o2, o3)
- g = (house_column, house_row)
- problem = MovingObstacles(ini, g)
- answer = breadth_first_graph_search(problem)
- print(answer.solution())
- '''
- Moving obstacles Problem 1 (0 / 0)
- Implement in Python representation of the state about the problem for which, one of the possible initial states is depicted in Figure 1:
- enter image description here
- Figure 1
- "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."
- enter image description here
- Figure 2
- 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.
- 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.
- The movements need to be defined as follows:
- Desno - move the man right
- Levo - move the man left
- Gore - move the man up
- Dolu - move the man down
- 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.
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement