mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-19 12:58:07 +00:00
Merge branch 'main' into rich/decimalmath
This commit is contained in:
commit
edba52d2a2
37 changed files with 3369 additions and 157 deletions
|
|
@ -6,12 +6,14 @@ from .basic_arithmetic import BasicArithmeticDataset, BasicArithmeticDatasetConf
|
|||
from .calendar_arithmetic import CalendarArithmeticConfig, CalendarArithmeticDataset
|
||||
from .chain_sum import ChainSumConfig, ChainSumDataset
|
||||
from .count_bits import CountBitsConfig, CountBitsDataset
|
||||
from .decimal_chain_sum import DecimalChainSumConfig, DecimalChainSumDataset
|
||||
from .dice import DiceConfig, DiceDataset
|
||||
from .fraction_simplification import FractionSimplificationConfig, FractionSimplificationDataset
|
||||
from .gcd import GCDConfig, GCDDataset
|
||||
from .gsm_symbolic.gsm_symbolic import GSMSymbolicDataset, GSMSymbolicDatasetConfig
|
||||
from .lcm import LCMConfig, LCMDataset
|
||||
from .leg_counting import LegCountingConfig, LegCountingDataset
|
||||
from .number_format import NumberFormatConfig, NumberFormatDataset
|
||||
from .power_function import PowerFunctionConfig, PowerFunctionDataset
|
||||
from .prime_factorization import PrimeFactorizationConfig, PrimeFactorizationDataset
|
||||
from .products import ProductsConfig, ProductsDataset
|
||||
|
|
@ -46,4 +48,6 @@ __all__ = [
|
|||
"CountBitsDataset",
|
||||
"DiceConfig",
|
||||
"DiceDataset",
|
||||
"NumberFormatConfig",
|
||||
"NumberFormatDataset",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -27,10 +27,6 @@ class ChainSumConfig:
|
|||
assert self.min_digits > 0, "min_digits must be positive"
|
||||
assert self.max_digits >= self.min_digits, "max_digits must be >= min_digits"
|
||||
|
||||
# Validate digit ranges make sense
|
||||
if self.min_digits > 1:
|
||||
assert 10 ** (self.min_digits - 1) >= 1, "min_digits would result in invalid number range"
|
||||
|
||||
|
||||
class ChainSumDataset(ProceduralDataset):
|
||||
"""Generates simple arithmetic tasks using only + and - operators"""
|
||||
|
|
|
|||
157
reasoning_gym/arithmetic/decimal_chain_sum.py
Normal file
157
reasoning_gym/arithmetic/decimal_chain_sum.py
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
import random
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from ..coaching import AttributeType, BaseCurriculum, RangeAttributeDefinition
|
||||
from ..factory import ProceduralDataset, register_dataset
|
||||
|
||||
|
||||
@dataclass
|
||||
class DecimalChainSumConfig:
|
||||
"""Configuration for decimal chain sum task generation"""
|
||||
|
||||
min_terms: int = 2
|
||||
max_terms: int = 6
|
||||
min_digits: int = 1
|
||||
max_digits: int = 4
|
||||
min_decimal_places: int = 1
|
||||
max_decimal_places: int = 4
|
||||
allow_negation: bool = False
|
||||
seed: Optional[int] = None
|
||||
size: int = 500
|
||||
|
||||
def validate(self) -> None:
|
||||
"""Validate configuration parameters"""
|
||||
assert self.size > 0, "size must be positive"
|
||||
assert self.min_terms > 0, "min_terms must be positive"
|
||||
assert self.max_terms >= self.min_terms, "max_terms must be >= min_terms"
|
||||
assert self.min_digits > 0, "min_digits must be positive"
|
||||
assert self.max_digits >= self.min_digits, "max_digits must be >= min_digits"
|
||||
assert self.min_decimal_places >= 0, "min_decimal_places must be non-negative"
|
||||
assert self.max_decimal_places >= self.min_decimal_places, "max_decimal_places must be >= min_decimal_places"
|
||||
|
||||
|
||||
class DecimalChainSumDataset(ProceduralDataset):
|
||||
"""Generates simple decimal arithmetic tasks using only + and - operators"""
|
||||
|
||||
def __init__(self, config: DecimalChainSumConfig):
|
||||
super().__init__(config=config, seed=config.seed, size=config.size)
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single decimal chain sum task
|
||||
|
||||
Args:
|
||||
idx: Index of the item to generate
|
||||
|
||||
Returns:
|
||||
dict with keys:
|
||||
- question: str, the formatted arithmetic expression
|
||||
- answer: str, the ground truth result
|
||||
- metadata: dict with generation parameters
|
||||
"""
|
||||
|
||||
rng = random.Random(self.seed + idx)
|
||||
|
||||
num_terms = rng.randint(self.config.min_terms, self.config.max_terms)
|
||||
num_digits = rng.randint(self.config.min_digits, self.config.max_digits)
|
||||
|
||||
# Calculate value ranges based on number of digits
|
||||
min_value = 0 if num_digits == 1 else 10 ** (num_digits - 1) # Special case for 1 digit
|
||||
max_value = (10**num_digits) - 1 # e.g., 999 for 3 digits
|
||||
|
||||
expression, result = self._generate_task(rng, num_terms, min_value, max_value)
|
||||
|
||||
return {
|
||||
"question": f"State the final answer to the following arithmetic problem: {expression} =",
|
||||
"answer": str(result),
|
||||
"metadata": {
|
||||
"difficulty": {
|
||||
"num_terms": num_terms,
|
||||
"num_digits": num_digits,
|
||||
},
|
||||
"expression": expression,
|
||||
},
|
||||
}
|
||||
|
||||
def _generate_task(self, rng: random.Random, num_terms: int, min_value: int, max_value: int) -> tuple[str, Decimal]:
|
||||
"""Generate a single decimal chain sum task
|
||||
|
||||
Args:
|
||||
rng: Random number generator
|
||||
num_terms: Number of terms in the expression
|
||||
min_value: Minimum value for generated numbers
|
||||
max_value: Maximum value for generated numbers
|
||||
min_decimal_places: Minimum number of decimal places
|
||||
max_decimal_places: Maximum number of decimal places
|
||||
|
||||
Returns:
|
||||
Tuple of (expression string, result Decimal)
|
||||
"""
|
||||
|
||||
# Convert constants to Decimal
|
||||
constants = [
|
||||
Decimal(
|
||||
str(
|
||||
rng.randint(-max_value, max_value)
|
||||
if self.config.allow_negation
|
||||
else rng.randint(min_value, max_value)
|
||||
)
|
||||
)
|
||||
for _ in range(num_terms)
|
||||
]
|
||||
|
||||
# Generate decimal places for each term
|
||||
decimal_places = [
|
||||
rng.randint(self.config.min_decimal_places, self.config.max_decimal_places) for _ in range(num_terms)
|
||||
]
|
||||
|
||||
# Add decimal parts using Decimal for precise arithmetic
|
||||
for i in range(num_terms):
|
||||
min_val = 0 if decimal_places[i] == 0 else 10 ** (decimal_places[i] - 1)
|
||||
max_val = (10 ** decimal_places[i]) - 1
|
||||
decimal_part = Decimal(str(rng.randint(min_val, max_val))) / Decimal(str(10 ** decimal_places[i]))
|
||||
constants[i] += decimal_part
|
||||
|
||||
operators = [rng.choice(["+", "-"]) for _ in range(num_terms - 1)]
|
||||
|
||||
expression_parts = []
|
||||
result = constants[0]
|
||||
|
||||
expression_parts.append(f"{constants[0]:.{decimal_places[0]}f}")
|
||||
for i, op in enumerate(operators):
|
||||
c = constants[i + 1]
|
||||
expression_parts.append(op)
|
||||
expression_parts.append(f"{c:.{decimal_places[i+1]}f}")
|
||||
|
||||
if op == "+":
|
||||
result += c
|
||||
else: # op == "-"
|
||||
result -= c
|
||||
|
||||
expression = " ".join(expression_parts)
|
||||
result = result.quantize(Decimal(f"0.{'0' * max(decimal_places)}"))
|
||||
return expression, result
|
||||
|
||||
def score_answer(self, answer: Optional[str], entry: Dict[str, Any]) -> float:
|
||||
"""Score the answer by comparing decimal values instead of strings.
|
||||
Args:
|
||||
answer: The answer to score
|
||||
entry: The entry containing the oracle answer
|
||||
|
||||
Returns:
|
||||
1.0 for exact numerical match, 0.01 otherwise
|
||||
"""
|
||||
if answer is None or len(answer.strip()) == 0:
|
||||
return 0.0
|
||||
|
||||
try:
|
||||
student_answer = Decimal(answer.strip())
|
||||
oracle_answer = Decimal(entry["answer"])
|
||||
|
||||
return 1.0 if student_answer == oracle_answer else 0.01
|
||||
except (ValueError, TypeError, ArithmeticError):
|
||||
return 0.01
|
||||
|
||||
|
||||
register_dataset("decimal_chain_sum", DecimalChainSumDataset, DecimalChainSumConfig)
|
||||
106
reasoning_gym/arithmetic/number_format.py
Normal file
106
reasoning_gym/arithmetic/number_format.py
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
"""Choose largest number out of several represented in various formats."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from random import Random
|
||||
from typing import Dict, Optional
|
||||
|
||||
from ..factory import ProceduralDataset, register_dataset
|
||||
|
||||
QUESTION_TEMPLATE = """Your task is to pick the largest/smallest number out of several options.
|
||||
|
||||
Example
|
||||
- Input: Pick the largest number of the following candidates: 857575.23 8.975554e+05 887,555.62
|
||||
- Output: 8.975554e+05
|
||||
- Explanation:
|
||||
- Sorting the numbers written in various notations we get: 857575.23 < 887,555.62 < 8.975554e+05
|
||||
- Therefore, the largest number is 8.975554e+05
|
||||
|
||||
Now, pick the {size} number of the following candidates: {numbers}
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class NumberFormatConfig:
|
||||
"""Configuration for Count Bits dataset generation"""
|
||||
|
||||
max_num_candidates: int = 5 # Maximum number of candidates
|
||||
min_n: float = 1_000 # Lower bound for the numbers
|
||||
max_n: float = 1_000_000_000 # Upper bound for the numbers
|
||||
max_delta: int = 1_000
|
||||
|
||||
size: int = 500 # Virtual dataset size
|
||||
seed: Optional[int] = None
|
||||
|
||||
def validate(self):
|
||||
"""Validate configuration parameters"""
|
||||
assert 2 <= self.max_num_candidates, "max_num_candidates must be at least 2"
|
||||
assert 1 <= self.min_n, "min_n must be at least 1"
|
||||
assert self.min_n < self.max_n, "min_n must be less than max_n"
|
||||
assert 1 <= self.max_delta, "max_delta must be at least 1"
|
||||
|
||||
|
||||
class NumberFormatDataset(ProceduralDataset):
|
||||
"""Generates Count Bits exercises with configurable difficulty"""
|
||||
|
||||
def __init__(self, config: NumberFormatConfig):
|
||||
super().__init__(config=config, seed=config.seed, size=config.size)
|
||||
|
||||
def _get_candidates(self, rng: Random, num_candidates: int) -> list:
|
||||
"""Generate a list of candidates"""
|
||||
base = round(rng.uniform(self.config.min_n, self.config.max_n), 3)
|
||||
candidates = [base]
|
||||
for _ in range(num_candidates - 1):
|
||||
delta = round(rng.uniform(-self.config.max_delta, self.config.max_delta), 3)
|
||||
candidates.append(base + delta)
|
||||
return candidates
|
||||
|
||||
def _transform_candidates(self, rng: Random, candidates: list[float]) -> list[str]:
|
||||
"""Randomly apply different number formats to the candidates"""
|
||||
output = []
|
||||
for candidate in candidates:
|
||||
format_type = rng.choice(["standard", "english", "scientific"])
|
||||
if format_type == "standard":
|
||||
output.append(f"{candidate:f}")
|
||||
elif format_type == "english":
|
||||
output.append(f"{candidate:,}")
|
||||
elif format_type == "scientific":
|
||||
output.append(f"{candidate:.15e}")
|
||||
return output
|
||||
|
||||
def score_answer(self, answer: Optional[str], entry: Dict[str, any]) -> float:
|
||||
"""Overwrite this method in derived classes if a single oracle answer is not available."""
|
||||
oracle_answer = entry["metadata"]["solution"]
|
||||
if answer is not None and len(answer) > 0:
|
||||
try:
|
||||
answer = float(answer.strip().replace(",", ""))
|
||||
if abs(answer - oracle_answer) < 1e-2:
|
||||
return 1.0
|
||||
return 0.01
|
||||
except:
|
||||
return 0.0
|
||||
return 0.0
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
"""Generate a single Count Bits question"""
|
||||
rng = Random(self.seed + idx)
|
||||
|
||||
num_candidates = rng.randint(2, self.config.max_num_candidates)
|
||||
candidates = self._get_candidates(rng, num_candidates)
|
||||
formatted_candidates = self._transform_candidates(rng, candidates)
|
||||
|
||||
size = rng.choice(["largest", "smallest"])
|
||||
answer = max(candidates) if size == "largest" else min(candidates)
|
||||
|
||||
return {
|
||||
"question": QUESTION_TEMPLATE.format(numbers=" ".join(formatted_candidates), size=size),
|
||||
"answer": str(answer),
|
||||
"metadata": {
|
||||
"candidates": candidates,
|
||||
"solution": answer,
|
||||
"formatted_candidates": formatted_candidates,
|
||||
"size": size,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
register_dataset("number_format", NumberFormatDataset, NumberFormatConfig)
|
||||
Loading…
Add table
Add a link
Reference in a new issue