Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sudoku): implement environment #125

Merged
merged 61 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
5214c68
feat: add sudoku
Egiob Apr 22, 2023
1c139a4
feat: make generator configurable and add puzzles data
Egiob Apr 27, 2023
72330e6
fix: add sudoku conf
Egiob Apr 28, 2023
2593612
fix: validate board
Egiob Apr 28, 2023
aef828c
feat(sudoku): change default version
Egiob Apr 28, 2023
db3845c
feat(sudoku): update very easy databse
Egiob Apr 28, 2023
521612a
feat(sudoku): update very easy data
Egiob Apr 28, 2023
89299cd
wip: debug train
Egiob Apr 28, 2023
bc61304
wip: debug train
Egiob Apr 28, 2023
7541e4c
wip: debug train
Egiob Apr 28, 2023
5b41051
wip: debug train
Egiob Apr 28, 2023
4bc8677
wip: debug train
Egiob Apr 28, 2023
04c120a
wip: debug train
Egiob Apr 28, 2023
e060543
wip: debug train
Egiob Apr 28, 2023
d156a03
wip: debug train
Egiob Apr 28, 2023
d11dc09
wip: debug train
Egiob Apr 28, 2023
d341e17
wip: debug train
Egiob Apr 28, 2023
0bba8a6
test: add utils test
Egiob Apr 28, 2023
aedccb6
docs: update sudoku versions
Egiob Apr 28, 2023
3248ef2
style: clean useless files
Egiob Apr 28, 2023
0260e82
style: clean notebook
Egiob Apr 28, 2023
9352a4d
fix: data not loadable
Egiob Apr 28, 2023
371f54f
wip: train invariant net
Egiob May 5, 2023
06f4d1f
docs: update README.md
Egiob May 5, 2023
b2d70e5
style: remove leading underscore
Egiob May 5, 2023
7598f50
fix: change signature of reward
Egiob May 5, 2023
2f6d256
fix: matplotlib animation ref
Egiob May 5, 2023
147f4f4
style: remove remaining actions
Egiob May 5, 2023
5ffb46d
wip: train a2c sudoku
Egiob May 5, 2023
81a8aec
wip: train a2c sudoku
Egiob May 5, 2023
770ac87
style: remove blank line
Egiob May 5, 2023
259fb41
style: remove blank line
Egiob May 5, 2023
2550ffa
wip: train sudoku
Egiob May 5, 2023
ccea58b
Merge branch 'feat/add-sudoku-environment' of github.com:Egiob/jumanj…
Egiob May 5, 2023
fc0bf0e
wip: train sudoku
Egiob May 6, 2023
ba99ca5
wip: train sudoku
Egiob May 6, 2023
659ef01
wip: train sudoku
Egiob May 6, 2023
4d30462
wip: train sudoku
Egiob May 6, 2023
ae2d286
wip: train a2c sudoku
Egiob May 9, 2023
098a6a9
style: minor changes
Egiob May 11, 2023
e770149
docs: add reference to paper
Egiob May 11, 2023
18f91ac
refactor: small comments
Egiob May 19, 2023
b8c0d5f
fix: add missing
Egiob May 19, 2023
e2a8b4c
fix: revert notebooks
Egiob May 19, 2023
0c0930c
fix: register env
Egiob May 19, 2023
241af11
style: remove print
Egiob May 19, 2023
61d35d3
style: address PR comments
Egiob May 30, 2023
f5dd0fb
Merge branch 'main' into feat/add-sudoku-environment
clement-bonnet May 30, 2023
81e96dc
Update docs/environments/sudoku.md
Egiob May 31, 2023
e3e21ac
style: add type hint
Egiob May 31, 2023
4af1f13
style: PR review
Egiob May 31, 2023
f6629d9
Merge branch 'feat/add-sudoku-environment' of github.com:Egiob/jumanj…
Egiob May 31, 2023
3d83925
fix: path to sudoku data
Egiob May 31, 2023
090b630
style: minor changes
Egiob Jun 1, 2023
873cb08
docs: add comment
Egiob Jun 1, 2023
83e087f
Merge branch 'main' into feat/add-sudoku-environment
clement-bonnet Jun 1, 2023
d6e98d6
Merge branch 'main' into feat/add-sudoku-environment
clement-bonnet Jun 1, 2023
2df72fd
style: type to jnp type
Egiob Jun 1, 2023
c0ec740
style: small changes
Egiob Jun 1, 2023
12306b0
Merge branch 'feat/add-sudoku-environment' of github.com:Egiob/jumanj…
Egiob Jun 1, 2023
fbc69e9
fix: broken line in
Egiob Jun 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ repos:
args:
- --license-filepath=license_header.txt
- --comment-style=#
exclude: .npy$
Egiob marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<img src="docs/env_anim/rubiks_cube.gif" alt="RubiksCube" width="30%" />
<img src="docs/env_anim/game_2048.gif" alt="Game2048" width="30%" />
<img src="docs/env_anim/minesweeper.gif" alt="Minesweeper" width="30%" />
<img src="docs/env_anim/sudoku.gif" alt="Sudoku" width="30%" />
Egiob marked this conversation as resolved.
Show resolved Hide resolved
Egiob marked this conversation as resolved.
Show resolved Hide resolved
</p>


Expand Down Expand Up @@ -81,6 +82,7 @@ problems.
| 🔢 Game2048 | Logic | `Game2048-v0` | [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/logic/game_2048/) | [doc](https://instadeepai.github.io/jumanji/environments/game_2048/) |
| 💣 Minesweeper | Logic | `Minesweeper-v0` | [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/logic/minesweeper/) | [doc](https://instadeepai.github.io/jumanji/environments/minesweeper/) |
| 🎲 RubiksCube | Logic | `RubiksCube-v0`<br/>`RubiksCube-partly-scrambled-v0` | [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/logic/rubiks_cube/) | [doc](https://instadeepai.github.io/jumanji/environments/rubiks_cube/) |
| ✏️ Sudoku | Logic | `Sudoku-v0` <br/>`Sudoku-very-easy-v0`| [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/logic/sudoku/) | [doc](https://instadeepai.github.io/jumanji/environments/sudoku/) |
clement-bonnet marked this conversation as resolved.
Show resolved Hide resolved
| 📦 BinPack (3D BinPacking Problem) | Packing | `BinPack-v1` | [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/packing/bin_pack/) | [doc](https://instadeepai.github.io/jumanji/environments/bin_pack/) |
| 🏭 JobShop (Job Shop Scheduling Problem) | Packing | `JobShop-v0` | [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/packing/job_shop/) | [doc](https://instadeepai.github.io/jumanji/environments/job_shop/) |
| 🎒 Knapsack | Packing | `Knapsack-v1` | [code](https://github.com/instadeepai/jumanji/tree/main/jumanji/environments/packing/knapsack/) | [doc](https://instadeepai.github.io/jumanji/environments/knapsack/) |
Expand Down
8 changes: 8 additions & 0 deletions docs/api/environments/sudoku.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
::: jumanji.environments.logic.sudoku.env.Sudoku
selection:
members:
- __init__
- reset
- step
- observation_spec
- action_spec
Binary file added docs/env_anim/sudoku.gif
Egiob marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/env_img/sudoku.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions docs/environments/sudoku.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Sudoku Environment

<p align="center">
<img src="../env_anim/sudoku.gif" width="500"/>
</p>

We provide here a Jax JIT-able implementation of the
[Sudoku](https://en.wikipedia.org/wiki/Sudoku) puzzle game.


## Observation
The observation given to the agent consists of:


- `board`: jax array (int32) of shape (9,9):
empty cells are represented by -1, and filled cells are represented by 0-8.
Egiob marked this conversation as resolved.
Show resolved Hide resolved
- `action_mask`: jax array (bool) of shape (9,9,9):
indicates which actions are valid.



## Action
The action space is a `MultiDiscreteArray` of integer values representing coordinates of the square
to explore and the digits to write in the cell, e.g. `[3, 6, 8]` for writing the digit `9`
the cell located on the third row and sixth column.
Egiob marked this conversation as resolved.
Show resolved Hide resolved


## Reward
Egiob marked this conversation as resolved.
Show resolved Hide resolved
The reward is `1` at the end of the episode if the board is correctly solved, and `0` in every
other case.


## Registered Versions 📖
- `Sudoku-v0`, the classic [game](https://en.wikipedia.org/wiki/Sudoku) on
a 9x9 grid, 10000 random puzzles with mixed difficulty are included by default.
- `Sudoku-very-easy-v0`, the classic [game](https://en.wikipedia.org/wiki/Sudoku) on
Egiob marked this conversation as resolved.
Show resolved Hide resolved
a 9x9 grid, only 1000 very-easy random puzzles (>46 clues) included by default.

## Using custom puzzle instances
If one wants to include its own database of puzzles, the `DatabaseGenerator` can be initialized with any collection of puzzles using the argument `custom_boards`.
Some references for databases of puzzle of various difficulties:
- https://www.kaggle.com/datasets/rohanrao/sudoku
- https://www.kaggle.com/datasets/informoney/4-million-sudoku-puzzles-easytohard
4 changes: 2 additions & 2 deletions examples/training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"outputs": [],
"source": [
"env = \"maze\" # @param ['bin_pack', 'cleaner', 'connector', 'cvrp', 'game_2048', 'job_shop', 'knapsack', 'maze', 'minesweeper', 'rubiks_cube', 'snake', 'tsp']\n",
"env = \"maze\" # @param ['bin_pack', 'cleaner', 'connector', 'cvrp', 'game_2048', 'job_shop', 'knapsack', 'maze', 'minesweeper', 'rubiks_cube', 'snake', 'sudoku', 'tsp']\n",
"agent = \"random\" # @param ['random', 'a2c']"
]
},
Expand Down Expand Up @@ -105,7 +105,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down
14 changes: 14 additions & 0 deletions jumanji/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from jumanji.env import Environment
from jumanji.environments.logic.rubiks_cube import generator as rubik_generator
from jumanji.environments.logic.sudoku import generator as sudoku_generator
from jumanji.registration import make, register, registered_environments
from jumanji.version import __version__

Expand Down Expand Up @@ -42,6 +43,19 @@
kwargs={"time_limit": 20, "generator": partly_scrambled_rubiks_cube_generator},
)

# Sudoku - the standard Sudoku puzzle with grid of size 9x9. By default 10000 puzzles
# of mixed difficulties are set for reset.
register(id="Sudoku-v0", entry_point="jumanji.environments:Sudoku")

# Sudoku - the standard Sudoku puzzle with grid of size 9x9. 1000 puzzles of very-easy
# difficulty (>46 clues) are set for reset.
very_easy_sudoku_generator = sudoku_generator.DatabaseGenerator(level="very-easy")
register(
id="Sudoku-very-easy-v0",
entry_point="jumanji.environments:Sudoku",
kwargs={"generator": very_easy_sudoku_generator},
)


###
# Packing Environments
Expand Down
1 change: 1 addition & 0 deletions jumanji/environments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from jumanji.environments.logic.game_2048.env import Game2048
from jumanji.environments.logic.minesweeper import Minesweeper
from jumanji.environments.logic.rubiks_cube import RubiksCube
from jumanji.environments.logic.sudoku import Sudoku
from jumanji.environments.packing import bin_pack, job_shop, knapsack
from jumanji.environments.packing.bin_pack.env import BinPack
from jumanji.environments.packing.job_shop.env import JobShop
Expand Down
16 changes: 16 additions & 0 deletions jumanji/environments/logic/sudoku/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2022 InstaDeep Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from jumanji.environments.logic.sudoku.env import Sudoku
from jumanji.environments.logic.sudoku.types import Observation, State
24 changes: 24 additions & 0 deletions jumanji/environments/logic/sudoku/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2022 InstaDeep Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from jumanji.environments.logic.sudoku.env import Sudoku
from jumanji.environments.logic.sudoku.generator import DummyGenerator


@pytest.fixture
def sudoku_env() -> Sudoku:
"""Fixture for a default sudoku environment."""
return Sudoku(generator=DummyGenerator())
28 changes: 28 additions & 0 deletions jumanji/environments/logic/sudoku/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2022 InstaDeep Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BOARD_WIDTH: int = 9
Egiob marked this conversation as resolved.
Show resolved Hide resolved


BOX_IDX = [
[0, 1, 2, 9, 10, 11, 18, 19, 20],
[27, 28, 29, 36, 37, 38, 45, 46, 47],
[54, 55, 56, 63, 64, 65, 72, 73, 74],
[3, 4, 5, 12, 13, 14, 21, 22, 23],
[30, 31, 32, 39, 40, 41, 48, 49, 50],
[57, 58, 59, 66, 67, 68, 75, 76, 77],
[6, 7, 8, 15, 16, 17, 24, 25, 26],
[33, 34, 35, 42, 43, 44, 51, 52, 53],
[60, 61, 62, 69, 70, 71, 78, 79, 80],
]
Binary file not shown.
Binary file not shown.
Loading