Upload alea/envs/base.py with huggingface_hub
Browse files- alea/envs/base.py +71 -0
alea/envs/base.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""DecisionEnv protocol for Alea RL environments.
|
| 2 |
+
|
| 3 |
+
Every env produces a stream of DECISION POINTS. At each one it emits a
|
| 4 |
+
(state_text, questions) observation in Alea's packed-request format, the
|
| 5 |
+
policy answers, and the env scores it.
|
| 6 |
+
|
| 7 |
+
Two reward modes, env-dependent:
|
| 8 |
+
- "dense": env.action_rewards() returns an exact per-action reward vector
|
| 9 |
+
(regret-normalized so the optimal action = 1.0). GRPO samples k actions
|
| 10 |
+
and scores each exactly — no rollout branching needed.
|
| 11 |
+
- "outcome": env.step() returns only the realized scalar reward; terminal
|
| 12 |
+
returns are assigned to every decision in the episode (GRPO over k
|
| 13 |
+
full-episode trajectories via clone()).
|
| 14 |
+
|
| 15 |
+
An env may also emit REPORT questions (score/regress/noul with exact
|
| 16 |
+
targets — e.g., "what is your belief over hidden states?") alongside the
|
| 17 |
+
action question. Report targets come from oracle_targets() and are scored
|
| 18 |
+
with the supervised proper-scoring losses — a variance-free calibration
|
| 19 |
+
signal no generative model can match.
|
| 20 |
+
"""
|
| 21 |
+
from __future__ import annotations
|
| 22 |
+
|
| 23 |
+
import copy
|
| 24 |
+
from dataclasses import dataclass
|
| 25 |
+
|
| 26 |
+
from ..packing import QuestionSpec
|
| 27 |
+
|
| 28 |
+
ACT_QID = "act"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@dataclass
|
| 32 |
+
class Decision:
|
| 33 |
+
"""One decision point."""
|
| 34 |
+
state: str # text/JSON the model reads
|
| 35 |
+
questions: list # [QuestionSpec]; must contain qid=ACT_QID
|
| 36 |
+
n_legal: int # how many leading options are legal
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class DecisionEnv:
|
| 40 |
+
"""Base class. Subclasses implement reset/observe/step."""
|
| 41 |
+
name = "base"
|
| 42 |
+
dense = False # True -> action_rewards() available
|
| 43 |
+
|
| 44 |
+
def reset(self, rng):
|
| 45 |
+
raise NotImplementedError
|
| 46 |
+
|
| 47 |
+
def observe(self) -> Decision:
|
| 48 |
+
"""Current state + questions. questions[0] must be the action
|
| 49 |
+
question (qid=ACT_QID); its options are the action labels."""
|
| 50 |
+
raise NotImplementedError
|
| 51 |
+
|
| 52 |
+
def action_rewards(self):
|
| 53 |
+
"""Dense mode only: exact reward per legal action, normalized so
|
| 54 |
+
max == 1.0 (optimal). Return None if unavailable."""
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
def oracle_targets(self) -> dict:
|
| 58 |
+
"""Exact targets for REPORT questions (qid -> target), and optionally
|
| 59 |
+
the optimal action distribution under key ACT_QID."""
|
| 60 |
+
return {}
|
| 61 |
+
|
| 62 |
+
def step(self, action_idx: int):
|
| 63 |
+
"""Apply action -> (reward, done)."""
|
| 64 |
+
raise NotImplementedError
|
| 65 |
+
|
| 66 |
+
def clone(self):
|
| 67 |
+
return copy.deepcopy(self)
|
| 68 |
+
|
| 69 |
+
@property
|
| 70 |
+
def done(self):
|
| 71 |
+
return self._done
|