alea-runs / alea /envs /base.py
Akahsizrr's picture
Upload alea/envs/base.py with huggingface_hub
fadcdf3 verified
Raw History Blame
2.48 kB
"""DecisionEnv protocol for Alea RL environments.
Every env produces a stream of DECISION POINTS. At each one it emits a
(state_text, questions) observation in Alea's packed-request format, the
policy answers, and the env scores it.
Two reward modes, env-dependent:
- "dense": env.action_rewards() returns an exact per-action reward vector
(regret-normalized so the optimal action = 1.0). GRPO samples k actions
and scores each exactly — no rollout branching needed.
- "outcome": env.step() returns only the realized scalar reward; terminal
returns are assigned to every decision in the episode (GRPO over k
full-episode trajectories via clone()).
An env may also emit REPORT questions (score/regress/noul with exact
targets — e.g., "what is your belief over hidden states?") alongside the
action question. Report targets come from oracle_targets() and are scored
with the supervised proper-scoring losses — a variance-free calibration
signal no generative model can match.
"""
from __future__ import annotations
import copy
from dataclasses import dataclass
from ..packing import QuestionSpec
ACT_QID = "act"
@dataclass
class Decision:
"""One decision point."""
state: str # text/JSON the model reads
questions: list # [QuestionSpec]; must contain qid=ACT_QID
n_legal: int # how many leading options are legal
class DecisionEnv:
"""Base class. Subclasses implement reset/observe/step."""
name = "base"
dense = False # True -> action_rewards() available
def reset(self, rng):
raise NotImplementedError
def observe(self) -> Decision:
"""Current state + questions. questions[0] must be the action
question (qid=ACT_QID); its options are the action labels."""
raise NotImplementedError
def action_rewards(self):
"""Dense mode only: exact reward per legal action, normalized so
max == 1.0 (optimal). Return None if unavailable."""
return None
def oracle_targets(self) -> dict:
"""Exact targets for REPORT questions (qid -> target), and optionally
the optimal action distribution under key ACT_QID."""
return {}
def step(self, action_idx: int):
"""Apply action -> (reward, done)."""
raise NotImplementedError
def clone(self):
return copy.deepcopy(self)
@property
def done(self):
return self._done