File size: 8,226 Bytes
d5f6dbd | 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 | """Forward-simulate the agent's parsed hypothesis.
Responsibility:
- Given a :class:`ParsedEquation`, the system's state-variable layout, the
agent's parameter dictionary, and a set of initial conditions and
timestamps, run ``scipy.integrate.odeint`` to produce a predicted
trajectory in the same shape as the system's observed data.
What this module does not do: parse text (see :mod:`physix.verifier.parser`),
score the trajectory (see :mod:`physix.verifier.metrics`), or compose rewards
(see :mod:`physix.verifier.reward`).
"""
from __future__ import annotations
from collections.abc import Iterable
from typing import Callable
import numpy as np
import sympy as sp
from scipy.integrate import odeint
from physix.verifier.parser import ParsedEquation
class SimulationError(RuntimeError):
"""Raised when the proposed system cannot be integrated numerically.
Common causes: NaN/Inf produced by ``odeint``, mass-matrix singularity,
or stiff dynamics that exhaust the integrator's step budget.
"""
def simulate_hypothesis(
parsed: ParsedEquation,
state_variables: Iterable[str],
parameters: dict[str, float],
initial_conditions: dict[str, float],
timestamps: np.ndarray,
) -> dict[str, np.ndarray]:
"""Integrate the agent's hypothesis forward in time.
Args:
parsed: Parsed equations from :func:`physix.verifier.parser.parse_equation`.
state_variables: Ordering of state variables in the underlying
physical system. Used to derive the integration state vector.
parameters: Numerical parameter substitutions supplied by the agent.
initial_conditions: Initial state values keyed by variable name.
timestamps: 1-D array of times at which to record state.
Returns:
A dict mapping each state variable to its predicted trajectory
(1-D array of the same length as ``timestamps``).
"""
state_vars = tuple(state_variables)
int_layout = _build_integration_layout(parsed, state_vars)
try:
rhs_callable = _compile_rhs(parsed, int_layout, parameters)
except Exception as exc: # noqa: BLE001 — surfaced as SimulationError below
raise SimulationError(f"failed to compile RHS: {exc}") from exc
initial_state = _build_initial_state(int_layout, initial_conditions)
try:
result = odeint(
rhs_callable,
initial_state,
timestamps,
full_output=False,
mxstep=2000,
rtol=1e-6,
atol=1e-9,
)
except Exception as exc: # noqa: BLE001
# ``odeint`` propagates whatever the user-supplied RHS callable
# raises. The model can emit equations that lambdify into Python
# code which then trips ``TypeError`` (e.g. ``np.sqrt`` on a
# SymPy ``Add``), ``ZeroDivisionError``, ``OverflowError``, etc.
# Any of those should be surfaced to the env as a clean
# ``SimulationError`` (= ``r_match=0`` for the turn) rather than
# crashing the route with a 500.
raise SimulationError(f"odeint failed: {exc}") from exc
if not np.all(np.isfinite(result)):
raise SimulationError("Predicted trajectory contains NaN or Inf values.")
return _project_back_to_state_vars(result, int_layout, state_vars)
_IntegrationLayout = tuple[tuple[str, ...], dict[str, int]]
def _build_integration_layout(
parsed: ParsedEquation,
state_vars: tuple[str, ...],
) -> _IntegrationLayout:
"""Return ``(integration_vars, index)`` for the integration state.
For a system with second-order ODEs we need to integrate both ``var`` and
``dvar/dt``. We prefix-derive the names: e.g. ``y -> [y, vy]``, with the
convention that ``v<name>`` is the first time derivative of ``<name>``
(matching the conventions used in :mod:`physix.systems.tier1`).
"""
integration_vars: list[str] = []
eq_by_var = {eq.var: eq for eq in parsed.equations}
for var in state_vars:
# Skip variables that are themselves first derivatives of another
# state variable (e.g. ``vy`` paired with ``y``); those will be added
# as part of the higher-order companion.
if _is_velocity_of(var, state_vars):
continue
eq = eq_by_var.get(var)
if eq is not None and eq.order == 2:
integration_vars.append(var)
integration_vars.append(_velocity_name(var))
continue
if eq is not None and eq.order == 1:
integration_vars.append(var)
continue
# No matching equation — accept anyway, treating as zero-derivative.
integration_vars.append(var)
# Now also append any equations whose var was not in state_vars (e.g.
# the agent named a derivative directly). We log these but do not crash;
# ``rhs_callable`` will simply not see them.
declared = set(integration_vars)
for eq in parsed.equations:
if eq.var in declared:
continue
if eq.order == 2:
integration_vars.append(eq.var)
integration_vars.append(_velocity_name(eq.var))
else:
integration_vars.append(eq.var)
index = {name: i for i, name in enumerate(integration_vars)}
return tuple(integration_vars), index
def _is_velocity_of(var: str, state_vars: tuple[str, ...]) -> bool:
"""True if ``var`` looks like the first derivative of another state var."""
if var.startswith("v"):
return var[1:] in state_vars
if var.startswith("d"):
return var[1:] in state_vars
return False
def _velocity_name(var: str) -> str:
"""Convention for naming a first time derivative."""
if var.startswith("theta"):
return "d" + var
return "v" + var
def _compile_rhs(
parsed: ParsedEquation,
layout: _IntegrationLayout,
parameters: dict[str, float],
) -> Callable[[np.ndarray, float], np.ndarray]:
"""Build a Python callable f(state, t) -> dstate/dt for ``odeint``."""
integration_vars, index = layout
eq_by_var = {eq.var: eq for eq in parsed.equations}
# Lambdify each equation's RHS once. Symbols are bound to integration
# variables (state) and to scalar parameters (substituted).
state_symbols = sp.symbols(" ".join(integration_vars))
if not isinstance(state_symbols, tuple):
state_symbols = (state_symbols,)
rhs_lambdas: dict[str, Callable[..., float]] = {}
for var, eq in eq_by_var.items():
rhs = eq.rhs.subs({sp.Symbol(k): v for k, v in parameters.items()})
rhs_lambdas[var] = sp.lambdify(state_symbols, rhs, modules="numpy")
def _rhs(state: np.ndarray, t: float) -> np.ndarray:
derivs = np.zeros_like(state)
for var, eq in eq_by_var.items():
if eq.order == 1:
idx = index[var]
derivs[idx] = float(rhs_lambdas[var](*state))
else: # order == 2
pos_idx = index[var]
vel_name = _velocity_name(var)
vel_idx = index[vel_name]
derivs[pos_idx] = state[vel_idx]
derivs[vel_idx] = float(rhs_lambdas[var](*state))
return derivs
return _rhs
def _build_initial_state(
layout: _IntegrationLayout,
initial_conditions: dict[str, float],
) -> np.ndarray:
"""Project the IC dict into the integration variable order."""
integration_vars, _ = layout
state = np.zeros(len(integration_vars), dtype=float)
for i, var in enumerate(integration_vars):
state[i] = float(initial_conditions.get(var, 0.0))
return state
def _project_back_to_state_vars(
result: np.ndarray,
layout: _IntegrationLayout,
state_vars: tuple[str, ...],
) -> dict[str, np.ndarray]:
"""Slice the integration output into per-system-variable trajectories."""
_, index = layout
out: dict[str, np.ndarray] = {}
for var in state_vars:
if var in index:
out[var] = result[:, index[var]]
else:
# The agent never modelled this variable; predict the first
# entry constant. This is rare and kept defensive.
out[var] = np.full(result.shape[0], float(result[0, 0]))
return out
|