mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-23 16:55:05 +00:00
formatting
This commit is contained in:
parent
98988c8481
commit
20069b2a7d
37 changed files with 504 additions and 666 deletions
|
|
@ -8,6 +8,7 @@ Algorithmic tasks for training reasoning capabilities:
|
|||
|
||||
from reasoning_gym.arithmetic.basic_arithmetic import basic_arithmetic_dataset
|
||||
from reasoning_gym.arithmetic.chain_sum import chain_sum_dataset
|
||||
|
||||
from .base_conversion import BaseConversionConfig, BaseConversionDataset, base_conversion_dataset
|
||||
from .letter_counting import LetterCountingConfig, LetterCountingDataset, letter_counting_dataset
|
||||
from .number_filtering import NumberFilteringConfig, NumberFilteringDataset, number_filtering_dataset
|
||||
|
|
@ -20,8 +21,8 @@ __all__ = [
|
|||
"BaseConversionDataset",
|
||||
"base_conversion_dataset",
|
||||
"chain_sum_dataset",
|
||||
"LetterCountingConfig",
|
||||
"LetterCountingDataset",
|
||||
"LetterCountingConfig",
|
||||
"LetterCountingDataset",
|
||||
"letter_counting_dataset",
|
||||
"NumberFilteringConfig",
|
||||
"NumberFilteringDataset",
|
||||
|
|
@ -31,5 +32,5 @@ __all__ = [
|
|||
"number_sorting_dataset",
|
||||
"WordReversalConfig",
|
||||
"WordReversalDataset",
|
||||
"word_reversal_dataset"
|
||||
"word_reversal_dataset",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,20 @@
|
|||
"""Base conversion task generator"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from random import Random
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class BaseConversionConfig:
|
||||
"""Configuration for base conversion task generation"""
|
||||
min_base: int = 2 # Minimum base (2=binary)
|
||||
max_base: int = 16 # Maximum base (16=hex)
|
||||
min_value: int = 0 # Minimum decimal value to convert
|
||||
max_value: int = 1000 # Maximum decimal value to convert
|
||||
|
||||
min_base: int = 2 # Minimum base (2=binary)
|
||||
max_base: int = 16 # Maximum base (16=hex)
|
||||
min_value: int = 0 # Minimum decimal value to convert
|
||||
max_value: int = 1000 # Maximum decimal value to convert
|
||||
seed: Optional[int] = None
|
||||
size: int = 500 # Virtual dataset size
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self):
|
||||
"""Validate configuration parameters"""
|
||||
|
|
@ -55,37 +58,37 @@ class BaseConversionDataset:
|
|||
def _generate_conversion(self, rng: Random) -> Tuple[int, int, int]:
|
||||
"""Generate random value and source/target bases"""
|
||||
value = rng.randint(self.config.min_value, self.config.max_value)
|
||||
|
||||
|
||||
# Choose source and target bases
|
||||
source_base = rng.randint(self.config.min_base, self.config.max_base)
|
||||
target_base = rng.randint(self.config.min_base, self.config.max_base)
|
||||
while target_base == source_base: # Ensure different bases
|
||||
target_base = rng.randint(self.config.min_base, self.config.max_base)
|
||||
|
||||
|
||||
return value, source_base, target_base
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single base conversion task"""
|
||||
rng = Random(self.seed + idx)
|
||||
|
||||
|
||||
value, source_base, target_base = self._generate_conversion(rng)
|
||||
|
||||
|
||||
# Convert decimal to source base representation
|
||||
source_repr = format(value, f'x' if source_base == 16 else f'b' if source_base == 2 else '').strip()
|
||||
source_repr = format(value, f"x" if source_base == 16 else f"b" if source_base == 2 else "").strip()
|
||||
if source_base not in (2, 16):
|
||||
source_repr = format(value, f'{source_base}x').lower().strip()
|
||||
|
||||
source_repr = format(value, f"{source_base}x").lower().strip()
|
||||
|
||||
# Convert decimal to target base for answer
|
||||
target_repr = format(value, f'x' if target_base == 16 else f'b' if target_base == 2 else '').strip()
|
||||
target_repr = format(value, f"x" if target_base == 16 else f"b" if target_base == 2 else "").strip()
|
||||
if target_base not in (2, 16):
|
||||
target_repr = format(value, f'{target_base}x').lower().strip()
|
||||
|
||||
target_repr = format(value, f"{target_base}x").lower().strip()
|
||||
|
||||
source_name = self._format_base_name(source_base)
|
||||
target_name = self._format_base_name(target_base)
|
||||
|
||||
|
||||
# Add hint for bases > 10 about using lowercase letters
|
||||
hint = " (use lowercase letters a-z for digits above 9)" if target_base > 10 else ""
|
||||
|
||||
|
||||
return {
|
||||
"question": f"Convert the {source_name} number {source_repr} to {target_name}{hint}",
|
||||
"answer": target_repr,
|
||||
|
|
@ -94,8 +97,8 @@ class BaseConversionDataset:
|
|||
"source_base": source_base,
|
||||
"target_base": target_base,
|
||||
"source_repr": source_repr,
|
||||
"target_repr": target_repr
|
||||
}
|
||||
"target_repr": target_repr,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,21 @@
|
|||
"""Letter counting task generator"""
|
||||
from dataclasses import dataclass
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from random import Random
|
||||
from typing import List, Optional
|
||||
|
||||
from reasoning_gym.data import read_data_file
|
||||
|
||||
|
||||
@dataclass
|
||||
class LetterCountingConfig:
|
||||
"""Configuration for letter counting task generation"""
|
||||
min_words: int = 5 # Minimum words in span
|
||||
max_words: int = 15 # Maximum words in span
|
||||
|
||||
min_words: int = 5 # Minimum words in span
|
||||
max_words: int = 15 # Maximum words in span
|
||||
seed: Optional[int] = None
|
||||
size: int = 500 # Virtual dataset size
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self):
|
||||
"""Validate configuration parameters"""
|
||||
|
|
@ -27,11 +30,11 @@ class LetterCountingDataset:
|
|||
self.config = config
|
||||
self.config.validate()
|
||||
self.seed = config.seed if config.seed is not None else Random().randint(0, 2**32)
|
||||
|
||||
|
||||
# Load and preprocess text
|
||||
text = read_data_file("in_the_year_2889.txt")
|
||||
# Extract words and clean them to contain only alphanumeric characters
|
||||
self.words = [word for word in re.findall(r'\b\w+\b', text) if word.isalnum()]
|
||||
self.words = [word for word in re.findall(r"\b\w+\b", text) if word.isalnum()]
|
||||
|
||||
def __len__(self) -> int:
|
||||
return self.config.size
|
||||
|
|
@ -50,31 +53,27 @@ class LetterCountingDataset:
|
|||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single letter counting task"""
|
||||
rng = Random(self.seed + idx)
|
||||
|
||||
|
||||
# Select random span of words
|
||||
span_length = rng.randint(self.config.min_words, self.config.max_words)
|
||||
start_idx = rng.randint(0, len(self.words) - span_length)
|
||||
span = self.words[start_idx:start_idx + span_length]
|
||||
|
||||
span = self.words[start_idx : start_idx + span_length]
|
||||
|
||||
# Get all unique letters from span
|
||||
letters = set(''.join(span).lower())
|
||||
letters = set("".join(span).lower())
|
||||
if not letters:
|
||||
letters = {'a'} # Fallback if span has no letters
|
||||
|
||||
letters = {"a"} # Fallback if span has no letters
|
||||
|
||||
# Select random letter that appears in the span
|
||||
target_letter = rng.choice(list(letters))
|
||||
|
||||
|
||||
# Count occurrences
|
||||
count = sum(word.lower().count(target_letter) for word in span)
|
||||
|
||||
|
||||
return {
|
||||
"question": f'How many times does the letter "{target_letter}" appear in the text: "{" ".join(span)}"?',
|
||||
"answer": str(count),
|
||||
"metadata": {
|
||||
"span_length": span_length,
|
||||
"target_letter": target_letter,
|
||||
"span": span
|
||||
}
|
||||
"metadata": {"span_length": span_length, "target_letter": target_letter, "span": span},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,23 @@
|
|||
"""Number filtering task generator"""
|
||||
from dataclasses import dataclass
|
||||
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
from random import Random
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class NumberFilteringConfig:
|
||||
"""Configuration for number filtering task generation"""
|
||||
min_numbers: int = 3 # Minimum numbers in list
|
||||
max_numbers: int = 10 # Maximum numbers in list
|
||||
min_decimals: int = 0 # Minimum decimal places
|
||||
max_decimals: int = 4 # Maximum decimal places
|
||||
min_value: float = -100.0 # Minimum number value
|
||||
max_value: float = 100.0 # Maximum number value
|
||||
|
||||
min_numbers: int = 3 # Minimum numbers in list
|
||||
max_numbers: int = 10 # Maximum numbers in list
|
||||
min_decimals: int = 0 # Minimum decimal places
|
||||
max_decimals: int = 4 # Maximum decimal places
|
||||
min_value: float = -100.0 # Minimum number value
|
||||
max_value: float = 100.0 # Maximum number value
|
||||
seed: Optional[int] = None
|
||||
size: int = 500 # Virtual dataset size
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self):
|
||||
"""Validate configuration parameters"""
|
||||
|
|
@ -56,23 +59,23 @@ class NumberFilteringDataset:
|
|||
count = rng.randint(self.config.min_numbers, self.config.max_numbers)
|
||||
numbers = []
|
||||
str_numbers = []
|
||||
|
||||
|
||||
for _ in range(count):
|
||||
num = rng.uniform(self.config.min_value, self.config.max_value)
|
||||
decimals = rng.randint(self.config.min_decimals, self.config.max_decimals)
|
||||
str_num = self._format_number(num, decimals)
|
||||
numbers.append(float(str_num)) # Convert back to simulate precision loss
|
||||
str_numbers.append(str_num)
|
||||
|
||||
|
||||
return numbers, str_numbers
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single number filtering task"""
|
||||
rng = Random(self.seed + idx)
|
||||
|
||||
|
||||
# Generate numbers and their string representations
|
||||
numbers, str_numbers = self._generate_numbers(rng)
|
||||
|
||||
|
||||
# Determine filter value between min and max of generated numbers
|
||||
min_val = min(numbers)
|
||||
max_val = max(numbers)
|
||||
|
|
@ -80,31 +83,33 @@ class NumberFilteringDataset:
|
|||
decimals = rng.randint(self.config.min_decimals, self.config.max_decimals)
|
||||
filter_str = self._format_number(filter_value, decimals)
|
||||
filter_value = float(filter_str) # Convert back to simulate precision loss
|
||||
|
||||
|
||||
# Randomly choose filter operation
|
||||
keep_larger = rng.choice([True, False])
|
||||
larger_smaller = "larger" if keep_larger else "smaller"
|
||||
keep_remove = "keep" if rng.choice([True, False]) else "remove"
|
||||
|
||||
|
||||
# Apply filter based on chosen operation
|
||||
if keep_remove == "keep":
|
||||
result = [n for n in numbers if (n > filter_value if keep_larger else n < filter_value)]
|
||||
else: # remove
|
||||
result = [n for n in numbers if (n <= filter_value if keep_larger else n >= filter_value)]
|
||||
|
||||
|
||||
# Format results as strings with original precision
|
||||
result_strs = [str_numbers[numbers.index(n)] for n in result]
|
||||
|
||||
|
||||
return {
|
||||
"question": (f"{keep_remove.capitalize()} all numbers {larger_smaller} than {filter_str} "
|
||||
f"in this list: {str_numbers}"),
|
||||
"question": (
|
||||
f"{keep_remove.capitalize()} all numbers {larger_smaller} than {filter_str} "
|
||||
f"in this list: {str_numbers}"
|
||||
),
|
||||
"answer": str(result_strs) if result_strs else "[]",
|
||||
"metadata": {
|
||||
"original_numbers": str_numbers,
|
||||
"filter_value": filter_str,
|
||||
"operation": f"{keep_remove}_{larger_smaller}",
|
||||
"result": result_strs
|
||||
}
|
||||
"result": result_strs,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,23 @@
|
|||
"""Number sorting task generator"""
|
||||
from dataclasses import dataclass
|
||||
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
from random import Random
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class NumberSortingConfig:
|
||||
"""Configuration for number sorting task generation"""
|
||||
min_numbers: int = 3 # Minimum numbers to sort
|
||||
max_numbers: int = 10 # Maximum numbers to sort
|
||||
min_decimals: int = 0 # Minimum decimal places
|
||||
max_decimals: int = 2 # Maximum decimal places
|
||||
|
||||
min_numbers: int = 3 # Minimum numbers to sort
|
||||
max_numbers: int = 10 # Maximum numbers to sort
|
||||
min_decimals: int = 0 # Minimum decimal places
|
||||
max_decimals: int = 2 # Maximum decimal places
|
||||
min_value: float = -100.0 # Minimum value
|
||||
max_value: float = 100.0 # Maximum value
|
||||
max_value: float = 100.0 # Maximum value
|
||||
seed: Optional[int] = None
|
||||
size: int = 500 # Virtual dataset size
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self):
|
||||
"""Validate configuration parameters"""
|
||||
|
|
@ -57,10 +60,10 @@ class NumberSortingDataset:
|
|||
"""Generate list of numbers and their string representations"""
|
||||
count = rng.randint(self.config.min_numbers, self.config.max_numbers)
|
||||
decimals = rng.randint(self.config.min_decimals, self.config.max_decimals)
|
||||
|
||||
|
||||
numbers = []
|
||||
number_strs = []
|
||||
|
||||
|
||||
for _ in range(count):
|
||||
num = rng.uniform(self.config.min_value, self.config.max_value)
|
||||
num_str = self._format_number(num, decimals)
|
||||
|
|
@ -68,37 +71,33 @@ class NumberSortingDataset:
|
|||
num = float(num_str)
|
||||
numbers.append(num)
|
||||
number_strs.append(num_str)
|
||||
|
||||
|
||||
return numbers, number_strs
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single sorting task"""
|
||||
rng = Random(self.seed + idx)
|
||||
|
||||
|
||||
numbers, number_strs = self._generate_numbers(rng)
|
||||
|
||||
|
||||
# Generate both ascending and descending answers
|
||||
asc_numbers = sorted(numbers)
|
||||
desc_numbers = sorted(numbers, reverse=True)
|
||||
|
||||
|
||||
# Format answers as string lists
|
||||
decimals = len(number_strs[0].split('.')[-1]) if '.' in number_strs[0] else 0
|
||||
decimals = len(number_strs[0].split(".")[-1]) if "." in number_strs[0] else 0
|
||||
asc_answer = [self._format_number(n, decimals) for n in asc_numbers]
|
||||
desc_answer = [self._format_number(n, decimals) for n in desc_numbers]
|
||||
|
||||
|
||||
# Randomly choose ascending or descending
|
||||
is_ascending = rng.choice([True, False])
|
||||
direction = "ascending" if is_ascending else "descending"
|
||||
answer = asc_answer if is_ascending else desc_answer
|
||||
|
||||
|
||||
return {
|
||||
"question": f"Sort these numbers in {direction} order: {', '.join(number_strs)}",
|
||||
"answer": str(answer),
|
||||
"metadata": {
|
||||
"original_numbers": number_strs,
|
||||
"direction": direction,
|
||||
"sorted_numbers": answer
|
||||
}
|
||||
"metadata": {"original_numbers": number_strs, "direction": direction, "sorted_numbers": answer},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,21 @@
|
|||
"""Word reversal task generator"""
|
||||
from dataclasses import dataclass
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from random import Random
|
||||
from typing import List, Optional
|
||||
|
||||
from reasoning_gym.data import read_data_file
|
||||
|
||||
|
||||
@dataclass
|
||||
class WordReversalConfig:
|
||||
"""Configuration for word reversal task generation"""
|
||||
min_words: int = 3 # Minimum words in list
|
||||
max_words: int = 8 # Maximum words in list
|
||||
|
||||
min_words: int = 3 # Minimum words in list
|
||||
max_words: int = 8 # Maximum words in list
|
||||
seed: Optional[int] = None
|
||||
size: int = 500 # Virtual dataset size
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self):
|
||||
"""Validate configuration parameters"""
|
||||
|
|
@ -27,11 +30,11 @@ class WordReversalDataset:
|
|||
self.config = config
|
||||
self.config.validate()
|
||||
self.seed = config.seed if config.seed is not None else Random().randint(0, 2**32)
|
||||
|
||||
|
||||
# Load and preprocess text
|
||||
text = read_data_file("in_the_year_2889.txt")
|
||||
# Extract words and clean them to contain only alphanumeric characters
|
||||
self.words = [word for word in re.findall(r'\b\w+\b', text) if word.isalnum()]
|
||||
self.words = [word for word in re.findall(r"\b\w+\b", text) if word.isalnum()]
|
||||
|
||||
def __len__(self) -> int:
|
||||
return self.config.size
|
||||
|
|
@ -50,23 +53,20 @@ class WordReversalDataset:
|
|||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single word reversal task"""
|
||||
rng = Random(self.seed + idx)
|
||||
|
||||
|
||||
# Select random words
|
||||
num_words = rng.randint(self.config.min_words, self.config.max_words)
|
||||
word_indices = rng.sample(range(len(self.words)), num_words)
|
||||
words = [self.words[i] for i in word_indices]
|
||||
|
||||
|
||||
# Create question and answer
|
||||
question = ", ".join(words)
|
||||
answer = ", ".join(reversed(words))
|
||||
|
||||
|
||||
return {
|
||||
"question": f"Reverse this list of words: {question}",
|
||||
"answer": answer,
|
||||
"metadata": {
|
||||
"num_words": num_words,
|
||||
"words": words
|
||||
}
|
||||
"metadata": {"num_words": num_words, "words": words},
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue