From 948b1f0de8e1e6e571f6a167c3bc2e2b757406b1 Mon Sep 17 00:00:00 2001 From: Navpreet Singh Date: Fri, 12 May 2023 18:09:35 -0400 Subject: [PATCH] Added the new maze environment --- minigrid/envs/__init__.py | 1 + minigrid/envs/maze.py | 103 +++++++++++++------------------------- 2 files changed, 36 insertions(+), 68 deletions(-) diff --git a/minigrid/envs/__init__.py b/minigrid/envs/__init__.py index 5401e7157..6dfe0bdb2 100644 --- a/minigrid/envs/__init__.py +++ b/minigrid/envs/__init__.py @@ -2,6 +2,7 @@ from minigrid.envs.blockedunlockpickup import BlockedUnlockPickupEnv from minigrid.envs.crossing import CrossingEnv +from minigrid.envs.maze import MazeEnv from minigrid.envs.distshift import DistShiftEnv from minigrid.envs.doorkey import DoorKeyEnv from minigrid.envs.dynamicobstacles import DynamicObstaclesEnv diff --git a/minigrid/envs/maze.py b/minigrid/envs/maze.py index b662c639e..86bbfd979 100644 --- a/minigrid/envs/maze.py +++ b/minigrid/envs/maze.py @@ -1,7 +1,5 @@ from __future__ import annotations -import itertools as itt - import numpy as np from minigrid.core.grid import Grid @@ -148,6 +146,13 @@ def delete_wall(walls, rand_wall): for wall in walls: if (wall[0] == rand_wall[0] and wall[1] == rand_wall[1]): walls.remove(wall) + + def mark(walls, cell, p, x, y, bound): + if (p != bound): + if (self.grid.get(x, y) != cell): + self.put_obj(self.obstacle_type(), x, y) + if ([x, y] not in walls): + walls.append([x, y]) while walls: # Pick a random wall @@ -156,7 +161,7 @@ def delete_wall(walls, rand_wall): # Check if it is a left wall if (rand_wall[1] != 0): - if (self.grid.get(rand_wall[0],rand_wall[1]-1) == None and self.grid.get(rand_wall[0],rand_wall[1]+1) == cell): + if (self.grid.get(rand_wall[0],rand_wall[1]-1) is None and self.grid.get(rand_wall[0],rand_wall[1]+1) == cell): # Find the number of surrounding cells s_cells = surroundingCells(rand_wall) if (s_cells < 2): @@ -165,26 +170,13 @@ def delete_wall(walls, rand_wall): # Mark the new walls # Upper cell - if (rand_wall[0] != 0): - if (self.grid.get(rand_wall[0]-1,rand_wall[1]) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0]-1, rand_wall[1]) - if ([rand_wall[0]-1, rand_wall[1]] not in walls): - walls.append([rand_wall[0]-1, rand_wall[1]]) - + mark(walls, cell, rand_wall[0], rand_wall[0]-1, rand_wall[1], 0) # Bottom cell - if (rand_wall[0] != height-1): - if (self.grid.get(rand_wall[0]+1,rand_wall[1]) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0]+1, rand_wall[1]) - if ([rand_wall[0]+1, rand_wall[1]] not in walls): - walls.append([rand_wall[0]+1, rand_wall[1]]) + mark(walls, cell, rand_wall[0], rand_wall[0]+1, rand_wall[1], height-1) # Leftmost cell - if (rand_wall[1] != 0): - if (self.grid.get(rand_wall[0],rand_wall[1]-1) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0], rand_wall[1]-1) - if ([rand_wall[0], rand_wall[1]-1] not in walls): - walls.append([rand_wall[0], rand_wall[1]-1]) + mark(walls, cell, rand_wall[1], rand_wall[0], rand_wall[1]-1, 0) # Delete wall @@ -194,7 +186,7 @@ def delete_wall(walls, rand_wall): # Check if it is an upper wall if (rand_wall[0] != 0): - if (self.grid.get(rand_wall[0]-1,rand_wall[1]) == None and self.grid.get(rand_wall[0]+1,rand_wall[1]) == cell): + if (self.grid.get(rand_wall[0]-1,rand_wall[1]) is None and self.grid.get(rand_wall[0]+1,rand_wall[1]) == cell): s_cells = surroundingCells(rand_wall) if (s_cells < 2): @@ -203,25 +195,13 @@ def delete_wall(walls, rand_wall): # Mark the new walls # Upper cell - if (rand_wall[0] != 0): - if (self.grid.get(rand_wall[0]-1,rand_wall[1]) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0]-1, rand_wall[1]) - if ([rand_wall[0]-1, rand_wall[1]] not in walls): - walls.append([rand_wall[0]-1, rand_wall[1]]) + mark(walls, cell, rand_wall[0], rand_wall[0]-1, rand_wall[1], 0) # Leftmost cell - if (rand_wall[1] != 0): - if (self.grid.get(rand_wall[0],rand_wall[1]-1) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0], rand_wall[1]-1) - if ([rand_wall[0], rand_wall[1]-1] not in walls): - walls.append([rand_wall[0], rand_wall[1]-1]) + mark(walls, cell, rand_wall[1], rand_wall[0], rand_wall[1]-1, 0) # Rightmost cell - if (rand_wall[1] != width-1): - if (self.grid.get(rand_wall[0],rand_wall[1]+1) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0], rand_wall[1]+1) - if ([rand_wall[0], rand_wall[1]+1] not in walls): - walls.append([rand_wall[0], rand_wall[1]+1]) + mark(walls, cell, rand_wall[1], rand_wall[0], rand_wall[1]+1, width-1) # Delete wall delete_wall(walls, rand_wall) @@ -231,7 +211,7 @@ def delete_wall(walls, rand_wall): # Check the bottom wall if (rand_wall[0] != height-1): - if (self.grid.get(rand_wall[0]+1,rand_wall[1]) == None and self.grid.get(rand_wall[0]-1,rand_wall[1]) == cell): + if (self.grid.get(rand_wall[0]+1,rand_wall[1]) is None and self.grid.get(rand_wall[0]-1,rand_wall[1]) == cell): s_cells = surroundingCells(rand_wall) if (s_cells < 2): @@ -239,21 +219,14 @@ def delete_wall(walls, rand_wall): self.grid.set(rand_wall[0], rand_wall[1], cell) # Mark the new walls - if (rand_wall[0] != height-1): - if (self.grid.get(rand_wall[0]+1,rand_wall[1]) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0]+1, rand_wall[1]) - if ([rand_wall[0]+1, rand_wall[1]] not in walls): - walls.append([rand_wall[0]+1, rand_wall[1]]) - if (rand_wall[1] != 0): - if (self.grid.get(rand_wall[0],rand_wall[1]-1) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0], rand_wall[1]-1) - if ([rand_wall[0], rand_wall[1]-1] not in walls): - walls.append([rand_wall[0], rand_wall[1]-1]) - if (rand_wall[1] != width-1): - if (self.grid.get(rand_wall[0],rand_wall[1]+1) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0], rand_wall[1]+1) - if ([rand_wall[0], rand_wall[1]+1] not in walls): - walls.append([rand_wall[0], rand_wall[1]+1]) + # Bottom cell + mark(walls, cell, rand_wall[0], rand_wall[0]+1, rand_wall[1], height-1) + + # Leftmost cell + mark(walls, cell, rand_wall[1], rand_wall[0], rand_wall[1]-1, 0) + + # Rightmost cell + mark(walls, cell, rand_wall[1], rand_wall[0], rand_wall[1]+1, width-1) # Delete wall delete_wall(walls, rand_wall) @@ -263,7 +236,7 @@ def delete_wall(walls, rand_wall): # Check the right wall if (rand_wall[1] != width-1): - if (self.grid.get(rand_wall[0],rand_wall[1]+1) == None and self.grid.get(rand_wall[0],rand_wall[1]-1) == cell): + if (self.grid.get(rand_wall[0],rand_wall[1]+1) is None and self.grid.get(rand_wall[0],rand_wall[1]-1) == cell): s_cells = surroundingCells(rand_wall) if (s_cells < 2): @@ -271,21 +244,15 @@ def delete_wall(walls, rand_wall): self.grid.set(rand_wall[0], rand_wall[1], cell) # Mark the new walls - if (rand_wall[1] != width-1): - if (self.grid.get(rand_wall[0],rand_wall[1]+1) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0], rand_wall[1]+1) - if ([rand_wall[0], rand_wall[1]+1] not in walls): - walls.append([rand_wall[0], rand_wall[1]+1]) - if (rand_wall[0] != height-1): - if (self.grid.get(rand_wall[0]+1,rand_wall[1]) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0]+1, rand_wall[1]) - if ([rand_wall[0]+1, rand_wall[1]] not in walls): - walls.append([rand_wall[0]+1, rand_wall[1]]) - if (rand_wall[0] != 0): - if (self.grid.get(rand_wall[0]-1,rand_wall[1]) != cell): - self.put_obj(self.obstacle_type(), rand_wall[0]-1, rand_wall[1]) - if ([rand_wall[0]-1, rand_wall[1]] not in walls): - walls.append([rand_wall[0]-1, rand_wall[1]]) + + # Rightmost cell + mark(walls, cell, rand_wall[1], rand_wall[0], rand_wall[1]+1, width-1) + + # Bottom cell + mark(walls, cell, rand_wall[0], rand_wall[0]+1, rand_wall[1], height-1) + + # Upper cell + mark(walls, cell, rand_wall[0], rand_wall[0]-1, rand_wall[1], 0) # Delete wall delete_wall(walls, rand_wall) @@ -315,7 +282,7 @@ def delete_wall(walls, rand_wall): # Mark the remaining unvisited cells as walls for i in range(0, height): for j in range(0, width): - if (self.grid.get(i,j) == None): + if (self.grid.get(i,j) is None): self.put_obj(self.obstacle_type(), i, j) if (self.grid.get(i,j) == cell): self.grid.set(i, j, None)