minigrid.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. import math
  2. import gym
  3. from enum import IntEnum
  4. import numpy as np
  5. from gym import error, spaces, utils
  6. from gym.utils import seeding
  7. # Size in pixels of a cell in the full-scale human view
  8. CELL_PIXELS = 32
  9. # Number of cells (width and height) in the agent view
  10. AGENT_VIEW_SIZE = 7
  11. # Size of the array given as an observation to the agent
  12. OBS_ARRAY_SIZE = (AGENT_VIEW_SIZE, AGENT_VIEW_SIZE, 3)
  13. # Map of color names to RGB values
  14. COLORS = {
  15. 'red' : np.array([255, 0, 0]),
  16. 'green' : np.array([0, 255, 0]),
  17. 'blue' : np.array([0, 0, 255]),
  18. 'purple': np.array([112, 39, 195]),
  19. 'yellow': np.array([255, 255, 0]),
  20. 'grey' : np.array([100, 100, 100])
  21. }
  22. COLOR_NAMES = sorted(list(COLORS.keys()))
  23. # Used to map colors to integers
  24. COLOR_TO_IDX = {
  25. 'red' : 0,
  26. 'green' : 1,
  27. 'blue' : 2,
  28. 'purple': 3,
  29. 'yellow': 4,
  30. 'grey' : 5
  31. }
  32. IDX_TO_COLOR = dict(zip(COLOR_TO_IDX.values(), COLOR_TO_IDX.keys()))
  33. # Map of object type to integers
  34. OBJECT_TO_IDX = {
  35. 'unseen' : 0,
  36. 'empty' : 1,
  37. 'wall' : 2,
  38. 'floor' : 3,
  39. 'door' : 4,
  40. 'key' : 5,
  41. 'ball' : 6,
  42. 'box' : 7,
  43. 'goal' : 8,
  44. 'lava' : 9
  45. }
  46. IDX_TO_OBJECT = dict(zip(OBJECT_TO_IDX.values(), OBJECT_TO_IDX.keys()))
  47. # Map of agent direction indices to vectors
  48. DIR_TO_VEC = [
  49. # Pointing right (positive X)
  50. np.array((1, 0)),
  51. # Down (positive Y)
  52. np.array((0, 1)),
  53. # Pointing left (negative X)
  54. np.array((-1, 0)),
  55. # Up (negative Y)
  56. np.array((0, -1)),
  57. ]
  58. class WorldObj:
  59. """
  60. Base class for grid world objects
  61. """
  62. def __init__(self, type, color):
  63. assert type in OBJECT_TO_IDX, type
  64. assert color in COLOR_TO_IDX, color
  65. self.type = type
  66. self.color = color
  67. self.contains = None
  68. # Initial position of the object
  69. self.init_pos = None
  70. # Current position of the object
  71. self.cur_pos = None
  72. def can_overlap(self):
  73. """Can the agent overlap with this?"""
  74. return False
  75. def can_pickup(self):
  76. """Can the agent pick this up?"""
  77. return False
  78. def can_contain(self):
  79. """Can this contain another object?"""
  80. return False
  81. def see_behind(self):
  82. """Can the agent see behind this object?"""
  83. return True
  84. def toggle(self, env, pos):
  85. """Method to trigger/toggle an action this object performs"""
  86. return False
  87. def render(self, r):
  88. """Draw this object with the given renderer"""
  89. raise NotImplementedError
  90. def _set_color(self, r):
  91. """Set the color of this object as the active drawing color"""
  92. c = COLORS[self.color]
  93. r.setLineColor(c[0], c[1], c[2])
  94. r.setColor(c[0], c[1], c[2])
  95. class Goal(WorldObj):
  96. def __init__(self):
  97. super().__init__('goal', 'green')
  98. def can_overlap(self):
  99. return True
  100. def render(self, r):
  101. self._set_color(r)
  102. r.drawPolygon([
  103. (0 , CELL_PIXELS),
  104. (CELL_PIXELS, CELL_PIXELS),
  105. (CELL_PIXELS, 0),
  106. (0 , 0)
  107. ])
  108. class Floor(WorldObj):
  109. """
  110. Colored floor tile the agent can walk over
  111. """
  112. def __init__(self, color='blue'):
  113. super().__init__('floor', color)
  114. def can_overlap(self):
  115. return True
  116. def render(self, r):
  117. # Give the floor a pale color
  118. c = COLORS[self.color]
  119. r.setLineColor(100, 100, 100, 0)
  120. r.setColor(*c/2)
  121. r.drawPolygon([
  122. (1 , CELL_PIXELS),
  123. (CELL_PIXELS, CELL_PIXELS),
  124. (CELL_PIXELS, 1),
  125. (1 , 1)
  126. ])
  127. class Lava(WorldObj):
  128. def __init__(self):
  129. super().__init__('lava', 'red')
  130. def can_overlap(self):
  131. return True
  132. def render(self, r):
  133. orange = 255, 128, 0
  134. r.setLineColor(*orange)
  135. r.setColor(*orange)
  136. r.drawPolygon([
  137. (0 , CELL_PIXELS),
  138. (CELL_PIXELS, CELL_PIXELS),
  139. (CELL_PIXELS, 0),
  140. (0 , 0)
  141. ])
  142. # drawing the waves
  143. r.setLineColor(0, 0, 0)
  144. r.drawPolyline([
  145. (.1 * CELL_PIXELS, .3 * CELL_PIXELS),
  146. (.3 * CELL_PIXELS, .4 * CELL_PIXELS),
  147. (.5 * CELL_PIXELS, .3 * CELL_PIXELS),
  148. (.7 * CELL_PIXELS, .4 * CELL_PIXELS),
  149. (.9 * CELL_PIXELS, .3 * CELL_PIXELS),
  150. ])
  151. r.drawPolyline([
  152. (.1 * CELL_PIXELS, .5 * CELL_PIXELS),
  153. (.3 * CELL_PIXELS, .6 * CELL_PIXELS),
  154. (.5 * CELL_PIXELS, .5 * CELL_PIXELS),
  155. (.7 * CELL_PIXELS, .6 * CELL_PIXELS),
  156. (.9 * CELL_PIXELS, .5 * CELL_PIXELS),
  157. ])
  158. r.drawPolyline([
  159. (.1 * CELL_PIXELS, .7 * CELL_PIXELS),
  160. (.3 * CELL_PIXELS, .8 * CELL_PIXELS),
  161. (.5 * CELL_PIXELS, .7 * CELL_PIXELS),
  162. (.7 * CELL_PIXELS, .8 * CELL_PIXELS),
  163. (.9 * CELL_PIXELS, .7 * CELL_PIXELS),
  164. ])
  165. class Wall(WorldObj):
  166. def __init__(self, color='grey'):
  167. super().__init__('wall', color)
  168. def see_behind(self):
  169. return False
  170. def render(self, r):
  171. self._set_color(r)
  172. r.drawPolygon([
  173. (0 , CELL_PIXELS),
  174. (CELL_PIXELS, CELL_PIXELS),
  175. (CELL_PIXELS, 0),
  176. (0 , 0)
  177. ])
  178. class Door(WorldObj):
  179. def __init__(self, color, is_open=False, is_locked=False):
  180. super().__init__('door', color)
  181. self.is_open = is_open
  182. self.is_locked = is_locked
  183. def can_overlap(self):
  184. """The agent can only walk over this cell when the door is open"""
  185. return self.is_open
  186. def see_behind(self):
  187. return self.is_open
  188. def toggle(self, env, pos):
  189. # If the player has the right key to open the door
  190. if self.is_locked:
  191. if isinstance(env.carrying, Key) and env.carrying.color == self.color:
  192. self.is_locked = False
  193. self.is_open = True
  194. return True
  195. return False
  196. self.is_open = not self.is_open
  197. return True
  198. def render(self, r):
  199. c = COLORS[self.color]
  200. r.setLineColor(c[0], c[1], c[2])
  201. r.setColor(c[0], c[1], c[2], 50 if self.is_locked else 0)
  202. if self.is_open:
  203. r.drawPolygon([
  204. (CELL_PIXELS-2, CELL_PIXELS),
  205. (CELL_PIXELS , CELL_PIXELS),
  206. (CELL_PIXELS , 0),
  207. (CELL_PIXELS-2, 0)
  208. ])
  209. return
  210. r.drawPolygon([
  211. (0 , CELL_PIXELS),
  212. (CELL_PIXELS, CELL_PIXELS),
  213. (CELL_PIXELS, 0),
  214. (0 , 0)
  215. ])
  216. r.drawPolygon([
  217. (2 , CELL_PIXELS-2),
  218. (CELL_PIXELS-2, CELL_PIXELS-2),
  219. (CELL_PIXELS-2, 2),
  220. (2 , 2)
  221. ])
  222. if self.is_locked:
  223. # Draw key slot
  224. r.drawLine(
  225. CELL_PIXELS * 0.55,
  226. CELL_PIXELS * 0.5,
  227. CELL_PIXELS * 0.75,
  228. CELL_PIXELS * 0.5
  229. )
  230. else:
  231. # Draw door handle
  232. r.drawCircle(CELL_PIXELS * 0.75, CELL_PIXELS * 0.5, 2)
  233. class Key(WorldObj):
  234. def __init__(self, color='blue'):
  235. super(Key, self).__init__('key', color)
  236. def can_pickup(self):
  237. return True
  238. def render(self, r):
  239. self._set_color(r)
  240. # Vertical quad
  241. r.drawPolygon([
  242. (16, 10),
  243. (20, 10),
  244. (20, 28),
  245. (16, 28)
  246. ])
  247. # Teeth
  248. r.drawPolygon([
  249. (12, 19),
  250. (16, 19),
  251. (16, 21),
  252. (12, 21)
  253. ])
  254. r.drawPolygon([
  255. (12, 26),
  256. (16, 26),
  257. (16, 28),
  258. (12, 28)
  259. ])
  260. r.drawCircle(18, 9, 6)
  261. r.setLineColor(0, 0, 0)
  262. r.setColor(0, 0, 0)
  263. r.drawCircle(18, 9, 2)
  264. class Ball(WorldObj):
  265. def __init__(self, color='blue'):
  266. super(Ball, self).__init__('ball', color)
  267. def can_pickup(self):
  268. return True
  269. def render(self, r):
  270. self._set_color(r)
  271. r.drawCircle(CELL_PIXELS * 0.5, CELL_PIXELS * 0.5, 10)
  272. class Box(WorldObj):
  273. def __init__(self, color, contains=None):
  274. super(Box, self).__init__('box', color)
  275. self.contains = contains
  276. def can_pickup(self):
  277. return True
  278. def render(self, r):
  279. c = COLORS[self.color]
  280. r.setLineColor(c[0], c[1], c[2])
  281. r.setColor(0, 0, 0)
  282. r.setLineWidth(2)
  283. r.drawPolygon([
  284. (4 , CELL_PIXELS-4),
  285. (CELL_PIXELS-4, CELL_PIXELS-4),
  286. (CELL_PIXELS-4, 4),
  287. (4 , 4)
  288. ])
  289. r.drawLine(
  290. 4,
  291. CELL_PIXELS / 2,
  292. CELL_PIXELS - 4,
  293. CELL_PIXELS / 2
  294. )
  295. r.setLineWidth(1)
  296. def toggle(self, env, pos):
  297. # Replace the box by its contents
  298. env.grid.set(*pos, self.contains)
  299. return True
  300. class Grid:
  301. """
  302. Represent a grid and operations on it
  303. """
  304. def __init__(self, width, height):
  305. assert width >= 4
  306. assert height >= 4
  307. self.width = width
  308. self.height = height
  309. self.grid = [None] * width * height
  310. def __contains__(self, key):
  311. if isinstance(key, WorldObj):
  312. for e in self.grid:
  313. if e is key:
  314. return True
  315. elif isinstance(key, tuple):
  316. for e in self.grid:
  317. if e is None:
  318. continue
  319. if (e.color, e.type) == key:
  320. return True
  321. if key[0] is None and key[1] == e.type:
  322. return True
  323. return False
  324. def __eq__(self, other):
  325. grid1 = self.encode()
  326. grid2 = other.encode()
  327. return np.array_equal(grid2, grid1)
  328. def __ne__(self, other):
  329. return not self == other
  330. def copy(self):
  331. from copy import deepcopy
  332. return deepcopy(self)
  333. def set(self, i, j, v):
  334. assert i >= 0 and i < self.width
  335. assert j >= 0 and j < self.height
  336. self.grid[j * self.width + i] = v
  337. def get(self, i, j):
  338. assert i >= 0 and i < self.width
  339. assert j >= 0 and j < self.height
  340. return self.grid[j * self.width + i]
  341. def horz_wall(self, x, y, length=None):
  342. if length is None:
  343. length = self.width - x
  344. for i in range(0, length):
  345. self.set(x + i, y, Wall())
  346. def vert_wall(self, x, y, length=None):
  347. if length is None:
  348. length = self.height - y
  349. for j in range(0, length):
  350. self.set(x, y + j, Wall())
  351. def wall_rect(self, x, y, w, h):
  352. self.horz_wall(x, y, w)
  353. self.horz_wall(x, y+h-1, w)
  354. self.vert_wall(x, y, h)
  355. self.vert_wall(x+w-1, y, h)
  356. def rotate_left(self):
  357. """
  358. Rotate the grid to the left (counter-clockwise)
  359. """
  360. grid = Grid(self.height, self.width)
  361. for i in range(self.width):
  362. for j in range(self.height):
  363. v = self.get(i, j)
  364. grid.set(j, grid.height - 1 - i, v)
  365. return grid
  366. def slice(self, topX, topY, width, height):
  367. """
  368. Get a subset of the grid
  369. """
  370. grid = Grid(width, height)
  371. for j in range(0, height):
  372. for i in range(0, width):
  373. x = topX + i
  374. y = topY + j
  375. if x >= 0 and x < self.width and \
  376. y >= 0 and y < self.height:
  377. v = self.get(x, y)
  378. else:
  379. v = Wall()
  380. grid.set(i, j, v)
  381. return grid
  382. def render(self, r, tile_size):
  383. """
  384. Render this grid at a given scale
  385. :param r: target renderer object
  386. :param tile_size: tile size in pixels
  387. """
  388. assert r.width == self.width * tile_size
  389. assert r.height == self.height * tile_size
  390. # Total grid size at native scale
  391. widthPx = self.width * CELL_PIXELS
  392. heightPx = self.height * CELL_PIXELS
  393. r.push()
  394. # Internally, we draw at the "large" full-grid resolution, but we
  395. # use the renderer to scale back to the desired size
  396. r.scale(tile_size / CELL_PIXELS, tile_size / CELL_PIXELS)
  397. # Draw the background of the in-world cells black
  398. r.fillRect(
  399. 0,
  400. 0,
  401. widthPx,
  402. heightPx,
  403. 0, 0, 0
  404. )
  405. # Draw grid lines
  406. r.setLineColor(100, 100, 100)
  407. for rowIdx in range(0, self.height):
  408. y = CELL_PIXELS * rowIdx
  409. r.drawLine(0, y, widthPx, y)
  410. for colIdx in range(0, self.width):
  411. x = CELL_PIXELS * colIdx
  412. r.drawLine(x, 0, x, heightPx)
  413. # Render the grid
  414. for j in range(0, self.height):
  415. for i in range(0, self.width):
  416. cell = self.get(i, j)
  417. if cell == None:
  418. continue
  419. r.push()
  420. r.translate(i * CELL_PIXELS, j * CELL_PIXELS)
  421. cell.render(r)
  422. r.pop()
  423. r.pop()
  424. def encode(self, vis_mask=None):
  425. """
  426. Produce a compact numpy encoding of the grid
  427. """
  428. if vis_mask is None:
  429. vis_mask = np.ones((self.width, self.height), dtype=bool)
  430. array = np.zeros((self.width, self.height, 3), dtype='uint8')
  431. for i in range(self.width):
  432. for j in range(self.height):
  433. if vis_mask[i, j]:
  434. v = self.get(i, j)
  435. if v is None:
  436. array[i, j, 0] = OBJECT_TO_IDX['empty']
  437. array[i, j, 1] = 0
  438. array[i, j, 2] = 0
  439. else:
  440. # State, 0: open, 1: closed, 2: locked
  441. state = 0
  442. if hasattr(v, 'is_open') and not v.is_open:
  443. state = 1
  444. if hasattr(v, 'is_locked') and v.is_locked:
  445. state = 2
  446. array[i, j, 0] = OBJECT_TO_IDX[v.type]
  447. array[i, j, 1] = COLOR_TO_IDX[v.color]
  448. array[i, j, 2] = state
  449. return array
  450. @staticmethod
  451. def decode(array):
  452. """
  453. Decode an array grid encoding back into a grid
  454. """
  455. width, height, channels = array.shape
  456. assert channels == 3
  457. grid = Grid(width, height)
  458. for i in range(width):
  459. for j in range(height):
  460. typeIdx, colorIdx, state = array[i, j]
  461. if typeIdx == OBJECT_TO_IDX['unseen'] or \
  462. typeIdx == OBJECT_TO_IDX['empty']:
  463. continue
  464. objType = IDX_TO_OBJECT[typeIdx]
  465. color = IDX_TO_COLOR[colorIdx]
  466. # State, 0: open, 1: closed, 2: locked
  467. is_open = state == 0
  468. is_locked = state == 2
  469. if objType == 'wall':
  470. v = Wall(color)
  471. elif objType == 'floor':
  472. v = Floor(color)
  473. elif objType == 'ball':
  474. v = Ball(color)
  475. elif objType == 'key':
  476. v = Key(color)
  477. elif objType == 'box':
  478. v = Box(color)
  479. elif objType == 'door':
  480. v = Door(color, is_open, is_locked)
  481. elif objType == 'goal':
  482. v = Goal()
  483. elif objType == 'lava':
  484. v = Lava()
  485. else:
  486. assert False, "unknown obj type in decode '%s'" % objType
  487. grid.set(i, j, v)
  488. return grid
  489. def process_vis(grid, agent_pos):
  490. mask = np.zeros(shape=(grid.width, grid.height), dtype=np.bool)
  491. mask[agent_pos[0], agent_pos[1]] = True
  492. for j in reversed(range(0, grid.height)):
  493. for i in range(0, grid.width-1):
  494. if not mask[i, j]:
  495. continue
  496. cell = grid.get(i, j)
  497. if cell and not cell.see_behind():
  498. continue
  499. mask[i+1, j] = True
  500. if j > 0:
  501. mask[i+1, j-1] = True
  502. mask[i, j-1] = True
  503. for i in reversed(range(1, grid.width)):
  504. if not mask[i, j]:
  505. continue
  506. cell = grid.get(i, j)
  507. if cell and not cell.see_behind():
  508. continue
  509. mask[i-1, j] = True
  510. if j > 0:
  511. mask[i-1, j-1] = True
  512. mask[i, j-1] = True
  513. for j in range(0, grid.height):
  514. for i in range(0, grid.width):
  515. if not mask[i, j]:
  516. grid.set(i, j, None)
  517. return mask
  518. class MiniGridEnv(gym.Env):
  519. """
  520. 2D grid world game environment
  521. """
  522. metadata = {
  523. 'render.modes': ['human', 'rgb_array', 'pixmap'],
  524. 'video.frames_per_second' : 10
  525. }
  526. # Enumeration of possible actions
  527. class Actions(IntEnum):
  528. # Turn left, turn right, move forward
  529. left = 0
  530. right = 1
  531. forward = 2
  532. # Pick up an object
  533. pickup = 3
  534. # Drop an object
  535. drop = 4
  536. # Toggle/activate an object
  537. toggle = 5
  538. # Done completing task
  539. done = 6
  540. def __init__(
  541. self,
  542. grid_size=16,
  543. max_steps=100,
  544. see_through_walls=False,
  545. seed=1337
  546. ):
  547. # Action enumeration for this environment
  548. self.actions = MiniGridEnv.Actions
  549. # Actions are discrete integer values
  550. self.action_space = spaces.Discrete(len(self.actions))
  551. # Observations are dictionaries containing an
  552. # encoding of the grid and a textual 'mission' string
  553. self.observation_space = spaces.Box(
  554. low=0,
  555. high=255,
  556. shape=OBS_ARRAY_SIZE,
  557. dtype='uint8'
  558. )
  559. self.observation_space = spaces.Dict({
  560. 'image': self.observation_space
  561. })
  562. # Range of possible rewards
  563. self.reward_range = (0, 1)
  564. # Renderer object used to render the whole grid (full-scale)
  565. self.grid_render = None
  566. # Renderer used to render observations (small-scale agent view)
  567. self.obs_render = None
  568. # Environment configuration
  569. self.grid_size = grid_size
  570. self.max_steps = max_steps
  571. self.see_through_walls = see_through_walls
  572. # Starting position and direction for the agent
  573. self.start_pos = None
  574. self.start_dir = None
  575. # Initialize the RNG
  576. self.seed(seed=seed)
  577. # Initialize the state
  578. self.reset()
  579. def reset(self):
  580. # Generate a new random grid at the start of each episode
  581. # To keep the same grid for each episode, call env.seed() with
  582. # the same seed before calling env.reset()
  583. self._gen_grid(self.grid_size, self.grid_size)
  584. # These fields should be defined by _gen_grid
  585. assert self.start_pos is not None
  586. assert self.start_dir is not None
  587. # Check that the agent doesn't overlap with an object
  588. start_cell = self.grid.get(*self.start_pos)
  589. assert start_cell is None or start_cell.can_overlap()
  590. # Place the agent in the starting position and direction
  591. self.agent_pos = self.start_pos
  592. self.agent_dir = self.start_dir
  593. # Item picked up, being carried, initially nothing
  594. self.carrying = None
  595. # Step count since episode start
  596. self.step_count = 0
  597. # Return first observation
  598. obs = self.gen_obs()
  599. return obs
  600. def seed(self, seed=1337):
  601. # Seed the random number generator
  602. self.np_random, _ = seeding.np_random(seed)
  603. return [seed]
  604. @property
  605. def steps_remaining(self):
  606. return self.max_steps - self.step_count
  607. def __str__(self):
  608. """
  609. Produce a pretty string of the environment's grid along with the agent.
  610. A grid cell is represented by 2-character string, the first one for
  611. the object and the second one for the color.
  612. """
  613. # Map of object types to short string
  614. OBJECT_TO_STR = {
  615. 'wall' : 'W',
  616. 'floor' : 'F',
  617. 'door' : 'D',
  618. 'key' : 'K',
  619. 'ball' : 'A',
  620. 'box' : 'B',
  621. 'goal' : 'G',
  622. 'lava' : 'V',
  623. }
  624. # Short string for opened door
  625. OPENDED_DOOR_IDS = '_'
  626. # Map agent's direction to short string
  627. AGENT_DIR_TO_STR = {
  628. 0: '>',
  629. 1: 'V',
  630. 2: '<',
  631. 3: '^'
  632. }
  633. str = ''
  634. for j in range(self.grid.height):
  635. for i in range(self.grid.width):
  636. if i == self.agent_pos[0] and j == self.agent_pos[1]:
  637. str += 2 * AGENT_DIR_TO_STR[self.agent_dir]
  638. continue
  639. c = self.grid.get(i, j)
  640. if c == None:
  641. str += ' '
  642. continue
  643. if c.type == 'door':
  644. if c.is_open:
  645. str += '__'
  646. elif c.is_locked:
  647. str += 'L' + c.color[0].upper()
  648. else:
  649. str += 'D' + c.color[0].upper()
  650. continue
  651. str += OBJECT_TO_STR[c.type] + c.color[0].upper()
  652. if j < self.grid.height - 1:
  653. str += '\n'
  654. return str
  655. def _gen_grid(self, width, height):
  656. assert False, "_gen_grid needs to be implemented by each environment"
  657. def _reward(self):
  658. """
  659. Compute the reward to be given upon success
  660. """
  661. return 1 - 0.9 * (self.step_count / self.max_steps)
  662. def _rand_int(self, low, high):
  663. """
  664. Generate random integer in [low,high[
  665. """
  666. return self.np_random.randint(low, high)
  667. def _rand_float(self, low, high):
  668. """
  669. Generate random float in [low,high[
  670. """
  671. return self.np_random.uniform(low, high)
  672. def _rand_bool(self):
  673. """
  674. Generate random boolean value
  675. """
  676. return (self.np_random.randint(0, 2) == 0)
  677. def _rand_elem(self, iterable):
  678. """
  679. Pick a random element in a list
  680. """
  681. lst = list(iterable)
  682. idx = self._rand_int(0, len(lst))
  683. return lst[idx]
  684. def _rand_subset(self, iterable, num_elems):
  685. """
  686. Sample a random subset of distinct elements of a list
  687. """
  688. lst = list(iterable)
  689. assert num_elems <= len(lst)
  690. out = []
  691. while len(out) < num_elems:
  692. elem = self._rand_elem(lst)
  693. lst.remove(elem)
  694. out.append(elem)
  695. return out
  696. def _rand_color(self):
  697. """
  698. Generate a random color name (string)
  699. """
  700. return self._rand_elem(COLOR_NAMES)
  701. def _rand_pos(self, xLow, xHigh, yLow, yHigh):
  702. """
  703. Generate a random (x,y) position tuple
  704. """
  705. return (
  706. self.np_random.randint(xLow, xHigh),
  707. self.np_random.randint(yLow, yHigh)
  708. )
  709. def place_obj(self,
  710. obj,
  711. top=None,
  712. size=None,
  713. reject_fn=None,
  714. max_tries=math.inf
  715. ):
  716. """
  717. Place an object at an empty position in the grid
  718. :param top: top-left position of the rectangle where to place
  719. :param size: size of the rectangle where to place
  720. :param reject_fn: function to filter out potential positions
  721. """
  722. if top is None:
  723. top = (0, 0)
  724. if size is None:
  725. size = (self.grid.width, self.grid.height)
  726. num_tries = 0
  727. while True:
  728. # This is to handle with rare cases where rejection sampling
  729. # gets stuck in an infinite loop
  730. if num_tries > max_tries:
  731. raise RecursionError('rejection sampling failed in place_obj')
  732. num_tries += 1
  733. pos = np.array((
  734. self._rand_int(top[0], top[0] + size[0]),
  735. self._rand_int(top[1], top[1] + size[1])
  736. ))
  737. # Don't place the object on top of another object
  738. if self.grid.get(*pos) != None:
  739. continue
  740. # Don't place the object where the agent is
  741. if np.array_equal(pos, self.start_pos):
  742. continue
  743. # Check if there is a filtering criterion
  744. if reject_fn and reject_fn(self, pos):
  745. continue
  746. break
  747. self.grid.set(*pos, obj)
  748. if obj is not None:
  749. obj.init_pos = pos
  750. obj.cur_pos = pos
  751. return pos
  752. def place_agent(
  753. self,
  754. top=None,
  755. size=None,
  756. rand_dir=True,
  757. max_tries=math.inf
  758. ):
  759. """
  760. Set the agent's starting point at an empty position in the grid
  761. """
  762. self.start_pos = None
  763. pos = self.place_obj(None, top, size, max_tries=max_tries)
  764. self.start_pos = pos
  765. if rand_dir:
  766. self.start_dir = self._rand_int(0, 4)
  767. return pos
  768. @property
  769. def dir_vec(self):
  770. """
  771. Get the direction vector for the agent, pointing in the direction
  772. of forward movement.
  773. """
  774. assert self.agent_dir >= 0 and self.agent_dir < 4
  775. return DIR_TO_VEC[self.agent_dir]
  776. @property
  777. def right_vec(self):
  778. """
  779. Get the vector pointing to the right of the agent.
  780. """
  781. dx, dy = self.dir_vec
  782. return np.array((-dy, dx))
  783. @property
  784. def front_pos(self):
  785. """
  786. Get the position of the cell that is right in front of the agent
  787. """
  788. return self.agent_pos + self.dir_vec
  789. def get_view_coords(self, i, j):
  790. """
  791. Translate and rotate absolute grid coordinates (i, j) into the
  792. agent's partially observable view (sub-grid). Note that the resulting
  793. coordinates may be negative or outside of the agent's view size.
  794. """
  795. ax, ay = self.agent_pos
  796. dx, dy = self.dir_vec
  797. rx, ry = self.right_vec
  798. # Compute the absolute coordinates of the top-left view corner
  799. sz = AGENT_VIEW_SIZE
  800. hs = AGENT_VIEW_SIZE // 2
  801. tx = ax + (dx * (sz-1)) - (rx * hs)
  802. ty = ay + (dy * (sz-1)) - (ry * hs)
  803. lx = i - tx
  804. ly = j - ty
  805. # Project the coordinates of the object relative to the top-left
  806. # corner onto the agent's own coordinate system
  807. vx = (rx*lx + ry*ly)
  808. vy = -(dx*lx + dy*ly)
  809. return vx, vy
  810. def get_view_exts(self):
  811. """
  812. Get the extents of the square set of tiles visible to the agent
  813. Note: the bottom extent indices are not included in the set
  814. """
  815. # Facing right
  816. if self.agent_dir == 0:
  817. topX = self.agent_pos[0]
  818. topY = self.agent_pos[1] - AGENT_VIEW_SIZE // 2
  819. # Facing down
  820. elif self.agent_dir == 1:
  821. topX = self.agent_pos[0] - AGENT_VIEW_SIZE // 2
  822. topY = self.agent_pos[1]
  823. # Facing left
  824. elif self.agent_dir == 2:
  825. topX = self.agent_pos[0] - AGENT_VIEW_SIZE + 1
  826. topY = self.agent_pos[1] - AGENT_VIEW_SIZE // 2
  827. # Facing up
  828. elif self.agent_dir == 3:
  829. topX = self.agent_pos[0] - AGENT_VIEW_SIZE // 2
  830. topY = self.agent_pos[1] - AGENT_VIEW_SIZE + 1
  831. else:
  832. assert False, "invalid agent direction"
  833. botX = topX + AGENT_VIEW_SIZE
  834. botY = topY + AGENT_VIEW_SIZE
  835. return (topX, topY, botX, botY)
  836. def relative_coords(self, x, y):
  837. """
  838. Check if a grid position belongs to the agent's field of view, and returns the corresponding coordinates
  839. """
  840. vx, vy = self.get_view_coords(x, y)
  841. if vx < 0 or vy < 0 or vx >= AGENT_VIEW_SIZE or vy >= AGENT_VIEW_SIZE:
  842. return None
  843. return vx, vy
  844. def in_view(self, x, y):
  845. """
  846. check if a grid position is visible to the agent
  847. """
  848. return self.relative_coords(x, y) is not None
  849. def agent_sees(self, x, y):
  850. """
  851. Check if a non-empty grid position is visible to the agent
  852. """
  853. coordinates = self.relative_coords(x, y)
  854. if coordinates is None:
  855. return False
  856. vx, vy = coordinates
  857. obs = self.gen_obs()
  858. obs_grid = Grid.decode(obs['image'])
  859. obs_cell = obs_grid.get(vx, vy)
  860. world_cell = self.grid.get(x, y)
  861. return obs_cell is not None and obs_cell.type == world_cell.type
  862. def step(self, action):
  863. self.step_count += 1
  864. reward = 0
  865. done = False
  866. # Get the position in front of the agent
  867. fwd_pos = self.front_pos
  868. # Get the contents of the cell in front of the agent
  869. fwd_cell = self.grid.get(*fwd_pos)
  870. # Rotate left
  871. if action == self.actions.left:
  872. self.agent_dir -= 1
  873. if self.agent_dir < 0:
  874. self.agent_dir += 4
  875. # Rotate right
  876. elif action == self.actions.right:
  877. self.agent_dir = (self.agent_dir + 1) % 4
  878. # Move forward
  879. elif action == self.actions.forward:
  880. if fwd_cell == None or fwd_cell.can_overlap():
  881. self.agent_pos = fwd_pos
  882. if fwd_cell != None and fwd_cell.type == 'goal':
  883. done = True
  884. reward = self._reward()
  885. if fwd_cell != None and fwd_cell.type == 'lava':
  886. done = True
  887. # Pick up an object
  888. elif action == self.actions.pickup:
  889. if fwd_cell and fwd_cell.can_pickup():
  890. if self.carrying is None:
  891. self.carrying = fwd_cell
  892. self.carrying.cur_pos = np.array([-1, -1])
  893. self.grid.set(*fwd_pos, None)
  894. # Drop an object
  895. elif action == self.actions.drop:
  896. if not fwd_cell and self.carrying:
  897. self.grid.set(*fwd_pos, self.carrying)
  898. self.carrying.cur_pos = fwd_pos
  899. self.carrying = None
  900. # Toggle/activate an object
  901. elif action == self.actions.toggle:
  902. if fwd_cell:
  903. fwd_cell.toggle(self, fwd_pos)
  904. # Done action (not used by default)
  905. elif action == self.actions.done:
  906. pass
  907. else:
  908. assert False, "unknown action"
  909. if self.step_count >= self.max_steps:
  910. done = True
  911. obs = self.gen_obs()
  912. return obs, reward, done, {}
  913. def gen_obs_grid(self):
  914. """
  915. Generate the sub-grid observed by the agent.
  916. This method also outputs a visibility mask telling us which grid
  917. cells the agent can actually see.
  918. """
  919. topX, topY, botX, botY = self.get_view_exts()
  920. grid = self.grid.slice(topX, topY, AGENT_VIEW_SIZE, AGENT_VIEW_SIZE)
  921. for i in range(self.agent_dir + 1):
  922. grid = grid.rotate_left()
  923. # Process occluders and visibility
  924. # Note that this incurs some performance cost
  925. if not self.see_through_walls:
  926. vis_mask = grid.process_vis(agent_pos=(AGENT_VIEW_SIZE // 2 , AGENT_VIEW_SIZE - 1))
  927. else:
  928. vis_mask = np.ones(shape=(grid.width, grid.height), dtype=np.bool)
  929. # Make it so the agent sees what it's carrying
  930. # We do this by placing the carried object at the agent's position
  931. # in the agent's partially observable view
  932. agent_pos = grid.width // 2, grid.height - 1
  933. if self.carrying:
  934. grid.set(*agent_pos, self.carrying)
  935. else:
  936. grid.set(*agent_pos, None)
  937. return grid, vis_mask
  938. def gen_obs(self):
  939. """
  940. Generate the agent's view (partially observable, low-resolution encoding)
  941. """
  942. grid, vis_mask = self.gen_obs_grid()
  943. # Encode the partially observable view into a numpy array
  944. image = grid.encode(vis_mask)
  945. assert hasattr(self, 'mission'), "environments must define a textual mission string"
  946. # Observations are dictionaries containing:
  947. # - an image (partially observable view of the environment)
  948. # - the agent's direction/orientation (acting as a compass)
  949. # - a textual mission string (instructions for the agent)
  950. obs = {
  951. 'image': image,
  952. 'direction': self.agent_dir,
  953. 'mission': self.mission
  954. }
  955. return obs
  956. def get_obs_render(self, obs, tile_pixels=CELL_PIXELS//2):
  957. """
  958. Render an agent observation for visualization
  959. """
  960. if self.obs_render == None:
  961. from gym_minigrid.rendering import Renderer
  962. self.obs_render = Renderer(
  963. AGENT_VIEW_SIZE * tile_pixels,
  964. AGENT_VIEW_SIZE * tile_pixels
  965. )
  966. r = self.obs_render
  967. r.beginFrame()
  968. grid = Grid.decode(obs)
  969. # Render the whole grid
  970. grid.render(r, tile_pixels)
  971. # Draw the agent
  972. ratio = tile_pixels / CELL_PIXELS
  973. r.push()
  974. r.scale(ratio, ratio)
  975. r.translate(
  976. CELL_PIXELS * (0.5 + AGENT_VIEW_SIZE // 2),
  977. CELL_PIXELS * (AGENT_VIEW_SIZE - 0.5)
  978. )
  979. r.rotate(3 * 90)
  980. r.setLineColor(255, 0, 0)
  981. r.setColor(255, 0, 0)
  982. r.drawPolygon([
  983. (-12, 10),
  984. ( 12, 0),
  985. (-12, -10)
  986. ])
  987. r.pop()
  988. r.endFrame()
  989. return r.getPixmap()
  990. def render(self, mode='human', close=False):
  991. """
  992. Render the whole-grid human view
  993. """
  994. if close:
  995. if self.grid_render:
  996. self.grid_render.close()
  997. return
  998. if self.grid_render is None:
  999. from gym_minigrid.rendering import Renderer
  1000. self.grid_render = Renderer(
  1001. self.grid_size * CELL_PIXELS,
  1002. self.grid_size * CELL_PIXELS,
  1003. True if mode == 'human' else False
  1004. )
  1005. r = self.grid_render
  1006. if r.window:
  1007. r.window.setText(self.mission)
  1008. r.beginFrame()
  1009. # Render the whole grid
  1010. self.grid.render(r, CELL_PIXELS)
  1011. # Draw the agent
  1012. r.push()
  1013. r.translate(
  1014. CELL_PIXELS * (self.agent_pos[0] + 0.5),
  1015. CELL_PIXELS * (self.agent_pos[1] + 0.5)
  1016. )
  1017. r.rotate(self.agent_dir * 90)
  1018. r.setLineColor(255, 0, 0)
  1019. r.setColor(255, 0, 0)
  1020. r.drawPolygon([
  1021. (-12, 10),
  1022. ( 12, 0),
  1023. (-12, -10)
  1024. ])
  1025. r.pop()
  1026. # Compute which cells are visible to the agent
  1027. _, vis_mask = self.gen_obs_grid()
  1028. # Compute the absolute coordinates of the bottom-left corner
  1029. # of the agent's view area
  1030. f_vec = self.dir_vec
  1031. r_vec = self.right_vec
  1032. top_left = self.agent_pos + f_vec * (AGENT_VIEW_SIZE-1) - r_vec * (AGENT_VIEW_SIZE // 2)
  1033. # For each cell in the visibility mask
  1034. for vis_j in range(0, AGENT_VIEW_SIZE):
  1035. for vis_i in range(0, AGENT_VIEW_SIZE):
  1036. # If this cell is not visible, don't highlight it
  1037. if not vis_mask[vis_i, vis_j]:
  1038. continue
  1039. # Compute the world coordinates of this cell
  1040. abs_i, abs_j = top_left - (f_vec * vis_j) + (r_vec * vis_i)
  1041. # Highlight the cell
  1042. r.fillRect(
  1043. abs_i * CELL_PIXELS,
  1044. abs_j * CELL_PIXELS,
  1045. CELL_PIXELS,
  1046. CELL_PIXELS,
  1047. 255, 255, 255, 75
  1048. )
  1049. r.endFrame()
  1050. if mode == 'rgb_array':
  1051. return r.getArray()
  1052. elif mode == 'pixmap':
  1053. return r.getPixmap()
  1054. return r