File size: 9,409 Bytes
b99b9ee 0eddd67 b99b9ee 0eddd67 b99b9ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | """OpenEnv :class:`Environment` subclass for PhysiX-Live.
Owns one episode's lifecycle (state + budget + termination) and orchestrates
the parser/simulator/metrics/reward modules. No scoring logic lives here.
"""
from __future__ import annotations
import logging
import uuid
from typing import Any, Optional
import numpy as np
from openenv.core.env_server import Environment
from physix.models import (
CONVERGENCE_THRESHOLD,
DEFAULT_MAX_TURNS,
HistoryEntry,
PhysiXAction,
PhysiXObservation,
PhysiXState,
RewardBreakdown,
)
from physix.systems import PhysicalSystem, SystemTier, get_system, list_systems_by_tier
from physix.systems.base import TrajectoryData
from physix.verifier import (
ParseError,
SimulationError,
compute_match,
compute_reward,
parse_equation,
residual_summary,
simulate_hypothesis,
summarize_mismatch,
)
_log = logging.getLogger(__name__)
class PhysiXEnvironment(Environment[PhysiXAction, PhysiXObservation, PhysiXState]):
"""OpenEnv environment that drives one episode of equation discovery."""
def __init__(
self,
*,
max_turns: int = DEFAULT_MAX_TURNS,
train_tiers: tuple[SystemTier, ...] = (SystemTier.TIER_1, SystemTier.TIER_2),
seed: Optional[int] = None,
) -> None:
super().__init__()
self._max_turns = max_turns
self._train_tiers = train_tiers
self._rng = np.random.default_rng(seed)
self._state = PhysiXState(max_turns=max_turns)
self._system: Optional[PhysicalSystem] = None
self._trajectory: Optional[TrajectoryData] = None
self._history: list[HistoryEntry] = []
def reset(
self,
seed: Optional[int] = None,
episode_id: Optional[str] = None,
**kwargs: Any,
) -> PhysiXObservation:
"""Start a new episode. Pass ``system_id=`` to force a specific system."""
if seed is not None:
self._rng = np.random.default_rng(seed)
forced_id = kwargs.get("system_id")
chosen_id = forced_id or self._sample_training_system_id()
self._system = get_system(chosen_id)
self._trajectory = self._system.simulate(self._rng)
self._history = []
self._state = PhysiXState(
episode_id=episode_id or str(uuid.uuid4()),
step_count=0,
system_id=chosen_id,
ground_truth_equation=self._system.ground_truth_equation(),
ground_truth_params=dict(self._system.parameters),
last_reward_total=0.0,
converged=False,
max_turns=self._max_turns,
)
return self._build_observation(
mismatch_summary="",
reward_breakdown=RewardBreakdown(),
)
def step(
self,
action: PhysiXAction,
timeout_s: Optional[float] = None,
**kwargs: Any,
) -> PhysiXObservation:
del timeout_s, kwargs # accepted for OpenEnv API conformance, unused
if self._system is None or self._trajectory is None:
raise RuntimeError("step() called before reset(); call reset() first.")
self._state.step_count = self._state.step_count + 1
breakdown, mismatch_text = self._score_hypothesis(action)
self._record_history(action, breakdown, mismatch_text)
self._state.last_reward_total = breakdown.total
self._state.last_r_match = breakdown.match
if breakdown.match >= CONVERGENCE_THRESHOLD:
self._state.converged = True
return self._build_observation(
mismatch_summary=mismatch_text,
reward_breakdown=breakdown,
)
@property
def state(self) -> PhysiXState:
return self._state
def current_observation(self) -> Optional[PhysiXObservation]:
"""Re-render the observation an external driver should feed to the
agent for the *next* turn (i.e. before calling :meth:`step`).
Used by the interactive HTTP router to build prompts mid-session.
Returns ``None`` before :meth:`reset` has been called.
"""
if self._system is None or self._trajectory is None:
return None
last = self._history[-1] if self._history else None
breakdown = (
RewardBreakdown(**last.reward_components)
if last is not None
else RewardBreakdown()
)
mismatch = last.mismatch_summary if last is not None else ""
return self._build_observation(
mismatch_summary=mismatch,
reward_breakdown=breakdown,
)
@property
def current_trajectory(self) -> Optional[TrajectoryData]:
return self._trajectory
@property
def current_system(self) -> Optional[PhysicalSystem]:
return self._system
def _is_done(self) -> bool:
if self._state.converged:
return True
return self._state.step_count >= self._max_turns
def _score_hypothesis(
self,
action: PhysiXAction,
) -> tuple[RewardBreakdown, str]:
assert self._system is not None
assert self._trajectory is not None
parameter_names = frozenset(action.params or {})
try:
parsed = parse_equation(
action.equation,
state_variables=self._system.state_variables,
parameter_names=parameter_names,
)
except ParseError as exc:
_log.debug("parse_equation failed: %s", exc)
breakdown = compute_reward(
parse_succeeded=False,
r_match=0.0,
operator_count=0,
previous_r_match=self._state.last_r_match,
)
return breakdown, f"Parse error: {exc}"
try:
predicted = simulate_hypothesis(
parsed,
state_variables=self._system.state_variables,
parameters=dict(action.params or {}),
initial_conditions=self._trajectory.initial_conditions,
timestamps=self._trajectory.timestamps,
)
except SimulationError as exc:
_log.debug("simulate_hypothesis failed: %s", exc)
breakdown = compute_reward(
parse_succeeded=True,
simulation_succeeded=False,
r_match=0.0,
operator_count=parsed.operator_count,
previous_r_match=self._state.last_r_match,
)
return breakdown, f"Simulation error: {exc}"
r_match = compute_match(
observed=self._trajectory.states,
predicted=predicted,
state_variables=self._system.state_variables,
)
residuals = residual_summary(
timestamps=self._trajectory.timestamps,
observed=self._trajectory.states,
predicted=predicted,
state_variables=self._system.state_variables,
)
mismatch_text = summarize_mismatch(
observed=self._trajectory.states,
predicted=predicted,
state_variables=self._system.state_variables,
timestamps=self._trajectory.timestamps,
summary=residuals,
)
breakdown = compute_reward(
parse_succeeded=True,
simulation_succeeded=True,
r_match=r_match,
operator_count=parsed.operator_count,
previous_r_match=self._state.last_r_match,
)
return breakdown, mismatch_text
def _record_history(
self,
action: PhysiXAction,
breakdown: RewardBreakdown,
mismatch_text: str,
) -> None:
entry = HistoryEntry(
turn=self._state.step_count,
equation=action.equation,
params=dict(action.params or {}),
reward_total=breakdown.total,
reward_components=breakdown.as_dict(),
mismatch_summary=mismatch_text,
)
self._history.append(entry)
def _build_observation(
self,
*,
mismatch_summary: str,
reward_breakdown: RewardBreakdown,
) -> PhysiXObservation:
assert self._system is not None
assert self._trajectory is not None
return PhysiXObservation(
done=self._is_done(),
reward=reward_breakdown.total,
trajectory=self._trajectory.to_observation_samples(),
state_variables=list(self._system.state_variables),
hint=self._system.hint(self._state.ground_truth_params),
history=[entry.as_dict() for entry in self._history],
mismatch_summary=mismatch_summary,
turn=self._state.step_count,
turn_remaining=max(0, self._max_turns - self._state.step_count),
system_id=self._state.system_id,
stats=self._trajectory.stats(),
reward_breakdown=reward_breakdown.as_dict(),
)
def _sample_training_system_id(self) -> str:
candidates: list[str] = []
for tier in self._train_tiers:
candidates.extend(list_systems_by_tier(tier))
if not candidates:
raise RuntimeError(
f"No training systems found for tiers {self._train_tiers!r}."
)
idx = int(self._rng.integers(0, len(candidates)))
return candidates[idx]
|