def test_get_neighbors__3_visited(): m = maze.Maze(width=10, height=10, start=(5, 5)) m.get_cell(5, 4).visited = True # North neighbor m.get_cell(5, 6).visited = True # South neighbor m.get_cell(6, 5).visited = True # East neighbor result = m.get_neighbors(5, 5) assert result == {'W': (4, 5)}
def B_2(): ################################## ############## B(2) ############## ################################## print('Running problem 1, task B (2)') # Description of the maze as a numpy array # with the convention # 0 = empty cell # 1 = obstacle # 2 = exit of the Maze maze = np.array([[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 1, 2, 0, 0]]) # Generate plot for maximal probability as a function of T can_stay = [False, True] colors = ['black', 'red'] max_T = 25 nr_iterations = 10000 method = 'DynProg' start = ((0, 0), (6, 5)) # ((Player_pose),(Minotaur_pose)) for i in range(len(can_stay)): env = mz.Maze(maze, can_stay=can_stay[i]) x_vec = list() y_vec = list() for T in range(max_T): print('T={}/{}'.format(T, max_T)) y_value = 0 horizon = T _, policy = mz.dynamic_programming(env, horizon) for _ in range(nr_iterations): result = False path = env.simulate(start, policy, method) last_step = start for step in path: if step[0] == (6, 5) and last_step[0] == (6, 5): result = True last_step = step if result: y_value += 1 y_vec.append(y_value / nr_iterations) x_vec.append(T) plt.figure(2) plt.plot(x_vec, y_vec, c=colors[i], label='Stay: {}'.format(can_stay[i])) plt.title('Probability of exiting the maze') plt.legend() plt.xlabel('T') plt.ylabel('Probability') plt.show()
def C(): ################################## ############## C ################# ################################## print('Running problem 1, task C') # Description of the maze as a numpy array # with the convention # 0 = empty cell # 1 = obstacle # 2 = exit of the Maze maze = np.array([[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 1, 2, 0, 0]]) # Create an environment can_stay = False env = mz.Maze(maze, can_stay=can_stay) # Solve the MDP problem with dynamic programming gamma = 1.0 - 1.0 / 30 epsilon = 0.01 print('Gamma: {:.2f}, Episilon: {:.2f}'.format(gamma, epsilon)) _, policy = mz.value_iteration(env, gamma=gamma, epsilon=epsilon) # Simulate the shortest path starting from position A with dynamic programming method = 'ValIter' start = ((0, 0), (6, 5)) # ((Player_pose),(Minotaur_pose)) out_n_alive = 0 iterations = 10000 for _ in range(iterations): result = False path = env.simulate( start, policy, method) # Generate the path for the player and the minotaur for step in path: if step[0] == (6, 5) and prev_step[0] == (6, 5): result = True break prev_step = step if result: out_n_alive += 1 print('Probability of getting out alive (10,000 runs): {}'.format( out_n_alive / iterations))
def B_1(): ################################## ############## B(1) ############## ################################## print('Running problem 1, task B (1)') # Description of the maze as a numpy array # with the convention # 0 = empty cell # 1 = obstacle # 2 = exit of the Maze maze = np.array([[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 1, 2, 0, 0]]) # mz.draw_maze(maze) # Create an environment can_stay = False env = mz.Maze(maze, can_stay=can_stay) env.show() # Finite horizon horizon = 20 # Solve the MDP problem with dynamic programming _, policy = mz.dynamic_programming(env, horizon) # Simulate the shortest path starting from position A with dynamic programming method = 'DynProg' start = ((0, 0), (6, 5)) # ((Player_pose),(Minotaur_pose)) path = env.simulate( start, policy, method) # Generate the path for the player and the minotaur # Animation of the game mz.animate_solution(maze, path, method, can_stay=can_stay)
def test_get_neighbors__north_edge(): m = maze.Maze(width=10, height=10, start=(5, 4)) result = m.get_neighbors(1, 0) assert result == {'S': (1, 1), 'E': (2, 0), 'W': (0, 0)}
def test_get_cell__different_start(): m = maze.Maze(width=10, height=10, start=(5, 4)) # Start cell will be visited, so we can test that assert m.get_cell(5, 4).visited
def test_get_cell__default_start(): m = maze.Maze(width=10, height=10) # Start cell will be visited, so we can test that assert m.get_cell(0, 0).visited
def test_init__path_width__default_is_1(): m = maze.Maze(width=10, height=10) assert m.path_width == 1
def test_get_neighbors__no_visited(): m = maze.Maze(width=10, height=10, start=(5, 5)) result = m.get_neighbors(5, 5) assert result == {'N': (5, 4), 'S': (5, 6), 'E': (6, 5), 'W': (4, 5)}
def test_get_neighbors__west_edge(): m = maze.Maze(width=10, height=10, start=(5, 4)) result = m.get_neighbors(0, 5) assert result == {'N': (0, 4), 'S': (0, 6), 'E': (1, 5)}
def test_init__stack_has_start_default(): m = maze.Maze(width=10, height=10) assert m.stack == [(0, 0)]
def test_init__maze_size(): WIDTH = 10 HEIGHT = 10 m = maze.Maze(width=WIDTH, height=HEIGHT) assert len(m.maze) == WIDTH * HEIGHT
def test_get_neighbors__1_visited(): m = maze.Maze(width=10, height=10, start=(5, 5)) m.get_cell(5, 4).visited = True result = m.get_neighbors(5, 5) assert result == {'S': (5, 6), 'E': (6, 5), 'W': (4, 5)}
def test_init__maze_cells__initialized_to_Cells(): m = maze.Maze(width=10, height=10) # Skip the first starting cell assert all(True for c in m.maze if isinstance(c, cell.Cell))
def test_get_neighbors__south_edge(): m = maze.Maze(width=10, height=10, start=(5, 4)) result = m.get_neighbors(1, 9) assert result == {'N': (1, 8), 'E': (2, 9), 'W': (0, 9)}
def test_get_neighbors__east_edge(): m = maze.Maze(width=10, height=10, start=(5, 4)) result = m.get_neighbors(9, 5) assert result == {'N': (9, 4), 'S': (9, 6), 'W': (8, 5)}
def test_init__stack_has_custom_start(): m = maze.Maze(width=10, height=10, start=(1, 1)) assert m.stack == [(1, 1)]
def test_get_neighbors__nw_corner(): m = maze.Maze(width=10, height=10, start=(5, 4)) result = m.get_neighbors(0, 0) assert result == {'S': (0, 1), 'E': (1, 0)}
def test_init__first_cell_visited(): m = maze.Maze(width=10, height=10) assert m.visited == 1
from src import maze, agents, graphics m1 = maze.Maze(20) m1 = maze.gen_from_maze_file("../maze_files/10by10.maze") print(m1) a1 = agents.LeftOrRight(m1) a1.run(m1, 1) a1 = agents.AllLeft(m1) a1.run(m1) graphics.show(a1, m1)
def test_init__width_and_height(): m = maze.Maze(width=10, height=10) assert m.width == 10 assert m.height == 10