SR Approach
SRToolkit.approaches.sr_approach
This module contains the SR_approach class, which is the base class for all symbolic regression approaches.
SR_approach
The base class for all symbolic regression approaches. Any symbolic regression approach should inherit from this class.
If the approach requires pretraining on an external corpus, this should be done inside __init__.
The approach is responsible for defining what corpus it expects and how to load it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the approach. |
required |
Source code in SRToolkit/approaches/sr_approach.py
adaptation_scope
property
Controls when adapt() is called by the framework, and whether
save_adapted_state() / load_adapted_state() are used.
"never":adapt()is never called (default).save_adapted_state()andload_adapted_state()do not need to be implemented."once":adapt()is called once; the framework caches the result viasave_adapted_state()and restores it viaload_adapted_state()on subsequent experiments with the same library. Both methods must be implemented when this scope is used."experiment":adapt()is called before everysearch().save_adapted_state()is called only ifsave_adapted_modelis alsoTrue.
For neural approaches, we recommend you save weights to disk with save_adapted_state() and
return an identifier (e.g. a file path). load_adapted_state() receives that identifier and loads the
weights from the disk, keeping the in-memory cache small.
Returns:
| Type | Description |
|---|---|
str
|
One of |
save_adapted_model
property
Whether the framework should call save_adapted_state() when adaptation_scope is "experiment".
Ignored for "never" and "once" scopes (the latter always saves). Default is False.
Returns:
| Type | Description |
|---|---|
bool
|
|
prepare
Reset the approach's per-experiment state in preparation for a new run.
Called by the framework before adapt() and search() for every
experiment, so any state accumulated during a previous run is cleared
before the next one starts.
Three cases:
- Stateless (e.g. random sampling): implement as
pass. - Stateful search (e.g. GP): reset the population or any other mutable search state to its initial configuration.
- Pretrained weights + per-run search state (e.g. neural): reset only the search state; leave pretrained weights untouched.
Source code in SRToolkit/approaches/sr_approach.py
adapt
Adapt the approach to the target dataset's input space and symbols in the symbol library.
Called by the framework after prepare() and before search(), according
to adaptation_scope. This method must NOT access target values (y) or evaluate
expressions (e.g. by calling sr_evaluator.evaluate_expr()) during adaptation —
only the points from the domain and the symbol library may be used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
input variables from the domain, shape |
required |
symbol_library
|
SymbolLibrary
|
The symbol library defining the available symbols/tokens. |
required |
Source code in SRToolkit/approaches/sr_approach.py
search
Run the symbolic regression search.
Implementations must call sr_evaluator.evaluate_expr(expr) to score
candidate expressions. The evaluator accumulates all results internally;
do not call sr_evaluator.get_results() — that is handled by the
framework after search finishes.
Stop conditions to respect
sr_evaluator.total_evaluations >= sr_evaluator.max_evaluations(whenmax_evaluations > 0)- The current best error has dropped below
sr_evaluator.success_threshold
The symbol library is available in sr_evaluator.symbol_library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sr_evaluator
|
SR_evaluator
|
Evaluator used to score expressions. All results are stored inside this object. |
required |
seed
|
Optional[int]
|
Optional random seed for reproducible expression generation. |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in SRToolkit/approaches/sr_approach.py
save_adapted_state
Saves the adapted model/approach and returns the saved state.
For neural approaches we suggest you save the model's weights to disk (e.g. via
torch.save) and return the file path. The returned value is stored
in memory by the framework and passed back to load_adapted_state()
when the cached state is needed.
Must be implemented when adaptation_scope == "symbol_library", or
when adaptation_scope == "experiment" and save_adapted_model is
True.
Returns:
| Type | Description |
|---|---|
Any
|
The saved state (e.g., a file path or the model). |
Source code in SRToolkit/approaches/sr_approach.py
load_adapted_state
Restore the previously saved state.
For neural approaches we suggest that this function loads weights from the disk
using the identifier returned by save_adapted_state() (e.g. a file path
passed to torch.load).
Must be implemented when adaptation_scope == "symbol_library"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Any
|
The identifier previously returned by |
required |