Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Implement in Python representation of the state about the problem for which, one of the possible initial states is depicted in Figure 1:
- 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."
- 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.
- """
- from utils import Problem
- from enum import Enum
- from uninformed_search import breadth_first_graph_search
- # write class definitions after this comment line
- class Actions(Enum):
- RIGHT = 1
- LEFT = 2
- UP = 3
- DOWN = 4
- class ObstacleProblem(Problem):
- def __init__(self, initial, goal):
- super().__init__(initial, goal)
- self.initial = initial
- self.goal = goal
- def goal_test(self, state):
- g = self.goal
- (x, y) = state[0]
- return x == g[0] and y == g[1]
- def successor(self, state):
- successors = dict()
- move(Actions.RIGHT, state, successors)
- move(Actions.LEFT, state, successors)
- move(Actions.UP, state, successors)
- move(Actions.DOWN, state, successors)
- return successors
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- possible = self.successor(state)
- return possible[action]
- def value(self):
- pass
- def move(action, state, successors):
- x, y = state[0]
- o1 = state[1]
- o2 = state[2]
- o3 = state[3]
- o1_new = update_obstacle1(o1)
- o2_new = update_obstacle2(o2)
- o3_new = update_obstacle3(o3)
- if action == Actions.RIGHT:
- y = y + 1
- action_done = "Desno"
- elif action == Actions.LEFT:
- y = y - 1
- action_done = "Levo"
- elif action == Actions.UP:
- x = x - 1
- action_done = "Gore"
- elif action == Actions.DOWN:
- x = x + 1
- action_done = "Dolu"
- else: action_done = "Invalid"
- if is_valid(x, y, o1_new, o2_new, o3_new):
- new_man_pos = (x, y)
- new_state = (new_man_pos, o1_new, o2_new, o3_new)
- successors[action_done] = new_state
- def update_obstacle1(obstacle):
- x = obstacle[0]
- y = obstacle[1]
- direction = obstacle[2]
- if (y == 0 and direction == -1) or (y == 4 and direction == 1):
- direction *= -1
- y_new = y + direction
- new_pos = (x, y_new, direction)
- return new_pos
- def update_obstacle2(obstacle):
- x = obstacle[0]
- y = obstacle[1]
- direction = obstacle[2]
- if (x == 5 and direction == 1) or (x == 9 and direction == -1):
- direction *= -1
- x_new = x - direction
- y_new = y + direction
- new_pos = x_new, y_new, direction
- return new_pos
- def update_obstacle3(obstacle):
- x = obstacle[0]
- y = obstacle[1]
- direction = obstacle[2]
- if (x == 5 and direction == 1) or (x == 9 and direction == -1):
- direction *= -1
- x_new = x - direction
- new_pos = x_new, y, direction
- return new_pos
- def is_valid(x, y, o1, o2, o3):
- # Map check
- if x < 0 or y < 0 or x > 10 or y > 10:
- return False
- if y > 5 and x < 5:
- return False
- # Obstacle collision check
- if (x == o1[0] and y == o1[1]) or (x == o1[0] and (y == o1[1] + 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] + 1 and y == o3[1]):
- return False
- return True
- 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
- goal_state = (house_row, house_column)
- man = (man_row, man_column)
- obstacle1 = (2, 2, -1) # left
- obstacle2 = (7, 2, 1) # upward-right
- obstacle3 = (7, 8, -1) # downward
- initial_state = (man, obstacle1, obstacle2, obstacle3)
- solution_space = ObstacleProblem(initial_state, goal_state)
- answer = breadth_first_graph_search(solution_space)
- print(answer.solution())
- """
- Black or white Problem 2 (0 / 0)
- Implement in Python representation of a state about the problem for which one of the possible initial states is depicted in Figure 1:
- Figure 1
- "An N x N board consists of white and black fields. When a field is selected (clicked), the color of that field, as well as the color of all of its immediate neighbors (left, right, up, and down), is changed to the opposite one, as shown in Figure 2. The goal is to color all of the board fields in black color. The problem must be solved using the smallest number of moves i.e. by clicking the least possible number of fields."
- Figure 2
- For all test examples, the shape of the board is the same as the one in Figure 1. Each test case has a different size N of the board and a different distribution of the black and white fields on it.
- The initial code reads the input arguments for each test case. n contains the size of the board (number of rows/columns); fields contains the color of each of the fields on the board (in the order: left to right, row by row, considering the board as a matrix), where 1 indicates that the field is colored in black, and 0 indicates that the field is colored in white.
- The field selections (moves) need to be named as follows:
- x: row, y: column
- where row and column are integer values representing the row and column of the selected (clicked) field (considering the board as a matrix).
- Your code should have one function call that prints the output on the standard screen - it should print the sequence of moves that have to be taken in order to color all the fields on the table in black. Use uninformed search. Use the test examples to decide which variant of uninformed search to use.
- """
- from utils import Problem
- from enum import Enum
- from uninformed_search import breadth_first_graph_search
- # write the code for modelling the problem after this comment
- class BlackWhiteProblem(Problem):
- def __init__(self, initial, goal):
- super().__init__(initial, goal)
- self.initial = initial
- self.goal = goal
- def goal_test(self, state):
- g = self.goal
- return g == state
- def successor(self, state):
- successors = dict()
- for i in range(0, state.__len__()):
- successors["x: " + str(i // n) + ", y: " + str(i % n)] = get_new_state(i, state)
- return successors
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- possible = self.successor(state)
- return possible[action]
- def value(self):
- pass
- def invert(num):
- if num == 1:
- num = 0
- else:
- num = 1
- return num
- def get_new_state(pos, state):
- state_list = list(state)
- state_list[pos] = invert(state_list[pos])
- size = n*n
- if (pos+n) < size:
- state_list[pos+n] = invert(state_list[pos+n])
- if (pos-n) >= 0:
- state_list[pos-n] = invert(state_list[pos-n])
- if (pos+1) < size and (pos % n) != n - 1:
- state_list[pos+1] = invert(state_list[pos+1])
- if (pos-1) >= 0 and (pos % n) != 0:
- state_list[pos-1] = invert(state_list[pos-1])
- return tuple(state_list)
- if __name__ == '__main__':
- n = int(input())
- fields = list(map(int, input().split(',')))
- # solve the problem after this comment line
- goal_state = []
- for i in range(0, n*n):
- goal_state.append(1)
- goal_state = tuple(goal_state)
- initial_state = tuple(fields)
- solution_space = BlackWhiteProblem(initial_state, goal_state)
- answer = breadth_first_graph_search(solution_space)
- print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement