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(env): Mahjong Puzzle Curriculum #263

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion reasoning_gym/games/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .emoji_mystery import EmojiMysteryConfig, EmojiMysteryDataset
from .futoshiki import FutoshikiConfig, FutoshikiDataset
from .knight_swap import KnightSwapConfig, KnightSwapDataset
from .mahjong import MahjongPuzzleConfig, MahjongPuzzleDataset
from .mahjong import MahjongPuzzleConfig, MahjongPuzzleCurriculum, MahjongPuzzleDataset
from .maze import MazeConfig, MazeDataset
from .mini_sudoku import MiniSudokuConfig, MiniSudokuDataset
from .n_queens import NQueensDataset
Expand Down Expand Up @@ -46,4 +46,5 @@
"KnightSwapDataset",
"MahjongPuzzleConfig",
"MahjongPuzzleDataset",
"MahjongPuzzleCurriculum",
]
22 changes: 21 additions & 1 deletion reasoning_gym/games/mahjong.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from random import Random
from typing import Optional

from ..coaching import AttributeType, BaseCurriculum, RangeAttributeDefinition
from ..factory import ProceduralDataset, register_dataset

QUESTION_TEMPLATE = """There are several letter cards, and the game rules are as follows:
Expand Down Expand Up @@ -44,7 +45,7 @@ class MahjongPuzzleConfig:

def validate(self):
"""Validate configuration parameters"""
assert 1 <= self.min_num_rounds, "min_num_rounds must be reater than 0"
assert 1 <= self.min_num_rounds, "min_num_rounds must be greater than 0"
assert self.min_num_rounds <= self.max_num_rounds, "min_num_rounds must be less than max_num_rounds"


Expand Down Expand Up @@ -128,4 +129,23 @@ def __getitem__(self, idx: int) -> dict:
}


class MahjongPuzzleCurriculum(BaseCurriculum):
def __init__(self):
super().__init__(MahjongPuzzleCurriculum.__name__, MahjongPuzzleConfig)

# Define attributes
self._define_attributes(
RangeAttributeDefinition(
name="num_rounds",
levels=[10, 50, 100, 500],
default_level=0,
description="Number of rounds in the game",
attr_type=AttributeType.APPEND,
min_value=1,
lower_field_name="min_num_rounds",
upper_field_name="max_num_rounds",
)
)


register_dataset("mahjong_puzzle", MahjongPuzzleDataset, MahjongPuzzleConfig)
23 changes: 22 additions & 1 deletion tests/test_mahjong_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from reasoning_gym.games.mahjong import MahjongPuzzleConfig, MahjongPuzzleDataset
from reasoning_gym.games.mahjong import MahjongPuzzleConfig, MahjongPuzzleCurriculum, MahjongPuzzleDataset


def test_mahjong_puzzle_config_validation():
Expand Down Expand Up @@ -95,3 +95,24 @@ def test_mahjong_puzzle_answer():
for c in string.ascii_lowercase:
assert dataset._check_peng(cards, new_card=c) == False
assert dataset._check_chi(cards, new_card=c) == False


def test_mahjong_puzzle_curriculum():
curriculum = MahjongPuzzleCurriculum()

base_value = {"size": 150, "seed": 1}

base_cfg: MahjongPuzzleConfig = curriculum.generate_configuration(base_value)
assert base_cfg.seed == 1
assert base_cfg.size == 150
assert base_cfg.min_num_rounds == 10 and base_cfg.max_num_rounds == 10

# test incrementing attribute levels for num_rounds attribute
curriculum.increment_attr_level("num_rounds")
increased_cfg = curriculum.generate_configuration(base_value)
assert increased_cfg.min_num_rounds == 10 and increased_cfg.max_num_rounds == 50

# test incrementing again
curriculum.increment_attr_level("num_rounds")
increased_cfg = curriculum.generate_configuration(base_value)
assert increased_cfg.min_num_rounds == 10 and increased_cfg.max_num_rounds == 100