Package: gym-sokoban
gym-sokobanSokobanis Japanese for warehouse keeper and a traditional video game.
The game is a transportation puzzle, where the player has to push all boxes in the room on the storage locations/ targets.
The possibility of making irreversible mistakes makes these puzzles so challenging especially forReinforcement Learningalgorithms, which mostly lack the ability to think ahead.The repository implements the game Sokoban based on the rules presentedDeepMind'spaperImagination Augmented Agents for Deep Reinforcement Learning.
The room generation is random and therefore, will allow to train Deep Neural Networks without overfitting on a set of predefined rooms.Example Game 1Example Game 2Example Game 31 InstallationVia PIPpipinstallgym-sokobanFrom Repositorygitclonegit@github.com:mpSchrader/gym-sokoban.gitcdgym-sokoban
pipinstall-e.Checkout theexampleson how to use an external gym environment.2 Game Environment2.1 Room ElementsEvery room consists of five main elements: walls, floor, boxes, box targets, and a player. They might have different states whether they overlap with a box target or not.TypeStateGraphicTinyWorldWallStaticFloorEmptyBox TargetEmptyBoxOff TargetBoxOn TargetPlayerOff TargetPlayerOn Target2.2 ActionsThe game provides 9 actions to interact with the environment.
Push and Move actions into the directions Up, Down, Left and Right.
The No Operation action is a void action, which does not change anything in the environment.
The mapping of the action numbers to the actual actions looks as followsActionIDNo Operation0Push Up1Push Down2Push Left3Push Right4Move Up5Move Down6Move Left7Move Right8Movesimply moves if there is a free field in the direction, which means no blocking box or wall.Pushpush tries to move an adjacent box if the next field behind the box is free.
This means no chain pushing of boxes is possible.
In case there is no box at the adjacent field, the push action is handled the same way as the move action into the same direction.2.3 RewardsFinishing the game by pushing all on the targets gives a reward of 10 in the last step.
Also pushing a box on or off a target gives a reward of 1 respectively of -1.
In addition a reward of -0.1 is given for every step, this penalizes solutions with many steps.ReasonRewardPerform Step-0.1Push Box on Target1.0Push Box off Target-1.0Push all boxes on targets10.02.4 Level GenerationEvery time a Sokoban environment is loaded or reset a new room is randomly generated.
The generation consists of 3 phases: Topology Generation, Placement of Targets and Players, and Reverse Playing.2.4.1 Topology GenerationTo generate the basic topology of the room, consisting of walls and empty floor, is based on a random walk, which changes its direction at probability 0.35.
At every step centered at the current position, a pattern of fields is set to empty spaces.
The patterns used can be found inFigure 2.Figure 2: Masks for creating a topology2.4.2 Placement of ElementsDuring this phase, the player including all n box targets are placed on randomly chosen empty spaces.2.4.3 Reverse PlayingThis is the crucial phase to  ensure a solvable room.
Now Sokoban is played in a reverse fashion, where a player can move and pull boxes.
The goal of this phase is to find the room state, with the highest room score, with aDepth First Search.
For every room explored during the search is a room score is calculated with the equation shown below.
The equation is a heuristic approach to evaluate the difficulty of the room.
BoxSwaps counts the number of times a player changes the box to pull.
BoxDisplacement is theManhattan Distancebetween a specific box and its origin box target.
As long as at least one box is on a target the RoomScore is always 0.2.5 ConfigurationSokoban has many different variations, such as: Room Size, Number of Boxes, Rendering Modes, or Rules.2.5.1 Rendering ModesBesides the regular Sokoban rendering, each configuration can be rendered as TinyWorld, which has a pixel size equal to the grid size.
To get an environment rendered as a tiny world just addtiny_in front of the rendering mode. E.g:env.render('tiny_rgb_array', scale=scale_tiny). Scale allows to increase the size of the rendered tiny world observation. Using scale in combination with the rendering modes,humanorrgb_array, does not influence the output size.
Available rendering modes are:ModeDescriptionrgb_arrayWell looking 2d rgb imagehumanDisplays the current state on screentiny_rgb_arrayEach pixel describing one element in the roomtiny_humanDisplays the tiny rgb_array on screen2.5.2 Size VariationsThe available room configurations are shown in the table below.Room IdGrid-SizePixels#BoxesExampleTinyWorldSokoban-v010x10160x1603Sokoban-v110x10160x1604Sokoban-v210x10160x1605Sokoban-small-v07x7112x1122Sokoban-small-v17x7112x1123Sokoban-large-v013x11208x1763Sokoban-large-v113x11208x1764Sokoban-large-v213x11208x1765Sokoban-huge-v013x13208x2085Please note that the larger rooms might take some time to be created, especially on a laptop.2.5.3 Other VariationsBesides the regular game of Sokoban, this repository implements or will implement variations, which might make the game easier or more complicated. Except noted differently the variations do not implement a Tiny-World version.VariationSummaryExpected DifficultyExampleTiny WorldStatusDetailsFixed TargetsEvery box has to be pushed on the target with the same color.More difficultYesimplementedReadMeMultiple PlayerThere are two players in the room. Every round one of the two players can be used. There is no order of moves between the two players.More difficultYesimplementedReadMePush&PullThe player can not only push the boxes, but also pull them. Therefore, no more irreversible moves exist.EasierYesimplementedReadMeBoxobanUses by DeepMindpregenerated Sokoban puzzles.SimilarYesImplementedReadMe3 CiteIf you are using this repository for your research please cite it with the following information:@misc{SchraderSokoban2018,
  author = {Schrader, Max-Philipp B.},
  title = {gym-sokoban},
  year = {2018},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/mpSchrader/gym-sokoban}},
  commit = {#CommitId}
}4 Connect & Contribute4.1 ConnectFeel free to get in touch with me to talk about this or other projects.
Either by creating anissueor mail me onLinkedIn.If you reached the end and liked the project, pleaseshow your appreciation by starting this project.4.2 ContributeFeel free to contribute to this project by forking the repo and implement whatever you are missing.
Alternatively, open a new issue in case you need help or want to have a feature added.