Sokoban
Bases: Environment[State, DiscreteArray, Observation]
A JAX implementation of the 'Sokoban' game from deepmind.
-
observation:
Observation
- grid: jax array (uint8) of shape (num_rows, num_cols, 2) Array that includes information about the agent, boxes, and targets in the game.
- step_count: jax array (int32) of shape () current number of steps in the episode.
-
action: jax array (int32) of shape () [0,1,2,3] -> [Up, Right, Down, Left].
-
reward: jax array (float) of shape () A reward of 1.0 is given for each box placed on a target and -1 when removed from a target and -0.1 for each timestep. 10 is awarded when all boxes are on targets.
-
episode termination:
- if the time limit is reached.
- if all boxes are on targets.
-
state:
State
- key: jax array (uint32) of shape (2,) used for auto-reset
- fixed_grid: jax array (uint8) of shape (num_rows, num_cols) array indicating the walls and targets in the level.
- variable_grid: jax array (uint8) of shape (num_rows, num_cols) array indicating the current location of the agent and boxes.
- agent_location: jax array (int32) of shape (2,) the agent's current location.
- step_count: jax array (int32) of shape () current number of steps in the episode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Instantiates a Sokoban
environment with a specific generator,
time limit, and viewer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
generator
|
Optional[Generator]
|
|
None
|
time_limit
|
int
|
int, max steps for the environment, defaults to 120. |
120
|
viewer
|
Optional[Viewer]
|
'Viewer' object, used to render the environment. |
None
|
Source code in jumanji/environments/routing/sokoban/env.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
action_spec: specs.DiscreteArray
cached
property
#
Returns the action specification for the Sokoban environment. There are 4 actions: [0,1,2,3] -> [Up, Right, Down, Left].
Returns:
Type | Description |
---|---|
DiscreteArray
|
specs.DiscreteArray: Discrete action specifications. |
observation_spec: specs.Spec[Observation]
cached
property
#
Returns the specifications of the observation of the Sokoban
environment.
Returns:
Type | Description |
---|---|
Spec[Observation]
|
specs.Spec[Observation]: The specifications of the observations. |
__repr__()
#
Returns a printable representation of the Sokoban environment.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string representation of the Sokoban environment. |
Source code in jumanji/environments/routing/sokoban/env.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
animate(states, interval=200, save_path=None)
#
Creates an animated gif of the Sokoban environment based on the sequence of states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
states
|
Sequence[State]
|
Sequence of 'State' object |
required |
interval
|
int
|
int, The interval between frames in the animation. |
200
|
save_path
|
Optional[str]
|
str The path where to save the animation. If not |
None
|
Returns:
Name | Type | Description |
---|---|---|
animation |
FuncAnimation
|
'matplotlib.animation.FuncAnimation'. |
Source code in jumanji/environments/routing/sokoban/env.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
|
check_space(grid, location, value)
#
Checks if a specific location in the grid contains a given value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grid
|
Array
|
Array (uint8) shape (num_rows, num_cols) The grid to check. |
required |
location
|
Array
|
Tuple size 2 of Array (int32) shape () containing the x |
required |
value
|
int
|
int The value to look for. |
required |
Returns:
Name | Type | Description |
---|---|---|
present |
Array
|
Array (bool) shape () indicating whether the location |
Array
|
in the grid contains the given value or not. |
Source code in jumanji/environments/routing/sokoban/env.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
detect_noop_action(variable_grid, fixed_grid, action, agent_location)
#
Masks actions to -1 that have no effect on the variable grid. Determines if there is space in the destination square or if there is a box in the destination square, it determines if the box destination square is valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_grid
|
Array
|
Array (uint8) shape (num_rows, num_cols). |
required |
action
|
Array
|
Array (int32) shape () The action to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
updated_action |
Array
|
Array (int32) shape () The updated action after |
Array
|
detecting noop action. |
Source code in jumanji/environments/routing/sokoban/env.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
|
grid_combine(variable_grid, fixed_grid)
#
Combines the variable grid and fixed grid into one single grid representation of the current Sokoban state required for visual representation of the Sokoban state. Takes care of two possible overlaps of fixed and variable entries (an agent on a target or a box on a target), introducing two additional encodings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_grid
|
Array
|
Array (uint8) of shape (num_rows, num_cols). |
required |
fixed_grid
|
Array
|
Array (uint8) of shape (num_rows, num_cols). |
required |
Returns:
Name | Type | Description |
---|---|---|
full_grid |
Array
|
Array (uint8) of shape (num_rows, num_cols, 2). |
Source code in jumanji/environments/routing/sokoban/env.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
in_grid(coordinates)
#
Checks if given coordinates are within the grid size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coordinates
|
Array
|
Array (uint8) shape (num_rows, num_cols) The |
required |
Returns: in_grid: Array (bool) shape () Boolean indicating whether the coordinates are within the grid.
Source code in jumanji/environments/routing/sokoban/env.py
405 406 407 408 409 410 411 412 413 414 415 416 |
|
level_complete(state)
#
Checks if the sokoban level is complete.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
State
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
complete |
Array
|
Boolean indicating whether the level is complete |
Array
|
or not. |
Source code in jumanji/environments/routing/sokoban/env.py
370 371 372 373 374 375 376 377 378 379 380 381 |
|
move_agent(variable_grid, action, current_location)
#
Executes the movement of the agent specified by the action and executes the movement of a box if present at the destination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable_grid
|
Array
|
Array (uint8) shape (num_rows, num_cols) |
required |
action
|
Array
|
Array (int32) shape () The action to take. |
required |
current_location
|
Array
|
Array (int32) shape (2,) |
required |
Returns:
Name | Type | Description |
---|---|---|
next_variable_grid |
Array
|
Array (uint8) shape (num_rows, num_cols) |
next_location |
Array
|
Array (int32) shape (2,) |
Source code in jumanji/environments/routing/sokoban/env.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
|
render(state)
#
Renders the current state of Sokoban.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
State
|
'State' object , the current state to be rendered. |
required |
Source code in jumanji/environments/routing/sokoban/env.py
543 544 545 546 547 548 549 550 551 |
|
reset(key)
#
Resets the environment by calling the instance generator for a new instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKey
|
random key used to sample new Sokoban problem. |
required |
Returns:
Name | Type | Description |
---|---|---|
state |
State
|
|
TimeStep[Observation]
|
environment after a reset. |
|
timestep |
Tuple[State, TimeStep[Observation]]
|
|
Tuple[State, TimeStep[Observation]]
|
returned by the environment after a reset. |
Source code in jumanji/environments/routing/sokoban/env.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
step(state, action)
#
Executes one timestep of the environment's dynamics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
State
|
'State' object representing the current state of the |
required |
action
|
Array
|
Array (int32) of shape (). - 0: move up. - 1: move down. - 2: move left. - 3: move right. |
required |
Returns:
Type | Description |
---|---|
State
|
state, timestep: next state of the environment and timestep to be |
TimeStep[Observation]
|
observed. |
Source code in jumanji/environments/routing/sokoban/env.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
|
update_box_push_action(fixed_grid, variable_grid, new_location, action)
#
Masks actions to -1 if pushing the box is not a valid move. If it would be pushed out of the grid or the resulting square is either a wall or another box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fixed_grid
|
Array
|
Array (uint8) shape (num_rows, num_cols) The fixed grid. |
required |
variable_grid
|
Array
|
Array (uint8) shape (num_rows, num_cols) The |
required |
new_location
|
Array
|
Array (int32) shape (2,) The new location of the agent. |
required |
action
|
Array
|
Array (int32) shape () The action to be executed. |
required |
Returns:
Name | Type | Description |
---|---|---|
updated_action |
Array
|
Array (int32) shape () The updated action after |
Array
|
checking if pushing the box is a valid move. |
Source code in jumanji/environments/routing/sokoban/env.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
|