|
| 1 | +""" |
| 2 | +Survo dataset, adapted for Reasoning Gym from SynthRL: https://github.com/MiniMax-AI/SynLogic/tree/main/games/tasks/survo |
| 3 | +""" |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from random import Random |
| 7 | +from typing import Any, Optional |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from ..coaching import BaseCurriculum, RangeAttributeDefinition |
| 12 | +from ..factory import ProceduralDataset, register_dataset |
| 13 | + |
| 14 | +DATASET_NAME = "survo" |
| 15 | + |
| 16 | +PROMPT_TEMPLATES = [ |
| 17 | + "Given a {n}*{n} matrix where the last element of each row and column equals the sum of the other elements in that row or column. The matrix is:\n{matrix}\nwhere some elements are replaced with 0. You have a set of numbers {numbers} that can be filled into the 0 positions to satisfy the rules. Please fill in the matrix. Each number can only be used once.", |
| 18 | + "You have a {n}*{n} matrix with some positions already filled with numbers and others marked with 0. The matrix is:\n{matrix}\nThe last number in each row and column represents the sum of all other numbers in that row or column. You need to fill in the 0 positions using the numbers {numbers} to satisfy these conditions. Each number can only be used once.", |
| 19 | + "Complete the following Survo puzzle. In this {n}*{n} matrix:\n{matrix}\nthe cells marked with 0 need to be filled with numbers. The last number in each row and column equals the sum of all other numbers in that row or column. You can use the following numbers: {numbers}. Each number can only be used once.", |
| 20 | + "In this {n}*{n} Survo matrix puzzle:\n{matrix}\nthe 0 cells need to be filled with numbers from the set {numbers}. The last element in each row and column is the sum of all other elements in that row or column. Each number can only be used once. Provide the completed matrix.", |
| 21 | + "Solve this {n}*{n} matrix puzzle:\n{matrix}\nwhere 0 represents empty cells that need to be filled. The last number in each row and column equals the sum of all other numbers in that row or column. You have the numbers {numbers} to place in the empty cells. Each number can only be used once.", |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class SurvoConfig: |
| 27 | + min_board_size: int = 4 |
| 28 | + max_board_size: int = 5 |
| 29 | + min_empty: int = 3 |
| 30 | + max_empty: int = 5 |
| 31 | + min_num: int = 1 |
| 32 | + max_num: int = 9 |
| 33 | + seed: Optional[int] = None |
| 34 | + size: int = 500 # Virtual dataset size |
| 35 | + |
| 36 | + def validate(self): |
| 37 | + """Validate configuration parameters""" |
| 38 | + assert self.min_board_size > 3, "min_board_size must be greater than 3" |
| 39 | + assert self.max_board_size >= self.min_board_size, "max_board_size must be >= min_board_size" |
| 40 | + assert self.min_empty > 0, "min_empty must be > 0" |
| 41 | + assert self.max_empty <= (self.min_board_size - 1) * ( |
| 42 | + self.min_board_size - 1 |
| 43 | + ), f"max_empty must be <= {(self.min_board_size - 1) * (self.min_board_size - 1)}" |
| 44 | + assert self.min_empty <= self.max_empty, "min_empty must be <= max_empty" |
| 45 | + assert self.min_num > 0, "min_num must be > 0" |
| 46 | + assert self.min_num < self.max_num, "min_num must be less than max_num" |
| 47 | + |
| 48 | + |
| 49 | +class SurvoDataset(ProceduralDataset): |
| 50 | + def __init__(self, config: SurvoConfig): |
| 51 | + super().__init__(config=config, seed=config.seed, size=config.size) |
| 52 | + |
| 53 | + def __len__(self) -> int: |
| 54 | + return self.config.size |
| 55 | + |
| 56 | + def __iter__(self): |
| 57 | + self._current_idx = 0 |
| 58 | + return self |
| 59 | + |
| 60 | + def __next__(self): |
| 61 | + if self._current_idx >= self.config.size: |
| 62 | + raise StopIteration |
| 63 | + item = self[self._current_idx] |
| 64 | + self._current_idx += 1 |
| 65 | + return item |
| 66 | + |
| 67 | + def __getitem__(self, idx: int) -> dict: |
| 68 | + rng = Random(self.config.seed + idx) |
| 69 | + |
| 70 | + board_size = rng.randint(self.config.min_board_size, self.config.max_board_size) |
| 71 | + num_empty = rng.randint(self.config.min_empty, self.config.max_empty) |
| 72 | + |
| 73 | + filled_matrix, puzzle, candidate_numbers = self._generate_valid_matrix( |
| 74 | + rng, board_size, num_empty, self.config.min_num, self.config.max_num |
| 75 | + ) |
| 76 | + |
| 77 | + puzzle_str = "\n".join(" ".join(str(x) for x in row) for row in puzzle) |
| 78 | + solution_str = "\n".join(" ".join(str(x) for x in row) for row in filled_matrix) |
| 79 | + |
| 80 | + question = rng.choice(PROMPT_TEMPLATES).format(n=board_size, matrix=puzzle_str, numbers=candidate_numbers) |
| 81 | + |
| 82 | + return { |
| 83 | + "question": question, |
| 84 | + "answer": solution_str, |
| 85 | + "metadata": { |
| 86 | + "source_dataset": DATASET_NAME, |
| 87 | + "source_idx": idx, |
| 88 | + "puzzle": puzzle.tolist(), |
| 89 | + "solution": filled_matrix.tolist(), |
| 90 | + "candidate_numbers": candidate_numbers, |
| 91 | + "board_size": board_size, |
| 92 | + "num_empty": num_empty, |
| 93 | + "min_num": self.config.min_num, |
| 94 | + "max_num": self.config.max_num, |
| 95 | + "difficulty": { |
| 96 | + "board_size": (self.config.min_board_size, self.config.max_board_size), |
| 97 | + "empty": (self.config.min_empty, self.config.max_empty), |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + def _generate_valid_matrix( |
| 103 | + self, rng: Random, n: int, num_empty: int, min_num: int, max_num: int |
| 104 | + ) -> tuple[np.ndarray, np.ndarray, list[int]]: |
| 105 | + matrix = np.zeros((n, n), dtype=int) |
| 106 | + |
| 107 | + for i in range(n - 1): |
| 108 | + for j in range(n - 1): |
| 109 | + matrix[i, j] = rng.randint(min_num, max_num) |
| 110 | + |
| 111 | + for i in range(n - 1): |
| 112 | + row_sum = sum(matrix[i, 0 : n - 1]) |
| 113 | + matrix[i, n - 1] = row_sum |
| 114 | + |
| 115 | + col_sum = sum(matrix[0 : n - 1, i]) |
| 116 | + matrix[n - 1, i] = col_sum |
| 117 | + |
| 118 | + matrix[n - 1, n - 1] = sum(matrix[0 : n - 1, n - 1]) |
| 119 | + |
| 120 | + filled_matrix = matrix.copy() |
| 121 | + |
| 122 | + positions = [(i, j) for i in range(n - 1) for j in range(n - 1)] |
| 123 | + selected_positions = rng.sample(positions, num_empty) |
| 124 | + |
| 125 | + candidate_numbers = [] |
| 126 | + for i, j in selected_positions: |
| 127 | + candidate_numbers.append(int(matrix[i, j])) |
| 128 | + matrix[i, j] = 0 |
| 129 | + |
| 130 | + return filled_matrix, matrix, candidate_numbers |
| 131 | + |
| 132 | + def score_answer(self, answer: Optional[str], entry: dict[str, Any]) -> float: |
| 133 | + if not isinstance(answer, str): |
| 134 | + return 0.0 |
| 135 | + |
| 136 | + board_size = entry["metadata"]["board_size"] |
| 137 | + grid = self._parse_grid(answer) |
| 138 | + true_grid = entry["metadata"]["solution"] |
| 139 | + |
| 140 | + if len(grid) != board_size or any(len(row) != board_size for row in grid): |
| 141 | + return 0.0 |
| 142 | + |
| 143 | + for i in range(board_size): |
| 144 | + for j in range(board_size): |
| 145 | + if grid[i][j] != true_grid[i][j]: |
| 146 | + return 0.0 |
| 147 | + |
| 148 | + return 1.0 |
| 149 | + |
| 150 | + def _parse_grid(self, answer: str) -> list[list[str]]: |
| 151 | + grid = [] |
| 152 | + for line in answer.strip().split("\n"): |
| 153 | + row = [] |
| 154 | + for c in line.strip().split(): |
| 155 | + try: |
| 156 | + row.append(int(c)) |
| 157 | + except ValueError: |
| 158 | + continue # Ignore non-integer values |
| 159 | + if row: |
| 160 | + grid.append(row) |
| 161 | + return grid |
| 162 | + |
| 163 | + |
| 164 | +class SurvoCurriculum(BaseCurriculum): |
| 165 | + def __init__(self): |
| 166 | + super().__init__(SurvoCurriculum.__name__, SurvoConfig) |
| 167 | + |
| 168 | + self._define_attributes( |
| 169 | + RangeAttributeDefinition( |
| 170 | + name="board_size", |
| 171 | + levels=[4, 5, 6, 7], |
| 172 | + description="Board size (n x n)", |
| 173 | + lower_field_name="min_board_size", |
| 174 | + upper_field_name="max_board_size", |
| 175 | + ), |
| 176 | + RangeAttributeDefinition( |
| 177 | + name="empty", |
| 178 | + levels=[4, 9, 16, 25], |
| 179 | + description="Number of empty cells", |
| 180 | + lower_field_name="min_empty", |
| 181 | + upper_field_name="max_empty", |
| 182 | + ), |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +register_dataset(DATASET_NAME, SurvoDataset, SurvoConfig, SurvoCurriculum) |
0 commit comments