def main(): # Define maze properties first 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) env = mz.Maze(maze, minotaur_stay=False) # Player starts at (0,0), Minotaur at (6,5). start = (0, 0, 6, 5) # Demonstrate optimal policy for T = 20. V, policy = mz.dynamic_programming(env, 20) demo_policy(env, policy) # Demonstrate optimal policy for T = 20 and minotaur stay. env = mz.Maze(maze, minotaur_stay=True) _, policy = mz.dynamic_programming(env, 20) demo_policy(env, policy) # Plot survival probability as a function of T. plot_survival_prob(maze, start, minotaur_stay=False) plot_survival_prob(maze, start, minotaur_stay=True) # Calculate geometric survival rate by solving for infinite MDP with discount factor 29/30. gamma = 29 / 30 # Geometric distribution, 30 on average. epsilon = 0.0001 _, policy = mz.value_iteration(env, gamma, epsilon) success_cnt = 0 for _ in range(10000): path = env.simulate_val_iter(start, policy) if path[-1][0:2] == (6, 5): success_cnt += 1 print(f"Probability of succeeding with E[T]=30: {success_cnt/1e4}")
import maze as mz import pandas as pd import matplotlib.pyplot as plt # Description of the maze as a numpy array 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) env = mz.Maze(maze) win_count = np.zeros(30) #env.show() iterations = 100 points = 30 for i in range(1,points+1): horizon = i # print("horizon = ", i) for _ in range(iterations): V, policy = mz.dynamic_programming(env, horizon); # Simulate the shortest path starting from position A method = 'DynProg'; start = (0, 0); path, pathm, win = env.simulate(start, policy, method); if win:
import player import maze import pygame import os import time if __name__ == "__main__": os.environ['SDL_VIDEO_CENTERED'] = '1' # To have a borderless maze, chose an odd width and height # maze = maze.Maze(31, 15, 20) maze = maze.Maze(199, 99, 5) pygame.init() maze.draw_maze() maze.dfs() maze.draw_maze_play() pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()
index = 1 elif player.direction == Maze.LEFT: index = 2 elif player.direction == Maze.RIGHT: index = 3 else: index = 0 SURFACE.blit(PLAYER_SPRITES[player.number][index], screen_position) for i in range(2): if (macguffins_collected[i] > 0 and MAZE.macguffin_locations[ PLAYERS[i].position[0]][PLAYERS[i].position[1]] == 3): MAZE.macguffin_locations[PLAYERS[i].position[0]][ PLAYERS[i].position[1]] = macguffins_collected[i] draw_maze(SQ_SIZE, MAZE, SURFACE, PLAYERS, wall_vertical_texture, wall_horizontal_texture, mcguffs, mac_small) #Move and draw the shadows for i in range(len(MAZE.CREATURES))[2:]: MAZE.CREATURES[i].navigate(MAZE) position = MAZE.CREATURES[i].position screen_position = (int(SQ_SIZE * (position[1] + 0.55)), int(SQ_SIZE * (position[0] + 0.6))) pygame.draw.circle(SURFACE, (122, 122, 122), screen_position, int(SQ_SIZE * 0.3)) pygame.display.update() macg_const = 5 for i in range(2): for j in range(3): if not (j == 2 and macguffins_collected[i] == 0):
index = 0 elif player.direction==Maze.TOP: index = 1 elif player.direction==Maze.LEFT: index = 2 elif player.direction==Maze.RIGHT: index = 3 else: index=0 SURFACE.blit(PLAYER_SPRITES[player.number][index],screen_position) for i in range(2): if(macguffins_collected[i] > 0 and MAZE.macguffin_locations[PLAYERS[i].position[0]][PLAYERS[i].position[1]] == 3): MAZE.macguffin_locations[PLAYERS[i].position[0]][PLAYERS[i].position[1]] = macguffins_collected[i] draw_maze(SQ_SIZE,MAZE,SURFACE,PLAYERS,wall_vertical_texture,wall_horizontal_texture, mcguffs, mac_small) #Move and draw the shadows for i in range(len(MAZE.CREATURES))[2:]: MAZE.CREATURES[i].navigate(MAZE) position = MAZE.CREATURES[i].position screen_position = (int(SQ_SIZE * (position[1] + 0.55)), int(SQ_SIZE * (position[0] + 0.6))) pygame.draw.circle(SURFACE, (122,122,122), screen_position, int(SQ_SIZE * 0.3)) pygame.display.update() macg_const = 5 for i in range(2): for j in range(3): if not(j ==2 and macguffins_collected[i] == 0): if MAZE.macguffin_locations[PLAYERS[i].position[0]][PLAYERS[i].position[1]] == str(j):