$ show WassimTenachi/PhySO
PhySO (Physical Symbolic Optimization) is a Python library for symbolic regression using deep reinforcement learning, with a strong emphasis on physics-informed constraints such as dimensional analysis. It discovers closed-form analytical expressions from data, supporting both single-dataset symbolic regression (SR) and multi-dataset class symbolic regression (Class SR) where the same functional form is shared across realizations with per-dataset free constants. The core architecture consists of a token-based program representation (reverse Polish notation), a custom LSTM-based policy network, a modular prior system (hard/soft length, physical units, nesting limits), and an efficient batch execution engine with optional parallel free-constant optimization. The library provides high-level entry points (`physo.SR()` and `physo.ClassSR()`), configuration presets, and a toolkit for expression manipulation and random generation. It includes extensive benchmarking infrastructure (Feynman and Class benchmarks), demos for real-world problems (e.g., Milky Way streams, US tariffs), and unit tests. External dependencies include PyTorch, NumPy, SymPy, pandas, scikit-learn, and matplotlib. While primarily a software library for symbolic regression, it is deeply research-oriented with benchmark result CSVs and analysis scripts, giving it a hybrid application/research character.
The LLM identified 5 files worth highlighting for reuse:
Implements FreeConstantsTable for managing class-wide and realization-specific free constants, with L-BFGS optimization via PyTorch. The design supports efficient batched optimization across multiple realizations, flattening constants to match data shapes. It is self-contained (imports only numpy, torch, scipy) and could be extracted for any symbolic regression system needing differentiable constant optimization.
A custom LSTM-based RNN cell with log-temperature annealing and an optional 'lobotomized' mode (replacing the LSTM with a linear layer). It is used as the policy network for token generation. The implementation is a standalone PyTorch module that can be reused in other RL-based sequence generation tasks, demonstrating non-obvious design for controlling exploration-exploitation trade-offs.
Provides a collection of modular priors (length, arity, nesting, trigonometric limits, useless inverses, physical units) that can be combined via PriorCollection. Each prior implements a standardized interface (sample_neglogprob, hard_constraint) and can be independently applied. This pattern is reusable in any generative model requiring structured constraints on sequence generation.
Implements bottom-up unit assignment and consistency checking for symbolic expressions represented as token trees. Units are encoded as float vectors (SI base units) and propagated through operations. The module enforces unit correctness during program generation, significantly reducing the search space. It is largely self-contained (depends on token definitions) and could be adapted for other physics-informed symbolic regression systems.
Central orchestrator that manages the RL loop: sampling tokens from the RNN, applying priors, constructing programs, computing rewards, and optimizing free constants. It encapsulates the entire batch generation and evaluation pipeline. While tightly integrated with other PhySO modules, its design pattern of a unified batch controller for RL-based program search is instructive and could serve as a template for similar systems.