Skip to content

Commit

Permalink
Added the new maze environment
Browse files Browse the repository at this point in the history
  • Loading branch information
navpreetnp7 committed May 12, 2023
1 parent 5884a64 commit 948b1f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 68 deletions.
1 change: 1 addition & 0 deletions minigrid/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 35 additions & 68 deletions minigrid/envs/maze.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import itertools as itt

import numpy as np

from minigrid.core.grid import Grid
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -231,29 +211,22 @@ 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):
# Denote the new path
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)
Expand All @@ -263,29 +236,23 @@ 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):
# Denote the new path
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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 948b1f0

Please sign in to comment.