minigrid.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. import hashlib
  2. import math
  3. from abc import abstractmethod
  4. from enum import IntEnum
  5. from typing import Any, Callable, Optional, Union
  6. import gym
  7. import numpy as np
  8. from gym import spaces
  9. from gym.utils import seeding
  10. # Size in pixels of a tile in the full-scale human view
  11. from gym_minigrid.rendering import (
  12. downsample,
  13. fill_coords,
  14. highlight_img,
  15. point_in_circle,
  16. point_in_line,
  17. point_in_rect,
  18. point_in_triangle,
  19. rotate_fn,
  20. )
  21. from gym_minigrid.window import Window
  22. TILE_PIXELS = 32
  23. # Map of color names to RGB values
  24. COLORS = {
  25. "red": np.array([255, 0, 0]),
  26. "green": np.array([0, 255, 0]),
  27. "blue": np.array([0, 0, 255]),
  28. "purple": np.array([112, 39, 195]),
  29. "yellow": np.array([255, 255, 0]),
  30. "grey": np.array([100, 100, 100]),
  31. }
  32. COLOR_NAMES = sorted(list(COLORS.keys()))
  33. # Used to map colors to integers
  34. COLOR_TO_IDX = {"red": 0, "green": 1, "blue": 2, "purple": 3, "yellow": 4, "grey": 5}
  35. IDX_TO_COLOR = dict(zip(COLOR_TO_IDX.values(), COLOR_TO_IDX.keys()))
  36. # Map of object type to integers
  37. OBJECT_TO_IDX = {
  38. "unseen": 0,
  39. "empty": 1,
  40. "wall": 2,
  41. "floor": 3,
  42. "door": 4,
  43. "key": 5,
  44. "ball": 6,
  45. "box": 7,
  46. "goal": 8,
  47. "lava": 9,
  48. "agent": 10,
  49. }
  50. IDX_TO_OBJECT = dict(zip(OBJECT_TO_IDX.values(), OBJECT_TO_IDX.keys()))
  51. # Map of state names to integers
  52. STATE_TO_IDX = {
  53. "open": 0,
  54. "closed": 1,
  55. "locked": 2,
  56. }
  57. # Map of agent direction indices to vectors
  58. DIR_TO_VEC = [
  59. # Pointing right (positive X)
  60. np.array((1, 0)),
  61. # Down (positive Y)
  62. np.array((0, 1)),
  63. # Pointing left (negative X)
  64. np.array((-1, 0)),
  65. # Up (negative Y)
  66. np.array((0, -1)),
  67. ]
  68. def check_if_no_duplicate(duplicate_list: list) -> bool:
  69. """Check if given list contains any duplicates"""
  70. return len(set(duplicate_list)) == len(duplicate_list)
  71. class MissionSpace(spaces.Space[str]):
  72. r"""A space representing a mission for the Gym-Minigrid environments.
  73. The space allows generating random mission strings constructed with an input placeholder list.
  74. Example Usage::
  75. >>> observation_space = MissionSpace(mission_func=lambda color: f"Get the {color} ball.",
  76. ordered_placeholders=[["green", "blue"]])
  77. >>> observation_space.sample()
  78. "Get the green ball."
  79. >>> observation_space = MissionSpace(mission_func=lambda : "Get the ball.".,
  80. ordered_placeholders=None)
  81. >>> observation_space.sample()
  82. "Get the ball."
  83. """
  84. def __init__(
  85. self,
  86. mission_func: Callable[..., str],
  87. ordered_placeholders: Optional["list[list[str]]"] = None,
  88. seed: Optional[Union[int, seeding.RandomNumberGenerator]] = None,
  89. ):
  90. r"""Constructor of :class:`MissionSpace` space.
  91. Args:
  92. mission_func (lambda _placeholders(str): _mission(str)): Function that generates a mission string from random placeholders.
  93. ordered_placeholders (Optional["list[list[str]]"]): List of lists of placeholders ordered in placing order in the mission function mission_func.
  94. seed: seed: The seed for sampling from the space.
  95. """
  96. # Check that the ordered placeholders and mission function are well defined.
  97. if ordered_placeholders is not None:
  98. assert (
  99. len(ordered_placeholders) == mission_func.__code__.co_argcount
  100. ), f"The number of placeholders {len(ordered_placeholders)} is different from the number of parameters in the mission function {mission_func.__code__.co_argcount}."
  101. for placeholder_list in ordered_placeholders:
  102. assert check_if_no_duplicate(
  103. placeholder_list
  104. ), "Make sure that the placeholders don't have any duplicate values."
  105. else:
  106. assert (
  107. mission_func.__code__.co_argcount == 0
  108. ), f"If the ordered placeholders are {ordered_placeholders}, the mission function shouldn't have any parameters."
  109. self.ordered_placeholders = ordered_placeholders
  110. self.mission_func = mission_func
  111. super().__init__(dtype=str, seed=seed)
  112. # Check that mission_func returns a string
  113. sampled_mission = self.sample()
  114. assert isinstance(
  115. sampled_mission, str
  116. ), f"mission_func must return type str not {type(sampled_mission)}"
  117. def sample(self) -> str:
  118. """Sample a random mission string."""
  119. if self.ordered_placeholders is not None:
  120. placeholders = []
  121. for rand_var_list in self.ordered_placeholders:
  122. idx = self.np_random.integers(0, len(rand_var_list))
  123. placeholders.append(rand_var_list[idx])
  124. return self.mission_func(*placeholders)
  125. else:
  126. return self.mission_func()
  127. def contains(self, x: Any) -> bool:
  128. """Return boolean specifying if x is a valid member of this space."""
  129. # Store a list of all the placeholders from self.ordered_placeholders that appear in x
  130. if self.ordered_placeholders is not None:
  131. check_placeholder_list = []
  132. for placeholder_list in self.ordered_placeholders:
  133. for placeholder in placeholder_list:
  134. if placeholder in x:
  135. check_placeholder_list.append(placeholder)
  136. # Remove duplicates from the list
  137. check_placeholder_list = list(set(check_placeholder_list))
  138. start_id_placeholder = []
  139. end_id_placeholder = []
  140. # Get the starting and ending id of the identified placeholders with possible duplicates
  141. new_check_placeholder_list = []
  142. for placeholder in check_placeholder_list:
  143. new_start_id_placeholder = [
  144. i for i in range(len(x)) if x.startswith(placeholder, i)
  145. ]
  146. new_check_placeholder_list += [placeholder] * len(
  147. new_start_id_placeholder
  148. )
  149. end_id_placeholder += [
  150. start_id + len(placeholder) - 1
  151. for start_id in new_start_id_placeholder
  152. ]
  153. start_id_placeholder += new_start_id_placeholder
  154. # Order by starting id the placeholders
  155. ordered_placeholder_list = sorted(
  156. zip(
  157. start_id_placeholder, end_id_placeholder, new_check_placeholder_list
  158. )
  159. )
  160. # Check for repeated placeholders contained in each other
  161. remove_placeholder_id = []
  162. for i, placeholder_1 in enumerate(ordered_placeholder_list):
  163. starting_id = i + 1
  164. for j, placeholder_2 in enumerate(
  165. ordered_placeholder_list[starting_id:]
  166. ):
  167. # Check if place holder ids overlap and keep the longest
  168. if max(placeholder_1[0], placeholder_2[0]) < min(
  169. placeholder_1[1], placeholder_2[1]
  170. ):
  171. remove_placeholder = min(
  172. placeholder_1[2], placeholder_2[2], key=len
  173. )
  174. if remove_placeholder == placeholder_1[2]:
  175. remove_placeholder_id.append(i)
  176. else:
  177. remove_placeholder_id.append(i + j + 1)
  178. for id in remove_placeholder_id:
  179. del ordered_placeholder_list[id]
  180. final_placeholders = [
  181. placeholder[2] for placeholder in ordered_placeholder_list
  182. ]
  183. # Check that the identified final placeholders are in the same order as the original placeholders.
  184. for orered_placeholder, final_placeholder in zip(
  185. self.ordered_placeholders, final_placeholders
  186. ):
  187. if final_placeholder in orered_placeholder:
  188. continue
  189. else:
  190. return False
  191. try:
  192. mission_string_with_placeholders = self.mission_func(
  193. *final_placeholders
  194. )
  195. except Exception as e:
  196. print(
  197. f"{x} is not contained in MissionSpace due to the following exception: {e}"
  198. )
  199. return False
  200. return bool(mission_string_with_placeholders == x)
  201. else:
  202. return bool(self.mission_func() == x)
  203. def __repr__(self) -> str:
  204. """Gives a string representation of this space."""
  205. return f"MissionSpace({self.mission_func}, {self.ordered_placeholders})"
  206. def __eq__(self, other) -> bool:
  207. """Check whether ``other`` is equivalent to this instance."""
  208. if isinstance(other, MissionSpace):
  209. # Check that place holder lists are the same
  210. if self.ordered_placeholders is not None:
  211. # Check length
  212. if (len(self.order_placeholder) == len(other.order_placeholder)) and (
  213. all(
  214. set(i) == set(j)
  215. for i, j in zip(self.order_placeholder, other.order_placeholder)
  216. )
  217. ):
  218. # Check mission string is the same with dummy space placeholders
  219. test_placeholders = [""] * len(self.order_placeholder)
  220. mission = self.mission_func(*test_placeholders)
  221. other_mission = other.mission_func(*test_placeholders)
  222. return mission == other_mission
  223. else:
  224. # Check that other is also None
  225. if other.ordered_placeholders is None:
  226. # Check mission string is the same
  227. mission = self.mission_func()
  228. other_mission = other.mission_func()
  229. return mission == other_mission
  230. # If none of the statements above return then False
  231. return False
  232. class WorldObj:
  233. """
  234. Base class for grid world objects
  235. """
  236. def __init__(self, type, color):
  237. assert type in OBJECT_TO_IDX, type
  238. assert color in COLOR_TO_IDX, color
  239. self.type = type
  240. self.color = color
  241. self.contains = None
  242. # Initial position of the object
  243. self.init_pos = None
  244. # Current position of the object
  245. self.cur_pos = None
  246. def can_overlap(self):
  247. """Can the agent overlap with this?"""
  248. return False
  249. def can_pickup(self):
  250. """Can the agent pick this up?"""
  251. return False
  252. def can_contain(self):
  253. """Can this contain another object?"""
  254. return False
  255. def see_behind(self):
  256. """Can the agent see behind this object?"""
  257. return True
  258. def toggle(self, env, pos):
  259. """Method to trigger/toggle an action this object performs"""
  260. return False
  261. def encode(self):
  262. """Encode the a description of this object as a 3-tuple of integers"""
  263. return (OBJECT_TO_IDX[self.type], COLOR_TO_IDX[self.color], 0)
  264. @staticmethod
  265. def decode(type_idx, color_idx, state):
  266. """Create an object from a 3-tuple state description"""
  267. obj_type = IDX_TO_OBJECT[type_idx]
  268. color = IDX_TO_COLOR[color_idx]
  269. if obj_type == "empty" or obj_type == "unseen":
  270. return None
  271. # State, 0: open, 1: closed, 2: locked
  272. is_open = state == 0
  273. is_locked = state == 2
  274. if obj_type == "wall":
  275. v = Wall(color)
  276. elif obj_type == "floor":
  277. v = Floor(color)
  278. elif obj_type == "ball":
  279. v = Ball(color)
  280. elif obj_type == "key":
  281. v = Key(color)
  282. elif obj_type == "box":
  283. v = Box(color)
  284. elif obj_type == "door":
  285. v = Door(color, is_open, is_locked)
  286. elif obj_type == "goal":
  287. v = Goal()
  288. elif obj_type == "lava":
  289. v = Lava()
  290. else:
  291. assert False, "unknown object type in decode '%s'" % obj_type
  292. return v
  293. def render(self, r):
  294. """Draw this object with the given renderer"""
  295. raise NotImplementedError
  296. class Goal(WorldObj):
  297. def __init__(self):
  298. super().__init__("goal", "green")
  299. def can_overlap(self):
  300. return True
  301. def render(self, img):
  302. fill_coords(img, point_in_rect(0, 1, 0, 1), COLORS[self.color])
  303. class Floor(WorldObj):
  304. """
  305. Colored floor tile the agent can walk over
  306. """
  307. def __init__(self, color="blue"):
  308. super().__init__("floor", color)
  309. def can_overlap(self):
  310. return True
  311. def render(self, img):
  312. # Give the floor a pale color
  313. color = COLORS[self.color] / 2
  314. fill_coords(img, point_in_rect(0.031, 1, 0.031, 1), color)
  315. class Lava(WorldObj):
  316. def __init__(self):
  317. super().__init__("lava", "red")
  318. def can_overlap(self):
  319. return True
  320. def render(self, img):
  321. c = (255, 128, 0)
  322. # Background color
  323. fill_coords(img, point_in_rect(0, 1, 0, 1), c)
  324. # Little waves
  325. for i in range(3):
  326. ylo = 0.3 + 0.2 * i
  327. yhi = 0.4 + 0.2 * i
  328. fill_coords(img, point_in_line(0.1, ylo, 0.3, yhi, r=0.03), (0, 0, 0))
  329. fill_coords(img, point_in_line(0.3, yhi, 0.5, ylo, r=0.03), (0, 0, 0))
  330. fill_coords(img, point_in_line(0.5, ylo, 0.7, yhi, r=0.03), (0, 0, 0))
  331. fill_coords(img, point_in_line(0.7, yhi, 0.9, ylo, r=0.03), (0, 0, 0))
  332. class Wall(WorldObj):
  333. def __init__(self, color="grey"):
  334. super().__init__("wall", color)
  335. def see_behind(self):
  336. return False
  337. def render(self, img):
  338. fill_coords(img, point_in_rect(0, 1, 0, 1), COLORS[self.color])
  339. class Door(WorldObj):
  340. def __init__(self, color, is_open=False, is_locked=False):
  341. super().__init__("door", color)
  342. self.is_open = is_open
  343. self.is_locked = is_locked
  344. def can_overlap(self):
  345. """The agent can only walk over this cell when the door is open"""
  346. return self.is_open
  347. def see_behind(self):
  348. return self.is_open
  349. def toggle(self, env, pos):
  350. # If the player has the right key to open the door
  351. if self.is_locked:
  352. if isinstance(env.carrying, Key) and env.carrying.color == self.color:
  353. self.is_locked = False
  354. self.is_open = True
  355. return True
  356. return False
  357. self.is_open = not self.is_open
  358. return True
  359. def encode(self):
  360. """Encode the a description of this object as a 3-tuple of integers"""
  361. # State, 0: open, 1: closed, 2: locked
  362. if self.is_open:
  363. state = 0
  364. elif self.is_locked:
  365. state = 2
  366. # if door is closed and unlocked
  367. elif not self.is_open:
  368. state = 1
  369. else:
  370. raise ValueError(
  371. f"There is no possible state encoding for the state:\n -Door Open: {self.is_open}\n -Door Closed: {not self.is_open}\n -Door Locked: {self.is_locked}"
  372. )
  373. return (OBJECT_TO_IDX[self.type], COLOR_TO_IDX[self.color], state)
  374. def render(self, img):
  375. c = COLORS[self.color]
  376. if self.is_open:
  377. fill_coords(img, point_in_rect(0.88, 1.00, 0.00, 1.00), c)
  378. fill_coords(img, point_in_rect(0.92, 0.96, 0.04, 0.96), (0, 0, 0))
  379. return
  380. # Door frame and door
  381. if self.is_locked:
  382. fill_coords(img, point_in_rect(0.00, 1.00, 0.00, 1.00), c)
  383. fill_coords(img, point_in_rect(0.06, 0.94, 0.06, 0.94), 0.45 * np.array(c))
  384. # Draw key slot
  385. fill_coords(img, point_in_rect(0.52, 0.75, 0.50, 0.56), c)
  386. else:
  387. fill_coords(img, point_in_rect(0.00, 1.00, 0.00, 1.00), c)
  388. fill_coords(img, point_in_rect(0.04, 0.96, 0.04, 0.96), (0, 0, 0))
  389. fill_coords(img, point_in_rect(0.08, 0.92, 0.08, 0.92), c)
  390. fill_coords(img, point_in_rect(0.12, 0.88, 0.12, 0.88), (0, 0, 0))
  391. # Draw door handle
  392. fill_coords(img, point_in_circle(cx=0.75, cy=0.50, r=0.08), c)
  393. class Key(WorldObj):
  394. def __init__(self, color="blue"):
  395. super().__init__("key", color)
  396. def can_pickup(self):
  397. return True
  398. def render(self, img):
  399. c = COLORS[self.color]
  400. # Vertical quad
  401. fill_coords(img, point_in_rect(0.50, 0.63, 0.31, 0.88), c)
  402. # Teeth
  403. fill_coords(img, point_in_rect(0.38, 0.50, 0.59, 0.66), c)
  404. fill_coords(img, point_in_rect(0.38, 0.50, 0.81, 0.88), c)
  405. # Ring
  406. fill_coords(img, point_in_circle(cx=0.56, cy=0.28, r=0.190), c)
  407. fill_coords(img, point_in_circle(cx=0.56, cy=0.28, r=0.064), (0, 0, 0))
  408. class Ball(WorldObj):
  409. def __init__(self, color="blue"):
  410. super().__init__("ball", color)
  411. def can_pickup(self):
  412. return True
  413. def render(self, img):
  414. fill_coords(img, point_in_circle(0.5, 0.5, 0.31), COLORS[self.color])
  415. class Box(WorldObj):
  416. def __init__(self, color, contains=None):
  417. super().__init__("box", color)
  418. self.contains = contains
  419. def can_pickup(self):
  420. return True
  421. def render(self, img):
  422. c = COLORS[self.color]
  423. # Outline
  424. fill_coords(img, point_in_rect(0.12, 0.88, 0.12, 0.88), c)
  425. fill_coords(img, point_in_rect(0.18, 0.82, 0.18, 0.82), (0, 0, 0))
  426. # Horizontal slit
  427. fill_coords(img, point_in_rect(0.16, 0.84, 0.47, 0.53), c)
  428. def toggle(self, env, pos):
  429. # Replace the box by its contents
  430. env.grid.set(pos[0], pos[1], self.contains)
  431. return True
  432. class Grid:
  433. """
  434. Represent a grid and operations on it
  435. """
  436. # Static cache of pre-renderer tiles
  437. tile_cache = {}
  438. def __init__(self, width, height):
  439. assert width >= 3
  440. assert height >= 3
  441. self.width = width
  442. self.height = height
  443. self.grid = [None] * width * height
  444. def __contains__(self, key):
  445. if isinstance(key, WorldObj):
  446. for e in self.grid:
  447. if e is key:
  448. return True
  449. elif isinstance(key, tuple):
  450. for e in self.grid:
  451. if e is None:
  452. continue
  453. if (e.color, e.type) == key:
  454. return True
  455. if key[0] is None and key[1] == e.type:
  456. return True
  457. return False
  458. def __eq__(self, other):
  459. grid1 = self.encode()
  460. grid2 = other.encode()
  461. return np.array_equal(grid2, grid1)
  462. def __ne__(self, other):
  463. return not self == other
  464. def copy(self):
  465. from copy import deepcopy
  466. return deepcopy(self)
  467. def set(self, i, j, v):
  468. assert i >= 0 and i < self.width
  469. assert j >= 0 and j < self.height
  470. self.grid[j * self.width + i] = v
  471. def get(self, i, j):
  472. assert i >= 0 and i < self.width
  473. assert j >= 0 and j < self.height
  474. return self.grid[j * self.width + i]
  475. def horz_wall(self, x, y, length=None, obj_type=Wall):
  476. if length is None:
  477. length = self.width - x
  478. for i in range(0, length):
  479. self.set(x + i, y, obj_type())
  480. def vert_wall(self, x, y, length=None, obj_type=Wall):
  481. if length is None:
  482. length = self.height - y
  483. for j in range(0, length):
  484. self.set(x, y + j, obj_type())
  485. def wall_rect(self, x, y, w, h):
  486. self.horz_wall(x, y, w)
  487. self.horz_wall(x, y + h - 1, w)
  488. self.vert_wall(x, y, h)
  489. self.vert_wall(x + w - 1, y, h)
  490. def rotate_left(self):
  491. """
  492. Rotate the grid to the left (counter-clockwise)
  493. """
  494. grid = Grid(self.height, self.width)
  495. for i in range(self.width):
  496. for j in range(self.height):
  497. v = self.get(i, j)
  498. grid.set(j, grid.height - 1 - i, v)
  499. return grid
  500. def slice(self, topX, topY, width, height):
  501. """
  502. Get a subset of the grid
  503. """
  504. grid = Grid(width, height)
  505. for j in range(0, height):
  506. for i in range(0, width):
  507. x = topX + i
  508. y = topY + j
  509. if x >= 0 and x < self.width and y >= 0 and y < self.height:
  510. v = self.get(x, y)
  511. else:
  512. v = Wall()
  513. grid.set(i, j, v)
  514. return grid
  515. @classmethod
  516. def render_tile(
  517. cls, obj, agent_dir=None, highlight=False, tile_size=TILE_PIXELS, subdivs=3
  518. ):
  519. """
  520. Render a tile and cache the result
  521. """
  522. # Hash map lookup key for the cache
  523. key = (agent_dir, highlight, tile_size)
  524. key = obj.encode() + key if obj else key
  525. if key in cls.tile_cache:
  526. return cls.tile_cache[key]
  527. img = np.zeros(
  528. shape=(tile_size * subdivs, tile_size * subdivs, 3), dtype=np.uint8
  529. )
  530. # Draw the grid lines (top and left edges)
  531. fill_coords(img, point_in_rect(0, 0.031, 0, 1), (100, 100, 100))
  532. fill_coords(img, point_in_rect(0, 1, 0, 0.031), (100, 100, 100))
  533. if obj is not None:
  534. obj.render(img)
  535. # Overlay the agent on top
  536. if agent_dir is not None:
  537. tri_fn = point_in_triangle(
  538. (0.12, 0.19),
  539. (0.87, 0.50),
  540. (0.12, 0.81),
  541. )
  542. # Rotate the agent based on its direction
  543. tri_fn = rotate_fn(tri_fn, cx=0.5, cy=0.5, theta=0.5 * math.pi * agent_dir)
  544. fill_coords(img, tri_fn, (255, 0, 0))
  545. # Highlight the cell if needed
  546. if highlight:
  547. highlight_img(img)
  548. # Downsample the image to perform supersampling/anti-aliasing
  549. img = downsample(img, subdivs)
  550. # Cache the rendered tile
  551. cls.tile_cache[key] = img
  552. return img
  553. def render(self, tile_size, agent_pos, agent_dir=None, highlight_mask=None):
  554. """
  555. Render this grid at a given scale
  556. :param r: target renderer object
  557. :param tile_size: tile size in pixels
  558. """
  559. if highlight_mask is None:
  560. highlight_mask = np.zeros(shape=(self.width, self.height), dtype=bool)
  561. # Compute the total grid size
  562. width_px = self.width * tile_size
  563. height_px = self.height * tile_size
  564. img = np.zeros(shape=(height_px, width_px, 3), dtype=np.uint8)
  565. # Render the grid
  566. for j in range(0, self.height):
  567. for i in range(0, self.width):
  568. cell = self.get(i, j)
  569. agent_here = np.array_equal(agent_pos, (i, j))
  570. tile_img = Grid.render_tile(
  571. cell,
  572. agent_dir=agent_dir if agent_here else None,
  573. highlight=highlight_mask[i, j],
  574. tile_size=tile_size,
  575. )
  576. ymin = j * tile_size
  577. ymax = (j + 1) * tile_size
  578. xmin = i * tile_size
  579. xmax = (i + 1) * tile_size
  580. img[ymin:ymax, xmin:xmax, :] = tile_img
  581. return img
  582. def encode(self, vis_mask=None):
  583. """
  584. Produce a compact numpy encoding of the grid
  585. """
  586. if vis_mask is None:
  587. vis_mask = np.ones((self.width, self.height), dtype=bool)
  588. array = np.zeros((self.width, self.height, 3), dtype="uint8")
  589. for i in range(self.width):
  590. for j in range(self.height):
  591. if vis_mask[i, j]:
  592. v = self.get(i, j)
  593. if v is None:
  594. array[i, j, 0] = OBJECT_TO_IDX["empty"]
  595. array[i, j, 1] = 0
  596. array[i, j, 2] = 0
  597. else:
  598. array[i, j, :] = v.encode()
  599. return array
  600. @staticmethod
  601. def decode(array):
  602. """
  603. Decode an array grid encoding back into a grid
  604. """
  605. width, height, channels = array.shape
  606. assert channels == 3
  607. vis_mask = np.ones(shape=(width, height), dtype=bool)
  608. grid = Grid(width, height)
  609. for i in range(width):
  610. for j in range(height):
  611. type_idx, color_idx, state = array[i, j]
  612. v = WorldObj.decode(type_idx, color_idx, state)
  613. grid.set(i, j, v)
  614. vis_mask[i, j] = type_idx != OBJECT_TO_IDX["unseen"]
  615. return grid, vis_mask
  616. def process_vis(self, agent_pos):
  617. mask = np.zeros(shape=(self.width, self.height), dtype=bool)
  618. mask[agent_pos[0], agent_pos[1]] = True
  619. for j in reversed(range(0, self.height)):
  620. for i in range(0, self.width - 1):
  621. if not mask[i, j]:
  622. continue
  623. cell = self.get(i, j)
  624. if cell and not cell.see_behind():
  625. continue
  626. mask[i + 1, j] = True
  627. if j > 0:
  628. mask[i + 1, j - 1] = True
  629. mask[i, j - 1] = True
  630. for i in reversed(range(1, self.width)):
  631. if not mask[i, j]:
  632. continue
  633. cell = self.get(i, j)
  634. if cell and not cell.see_behind():
  635. continue
  636. mask[i - 1, j] = True
  637. if j > 0:
  638. mask[i - 1, j - 1] = True
  639. mask[i, j - 1] = True
  640. for j in range(0, self.height):
  641. for i in range(0, self.width):
  642. if not mask[i, j]:
  643. self.set(i, j, None)
  644. return mask
  645. class MiniGridEnv(gym.Env):
  646. """
  647. 2D grid world game environment
  648. """
  649. metadata = {
  650. # Deprecated: use 'render_modes' instead
  651. "render.modes": ["human", "rgb_array"],
  652. "video.frames_per_second": 10, # Deprecated: use 'render_fps' instead
  653. "render_modes": ["human", "rgb_array", "single_rgb_array"],
  654. "render_fps": 10,
  655. }
  656. # Enumeration of possible actions
  657. class Actions(IntEnum):
  658. # Turn left, turn right, move forward
  659. left = 0
  660. right = 1
  661. forward = 2
  662. # Pick up an object
  663. pickup = 3
  664. # Drop an object
  665. drop = 4
  666. # Toggle/activate an object
  667. toggle = 5
  668. # Done completing task
  669. done = 6
  670. def __init__(
  671. self,
  672. mission_space: MissionSpace,
  673. grid_size: int = None,
  674. width: int = None,
  675. height: int = None,
  676. max_steps: int = 100,
  677. see_through_walls: bool = False,
  678. agent_view_size: int = 7,
  679. render_mode: Optional[str] = None,
  680. highlight: bool = True,
  681. tile_size: int = TILE_PIXELS,
  682. agent_pov: bool = False,
  683. **kwargs,
  684. ):
  685. # Rendering attributes
  686. self.render_mode = render_mode
  687. self.highlight = highlight
  688. self.tile_size = tile_size
  689. self.agent_pov = agent_pov
  690. # Initialize mission
  691. self.mission = mission_space.sample()
  692. # Can't set both grid_size and width/height
  693. if grid_size:
  694. assert width is None and height is None
  695. width = grid_size
  696. height = grid_size
  697. # Action enumeration for this environment
  698. self.actions = MiniGridEnv.Actions
  699. # Actions are discrete integer values
  700. self.action_space = spaces.Discrete(len(self.actions))
  701. # Number of cells (width and height) in the agent view
  702. assert agent_view_size % 2 == 1
  703. assert agent_view_size >= 3
  704. self.agent_view_size = agent_view_size
  705. # Observations are dictionaries containing an
  706. # encoding of the grid and a textual 'mission' string
  707. image_observation_space = spaces.Box(
  708. low=0,
  709. high=255,
  710. shape=(self.agent_view_size, self.agent_view_size, 3),
  711. dtype="uint8",
  712. )
  713. self.observation_space = spaces.Dict(
  714. {
  715. "image": image_observation_space,
  716. "direction": spaces.Discrete(4),
  717. "mission": mission_space,
  718. }
  719. )
  720. # Range of possible rewards
  721. self.reward_range = (0, 1)
  722. self.window: Window = None
  723. # Environment configuration
  724. self.width = width
  725. self.height = height
  726. self.max_steps = max_steps
  727. self.see_through_walls = see_through_walls
  728. # Current position and direction of the agent
  729. self.agent_pos: np.ndarray = None
  730. self.agent_dir: int = None
  731. # Current grid and mission and carryinh
  732. self.grid = Grid(width, height)
  733. self.carrying = None
  734. # Initialize the state
  735. self.reset()
  736. def reset(self, *, seed=None, return_info=False, options=None):
  737. super().reset(seed=seed)
  738. # Reinitialize episode-specific variables
  739. self.agent_pos = (-1, -1)
  740. self.agent_dir = -1
  741. # Generate a new random grid at the start of each episode
  742. self._gen_grid(self.width, self.height)
  743. # These fields should be defined by _gen_grid
  744. assert (
  745. self.agent_pos >= (0, 0)
  746. if isinstance(self.agent_pos, tuple)
  747. else all(self.agent_pos >= 0) and self.agent_dir >= 0
  748. )
  749. # Check that the agent doesn't overlap with an object
  750. start_cell = self.grid.get(*self.agent_pos)
  751. assert start_cell is None or start_cell.can_overlap()
  752. # Item picked up, being carried, initially nothing
  753. self.carrying = None
  754. # Step count since episode start
  755. self.step_count = 0
  756. # Return first observation
  757. obs = self.gen_obs()
  758. if not return_info:
  759. return obs
  760. else:
  761. return obs, {}
  762. def hash(self, size=16):
  763. """Compute a hash that uniquely identifies the current state of the environment.
  764. :param size: Size of the hashing
  765. """
  766. sample_hash = hashlib.sha256()
  767. to_encode = [self.grid.encode().tolist(), self.agent_pos, self.agent_dir]
  768. for item in to_encode:
  769. sample_hash.update(str(item).encode("utf8"))
  770. return sample_hash.hexdigest()[:size]
  771. @property
  772. def steps_remaining(self):
  773. return self.max_steps - self.step_count
  774. def __str__(self):
  775. """
  776. Produce a pretty string of the environment's grid along with the agent.
  777. A grid cell is represented by 2-character string, the first one for
  778. the object and the second one for the color.
  779. """
  780. # Map of object types to short string
  781. OBJECT_TO_STR = {
  782. "wall": "W",
  783. "floor": "F",
  784. "door": "D",
  785. "key": "K",
  786. "ball": "A",
  787. "box": "B",
  788. "goal": "G",
  789. "lava": "V",
  790. }
  791. # Map agent's direction to short string
  792. AGENT_DIR_TO_STR = {0: ">", 1: "V", 2: "<", 3: "^"}
  793. str = ""
  794. for j in range(self.grid.height):
  795. for i in range(self.grid.width):
  796. if i == self.agent_pos[0] and j == self.agent_pos[1]:
  797. str += 2 * AGENT_DIR_TO_STR[self.agent_dir]
  798. continue
  799. c = self.grid.get(i, j)
  800. if c is None:
  801. str += " "
  802. continue
  803. if c.type == "door":
  804. if c.is_open:
  805. str += "__"
  806. elif c.is_locked:
  807. str += "L" + c.color[0].upper()
  808. else:
  809. str += "D" + c.color[0].upper()
  810. continue
  811. str += OBJECT_TO_STR[c.type] + c.color[0].upper()
  812. if j < self.grid.height - 1:
  813. str += "\n"
  814. return str
  815. @abstractmethod
  816. def _gen_grid(self, width, height):
  817. pass
  818. def _reward(self):
  819. """
  820. Compute the reward to be given upon success
  821. """
  822. return 1 - 0.9 * (self.step_count / self.max_steps)
  823. def _rand_int(self, low, high):
  824. """
  825. Generate random integer in [low,high[
  826. """
  827. return self.np_random.integers(low, high)
  828. def _rand_float(self, low, high):
  829. """
  830. Generate random float in [low,high[
  831. """
  832. return self.np_random.uniform(low, high)
  833. def _rand_bool(self):
  834. """
  835. Generate random boolean value
  836. """
  837. return self.np_random.integers(0, 2) == 0
  838. def _rand_elem(self, iterable):
  839. """
  840. Pick a random element in a list
  841. """
  842. lst = list(iterable)
  843. idx = self._rand_int(0, len(lst))
  844. return lst[idx]
  845. def _rand_subset(self, iterable, num_elems):
  846. """
  847. Sample a random subset of distinct elements of a list
  848. """
  849. lst = list(iterable)
  850. assert num_elems <= len(lst)
  851. out = []
  852. while len(out) < num_elems:
  853. elem = self._rand_elem(lst)
  854. lst.remove(elem)
  855. out.append(elem)
  856. return out
  857. def _rand_color(self):
  858. """
  859. Generate a random color name (string)
  860. """
  861. return self._rand_elem(COLOR_NAMES)
  862. def _rand_pos(self, xLow, xHigh, yLow, yHigh):
  863. """
  864. Generate a random (x,y) position tuple
  865. """
  866. return (
  867. self.np_random.integers(xLow, xHigh),
  868. self.np_random.integers(yLow, yHigh),
  869. )
  870. def place_obj(self, obj, top=None, size=None, reject_fn=None, max_tries=math.inf):
  871. """
  872. Place an object at an empty position in the grid
  873. :param top: top-left position of the rectangle where to place
  874. :param size: size of the rectangle where to place
  875. :param reject_fn: function to filter out potential positions
  876. """
  877. if top is None:
  878. top = (0, 0)
  879. else:
  880. top = (max(top[0], 0), max(top[1], 0))
  881. if size is None:
  882. size = (self.grid.width, self.grid.height)
  883. num_tries = 0
  884. while True:
  885. # This is to handle with rare cases where rejection sampling
  886. # gets stuck in an infinite loop
  887. if num_tries > max_tries:
  888. raise RecursionError("rejection sampling failed in place_obj")
  889. num_tries += 1
  890. pos = np.array(
  891. (
  892. self._rand_int(top[0], min(top[0] + size[0], self.grid.width)),
  893. self._rand_int(top[1], min(top[1] + size[1], self.grid.height)),
  894. )
  895. )
  896. pos = tuple(pos)
  897. # Don't place the object on top of another object
  898. if self.grid.get(*pos) is not None:
  899. continue
  900. # Don't place the object where the agent is
  901. if np.array_equal(pos, self.agent_pos):
  902. continue
  903. # Check if there is a filtering criterion
  904. if reject_fn and reject_fn(self, pos):
  905. continue
  906. break
  907. self.grid.set(pos[0], pos[1], obj)
  908. if obj is not None:
  909. obj.init_pos = pos
  910. obj.cur_pos = pos
  911. return pos
  912. def put_obj(self, obj, i, j):
  913. """
  914. Put an object at a specific position in the grid
  915. """
  916. self.grid.set(i, j, obj)
  917. obj.init_pos = (i, j)
  918. obj.cur_pos = (i, j)
  919. def place_agent(self, top=None, size=None, rand_dir=True, max_tries=math.inf):
  920. """
  921. Set the agent's starting point at an empty position in the grid
  922. """
  923. self.agent_pos = (-1, -1)
  924. pos = self.place_obj(None, top, size, max_tries=max_tries)
  925. self.agent_pos = pos
  926. if rand_dir:
  927. self.agent_dir = self._rand_int(0, 4)
  928. return pos
  929. @property
  930. def dir_vec(self):
  931. """
  932. Get the direction vector for the agent, pointing in the direction
  933. of forward movement.
  934. """
  935. assert self.agent_dir >= 0 and self.agent_dir < 4
  936. return DIR_TO_VEC[self.agent_dir]
  937. @property
  938. def right_vec(self):
  939. """
  940. Get the vector pointing to the right of the agent.
  941. """
  942. dx, dy = self.dir_vec
  943. return np.array((-dy, dx))
  944. @property
  945. def front_pos(self):
  946. """
  947. Get the position of the cell that is right in front of the agent
  948. """
  949. return self.agent_pos + self.dir_vec
  950. def get_view_coords(self, i, j):
  951. """
  952. Translate and rotate absolute grid coordinates (i, j) into the
  953. agent's partially observable view (sub-grid). Note that the resulting
  954. coordinates may be negative or outside of the agent's view size.
  955. """
  956. ax, ay = self.agent_pos
  957. dx, dy = self.dir_vec
  958. rx, ry = self.right_vec
  959. # Compute the absolute coordinates of the top-left view corner
  960. sz = self.agent_view_size
  961. hs = self.agent_view_size // 2
  962. tx = ax + (dx * (sz - 1)) - (rx * hs)
  963. ty = ay + (dy * (sz - 1)) - (ry * hs)
  964. lx = i - tx
  965. ly = j - ty
  966. # Project the coordinates of the object relative to the top-left
  967. # corner onto the agent's own coordinate system
  968. vx = rx * lx + ry * ly
  969. vy = -(dx * lx + dy * ly)
  970. return vx, vy
  971. def get_view_exts(self, agent_view_size=None):
  972. """
  973. Get the extents of the square set of tiles visible to the agent
  974. Note: the bottom extent indices are not included in the set
  975. if agent_view_size is None, use self.agent_view_size
  976. """
  977. agent_view_size = agent_view_size or self.agent_view_size
  978. # Facing right
  979. if self.agent_dir == 0:
  980. topX = self.agent_pos[0]
  981. topY = self.agent_pos[1] - agent_view_size // 2
  982. # Facing down
  983. elif self.agent_dir == 1:
  984. topX = self.agent_pos[0] - agent_view_size // 2
  985. topY = self.agent_pos[1]
  986. # Facing left
  987. elif self.agent_dir == 2:
  988. topX = self.agent_pos[0] - agent_view_size + 1
  989. topY = self.agent_pos[1] - agent_view_size // 2
  990. # Facing up
  991. elif self.agent_dir == 3:
  992. topX = self.agent_pos[0] - agent_view_size // 2
  993. topY = self.agent_pos[1] - agent_view_size + 1
  994. else:
  995. assert False, "invalid agent direction"
  996. botX = topX + agent_view_size
  997. botY = topY + agent_view_size
  998. return (topX, topY, botX, botY)
  999. def relative_coords(self, x, y):
  1000. """
  1001. Check if a grid position belongs to the agent's field of view, and returns the corresponding coordinates
  1002. """
  1003. vx, vy = self.get_view_coords(x, y)
  1004. if vx < 0 or vy < 0 or vx >= self.agent_view_size or vy >= self.agent_view_size:
  1005. return None
  1006. return vx, vy
  1007. def in_view(self, x, y):
  1008. """
  1009. check if a grid position is visible to the agent
  1010. """
  1011. return self.relative_coords(x, y) is not None
  1012. def agent_sees(self, x, y):
  1013. """
  1014. Check if a non-empty grid position is visible to the agent
  1015. """
  1016. coordinates = self.relative_coords(x, y)
  1017. if coordinates is None:
  1018. return False
  1019. vx, vy = coordinates
  1020. obs = self.gen_obs()
  1021. obs_grid, _ = Grid.decode(obs["image"])
  1022. obs_cell = obs_grid.get(vx, vy)
  1023. world_cell = self.grid.get(x, y)
  1024. assert world_cell is not None
  1025. return obs_cell is not None and obs_cell.type == world_cell.type
  1026. def step(self, action):
  1027. self.step_count += 1
  1028. reward = 0
  1029. terminated = False
  1030. truncated = False
  1031. # Get the position in front of the agent
  1032. fwd_pos = self.front_pos
  1033. # Get the contents of the cell in front of the agent
  1034. fwd_cell = self.grid.get(*fwd_pos)
  1035. # Rotate left
  1036. if action == self.actions.left:
  1037. self.agent_dir -= 1
  1038. if self.agent_dir < 0:
  1039. self.agent_dir += 4
  1040. # Rotate right
  1041. elif action == self.actions.right:
  1042. self.agent_dir = (self.agent_dir + 1) % 4
  1043. # Move forward
  1044. elif action == self.actions.forward:
  1045. if fwd_cell is None or fwd_cell.can_overlap():
  1046. self.agent_pos = tuple(fwd_pos)
  1047. if fwd_cell is not None and fwd_cell.type == "goal":
  1048. terminated = True
  1049. reward = self._reward()
  1050. if fwd_cell is not None and fwd_cell.type == "lava":
  1051. terminated = True
  1052. # Pick up an object
  1053. elif action == self.actions.pickup:
  1054. if fwd_cell and fwd_cell.can_pickup():
  1055. if self.carrying is None:
  1056. self.carrying = fwd_cell
  1057. self.carrying.cur_pos = np.array([-1, -1])
  1058. self.grid.set(fwd_pos[0], fwd_pos[1], None)
  1059. # Drop an object
  1060. elif action == self.actions.drop:
  1061. if not fwd_cell and self.carrying:
  1062. self.grid.set(fwd_pos[0], fwd_pos[1], self.carrying)
  1063. self.carrying.cur_pos = fwd_pos
  1064. self.carrying = None
  1065. # Toggle/activate an object
  1066. elif action == self.actions.toggle:
  1067. if fwd_cell:
  1068. fwd_cell.toggle(self, fwd_pos)
  1069. # Done action (not used by default)
  1070. elif action == self.actions.done:
  1071. pass
  1072. else:
  1073. raise ValueError(f"Unknown action: {action}")
  1074. if self.step_count >= self.max_steps:
  1075. truncated = True
  1076. obs = self.gen_obs()
  1077. return obs, reward, terminated, truncated, {}
  1078. def gen_obs_grid(self, agent_view_size=None):
  1079. """
  1080. Generate the sub-grid observed by the agent.
  1081. This method also outputs a visibility mask telling us which grid
  1082. cells the agent can actually see.
  1083. if agent_view_size is None, self.agent_view_size is used
  1084. """
  1085. topX, topY, botX, botY = self.get_view_exts(agent_view_size)
  1086. agent_view_size = agent_view_size or self.agent_view_size
  1087. grid = self.grid.slice(topX, topY, agent_view_size, agent_view_size)
  1088. for i in range(self.agent_dir + 1):
  1089. grid = grid.rotate_left()
  1090. # Process occluders and visibility
  1091. # Note that this incurs some performance cost
  1092. if not self.see_through_walls:
  1093. vis_mask = grid.process_vis(
  1094. agent_pos=(agent_view_size // 2, agent_view_size - 1)
  1095. )
  1096. else:
  1097. vis_mask = np.ones(shape=(grid.width, grid.height), dtype=bool)
  1098. # Make it so the agent sees what it's carrying
  1099. # We do this by placing the carried object at the agent's position
  1100. # in the agent's partially observable view
  1101. agent_pos = grid.width // 2, grid.height - 1
  1102. if self.carrying:
  1103. grid.set(*agent_pos, self.carrying)
  1104. else:
  1105. grid.set(*agent_pos, None)
  1106. return grid, vis_mask
  1107. def gen_obs(self):
  1108. """
  1109. Generate the agent's view (partially observable, low-resolution encoding)
  1110. """
  1111. grid, vis_mask = self.gen_obs_grid()
  1112. # Encode the partially observable view into a numpy array
  1113. image = grid.encode(vis_mask)
  1114. # Observations are dictionaries containing:
  1115. # - an image (partially observable view of the environment)
  1116. # - the agent's direction/orientation (acting as a compass)
  1117. # - a textual mission string (instructions for the agent)
  1118. obs = {"image": image, "direction": self.agent_dir, "mission": self.mission}
  1119. return obs
  1120. def get_pov_render(self):
  1121. """
  1122. Render an agent's POV observation for visualization
  1123. """
  1124. grid, vis_mask = self.gen_obs_grid()
  1125. # Render the whole grid
  1126. img = grid.render(
  1127. self.tile_size,
  1128. agent_pos=(self.agent_view_size // 2, self.agent_view_size - 1),
  1129. agent_dir=3,
  1130. highlight_mask=vis_mask,
  1131. )
  1132. return img
  1133. def get_full_render(self):
  1134. """
  1135. Render a non-paratial observation for visualization
  1136. """
  1137. tile_size = self.tile_size
  1138. highlight = self.highlight
  1139. # Compute which cells are visible to the agent
  1140. _, vis_mask = self.gen_obs_grid()
  1141. # Compute the world coordinates of the bottom-left corner
  1142. # of the agent's view area
  1143. f_vec = self.dir_vec
  1144. r_vec = self.right_vec
  1145. top_left = (
  1146. self.agent_pos
  1147. + f_vec * (self.agent_view_size - 1)
  1148. - r_vec * (self.agent_view_size // 2)
  1149. )
  1150. # Mask of which cells to highlight
  1151. highlight_mask = np.zeros(shape=(self.width, self.height), dtype=bool)
  1152. # For each cell in the visibility mask
  1153. for vis_j in range(0, self.agent_view_size):
  1154. for vis_i in range(0, self.agent_view_size):
  1155. # If this cell is not visible, don't highlight it
  1156. if not vis_mask[vis_i, vis_j]:
  1157. continue
  1158. # Compute the world coordinates of this cell
  1159. abs_i, abs_j = top_left - (f_vec * vis_j) + (r_vec * vis_i)
  1160. if abs_i < 0 or abs_i >= self.width:
  1161. continue
  1162. if abs_j < 0 or abs_j >= self.height:
  1163. continue
  1164. # Mark this cell to be highlighted
  1165. highlight_mask[abs_i, abs_j] = True
  1166. # Render the whole grid
  1167. img = self.grid.render(
  1168. tile_size,
  1169. self.agent_pos,
  1170. self.agent_dir,
  1171. highlight_mask=highlight_mask if highlight else None,
  1172. )
  1173. return img
  1174. def get_render(self):
  1175. "Returns an image corresponding to the whole environment or the agent's pov"
  1176. if self.agent_pov:
  1177. return self.get_pov_render()
  1178. else:
  1179. return self.get_full_render()
  1180. def render(self):
  1181. """
  1182. Render the whole-grid human view
  1183. """
  1184. img = self.get_render()
  1185. mode = self.render_mode
  1186. if mode == "human":
  1187. if self.window is None:
  1188. self.window = Window("gym_minigrid")
  1189. self.window.show(block=False)
  1190. self.window.set_caption(self.mission)
  1191. self.window.show_img(img)
  1192. else:
  1193. return img
  1194. def close(self):
  1195. if self.window:
  1196. self.window.close()